From 2c7b6ba5407fb19ba0e8206dfe5f4c2fa34b8751 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 16 Feb 2021 18:45:45 +0100 Subject: [PATCH] [skip-changelog] Added flag to disable check for sketch foldername matching sketch name (#1187) * Added flag to disable check for sketch foldername This is required to keep backward compatibility for arduino-builder that doesn't enforce this check. * Added missing source doc * Changed module sketch_test -> sketch * Fixed test * Return the detected sketch as part of the error --- arduino/sketch/sketch.go | 31 ++++++++++++++++++++++++------- arduino/sketch/sketch_test.go | 25 ++++++++++++++----------- legacy/builder/container_setup.go | 6 +++++- legacy/builder/types/context.go | 27 ++++++++++++++------------- 4 files changed, 57 insertions(+), 32 deletions(-) diff --git a/arduino/sketch/sketch.go b/arduino/sketch/sketch.go index 16ce6f6f803..a67e6ad6bb9 100644 --- a/arduino/sketch/sketch.go +++ b/arduino/sketch/sketch.go @@ -16,6 +16,7 @@ package sketch import ( + "fmt" "io/ioutil" "path/filepath" "sort" @@ -124,17 +125,22 @@ func New(sketchFolderPath, mainFilePath, buildPath string, allFilesPaths []strin sort.Sort(ItemByPath(otherSketchFiles)) sort.Sort(ItemByPath(rootFolderFiles)) - if err := CheckSketchCasing(sketchFolderPath); err != nil { - return nil, err - } - - return &Sketch{ + sk := &Sketch{ MainFile: mainFile, LocationPath: sketchFolderPath, OtherSketchFiles: otherSketchFiles, AdditionalFiles: additionalFiles, RootFolderFiles: rootFolderFiles, - }, nil + } + err := CheckSketchCasing(sketchFolderPath) + if e, ok := err.(*InvalidSketchFoldernameError); ok { + e.Sketch = sk + return nil, e + } + if err != nil { + return nil, err + } + return sk, nil } // CheckSketchCasing returns an error if the casing of the sketch folder and the main file are different. @@ -160,8 +166,19 @@ func CheckSketchCasing(sketchFolder string) error { if files.Len() == 0 { sketchFolderPath := paths.New(sketchFolder) sketchFile := sketchFolderPath.Join(sketchFolderPath.Base() + globals.MainFileValidExtension) - return errors.Errorf("no valid sketch found in %s: missing %s", sketchFolderPath, sketchFile) + return &InvalidSketchFoldernameError{SketchFolder: sketchFolderPath, SketchFile: sketchFile} } return nil } + +// InvalidSketchFoldernameError is returned when the sketch directory doesn't match the sketch name +type InvalidSketchFoldernameError struct { + SketchFolder *paths.Path + SketchFile *paths.Path + Sketch *Sketch +} + +func (e *InvalidSketchFoldernameError) Error() string { + return fmt.Sprintf("no valid sketch found in %s: missing %s", e.SketchFolder, e.SketchFile) +} diff --git a/arduino/sketch/sketch_test.go b/arduino/sketch/sketch_test.go index 8d7069590e6..bfe4b213688 100644 --- a/arduino/sketch/sketch_test.go +++ b/arduino/sketch/sketch_test.go @@ -13,7 +13,7 @@ // Arduino software without disclosing the source code of your own applications. // To purchase a commercial license, send an email to license@arduino.cc. -package sketch_test +package sketch import ( "fmt" @@ -21,7 +21,6 @@ import ( "sort" "testing" - "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -29,7 +28,7 @@ import ( func TestNewItem(t *testing.T) { sketchItem := filepath.Join("testdata", t.Name()+".ino") - item := sketch.NewItem(sketchItem) + item := NewItem(sketchItem) assert.Equal(t, sketchItem, item.Path) sourceBytes, err := item.GetSourceBytes() assert.Nil(t, err) @@ -38,20 +37,20 @@ func TestNewItem(t *testing.T) { assert.Nil(t, err) assert.Equal(t, "#include ", sourceStr) - item = sketch.NewItem("doesnt/exist") + item = NewItem("doesnt/exist") sourceBytes, err = item.GetSourceBytes() assert.Nil(t, sourceBytes) assert.NotNil(t, err) } func TestSort(t *testing.T) { - items := []*sketch.Item{ + items := []*Item{ {"foo"}, {"baz"}, {"bar"}, } - sort.Sort(sketch.ItemByPath(items)) + sort.Sort(ItemByPath(items)) assert.Equal(t, "bar", items[0].Path) assert.Equal(t, "baz", items[1].Path) @@ -67,7 +66,7 @@ func TestNew(t *testing.T) { otherFile, } - sketch, err := sketch.New(sketchFolderPath, mainFilePath, "", allFilesPaths) + sketch, err := New(sketchFolderPath, mainFilePath, "", allFilesPaths) assert.Nil(t, err) assert.Equal(t, mainFilePath, sketch.MainFile.Path) assert.Equal(t, sketchFolderPath, sketch.LocationPath) @@ -81,8 +80,12 @@ func TestNew(t *testing.T) { func TestNewSketchCasingWrong(t *testing.T) { sketchPath := paths.New("testdata", "SketchCasingWrong") mainFilePath := paths.New("testadata", "sketchcasingwrong.ino").String() - sketch, err := sketch.New(sketchPath.String(), mainFilePath, "", []string{mainFilePath}) + sketch, err := New(sketchPath.String(), mainFilePath, "", []string{mainFilePath}) assert.Nil(t, sketch) + assert.Error(t, err) + assert.IsType(t, &InvalidSketchFoldernameError{}, err) + e := err.(*InvalidSketchFoldernameError) + assert.NotNil(t, e.Sketch) expectedError := fmt.Sprintf("no valid sketch found in %s: missing %s", sketchPath.String(), sketchPath.Join(sketchPath.Base()+".ino")) assert.EqualError(t, err, expectedError) } @@ -90,7 +93,7 @@ func TestNewSketchCasingWrong(t *testing.T) { func TestNewSketchCasingCorrect(t *testing.T) { sketchPath := paths.New("testdata", "SketchCasingCorrect").String() mainFilePath := paths.New("testadata", "SketchCasingCorrect.ino").String() - sketch, err := sketch.New(sketchPath, mainFilePath, "", []string{mainFilePath}) + sketch, err := New(sketchPath, mainFilePath, "", []string{mainFilePath}) assert.NotNil(t, sketch) assert.NoError(t, err) assert.Equal(t, sketchPath, sketch.LocationPath) @@ -102,13 +105,13 @@ func TestNewSketchCasingCorrect(t *testing.T) { func TestCheckSketchCasingWrong(t *testing.T) { sketchFolder := paths.New("testdata", "SketchCasingWrong") - err := sketch.CheckSketchCasing(sketchFolder.String()) + err := CheckSketchCasing(sketchFolder.String()) expectedError := fmt.Sprintf("no valid sketch found in %s: missing %s", sketchFolder, sketchFolder.Join(sketchFolder.Base()+".ino")) assert.EqualError(t, err, expectedError) } func TestCheckSketchCasingCorrect(t *testing.T) { sketchFolder := paths.New("testdata", "SketchCasingCorrect").String() - err := sketch.CheckSketchCasing(sketchFolder) + err := CheckSketchCasing(sketchFolder) require.NoError(t, err) } diff --git a/legacy/builder/container_setup.go b/legacy/builder/container_setup.go index e562e0853b3..2ce501fcf4f 100644 --- a/legacy/builder/container_setup.go +++ b/legacy/builder/container_setup.go @@ -19,6 +19,7 @@ import ( "fmt" bldr "github.com/arduino/arduino-cli/arduino/builder" + sk "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/legacy/builder/builder_utils" "github.com/arduino/arduino-cli/legacy/builder/types" "github.com/arduino/go-paths-helper" @@ -63,7 +64,10 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context) // load sketch sketch, err := bldr.SketchLoad(sketchLocation.String(), ctx.BuildPath.String()) - if err != nil { + if e, ok := err.(*sk.InvalidSketchFoldernameError); ctx.IgnoreSketchFolderNameErrors && ok { + // ignore error + sketch = e.Sketch + } else if err != nil { return errors.WithStack(err) } if sketch.MainFile == nil { diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 4a8e7ad9ff4..9b9eb113fb1 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -91,19 +91,20 @@ type Context struct { PlatformKeyRewrites PlatforKeysRewrite HardwareRewriteResults map[*cores.PlatformRelease][]PlatforKeyRewrite - BuildProperties *properties.Map - BuildCore string - BuildPath *paths.Path - BuildCachePath *paths.Path - SketchBuildPath *paths.Path - CoreBuildPath *paths.Path - CoreBuildCachePath *paths.Path - CoreArchiveFilePath *paths.Path - CoreObjectsFiles paths.PathList - LibrariesBuildPath *paths.Path - LibrariesObjectFiles paths.PathList - PreprocPath *paths.Path - SketchObjectFiles paths.PathList + BuildProperties *properties.Map + BuildCore string + BuildPath *paths.Path + BuildCachePath *paths.Path + SketchBuildPath *paths.Path + CoreBuildPath *paths.Path + CoreBuildCachePath *paths.Path + CoreArchiveFilePath *paths.Path + CoreObjectsFiles paths.PathList + LibrariesBuildPath *paths.Path + LibrariesObjectFiles paths.PathList + PreprocPath *paths.Path + SketchObjectFiles paths.PathList + IgnoreSketchFolderNameErrors bool CollectedSourceFiles *UniqueSourceFileQueue