Skip to content

Commit

Permalink
Fix crash in upload if the upload tool platform is missing (#2211)
Browse files Browse the repository at this point in the history
* Added integration test

* Fixed panic in upload tool selection
  • Loading branch information
cmaglie authored Jun 13, 2023
1 parent 8cd7297 commit 422a42c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
17 changes: 11 additions & 6 deletions commands/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,18 @@ func runProgramAction(pme *packagemanager.Explorer,
Property: fmt.Sprintf("%s.tool.%s", action, port.Protocol), // TODO: Can be done better, maybe inline getToolID(...)
Value: uploadToolID}
} else if len(split) == 2 {
p := pme.FindPlatform(&packagemanager.PlatformReference{
Package: split[0],
PlatformArchitecture: boardPlatform.Platform.Architecture,
})
if p == nil {
return &arduino.PlatformNotFoundError{Platform: split[0] + ":" + boardPlatform.Platform.Architecture}
}
uploadToolID = split[1]
uploadToolPlatform = pme.GetInstalledPlatformRelease(
pme.FindPlatform(&packagemanager.PlatformReference{
Package: split[0],
PlatformArchitecture: boardPlatform.Platform.Architecture,
}),
)
uploadToolPlatform = pme.GetInstalledPlatformRelease(p)
if uploadToolPlatform == nil {
return &arduino.PlatformNotFoundError{Platform: split[0] + ":" + boardPlatform.Platform.Architecture}
}
}

// Build configuration for upload
Expand Down
24 changes: 24 additions & 0 deletions internal/integrationtest/upload/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,27 @@ func TestCompileAndUploadToPortWithBoardAutodetect(t *testing.T) {
require.NoError(t, err)
}
}

func TestPanicIfUploadToolReferenceAnInexistentPlatform(t *testing.T) {
// See: https://github.com/arduino/arduino-cli/issues/2042

env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

// Run update-index with our test index
_, _, err := cli.Run("core", "install", "arduino:avr@1.8.5")
require.NoError(t, err)

// Install test data into datadir
boardsTxt := cli.DataDir().Join("packages", "arduino", "hardware", "avr", "1.8.5", "boards.txt")
boardsTxtData, err := boardsTxt.ReadFile()
require.NoError(t, err)
boardsTxtData = append(boardsTxtData, []byte("uno.upload.tool.default=foo:bar\n")...)
require.NoError(t, boardsTxt.WriteFile(boardsTxtData))

sketch, err := paths.New("..", "testdata", "bare_minimum").Abs()
require.NoError(t, err)

stdout, _, _ := cli.Run("compile", "-b", "arduino:avr:uno", "--upload", sketch.String())
require.NotContains(t, stdout, []byte("panic:"))
}

0 comments on commit 422a42c

Please sign in to comment.