Unverified Commit 2c7b6ba5 authored by Cristian Maglie's avatar Cristian Maglie Committed by GitHub

[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
parent c977a238
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package sketch package sketch
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"sort" "sort"
...@@ -124,17 +125,22 @@ func New(sketchFolderPath, mainFilePath, buildPath string, allFilesPaths []strin ...@@ -124,17 +125,22 @@ func New(sketchFolderPath, mainFilePath, buildPath string, allFilesPaths []strin
sort.Sort(ItemByPath(otherSketchFiles)) sort.Sort(ItemByPath(otherSketchFiles))
sort.Sort(ItemByPath(rootFolderFiles)) sort.Sort(ItemByPath(rootFolderFiles))
if err := CheckSketchCasing(sketchFolderPath); err != nil { sk := &Sketch{
return nil, err
}
return &Sketch{
MainFile: mainFile, MainFile: mainFile,
LocationPath: sketchFolderPath, LocationPath: sketchFolderPath,
OtherSketchFiles: otherSketchFiles, OtherSketchFiles: otherSketchFiles,
AdditionalFiles: additionalFiles, AdditionalFiles: additionalFiles,
RootFolderFiles: rootFolderFiles, 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. // 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 { ...@@ -160,8 +166,19 @@ func CheckSketchCasing(sketchFolder string) error {
if files.Len() == 0 { if files.Len() == 0 {
sketchFolderPath := paths.New(sketchFolder) sketchFolderPath := paths.New(sketchFolder)
sketchFile := sketchFolderPath.Join(sketchFolderPath.Base() + globals.MainFileValidExtension) 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 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)
}
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
// Arduino software without disclosing the source code of your own applications. // Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc. // To purchase a commercial license, send an email to license@arduino.cc.
package sketch_test package sketch
import ( import (
"fmt" "fmt"
...@@ -21,7 +21,6 @@ import ( ...@@ -21,7 +21,6 @@ import (
"sort" "sort"
"testing" "testing"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/go-paths-helper" "github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
...@@ -29,7 +28,7 @@ import ( ...@@ -29,7 +28,7 @@ import (
func TestNewItem(t *testing.T) { func TestNewItem(t *testing.T) {
sketchItem := filepath.Join("testdata", t.Name()+".ino") sketchItem := filepath.Join("testdata", t.Name()+".ino")
item := sketch.NewItem(sketchItem) item := NewItem(sketchItem)
assert.Equal(t, sketchItem, item.Path) assert.Equal(t, sketchItem, item.Path)
sourceBytes, err := item.GetSourceBytes() sourceBytes, err := item.GetSourceBytes()
assert.Nil(t, err) assert.Nil(t, err)
...@@ -38,20 +37,20 @@ func TestNewItem(t *testing.T) { ...@@ -38,20 +37,20 @@ func TestNewItem(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "#include <testlib.h>", sourceStr) assert.Equal(t, "#include <testlib.h>", sourceStr)
item = sketch.NewItem("doesnt/exist") item = NewItem("doesnt/exist")
sourceBytes, err = item.GetSourceBytes() sourceBytes, err = item.GetSourceBytes()
assert.Nil(t, sourceBytes) assert.Nil(t, sourceBytes)
assert.NotNil(t, err) assert.NotNil(t, err)
} }
func TestSort(t *testing.T) { func TestSort(t *testing.T) {
items := []*sketch.Item{ items := []*Item{
{"foo"}, {"foo"},
{"baz"}, {"baz"},
{"bar"}, {"bar"},
} }
sort.Sort(sketch.ItemByPath(items)) sort.Sort(ItemByPath(items))
assert.Equal(t, "bar", items[0].Path) assert.Equal(t, "bar", items[0].Path)
assert.Equal(t, "baz", items[1].Path) assert.Equal(t, "baz", items[1].Path)
...@@ -67,7 +66,7 @@ func TestNew(t *testing.T) { ...@@ -67,7 +66,7 @@ func TestNew(t *testing.T) {
otherFile, otherFile,
} }
sketch, err := sketch.New(sketchFolderPath, mainFilePath, "", allFilesPaths) sketch, err := New(sketchFolderPath, mainFilePath, "", allFilesPaths)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, mainFilePath, sketch.MainFile.Path) assert.Equal(t, mainFilePath, sketch.MainFile.Path)
assert.Equal(t, sketchFolderPath, sketch.LocationPath) assert.Equal(t, sketchFolderPath, sketch.LocationPath)
...@@ -81,8 +80,12 @@ func TestNew(t *testing.T) { ...@@ -81,8 +80,12 @@ func TestNew(t *testing.T) {
func TestNewSketchCasingWrong(t *testing.T) { func TestNewSketchCasingWrong(t *testing.T) {
sketchPath := paths.New("testdata", "SketchCasingWrong") sketchPath := paths.New("testdata", "SketchCasingWrong")
mainFilePath := paths.New("testadata", "sketchcasingwrong.ino").String() 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.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")) expectedError := fmt.Sprintf("no valid sketch found in %s: missing %s", sketchPath.String(), sketchPath.Join(sketchPath.Base()+".ino"))
assert.EqualError(t, err, expectedError) assert.EqualError(t, err, expectedError)
} }
...@@ -90,7 +93,7 @@ func TestNewSketchCasingWrong(t *testing.T) { ...@@ -90,7 +93,7 @@ func TestNewSketchCasingWrong(t *testing.T) {
func TestNewSketchCasingCorrect(t *testing.T) { func TestNewSketchCasingCorrect(t *testing.T) {
sketchPath := paths.New("testdata", "SketchCasingCorrect").String() sketchPath := paths.New("testdata", "SketchCasingCorrect").String()
mainFilePath := paths.New("testadata", "SketchCasingCorrect.ino").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.NotNil(t, sketch)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, sketchPath, sketch.LocationPath) assert.Equal(t, sketchPath, sketch.LocationPath)
...@@ -102,13 +105,13 @@ func TestNewSketchCasingCorrect(t *testing.T) { ...@@ -102,13 +105,13 @@ func TestNewSketchCasingCorrect(t *testing.T) {
func TestCheckSketchCasingWrong(t *testing.T) { func TestCheckSketchCasingWrong(t *testing.T) {
sketchFolder := paths.New("testdata", "SketchCasingWrong") 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")) expectedError := fmt.Sprintf("no valid sketch found in %s: missing %s", sketchFolder, sketchFolder.Join(sketchFolder.Base()+".ino"))
assert.EqualError(t, err, expectedError) assert.EqualError(t, err, expectedError)
} }
func TestCheckSketchCasingCorrect(t *testing.T) { func TestCheckSketchCasingCorrect(t *testing.T) {
sketchFolder := paths.New("testdata", "SketchCasingCorrect").String() sketchFolder := paths.New("testdata", "SketchCasingCorrect").String()
err := sketch.CheckSketchCasing(sketchFolder) err := CheckSketchCasing(sketchFolder)
require.NoError(t, err) require.NoError(t, err)
} }
...@@ -19,6 +19,7 @@ import ( ...@@ -19,6 +19,7 @@ import (
"fmt" "fmt"
bldr "github.com/arduino/arduino-cli/arduino/builder" 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/builder_utils"
"github.com/arduino/arduino-cli/legacy/builder/types" "github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/go-paths-helper" "github.com/arduino/go-paths-helper"
...@@ -63,7 +64,10 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context) ...@@ -63,7 +64,10 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
// load sketch // load sketch
sketch, err := bldr.SketchLoad(sketchLocation.String(), ctx.BuildPath.String()) 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) return errors.WithStack(err)
} }
if sketch.MainFile == nil { if sketch.MainFile == nil {
......
...@@ -91,19 +91,20 @@ type Context struct { ...@@ -91,19 +91,20 @@ type Context struct {
PlatformKeyRewrites PlatforKeysRewrite PlatformKeyRewrites PlatforKeysRewrite
HardwareRewriteResults map[*cores.PlatformRelease][]PlatforKeyRewrite HardwareRewriteResults map[*cores.PlatformRelease][]PlatforKeyRewrite
BuildProperties *properties.Map BuildProperties *properties.Map
BuildCore string BuildCore string
BuildPath *paths.Path BuildPath *paths.Path
BuildCachePath *paths.Path BuildCachePath *paths.Path
SketchBuildPath *paths.Path SketchBuildPath *paths.Path
CoreBuildPath *paths.Path CoreBuildPath *paths.Path
CoreBuildCachePath *paths.Path CoreBuildCachePath *paths.Path
CoreArchiveFilePath *paths.Path CoreArchiveFilePath *paths.Path
CoreObjectsFiles paths.PathList CoreObjectsFiles paths.PathList
LibrariesBuildPath *paths.Path LibrariesBuildPath *paths.Path
LibrariesObjectFiles paths.PathList LibrariesObjectFiles paths.PathList
PreprocPath *paths.Path PreprocPath *paths.Path
SketchObjectFiles paths.PathList SketchObjectFiles paths.PathList
IgnoreSketchFolderNameErrors bool
CollectedSourceFiles *UniqueSourceFileQueue CollectedSourceFiles *UniqueSourceFileQueue
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment