Skip to content

Commit

Permalink
Further refine the cases handled by --input-dir (arduino#991)
Browse files Browse the repository at this point in the history
* 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 arduino#765 (comment)
  • Loading branch information
cmaglie authored Oct 14, 2020
1 parent 9f49ea3 commit 969473d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions commands/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,15 @@ func detectSketchNameFromBuildPath(buildPath *paths.Path) (string, error) {
return "", err
}

if absBuildPath, err := buildPath.Abs(); err == nil {
candidateName := absBuildPath.Base() + ".ino"
f := files.Clone()
f.FilterPrefix(candidateName + ".")
if f.Len() > 0 {
return candidateName, nil
}
}

candidateName := ""
var candidateFile *paths.Path
for _, file := range files {
Expand Down
46 changes: 24 additions & 22 deletions commands/upload/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
fqbn *cores.FQBN
resBuildPath string
resSketchName string
hasError bool
}

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

tests := []test{
// 00: error: no data passed in
{"", "", nil, nil, "<nil>", "", true},
{"", "", nil, nil, "<nil>", ""},
// 01: use importFile to detect build.path and project_name
{"testdata/build_path_2/Blink.ino.hex", "", nil, nil, "testdata/build_path_2", "Blink.ino", false},
{"testdata/build_path_2/Blink.ino.hex", "", nil, nil, "testdata/build_path_2", "Blink.ino"},
// 02: use importPath as build.path and project_name
{"", "testdata/build_path_2", nil, nil, "testdata/build_path_2", "Blink.ino", false},
{"", "testdata/build_path_2", nil, nil, "testdata/build_path_2", "Blink.ino"},
// 03: error: used both importPath and importFile
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, nil, "<nil>", "", true},
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, nil, "<nil>", ""},
// 04: error: only sketch without FQBN
{"", "", blonk, nil, "<nil>", "", true},
{"", "", blonk, nil, "<nil>", ""},
// 05: use importFile to detect build.path and project_name, sketch is ignored.
{"testdata/build_path_2/Blink.ino.hex", "", blonk, nil, "testdata/build_path_2", "Blink.ino", false},
{"testdata/build_path_2/Blink.ino.hex", "", blonk, nil, "testdata/build_path_2", "Blink.ino"},
// 06: use importPath as build.path and Blink as project name, ignore the sketch Blonk
{"", "testdata/build_path_2", blonk, nil, "testdata/build_path_2", "Blink.ino", false},
{"", "testdata/build_path_2", blonk, nil, "testdata/build_path_2", "Blink.ino"},
// 07: error: used both importPath and importFile
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", blonk, nil, "<nil>", "", true},
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", blonk, nil, "<nil>", ""},

// 08: error: no data passed in
{"", "", nil, fqbn, "<nil>", "", true},
{"", "", nil, fqbn, "<nil>", ""},
// 09: use importFile to detect build.path and project_name, fqbn ignored
{"testdata/build_path_2/Blink.ino.hex", "", nil, fqbn, "testdata/build_path_2", "Blink.ino", false},
{"testdata/build_path_2/Blink.ino.hex", "", nil, fqbn, "testdata/build_path_2", "Blink.ino"},
// 10: use importPath as build.path and project_name, fqbn ignored
{"", "testdata/build_path_2", nil, fqbn, "testdata/build_path_2", "Blink.ino", false},
{"", "testdata/build_path_2", nil, fqbn, "testdata/build_path_2", "Blink.ino"},
// 11: error: used both importPath and importFile
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, fqbn, "<nil>", "", true},
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, fqbn, "<nil>", ""},
// 12: use sketch to determine project name and sketch+fqbn to determine build path
{"", "", blonk, fqbn, "testdata/Blonk/build/arduino.samd.mkr1000", "Blonk.ino", false},
{"", "", blonk, fqbn, "testdata/Blonk/build/arduino.samd.mkr1000", "Blonk.ino"},
// 13: use importFile to detect build.path and project_name, sketch+fqbn is ignored.
{"testdata/build_path_2/Blink.ino.hex", "", blonk, fqbn, "testdata/build_path_2", "Blink.ino", false},
{"testdata/build_path_2/Blink.ino.hex", "", blonk, fqbn, "testdata/build_path_2", "Blink.ino"},
// 14: use importPath as build.path and Blink as project name, ignore the sketch Blonk, ignore fqbn
{"", "testdata/build_path_2", blonk, fqbn, "testdata/build_path_2", "Blink.ino", false},
{"", "testdata/build_path_2", blonk, fqbn, "testdata/build_path_2", "Blink.ino"},
// 15: error: used both importPath and importFile
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", blonk, fqbn, "<nil>", "", true},
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", blonk, fqbn, "<nil>", ""},

// 16: importPath containing multiple firmwares, but one has the same name as the containing folder
{"", "testdata/firmware", nil, fqbn, "testdata/firmware", "firmware.ino"},
// 17: importFile among multiple firmwares
{"testdata/firmware/another_firmware.ino.bin", "", nil, fqbn, "testdata/firmware", "another_firmware.ino"},
}
for i, test := range tests {
t.Run(fmt.Sprintf("SubTest%02d", i), func(t *testing.T) {
buildPath, sketchName, err := determineBuildPathAndSketchName(test.importFile, test.importDir, test.sketch, test.fqbn)
if test.hasError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
if test.resBuildPath == "<nil>" {
require.Error(t, err)
require.Nil(t, buildPath)
} else {
require.NoError(t, err)
resBuildPath := paths.New(test.resBuildPath)
require.NoError(t, resBuildPath.ToAbs())
require.NotNil(t, buildPath)
require.NoError(t, buildPath.ToAbs())
require.Equal(t, resBuildPath.String(), buildPath.String())
}
Expand Down

0 comments on commit 969473d

Please sign in to comment.