Skip to content

Commit 969473d

Browse files
authored
Further refine the cases handled by --input-dir (#991)
* Sligthly simplified upload test cases handling * Make --input-dir handle gracefully some rare cases Using `--input-dir path/to/firmware` with a directory containing: path/to/firmware/firmware.ino.bin path/to/firmware/some_other_firmware_name.ino.bin should not fail but select `firmware.ino.bin` for upload because the containing folder has the same name. See #765 (comment)
1 parent 9f49ea3 commit 969473d

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

Diff for: commands/upload/testdata/firmware/another_firmware.ino.bin

Whitespace-only changes.

Diff for: commands/upload/testdata/firmware/firmware.ino.bin

Whitespace-only changes.

Diff for: commands/upload/upload.go

+9
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,15 @@ func detectSketchNameFromBuildPath(buildPath *paths.Path) (string, error) {
438438
return "", err
439439
}
440440

441+
if absBuildPath, err := buildPath.Abs(); err == nil {
442+
candidateName := absBuildPath.Base() + ".ino"
443+
f := files.Clone()
444+
f.FilterPrefix(candidateName + ".")
445+
if f.Len() > 0 {
446+
return candidateName, nil
447+
}
448+
}
449+
441450
candidateName := ""
442451
var candidateFile *paths.Path
443452
for _, file := range files {

Diff for: commands/upload/upload_test.go

+24-22
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
5555
fqbn *cores.FQBN
5656
resBuildPath string
5757
resSketchName string
58-
hasError bool
5958
}
6059

6160
blonk, err := sketches.NewSketchFromPath(paths.New("testdata/Blonk"))
@@ -66,52 +65,55 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
6665

6766
tests := []test{
6867
// 00: error: no data passed in
69-
{"", "", nil, nil, "<nil>", "", true},
68+
{"", "", nil, nil, "<nil>", ""},
7069
// 01: use importFile to detect build.path and project_name
71-
{"testdata/build_path_2/Blink.ino.hex", "", nil, nil, "testdata/build_path_2", "Blink.ino", false},
70+
{"testdata/build_path_2/Blink.ino.hex", "", nil, nil, "testdata/build_path_2", "Blink.ino"},
7271
// 02: use importPath as build.path and project_name
73-
{"", "testdata/build_path_2", nil, nil, "testdata/build_path_2", "Blink.ino", false},
72+
{"", "testdata/build_path_2", nil, nil, "testdata/build_path_2", "Blink.ino"},
7473
// 03: error: used both importPath and importFile
75-
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, nil, "<nil>", "", true},
74+
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, nil, "<nil>", ""},
7675
// 04: error: only sketch without FQBN
77-
{"", "", blonk, nil, "<nil>", "", true},
76+
{"", "", blonk, nil, "<nil>", ""},
7877
// 05: use importFile to detect build.path and project_name, sketch is ignored.
79-
{"testdata/build_path_2/Blink.ino.hex", "", blonk, nil, "testdata/build_path_2", "Blink.ino", false},
78+
{"testdata/build_path_2/Blink.ino.hex", "", blonk, nil, "testdata/build_path_2", "Blink.ino"},
8079
// 06: use importPath as build.path and Blink as project name, ignore the sketch Blonk
81-
{"", "testdata/build_path_2", blonk, nil, "testdata/build_path_2", "Blink.ino", false},
80+
{"", "testdata/build_path_2", blonk, nil, "testdata/build_path_2", "Blink.ino"},
8281
// 07: error: used both importPath and importFile
83-
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", blonk, nil, "<nil>", "", true},
82+
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", blonk, nil, "<nil>", ""},
8483

8584
// 08: error: no data passed in
86-
{"", "", nil, fqbn, "<nil>", "", true},
85+
{"", "", nil, fqbn, "<nil>", ""},
8786
// 09: use importFile to detect build.path and project_name, fqbn ignored
88-
{"testdata/build_path_2/Blink.ino.hex", "", nil, fqbn, "testdata/build_path_2", "Blink.ino", false},
87+
{"testdata/build_path_2/Blink.ino.hex", "", nil, fqbn, "testdata/build_path_2", "Blink.ino"},
8988
// 10: use importPath as build.path and project_name, fqbn ignored
90-
{"", "testdata/build_path_2", nil, fqbn, "testdata/build_path_2", "Blink.ino", false},
89+
{"", "testdata/build_path_2", nil, fqbn, "testdata/build_path_2", "Blink.ino"},
9190
// 11: error: used both importPath and importFile
92-
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, fqbn, "<nil>", "", true},
91+
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, fqbn, "<nil>", ""},
9392
// 12: use sketch to determine project name and sketch+fqbn to determine build path
94-
{"", "", blonk, fqbn, "testdata/Blonk/build/arduino.samd.mkr1000", "Blonk.ino", false},
93+
{"", "", blonk, fqbn, "testdata/Blonk/build/arduino.samd.mkr1000", "Blonk.ino"},
9594
// 13: use importFile to detect build.path and project_name, sketch+fqbn is ignored.
96-
{"testdata/build_path_2/Blink.ino.hex", "", blonk, fqbn, "testdata/build_path_2", "Blink.ino", false},
95+
{"testdata/build_path_2/Blink.ino.hex", "", blonk, fqbn, "testdata/build_path_2", "Blink.ino"},
9796
// 14: use importPath as build.path and Blink as project name, ignore the sketch Blonk, ignore fqbn
98-
{"", "testdata/build_path_2", blonk, fqbn, "testdata/build_path_2", "Blink.ino", false},
97+
{"", "testdata/build_path_2", blonk, fqbn, "testdata/build_path_2", "Blink.ino"},
9998
// 15: error: used both importPath and importFile
100-
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", blonk, fqbn, "<nil>", "", true},
99+
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", blonk, fqbn, "<nil>", ""},
100+
101+
// 16: importPath containing multiple firmwares, but one has the same name as the containing folder
102+
{"", "testdata/firmware", nil, fqbn, "testdata/firmware", "firmware.ino"},
103+
// 17: importFile among multiple firmwares
104+
{"testdata/firmware/another_firmware.ino.bin", "", nil, fqbn, "testdata/firmware", "another_firmware.ino"},
101105
}
102106
for i, test := range tests {
103107
t.Run(fmt.Sprintf("SubTest%02d", i), func(t *testing.T) {
104108
buildPath, sketchName, err := determineBuildPathAndSketchName(test.importFile, test.importDir, test.sketch, test.fqbn)
105-
if test.hasError {
106-
require.Error(t, err)
107-
} else {
108-
require.NoError(t, err)
109-
}
110109
if test.resBuildPath == "<nil>" {
110+
require.Error(t, err)
111111
require.Nil(t, buildPath)
112112
} else {
113+
require.NoError(t, err)
113114
resBuildPath := paths.New(test.resBuildPath)
114115
require.NoError(t, resBuildPath.ToAbs())
116+
require.NotNil(t, buildPath)
115117
require.NoError(t, buildPath.ToAbs())
116118
require.Equal(t, resBuildPath.String(), buildPath.String())
117119
}

0 commit comments

Comments
 (0)