From 457b29435d6a3fc99aae906a938929d062c66ed4 Mon Sep 17 00:00:00 2001 From: Umberto Baldi <34278123+umbynos@users.noreply.github.com> Date: Mon, 26 Feb 2024 16:38:46 +0100 Subject: [PATCH] Allow the user to specify `{fqbn}` parameter in the commandline (#905) * 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 --- upload/upload.go | 1 + upload/upload_test.go | 61 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/upload/upload.go b/upload/upload.go index 0a3b0bace..6e899c817 100644 --- a/upload/upload.go +++ b/upload/upload.go @@ -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("\\{(.*?)\\}") diff --git a/upload/upload_test.go b/upload/upload_test.go index 7f5f46418..1e030b602 100644 --- a/upload/upload_test.go +++ b/upload/upload_test.go @@ -13,14 +13,13 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -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" ) @@ -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) { @@ -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) } } @@ -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