Skip to content

Commit

Permalink
Support .pde extension by compile when only folder specified (#386)
Browse files Browse the repository at this point in the history
When only specifying folder, the logic will try both <DirName>.ino and
<DirName>.pde as the main sketch file. If both are found, and error is
also thrown (it would break on compile later on).

Added couple of unit tests also.

Signed-off-by: Lluis Campos <lluis.campos@northern.tech>
  • Loading branch information
lluiscampos committed Nov 28, 2019
1 parent c30151b commit 5c38844
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
23 changes: 20 additions & 3 deletions arduino/builder/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,31 @@ func SketchLoad(sketchPath, buildPath string) (*sketch.Sketch, error) {

var sketchFolder, mainSketchFile string

// if a sketch folder was passed, save the parent and point sketchPath to the main .ino file
// if a sketch folder was passed, save the parent and point sketchPath to the main sketch file
if stat.IsDir() {
sketchFolder = sketchPath
mainSketchFile = filepath.Join(sketchPath, stat.Name()+".ino")
// allowed extensions are .ino and .pde (but not both)
allowedSketchExtensions := [...]string{".ino", ".pde"}
for _, extension := range allowedSketchExtensions {
candidateSketchFile := filepath.Join(sketchPath, stat.Name()+extension)
if _, err := os.Stat(candidateSketchFile); !os.IsNotExist(err) {
if mainSketchFile == "" {
mainSketchFile = candidateSketchFile
} else {
return nil, errors.Errorf("more than one main sketch file found (%v,%v)",
filepath.Base(mainSketchFile),
filepath.Base(candidateSketchFile))
}
}
}
// check that .pde or .ino was found
if mainSketchFile == "" {
return nil, errors.Errorf("unable to find an sketch file in directory %v", sketchFolder)
}
// in the case a dir was passed, ensure the main file exists and is readable
f, err := os.Open(mainSketchFile)
if err != nil {
return nil, errors.Wrap(err, "unable to find the main sketch file")
return nil, errors.Wrap(err, "unable to open the main sketch file")
}
f.Close()
// ensure it is not a directory
Expand Down
30 changes: 29 additions & 1 deletion arduino/builder/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,34 @@ func TestLoadSketchFolder(t *testing.T) {
require.Equal(t, "helper.h", filepath.Base(s.AdditionalFiles[2].Path))
}

func TestLoadSketchFolderPde(t *testing.T) {
// pass the path to the sketch folder
sketchPath := filepath.Join("testdata", t.Name())
mainFilePath := filepath.Join(sketchPath, t.Name()+".pde")
s, err := builder.SketchLoad(sketchPath, "")
require.Nil(t, err)
require.NotNil(t, s)
require.Equal(t, mainFilePath, s.MainFile.Path)
require.Equal(t, sketchPath, s.LocationPath)
require.Len(t, s.OtherSketchFiles, 2)
require.Equal(t, "old.pde", filepath.Base(s.OtherSketchFiles[0].Path))
require.Equal(t, "other.ino", filepath.Base(s.OtherSketchFiles[1].Path))
require.Len(t, s.AdditionalFiles, 3)
require.Equal(t, "header.h", filepath.Base(s.AdditionalFiles[0].Path))
require.Equal(t, "s_file.S", filepath.Base(s.AdditionalFiles[1].Path))
require.Equal(t, "helper.h", filepath.Base(s.AdditionalFiles[2].Path))
}

func TestLoadSketchFolderBothInoAndPde(t *testing.T) {
// pass the path to the sketch folder containing two main sketches, .ino and .pde
sketchPath := filepath.Join("testdata", t.Name())
_, err := builder.SketchLoad(sketchPath, "")
require.Error(t, err)
require.Contains(t, err.Error(), "more than one main sketch file found")
require.Contains(t, err.Error(), t.Name()+".ino")
require.Contains(t, err.Error(), t.Name()+".pde")
}

func TestLoadSketchFolderSymlink(t *testing.T) {
// pass the path to the sketch folder
symlinkSketchPath := filepath.Join("testdata", t.Name())
Expand Down Expand Up @@ -128,7 +156,7 @@ func TestLoadSketchFolderWrongMain(t *testing.T) {
sketchPath := filepath.Join("testdata", t.Name())
_, err := builder.SketchLoad(sketchPath, "")
require.Error(t, err)
require.Contains(t, err.Error(), "unable to find the main sketch file")
require.Contains(t, err.Error(), "unable to find an sketch file in directory testdata")

_, err = builder.SketchLoad("does/not/exist", "")
require.Error(t, err)
Expand Down

0 comments on commit 5c38844

Please sign in to comment.