From 174c49e2d8b48bb1076004e4c34a13adcc43ec1a Mon Sep 17 00:00:00 2001 From: Lluis Campos Date: Wed, 20 Nov 2019 12:27:07 +0100 Subject: [PATCH] Support .pde extension by compile when only folder specified (#386) When only specifying folder, the logic will try both .ino and .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 --- arduino/builder/sketch.go | 23 ++++++++++++-- arduino/builder/sketch_test.go | 30 ++++++++++++++++++- .../.#sketch.ino | 2 ++ .../TestLoadSketchFolderBothInoAndPde.ino | 7 +++++ .../TestLoadSketchFolderBothInoAndPde.pde | 7 +++++ .../TestLoadSketchFolderBothInoAndPde/doc.txt | 0 .../header.h | 1 + .../TestLoadSketchFolderBothInoAndPde/old.pde | 0 .../other.ino | 3 ++ .../s_file.S | 0 .../src/dont_load_me.ino | 2 ++ .../src/helper.h | 0 .../TestLoadSketchFolderPde/.#sketch.ino | 2 ++ .../TestLoadSketchFolderPde.pde | 7 +++++ .../testdata/TestLoadSketchFolderPde/doc.txt | 0 .../testdata/TestLoadSketchFolderPde/header.h | 1 + .../testdata/TestLoadSketchFolderPde/old.pde | 0 .../TestLoadSketchFolderPde/other.ino | 3 ++ .../testdata/TestLoadSketchFolderPde/s_file.S | 0 .../src/dont_load_me.ino | 2 ++ .../TestLoadSketchFolderPde/src/helper.h | 0 21 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/.#sketch.ino create mode 100644 arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/TestLoadSketchFolderBothInoAndPde.ino create mode 100644 arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/TestLoadSketchFolderBothInoAndPde.pde create mode 100644 arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/doc.txt create mode 100644 arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/header.h create mode 100644 arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/old.pde create mode 100644 arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/other.ino create mode 100644 arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/s_file.S create mode 100644 arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/src/dont_load_me.ino create mode 100644 arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/src/helper.h create mode 100644 arduino/builder/testdata/TestLoadSketchFolderPde/.#sketch.ino create mode 100644 arduino/builder/testdata/TestLoadSketchFolderPde/TestLoadSketchFolderPde.pde create mode 100644 arduino/builder/testdata/TestLoadSketchFolderPde/doc.txt create mode 100644 arduino/builder/testdata/TestLoadSketchFolderPde/header.h create mode 100644 arduino/builder/testdata/TestLoadSketchFolderPde/old.pde create mode 100644 arduino/builder/testdata/TestLoadSketchFolderPde/other.ino create mode 100644 arduino/builder/testdata/TestLoadSketchFolderPde/s_file.S create mode 100644 arduino/builder/testdata/TestLoadSketchFolderPde/src/dont_load_me.ino create mode 100644 arduino/builder/testdata/TestLoadSketchFolderPde/src/helper.h diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index d9ad4c50385..c3425b8d041 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -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 diff --git a/arduino/builder/sketch_test.go b/arduino/builder/sketch_test.go index 409e258d541..17ed07a016e 100644 --- a/arduino/builder/sketch_test.go +++ b/arduino/builder/sketch_test.go @@ -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()) @@ -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) diff --git a/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/.#sketch.ino b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/.#sketch.ino new file mode 100644 index 00000000000..71048175432 --- /dev/null +++ b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/.#sketch.ino @@ -0,0 +1,2 @@ +void setup() +void loop) } \ No newline at end of file diff --git a/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/TestLoadSketchFolderBothInoAndPde.ino b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/TestLoadSketchFolderBothInoAndPde.ino new file mode 100644 index 00000000000..0d5e0f5ceb9 --- /dev/null +++ b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/TestLoadSketchFolderBothInoAndPde.ino @@ -0,0 +1,7 @@ +void setup() { + +} + +void loop() { + +} \ No newline at end of file diff --git a/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/TestLoadSketchFolderBothInoAndPde.pde b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/TestLoadSketchFolderBothInoAndPde.pde new file mode 100644 index 00000000000..0d5e0f5ceb9 --- /dev/null +++ b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/TestLoadSketchFolderBothInoAndPde.pde @@ -0,0 +1,7 @@ +void setup() { + +} + +void loop() { + +} \ No newline at end of file diff --git a/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/doc.txt b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/doc.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/header.h b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/header.h new file mode 100644 index 00000000000..0e7d3b1a6a9 --- /dev/null +++ b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/header.h @@ -0,0 +1 @@ +#define FOO "BAR" \ No newline at end of file diff --git a/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/old.pde b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/old.pde new file mode 100644 index 00000000000..e69de29bb2d diff --git a/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/other.ino b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/other.ino new file mode 100644 index 00000000000..c426196c017 --- /dev/null +++ b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/other.ino @@ -0,0 +1,3 @@ +String hello() { + return "world"; +} \ No newline at end of file diff --git a/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/s_file.S b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/s_file.S new file mode 100644 index 00000000000..e69de29bb2d diff --git a/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/src/dont_load_me.ino b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/src/dont_load_me.ino new file mode 100644 index 00000000000..46b07018d09 --- /dev/null +++ b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/src/dont_load_me.ino @@ -0,0 +1,2 @@ +#include +#error "Whattya looking at?" diff --git a/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/src/helper.h b/arduino/builder/testdata/TestLoadSketchFolderBothInoAndPde/src/helper.h new file mode 100644 index 00000000000..e69de29bb2d diff --git a/arduino/builder/testdata/TestLoadSketchFolderPde/.#sketch.ino b/arduino/builder/testdata/TestLoadSketchFolderPde/.#sketch.ino new file mode 100644 index 00000000000..71048175432 --- /dev/null +++ b/arduino/builder/testdata/TestLoadSketchFolderPde/.#sketch.ino @@ -0,0 +1,2 @@ +void setup() +void loop) } \ No newline at end of file diff --git a/arduino/builder/testdata/TestLoadSketchFolderPde/TestLoadSketchFolderPde.pde b/arduino/builder/testdata/TestLoadSketchFolderPde/TestLoadSketchFolderPde.pde new file mode 100644 index 00000000000..0d5e0f5ceb9 --- /dev/null +++ b/arduino/builder/testdata/TestLoadSketchFolderPde/TestLoadSketchFolderPde.pde @@ -0,0 +1,7 @@ +void setup() { + +} + +void loop() { + +} \ No newline at end of file diff --git a/arduino/builder/testdata/TestLoadSketchFolderPde/doc.txt b/arduino/builder/testdata/TestLoadSketchFolderPde/doc.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/arduino/builder/testdata/TestLoadSketchFolderPde/header.h b/arduino/builder/testdata/TestLoadSketchFolderPde/header.h new file mode 100644 index 00000000000..0e7d3b1a6a9 --- /dev/null +++ b/arduino/builder/testdata/TestLoadSketchFolderPde/header.h @@ -0,0 +1 @@ +#define FOO "BAR" \ No newline at end of file diff --git a/arduino/builder/testdata/TestLoadSketchFolderPde/old.pde b/arduino/builder/testdata/TestLoadSketchFolderPde/old.pde new file mode 100644 index 00000000000..e69de29bb2d diff --git a/arduino/builder/testdata/TestLoadSketchFolderPde/other.ino b/arduino/builder/testdata/TestLoadSketchFolderPde/other.ino new file mode 100644 index 00000000000..c426196c017 --- /dev/null +++ b/arduino/builder/testdata/TestLoadSketchFolderPde/other.ino @@ -0,0 +1,3 @@ +String hello() { + return "world"; +} \ No newline at end of file diff --git a/arduino/builder/testdata/TestLoadSketchFolderPde/s_file.S b/arduino/builder/testdata/TestLoadSketchFolderPde/s_file.S new file mode 100644 index 00000000000..e69de29bb2d diff --git a/arduino/builder/testdata/TestLoadSketchFolderPde/src/dont_load_me.ino b/arduino/builder/testdata/TestLoadSketchFolderPde/src/dont_load_me.ino new file mode 100644 index 00000000000..46b07018d09 --- /dev/null +++ b/arduino/builder/testdata/TestLoadSketchFolderPde/src/dont_load_me.ino @@ -0,0 +1,2 @@ +#include +#error "Whattya looking at?" diff --git a/arduino/builder/testdata/TestLoadSketchFolderPde/src/helper.h b/arduino/builder/testdata/TestLoadSketchFolderPde/src/helper.h new file mode 100644 index 00000000000..e69de29bb2d