|
16 | 16 | package upload
|
17 | 17 |
|
18 | 18 | import (
|
| 19 | + "bytes" |
19 | 20 | "fmt"
|
| 21 | + "strings" |
20 | 22 | "testing"
|
21 | 23 |
|
22 | 24 | "github.com/arduino/arduino-cli/arduino/cores"
|
| 25 | + "github.com/arduino/arduino-cli/arduino/cores/packagemanager" |
23 | 26 | "github.com/arduino/arduino-cli/arduino/sketches"
|
24 | 27 | paths "github.com/arduino/go-paths-helper"
|
| 28 | + "github.com/sirupsen/logrus" |
25 | 29 | "github.com/stretchr/testify/require"
|
26 | 30 | )
|
27 | 31 |
|
@@ -121,3 +125,95 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
|
121 | 125 | })
|
122 | 126 | }
|
123 | 127 | }
|
| 128 | + |
| 129 | +func TestUploadPropertiesComposition(t *testing.T) { |
| 130 | + pm := packagemanager.NewPackageManager(nil, nil, nil, nil) |
| 131 | + err := pm.LoadHardwareFromDirectory(paths.New("testdata", "hardware")) |
| 132 | + require.NoError(t, err) |
| 133 | + buildPath1 := paths.New("testdata", "build_path_1") |
| 134 | + logrus.SetLevel(logrus.TraceLevel) |
| 135 | + type test struct { |
| 136 | + importDir *paths.Path |
| 137 | + fqbn string |
| 138 | + port string |
| 139 | + programmer string |
| 140 | + burnBootloader bool |
| 141 | + expectedOutput string |
| 142 | + expectedOutput2 string |
| 143 | + } |
| 144 | + |
| 145 | + cwdPath, err := paths.Getwd() |
| 146 | + require.NoError(t, err) |
| 147 | + cwd := strings.ReplaceAll(cwdPath.String(), "\\", "/") |
| 148 | + |
| 149 | + tests := []test{ |
| 150 | + // 0: classic upload, requires port |
| 151 | + {buildPath1, "alice:avr:board1", "port", "", false, "conf-board1 conf-general conf-upload $$VERBOSE-VERIFY$$ protocol port -bspeed testdata/build_path_1/sketch.ino.hex\n", ""}, |
| 152 | + {buildPath1, "alice:avr:board1", "", "", false, "FAIL", ""}, |
| 153 | + // 2: classic upload, no port |
| 154 | + {buildPath1, "alice:avr:board2", "port", "", false, "conf-board1 conf-general conf-upload $$VERBOSE-VERIFY$$ protocol -bspeed testdata/build_path_1/sketch.ino.hex\n", ""}, |
| 155 | + {buildPath1, "alice:avr:board2", "", "", false, "conf-board1 conf-general conf-upload $$VERBOSE-VERIFY$$ protocol -bspeed testdata/build_path_1/sketch.ino.hex\n", ""}, |
| 156 | + |
| 157 | + // 4: upload with programmer, requires port |
| 158 | + {buildPath1, "alice:avr:board1", "port", "progr1", false, "conf-board1 conf-general conf-program $$VERBOSE-VERIFY$$ progprotocol port -bspeed testdata/build_path_1/sketch.ino.hex\n", ""}, |
| 159 | + {buildPath1, "alice:avr:board1", "", "progr1", false, "FAIL", ""}, |
| 160 | + // 6: upload with programmer, no port |
| 161 | + {buildPath1, "alice:avr:board1", "port", "progr2", false, "conf-board1 conf-general conf-program $$VERBOSE-VERIFY$$ prog2protocol -bspeed testdata/build_path_1/sketch.ino.hex\n", ""}, |
| 162 | + {buildPath1, "alice:avr:board1", "", "progr2", false, "conf-board1 conf-general conf-program $$VERBOSE-VERIFY$$ prog2protocol -bspeed testdata/build_path_1/sketch.ino.hex\n", ""}, |
| 163 | + // 8: upload with programmer, require port through extra params |
| 164 | + {buildPath1, "alice:avr:board1", "port", "progr3", false, "conf-board1 conf-general conf-program $$VERBOSE-VERIFY$$ prog3protocol port -bspeed testdata/build_path_1/sketch.ino.hex\n", ""}, |
| 165 | + {buildPath1, "alice:avr:board1", "", "progr3", false, "FAIL", ""}, |
| 166 | + |
| 167 | + // 10: burn bootloader, require port |
| 168 | + {buildPath1, "alice:avr:board1", "port", "", true, "FAIL", ""}, // requires programmer |
| 169 | + {buildPath1, "alice:avr:board1", "port", "progr1", true, |
| 170 | + "ERASE conf-board1 conf-general conf-erase $$VERBOSE-VERIFY$$ genprog1protocol port -bspeed\n", |
| 171 | + "BURN conf-board1 conf-general conf-bootloader $$VERBOSE-VERIFY$$ genprog1protocol port -bspeed -F0xFF " + cwd + "/testdata/hardware/alice/avr/bootloaders/niceboot/niceboot.hex\n"}, |
| 172 | + |
| 173 | + // 12: burn bootloader, preferences override from programmers.txt |
| 174 | + {buildPath1, "alice:avr:board1", "port", "progr4", true, |
| 175 | + "ERASE conf-board1 conf-two-general conf-two-erase $$VERBOSE-VERIFY$$ prog4protocol-bootloader port -bspeed\n", |
| 176 | + "BURN conf-board1 conf-two-general conf-two-bootloader $$VERBOSE-VERIFY$$ prog4protocol-bootloader port -bspeed -F0xFF " + cwd + "/testdata/hardware/alice/avr/bootloaders/niceboot/niceboot.hex\n"}, |
| 177 | + } |
| 178 | + |
| 179 | + testRunner := func(t *testing.T, test test, verboseVerify bool) { |
| 180 | + outStream := &bytes.Buffer{} |
| 181 | + errStream := &bytes.Buffer{} |
| 182 | + err := runProgramAction( |
| 183 | + pm, |
| 184 | + nil, // sketch |
| 185 | + "", // importFile |
| 186 | + test.importDir.String(), // importDir |
| 187 | + test.fqbn, // FQBN |
| 188 | + test.port, // port |
| 189 | + test.programmer, // programmer |
| 190 | + verboseVerify, // verbose |
| 191 | + verboseVerify, // verify |
| 192 | + test.burnBootloader, // burnBootloader |
| 193 | + outStream, |
| 194 | + errStream, |
| 195 | + ) |
| 196 | + verboseVerifyOutput := "verbose verify" |
| 197 | + if !verboseVerify { |
| 198 | + verboseVerifyOutput = "quiet noverify" |
| 199 | + } |
| 200 | + if test.expectedOutput == "FAIL" { |
| 201 | + require.Error(t, err) |
| 202 | + } else { |
| 203 | + require.NoError(t, err) |
| 204 | + outFiltered := strings.ReplaceAll(outStream.String(), "\r", "") |
| 205 | + outFiltered = strings.ReplaceAll(outFiltered, "\\", "/") |
| 206 | + require.Contains(t, outFiltered, strings.ReplaceAll(test.expectedOutput, "$$VERBOSE-VERIFY$$", verboseVerifyOutput)) |
| 207 | + require.Contains(t, outFiltered, strings.ReplaceAll(test.expectedOutput2, "$$VERBOSE-VERIFY$$", verboseVerifyOutput)) |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + for i, test := range tests { |
| 212 | + t.Run(fmt.Sprintf("SubTest%02d", i), func(t *testing.T) { |
| 213 | + testRunner(t, test, false) |
| 214 | + }) |
| 215 | + t.Run(fmt.Sprintf("SubTest%02d-WithVerifyAndVerbose", i), func(t *testing.T) { |
| 216 | + testRunner(t, test, true) |
| 217 | + }) |
| 218 | + } |
| 219 | +} |
0 commit comments