Skip to content

Further refine the cases handled by --input-dir #991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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