Skip to content

Commit 54e0a8f

Browse files
author
Federico Fissore
committed
When collecting sketch files, .ino files are valid only if located in the same
folder of main .ino file. Fixes #31 Signed-off-by: Federico Fissore <[email protected]>
1 parent 88c6693 commit 54e0a8f

File tree

6 files changed

+89
-13
lines changed

6 files changed

+89
-13
lines changed

Diff for: src/arduino.cc/builder/sketch_loader.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ func (s *SketchLoader) Run(context map[string]interface{}) error {
7575

7676
logger := context[constants.CTX_LOGGER].(i18n.Logger)
7777
sketch, err := makeSketch(sketchLocation, allSketchFilePaths, logger)
78+
if err != nil {
79+
return utils.WrapError(err)
80+
}
7881

7982
context[constants.CTX_SKETCH_LOCATION] = sketchLocation
8083
context[constants.CTX_SKETCH] = sketch
@@ -107,10 +110,13 @@ func makeSketch(sketchLocation string, allSketchFilePaths []string, logger i18n.
107110

108111
additionalFiles := []types.SketchFile{}
109112
otherSketchFiles := []types.SketchFile{}
113+
mainFileDir := filepath.Dir(mainFile.Name)
110114
for _, sketchFile := range sketchFilesMap {
111115
ext := strings.ToLower(filepath.Ext(sketchFile.Name))
112116
if MAIN_FILE_VALID_EXTENSIONS[ext] {
113-
otherSketchFiles = append(otherSketchFiles, sketchFile)
117+
if filepath.Dir(sketchFile.Name) == mainFileDir {
118+
otherSketchFiles = append(otherSketchFiles, sketchFile)
119+
}
114120
} else if ADDITIONAL_FILE_VALID_EXTENSIONS[ext] {
115121
additionalFiles = append(additionalFiles, sketchFile)
116122
} else {

Diff for: src/arduino.cc/builder/test/builder_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,26 @@ func TestBuilderSketchNoFunctions(t *testing.T) {
265265
require.Error(t, err)
266266
}
267267

268+
func TestBuilderSketchWithBackup(t *testing.T) {
269+
DownloadCoresAndToolsAndLibraries(t)
270+
271+
context := make(map[string]interface{})
272+
273+
buildPath := SetupBuildPath(t, context)
274+
defer os.RemoveAll(buildPath)
275+
276+
context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"}
277+
context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "downloaded_board_manager_stuff"}
278+
context[constants.CTX_FQBN] = "arduino:avr:uno"
279+
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_with_backup_files", "sketch.ino")
280+
context[constants.CTX_LIBRARIES_FOLDERS] = []string{"libraries", "downloaded_libraries"}
281+
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"
282+
283+
command := builder.Builder{}
284+
err := command.Run(context)
285+
NoError(t, err)
286+
}
287+
268288
/*
269289
func TestBuilderALinkage(t *testing.T) {
270290
DownloadCoresAndToolsAndLibraries(t)

Diff for: src/arduino.cc/builder/test/sketch_loader_test.go

+39-12
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ func TestLoadSketch(t *testing.T) {
7777
context := make(map[string]interface{})
7878
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino")
7979

80-
loggerCommand := builder.SetupHumanLoggerIfMissing{}
81-
err := loggerCommand.Run(context)
82-
NoError(t, err)
80+
commands := []types.Command{
81+
&builder.SetupHumanLoggerIfMissing{},
82+
&builder.SketchLoader{},
83+
}
8384

84-
loader := builder.SketchLoader{}
85-
err = loader.Run(context)
86-
NoError(t, err)
85+
for _, command := range commands {
86+
err := command.Run(context)
87+
NoError(t, err)
88+
}
8789

8890
sketch := context[constants.CTX_SKETCH].(*types.Sketch)
8991
require.NotNil(t, sketch)
@@ -119,13 +121,15 @@ func TestLoadSketchFromFolder(t *testing.T) {
119121
context := make(map[string]interface{})
120122
context[constants.CTX_SKETCH_LOCATION] = "sketch_with_subfolders"
121123

122-
loggerCommand := builder.SetupHumanLoggerIfMissing{}
123-
err := loggerCommand.Run(context)
124-
NoError(t, err)
124+
commands := []types.Command{
125+
&builder.SetupHumanLoggerIfMissing{},
126+
&builder.SketchLoader{},
127+
}
125128

126-
loader := builder.SketchLoader{}
127-
err = loader.Run(context)
128-
NoError(t, err)
129+
for _, command := range commands {
130+
err := command.Run(context)
131+
NoError(t, err)
132+
}
129133

130134
sketch := context[constants.CTX_SKETCH].(*types.Sketch)
131135
require.NotNil(t, sketch)
@@ -135,3 +139,26 @@ func TestLoadSketchFromFolder(t *testing.T) {
135139
require.Equal(t, 1, len(sketch.AdditionalFiles))
136140
require.True(t, strings.Index(sketch.AdditionalFiles[0].Name, "other.cpp") != -1)
137141
}
142+
143+
func TestLoadSketchWithBackup(t *testing.T) {
144+
context := make(map[string]interface{})
145+
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_with_backup_files", "sketch.ino")
146+
147+
commands := []types.Command{
148+
&builder.SetupHumanLoggerIfMissing{},
149+
&builder.SketchLoader{},
150+
}
151+
152+
for _, command := range commands {
153+
err := command.Run(context)
154+
NoError(t, err)
155+
}
156+
157+
sketch := context[constants.CTX_SKETCH].(*types.Sketch)
158+
require.NotNil(t, sketch)
159+
160+
require.True(t, strings.Index(sketch.MainFile.Name, "sketch.ino") != -1)
161+
162+
require.Equal(t, 0, len(sketch.AdditionalFiles))
163+
require.Equal(t, 0, len(sketch.OtherSketchFiles))
164+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
void setup() {
2+
3+
}
4+
5+
void loop() {
6+
7+
broken
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
void setup() {
2+
3+
}
4+
5+
void loop() {
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
void setup(){
2+
func()();
3+
}
4+
5+
void loop(){}
6+
7+
void (*func())(){
8+
return setup;
9+
}

0 commit comments

Comments
 (0)