Skip to content

Added flag to disable check for sketch foldername matching sketch name #1187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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