Skip to content

Commit 2c7b6ba

Browse files
authored
[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
1 parent c977a23 commit 2c7b6ba

File tree

4 files changed

+57
-32
lines changed

4 files changed

+57
-32
lines changed

arduino/sketch/sketch.go

+24-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package sketch
1717

1818
import (
19+
"fmt"
1920
"io/ioutil"
2021
"path/filepath"
2122
"sort"
@@ -124,17 +125,22 @@ func New(sketchFolderPath, mainFilePath, buildPath string, allFilesPaths []strin
124125
sort.Sort(ItemByPath(otherSketchFiles))
125126
sort.Sort(ItemByPath(rootFolderFiles))
126127

127-
if err := CheckSketchCasing(sketchFolderPath); err != nil {
128-
return nil, err
129-
}
130-
131-
return &Sketch{
128+
sk := &Sketch{
132129
MainFile: mainFile,
133130
LocationPath: sketchFolderPath,
134131
OtherSketchFiles: otherSketchFiles,
135132
AdditionalFiles: additionalFiles,
136133
RootFolderFiles: rootFolderFiles,
137-
}, nil
134+
}
135+
err := CheckSketchCasing(sketchFolderPath)
136+
if e, ok := err.(*InvalidSketchFoldernameError); ok {
137+
e.Sketch = sk
138+
return nil, e
139+
}
140+
if err != nil {
141+
return nil, err
142+
}
143+
return sk, nil
138144
}
139145

140146
// 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 {
160166
if files.Len() == 0 {
161167
sketchFolderPath := paths.New(sketchFolder)
162168
sketchFile := sketchFolderPath.Join(sketchFolderPath.Base() + globals.MainFileValidExtension)
163-
return errors.Errorf("no valid sketch found in %s: missing %s", sketchFolderPath, sketchFile)
169+
return &InvalidSketchFoldernameError{SketchFolder: sketchFolderPath, SketchFile: sketchFile}
164170
}
165171

166172
return nil
167173
}
174+
175+
// InvalidSketchFoldernameError is returned when the sketch directory doesn't match the sketch name
176+
type InvalidSketchFoldernameError struct {
177+
SketchFolder *paths.Path
178+
SketchFile *paths.Path
179+
Sketch *Sketch
180+
}
181+
182+
func (e *InvalidSketchFoldernameError) Error() string {
183+
return fmt.Sprintf("no valid sketch found in %s: missing %s", e.SketchFolder, e.SketchFile)
184+
}

arduino/sketch/sketch_test.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,22 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to [email protected].
1515

16-
package sketch_test
16+
package sketch
1717

1818
import (
1919
"fmt"
2020
"path/filepath"
2121
"sort"
2222
"testing"
2323

24-
"github.com/arduino/arduino-cli/arduino/sketch"
2524
"github.com/arduino/go-paths-helper"
2625
"github.com/stretchr/testify/assert"
2726
"github.com/stretchr/testify/require"
2827
)
2928

3029
func TestNewItem(t *testing.T) {
3130
sketchItem := filepath.Join("testdata", t.Name()+".ino")
32-
item := sketch.NewItem(sketchItem)
31+
item := NewItem(sketchItem)
3332
assert.Equal(t, sketchItem, item.Path)
3433
sourceBytes, err := item.GetSourceBytes()
3534
assert.Nil(t, err)
@@ -38,20 +37,20 @@ func TestNewItem(t *testing.T) {
3837
assert.Nil(t, err)
3938
assert.Equal(t, "#include <testlib.h>", sourceStr)
4039

41-
item = sketch.NewItem("doesnt/exist")
40+
item = NewItem("doesnt/exist")
4241
sourceBytes, err = item.GetSourceBytes()
4342
assert.Nil(t, sourceBytes)
4443
assert.NotNil(t, err)
4544
}
4645

4746
func TestSort(t *testing.T) {
48-
items := []*sketch.Item{
47+
items := []*Item{
4948
{"foo"},
5049
{"baz"},
5150
{"bar"},
5251
}
5352

54-
sort.Sort(sketch.ItemByPath(items))
53+
sort.Sort(ItemByPath(items))
5554

5655
assert.Equal(t, "bar", items[0].Path)
5756
assert.Equal(t, "baz", items[1].Path)
@@ -67,7 +66,7 @@ func TestNew(t *testing.T) {
6766
otherFile,
6867
}
6968

70-
sketch, err := sketch.New(sketchFolderPath, mainFilePath, "", allFilesPaths)
69+
sketch, err := New(sketchFolderPath, mainFilePath, "", allFilesPaths)
7170
assert.Nil(t, err)
7271
assert.Equal(t, mainFilePath, sketch.MainFile.Path)
7372
assert.Equal(t, sketchFolderPath, sketch.LocationPath)
@@ -81,16 +80,20 @@ func TestNew(t *testing.T) {
8180
func TestNewSketchCasingWrong(t *testing.T) {
8281
sketchPath := paths.New("testdata", "SketchCasingWrong")
8382
mainFilePath := paths.New("testadata", "sketchcasingwrong.ino").String()
84-
sketch, err := sketch.New(sketchPath.String(), mainFilePath, "", []string{mainFilePath})
83+
sketch, err := New(sketchPath.String(), mainFilePath, "", []string{mainFilePath})
8584
assert.Nil(t, sketch)
85+
assert.Error(t, err)
86+
assert.IsType(t, &InvalidSketchFoldernameError{}, err)
87+
e := err.(*InvalidSketchFoldernameError)
88+
assert.NotNil(t, e.Sketch)
8689
expectedError := fmt.Sprintf("no valid sketch found in %s: missing %s", sketchPath.String(), sketchPath.Join(sketchPath.Base()+".ino"))
8790
assert.EqualError(t, err, expectedError)
8891
}
8992

9093
func TestNewSketchCasingCorrect(t *testing.T) {
9194
sketchPath := paths.New("testdata", "SketchCasingCorrect").String()
9295
mainFilePath := paths.New("testadata", "SketchCasingCorrect.ino").String()
93-
sketch, err := sketch.New(sketchPath, mainFilePath, "", []string{mainFilePath})
96+
sketch, err := New(sketchPath, mainFilePath, "", []string{mainFilePath})
9497
assert.NotNil(t, sketch)
9598
assert.NoError(t, err)
9699
assert.Equal(t, sketchPath, sketch.LocationPath)
@@ -102,13 +105,13 @@ func TestNewSketchCasingCorrect(t *testing.T) {
102105

103106
func TestCheckSketchCasingWrong(t *testing.T) {
104107
sketchFolder := paths.New("testdata", "SketchCasingWrong")
105-
err := sketch.CheckSketchCasing(sketchFolder.String())
108+
err := CheckSketchCasing(sketchFolder.String())
106109
expectedError := fmt.Sprintf("no valid sketch found in %s: missing %s", sketchFolder, sketchFolder.Join(sketchFolder.Base()+".ino"))
107110
assert.EqualError(t, err, expectedError)
108111
}
109112

110113
func TestCheckSketchCasingCorrect(t *testing.T) {
111114
sketchFolder := paths.New("testdata", "SketchCasingCorrect").String()
112-
err := sketch.CheckSketchCasing(sketchFolder)
115+
err := CheckSketchCasing(sketchFolder)
113116
require.NoError(t, err)
114117
}

legacy/builder/container_setup.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020

2121
bldr "github.com/arduino/arduino-cli/arduino/builder"
22+
sk "github.com/arduino/arduino-cli/arduino/sketch"
2223
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
2324
"github.com/arduino/arduino-cli/legacy/builder/types"
2425
"github.com/arduino/go-paths-helper"
@@ -63,7 +64,10 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
6364

6465
// load sketch
6566
sketch, err := bldr.SketchLoad(sketchLocation.String(), ctx.BuildPath.String())
66-
if err != nil {
67+
if e, ok := err.(*sk.InvalidSketchFoldernameError); ctx.IgnoreSketchFolderNameErrors && ok {
68+
// ignore error
69+
sketch = e.Sketch
70+
} else if err != nil {
6771
return errors.WithStack(err)
6872
}
6973
if sketch.MainFile == nil {

legacy/builder/types/context.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,20 @@ type Context struct {
9191
PlatformKeyRewrites PlatforKeysRewrite
9292
HardwareRewriteResults map[*cores.PlatformRelease][]PlatforKeyRewrite
9393

94-
BuildProperties *properties.Map
95-
BuildCore string
96-
BuildPath *paths.Path
97-
BuildCachePath *paths.Path
98-
SketchBuildPath *paths.Path
99-
CoreBuildPath *paths.Path
100-
CoreBuildCachePath *paths.Path
101-
CoreArchiveFilePath *paths.Path
102-
CoreObjectsFiles paths.PathList
103-
LibrariesBuildPath *paths.Path
104-
LibrariesObjectFiles paths.PathList
105-
PreprocPath *paths.Path
106-
SketchObjectFiles paths.PathList
94+
BuildProperties *properties.Map
95+
BuildCore string
96+
BuildPath *paths.Path
97+
BuildCachePath *paths.Path
98+
SketchBuildPath *paths.Path
99+
CoreBuildPath *paths.Path
100+
CoreBuildCachePath *paths.Path
101+
CoreArchiveFilePath *paths.Path
102+
CoreObjectsFiles paths.PathList
103+
LibrariesBuildPath *paths.Path
104+
LibrariesObjectFiles paths.PathList
105+
PreprocPath *paths.Path
106+
SketchObjectFiles paths.PathList
107+
IgnoreSketchFolderNameErrors bool
107108

108109
CollectedSourceFiles *UniqueSourceFileQueue
109110

0 commit comments

Comments
 (0)