Skip to content

Commit c78e1d3

Browse files
committed
Add convenience function for detecting main sketch files in a path
1 parent 0b0292d commit c78e1d3

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

Diff for: project/sketch/sketch.go

+26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ See: https://arduino.github.io/arduino-cli/latest/sketch-specification/
2020
package sketch
2121

2222
import (
23+
"fmt"
24+
2325
"github.com/arduino/arduino-cli/arduino/globals"
2426
"github.com/arduino/go-paths-helper"
2527
)
@@ -31,6 +33,30 @@ func HasMainFileValidExtension(filePath *paths.Path) bool {
3133
return hasMainFileValidExtension
3234
}
3335

36+
// ContainsMainSketchFile checks whether the provided path contains a file with valid main sketch file extension.
37+
func ContainsMainSketchFile(searchPath *paths.Path) bool {
38+
if searchPath.NotExist() {
39+
panic(fmt.Sprintf("Error: provided path %s does not exist.", searchPath))
40+
}
41+
if searchPath.IsNotDir() {
42+
panic(fmt.Sprintf("Error: provided path %s is not a directory.", searchPath))
43+
}
44+
45+
directoryListing, err := searchPath.ReadDir()
46+
if err != nil {
47+
panic(err)
48+
}
49+
50+
directoryListing.FilterOutDirs()
51+
for _, potentialHeaderFile := range directoryListing {
52+
if HasMainFileValidExtension(potentialHeaderFile) {
53+
return true
54+
}
55+
}
56+
57+
return false
58+
}
59+
3460
// HasSupportedExtension returns whether the file at the given path has any of the file extensions supported for source/header files of a sketch.
3561
func HasSupportedExtension(filePath *paths.Path) bool {
3662
_, hasAdditionalFileValidExtensions := globals.AdditionalFileValidExtensions[filePath.Ext()]

Diff for: project/sketch/sketch_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,30 @@
1616
package sketch
1717

1818
import (
19+
"os"
1920
"testing"
2021

2122
"github.com/arduino/go-paths-helper"
2223
"github.com/stretchr/testify/assert"
2324
)
2425

26+
var testDataPath *paths.Path
27+
28+
func init() {
29+
workingDirectory, _ := os.Getwd()
30+
testDataPath = paths.New(workingDirectory, "testdata")
31+
}
32+
2533
func TestHasMainFileValidExtension(t *testing.T) {
2634
assert.True(t, HasMainFileValidExtension(paths.New("/foo/bar.ino")))
2735
assert.False(t, HasMainFileValidExtension(paths.New("/foo/bar.h")))
2836
}
2937

38+
func TestContainsMainSketchFile(t *testing.T) {
39+
assert.True(t, ContainsMainSketchFile(testDataPath.Join("Valid")))
40+
assert.False(t, ContainsMainSketchFile(testDataPath.Join("ContainsNoMainSketchFile")))
41+
}
42+
3043
func TestHasSupportedExtension(t *testing.T) {
3144
assert.True(t, HasSupportedExtension(paths.New("/foo/bar.ino")))
3245
assert.True(t, HasSupportedExtension(paths.New("/foo/bar.h")))

Diff for: project/sketch/testdata/ContainsNoMainSketchFile/foo.bar

Whitespace-only changes.

Diff for: project/sketch/testdata/Valid/Valid.ino

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void setup() {}
2+
void loop() {}

0 commit comments

Comments
 (0)