Skip to content

Commit

Permalink
Allow the user to specify {fqbn} parameter in the commandline (#905)
Browse files Browse the repository at this point in the history
* Put upload test under upload package

* Allow user to specify fqbn in the commandline using the board parameter

* Add test

* apply suggestion from code review
  • Loading branch information
umbynos authored Feb 26, 2024
1 parent 16f322f commit 457b294
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func PartiallyResolve(board, file, platformPath, commandline string, extra Extra
commandline = strings.Replace(commandline, "{build.path}", filepath.ToSlash(filepath.Dir(file)), -1)
commandline = strings.Replace(commandline, "{build.project_name}", strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file))), -1)
commandline = strings.Replace(commandline, "{runtime.platform.path}", filepath.ToSlash(platformPath), -1)
commandline = strings.Replace(commandline, "{fqbn}", board, -1)

// search for runtime variables and replace with values from Locater
var runtimeRe = regexp.MustCompile("\\{(.*?)\\}")
Expand Down
61 changes: 50 additions & 11 deletions upload/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package upload_test
package upload

import (
"log"
"strings"
"testing"

"github.com/arduino/arduino-create-agent/upload"
homedir "github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
)
Expand All @@ -36,11 +35,11 @@ var TestSerialData = []struct {
Name string
Port string
Commandline string
Extra upload.Extra
Extra Extra
}{
{
"leonardo", "/dev/ttyACM0",
`"$HOME/.arduino-create/avrdude/6.3.0-arduino6/bin/avrdude" "-C$HOME/.arduino-create/avrdude/6.3.0-arduino6/etc/avrdude.conf" -v -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D "-Uflash:w:./upload_test.hex:i"`, upload.Extra{Use1200bpsTouch: true, WaitForUploadPort: true}},
`"$HOME/.arduino-create/avrdude/6.3.0-arduino6/bin/avrdude" "-C$HOME/.arduino-create/avrdude/6.3.0-arduino6/etc/avrdude.conf" -v -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D "-Uflash:w:./upload_test.hex:i"`, Extra{Use1200bpsTouch: true, WaitForUploadPort: true}},
}

func TestSerial(t *testing.T) {
Expand All @@ -51,7 +50,7 @@ func TestSerial(t *testing.T) {

for _, test := range TestSerialData {
commandline := strings.Replace(test.Commandline, "$HOME", home, -1)
err := upload.Serial(test.Port, commandline, test.Extra, logger)
err := Serial(test.Port, commandline, test.Extra, logger)
log.Println(err)
}
}
Expand All @@ -60,18 +59,58 @@ var TestResolveData = []struct {
Board string
File string
PlatformPath string
Commandline string
Extra upload.Extra
CommandLine string
Extra Extra
Result string
}{
{"arduino:avr:leonardo", "./upload_test.hex", "",
`"{runtime.tools.avrdude.path}/bin/avrdude" "-C{runtime.tools.avrdude.path}/etc/avrdude.conf" -v {upload.verify} -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D "-Uflash:w:{build.path}/{build.project_name}.hex:i"`, upload.Extra{Use1200bpsTouch: true, WaitForUploadPort: true},
`"$loc$loc{runtime.tools.avrdude.path}/bin/avrdude" "-C{runtime.tools.avrdude.path}/etc/avrdude.conf" -v $loc{upload.verify} -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D "-Uflash:w:./upload_test.hex:i"`},
{
Board: "arduino:avr:leonardo",
File: "./upload_test.hex",
PlatformPath: "",
CommandLine: `{runtime.tools.avrdude.path}/bin/avrdude -C{runtime.tools.avrdude.path}/etc/avrdude.conf -v {upload.verify} -patmega32u4 -cavr109 -P{serial.port} -b57600 -D "-Uflash:w:{build.path}/{build.project_name}.hex:i"`,
Extra: Extra{Use1200bpsTouch: true, WaitForUploadPort: true},
Result: `$loc$loc{runtime.tools.avrdude.path}/bin/avrdude -C{runtime.tools.avrdude.path}/etc/avrdude.conf -v $loc{upload.verify} -patmega32u4 -cavr109 -P$loc{serial.port} -b57600 -D "-Uflash:w:./upload_test.hex:i"`,
},
{
Board: "arduino:renesas_uno:unor4wifi",
File: "UpdateFirmware.bin",
PlatformPath: "",
CommandLine: `{runtime.tools.arduino-fwuploader.path}/arduino-fwuploader firmware flash -a {serial.port} -b {fqbn} -v --retries 5"`,
Extra: Extra{Use1200bpsTouch: true, WaitForUploadPort: true},
Result: `$loc{runtime.tools.arduino-fwuploader.path}/arduino-fwuploader firmware flash -a $loc{serial.port} -b arduino:renesas_uno:unor4wifi -v --retries 5"`,
},
}

func TestResolve(t *testing.T) {
for _, test := range TestResolveData {
result, _ := upload.PartiallyResolve(test.Board, test.File, test.PlatformPath, test.Commandline, test.Extra, mockTools{})
result, _ := PartiallyResolve(test.Board, test.File, test.PlatformPath, test.CommandLine, test.Extra, mockTools{})
if result != test.Result {
t.Error("expected " + test.Result + ", got " + result)
continue
}
}
}

var TestFixupData = []struct {
Port string
CommandLine string
Result string
}{
{
Port: "/dev/ttyACM0",
CommandLine: `{runtime.tools.avrdude.path}/bin/avrdude -C{runtime.tools.avrdude.path}/etc/avrdude.conf -v {upload.verify} -patmega32u4 -cavr109 -P{serial.port} -b57600 -D "-Uflash:w:{build.path}/{build.project_name}.hex:i"`,
Result: `{runtime.tools.avrdude.path}/bin/avrdude -C{runtime.tools.avrdude.path}/etc/avrdude.conf -v {upload.verify} -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D "-Uflash:w:{build.path}/{build.project_name}.hex:i"`,
},
{
Port: "/dev/cu.usbmodemDC5475C5557C2",
CommandLine: `{runtime.tools.arduino-fwuploader.path}/arduino-fwuploader firmware flash -a {serial.port} -b arduino:renesas_uno:unor4wifi -v --retries 5"`,
Result: `{runtime.tools.arduino-fwuploader.path}/arduino-fwuploader firmware flash -a /dev/cu.usbmodemDC5475C5557C2 -b arduino:renesas_uno:unor4wifi -v --retries 5"`,
},
}

func TestFixupPort(t *testing.T) {
for _, test := range TestFixupData {
result := fixupPort(test.Port, test.CommandLine)
if result != test.Result {
t.Error("expected " + test.Result + ", got " + result)
continue
Expand Down

0 comments on commit 457b294

Please sign in to comment.