Skip to content

Commit

Permalink
[skip-changelog] Added flag to disable check for sketch foldername ma…
Browse files Browse the repository at this point in the history
…tching 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
  • Loading branch information
cmaglie authored Feb 16, 2021
1 parent c977a23 commit 2c7b6ba
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 32 deletions.
31 changes: 24 additions & 7 deletions arduino/sketch/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package sketch

import (
"fmt"
"io/ioutil"
"path/filepath"
"sort"
Expand Down Expand Up @@ -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.
Expand All @@ -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)
}
25 changes: 14 additions & 11 deletions arduino/sketch/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package sketch_test
package sketch

import (
"fmt"
"path/filepath"
"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"
)

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)
Expand All @@ -38,20 +37,20 @@ func TestNewItem(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "#include <testlib.h>", 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)
Expand All @@ -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)
Expand All @@ -81,16 +80,20 @@ 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)
}

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)
Expand All @@ -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)
}
6 changes: 5 additions & 1 deletion legacy/builder/container_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 14 additions & 13 deletions legacy/builder/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 2c7b6ba

Please sign in to comment.