|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2022 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package compile_part_1_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "crypto/md5" |
| 20 | + "encoding/hex" |
| 21 | + "encoding/json" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/arduino/arduino-cli/internal/integrationtest" |
| 26 | + "github.com/arduino/go-paths-helper" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + "go.bug.st/testifyjson/requirejson" |
| 29 | +) |
| 30 | + |
| 31 | +func TestCompileWithOutputDirFlag(t *testing.T) { |
| 32 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 33 | + defer env.CleanUp() |
| 34 | + |
| 35 | + // Init the environment explicitly |
| 36 | + _, _, err := cli.Run("core", "update-index") |
| 37 | + require.NoError(t, err) |
| 38 | + |
| 39 | + // Download latest AVR |
| 40 | + _, _, err = cli.Run("core", "install", "arduino:avr") |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + sketchName := "CompileWithOutputDir" |
| 44 | + sketchPath := cli.SketchbookDir().Join(sketchName) |
| 45 | + fqbn := "arduino:avr:uno" |
| 46 | + |
| 47 | + // Create a test sketch |
| 48 | + stdout, _, err := cli.Run("sketch", "new", sketchPath.String()) |
| 49 | + require.NoError(t, err) |
| 50 | + require.Contains(t, string(stdout), "Sketch created in: "+sketchPath.String()) |
| 51 | + |
| 52 | + // Test the --output-dir flag with absolute path |
| 53 | + outputDir := cli.SketchbookDir().Join("test_dir", "output_dir") |
| 54 | + _, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--output-dir", outputDir.String()) |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + // Verifies expected binaries have been built |
| 58 | + md5 := md5.Sum(([]byte(sketchPath.String()))) |
| 59 | + sketchPathMd5 := strings.ToUpper(hex.EncodeToString(md5[:])) |
| 60 | + require.NotEmpty(t, sketchPathMd5) |
| 61 | + buildDir := paths.TempDir().Join("arduino-sketch-" + sketchPathMd5) |
| 62 | + require.FileExists(t, buildDir.Join(sketchName+".ino.eep").String()) |
| 63 | + require.FileExists(t, buildDir.Join(sketchName+".ino.elf").String()) |
| 64 | + require.FileExists(t, buildDir.Join(sketchName+".ino.hex").String()) |
| 65 | + require.FileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.bin").String()) |
| 66 | + require.FileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.hex").String()) |
| 67 | + |
| 68 | + // Verifies binaries are exported when --output-dir flag is specified |
| 69 | + require.DirExists(t, outputDir.String()) |
| 70 | + require.FileExists(t, outputDir.Join(sketchName+".ino.eep").String()) |
| 71 | + require.FileExists(t, outputDir.Join(sketchName+".ino.elf").String()) |
| 72 | + require.FileExists(t, outputDir.Join(sketchName+".ino.hex").String()) |
| 73 | + require.FileExists(t, outputDir.Join(sketchName+".ino.with_bootloader.bin").String()) |
| 74 | + require.FileExists(t, outputDir.Join(sketchName+".ino.with_bootloader.hex").String()) |
| 75 | +} |
| 76 | + |
| 77 | +func TestCompileWithExportBinariesFlag(t *testing.T) { |
| 78 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 79 | + defer env.CleanUp() |
| 80 | + |
| 81 | + // Init the environment explicitly |
| 82 | + _, _, err := cli.Run("core", "update-index") |
| 83 | + require.NoError(t, err) |
| 84 | + |
| 85 | + // Download latest AVR |
| 86 | + _, _, err = cli.Run("core", "install", "arduino:avr") |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + sketchName := "CompileWithExportBinariesFlag" |
| 90 | + sketchPath := cli.SketchbookDir().Join(sketchName) |
| 91 | + fqbn := "arduino:avr:uno" |
| 92 | + |
| 93 | + // Create a test sketch |
| 94 | + _, _, err = cli.Run("sketch", "new", sketchPath.String()) |
| 95 | + require.NoError(t, err) |
| 96 | + |
| 97 | + // Test the --output-dir flag with absolute path |
| 98 | + _, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--export-binaries") |
| 99 | + require.NoError(t, err) |
| 100 | + require.DirExists(t, sketchPath.Join("build").String()) |
| 101 | + |
| 102 | + // Verifies binaries are exported when --export-binaries flag is set |
| 103 | + fqbn = strings.ReplaceAll(fqbn, ":", ".") |
| 104 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.eep").String()) |
| 105 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.elf").String()) |
| 106 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.hex").String()) |
| 107 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.with_bootloader.bin").String()) |
| 108 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.with_bootloader.hex").String()) |
| 109 | +} |
| 110 | + |
| 111 | +func TestCompileWithCustomBuildPath(t *testing.T) { |
| 112 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 113 | + defer env.CleanUp() |
| 114 | + |
| 115 | + // Init the environment explicitly |
| 116 | + _, _, err := cli.Run("core", "update-index") |
| 117 | + require.NoError(t, err) |
| 118 | + |
| 119 | + // Download latest AVR |
| 120 | + _, _, err = cli.Run("core", "install", "arduino:avr") |
| 121 | + require.NoError(t, err) |
| 122 | + |
| 123 | + sketchName := "CompileWithBuildPath" |
| 124 | + sketchPath := cli.SketchbookDir().Join(sketchName) |
| 125 | + fqbn := "arduino:avr:uno" |
| 126 | + |
| 127 | + // Create a test sketch |
| 128 | + _, _, err = cli.Run("sketch", "new", sketchPath.String()) |
| 129 | + require.NoError(t, err) |
| 130 | + |
| 131 | + // Test the --build-path flag with absolute path |
| 132 | + buildPath := cli.DataDir().Join("test_dir", "build_dir") |
| 133 | + _, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--build-path", buildPath.String()) |
| 134 | + require.NoError(t, err) |
| 135 | + |
| 136 | + // Verifies expected binaries have been built to build_path |
| 137 | + require.DirExists(t, buildPath.String()) |
| 138 | + require.FileExists(t, buildPath.Join(sketchName+".ino.eep").String()) |
| 139 | + require.FileExists(t, buildPath.Join(sketchName+".ino.elf").String()) |
| 140 | + require.FileExists(t, buildPath.Join(sketchName+".ino.hex").String()) |
| 141 | + require.FileExists(t, buildPath.Join(sketchName+".ino.with_bootloader.bin").String()) |
| 142 | + require.FileExists(t, buildPath.Join(sketchName+".ino.with_bootloader.hex").String()) |
| 143 | + |
| 144 | + // Verifies there are no binaries in temp directory |
| 145 | + md5 := md5.Sum(([]byte(sketchPath.String()))) |
| 146 | + sketchPathMd5 := strings.ToUpper(hex.EncodeToString(md5[:])) |
| 147 | + require.NotEmpty(t, sketchPathMd5) |
| 148 | + buildDir := paths.TempDir().Join("arduino-sketch-" + sketchPathMd5) |
| 149 | + require.NoFileExists(t, buildDir.Join(sketchName+".ino.eep").String()) |
| 150 | + require.NoFileExists(t, buildDir.Join(sketchName+".ino.elf").String()) |
| 151 | + require.NoFileExists(t, buildDir.Join(sketchName+".ino.hex").String()) |
| 152 | + require.NoFileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.bin").String()) |
| 153 | + require.NoFileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.hex").String()) |
| 154 | +} |
| 155 | + |
| 156 | +func TestCompileWithExportBinariesEnvVar(t *testing.T) { |
| 157 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 158 | + defer env.CleanUp() |
| 159 | + |
| 160 | + // Init the environment explicitly |
| 161 | + _, _, err := cli.Run("core", "update-index") |
| 162 | + require.NoError(t, err) |
| 163 | + |
| 164 | + // Download latest AVR |
| 165 | + _, _, err = cli.Run("core", "install", "arduino:avr") |
| 166 | + require.NoError(t, err) |
| 167 | + |
| 168 | + sketchName := "CompileWithExportBinariesEnvVar" |
| 169 | + sketchPath := cli.SketchbookDir().Join(sketchName) |
| 170 | + fqbn := "arduino:avr:uno" |
| 171 | + |
| 172 | + // Create a test sketch |
| 173 | + _, _, err = cli.Run("sketch", "new", sketchPath.String()) |
| 174 | + require.NoError(t, err) |
| 175 | + |
| 176 | + envVar := cli.GetDefaultEnv() |
| 177 | + envVar["ARDUINO_SKETCH_ALWAYS_EXPORT_BINARIES"] = "true" |
| 178 | + |
| 179 | + // Test compilation with export binaries env var set |
| 180 | + _, _, err = cli.RunWithCustomEnv(envVar, "compile", "-b", fqbn, sketchPath.String()) |
| 181 | + require.NoError(t, err) |
| 182 | + require.DirExists(t, sketchPath.Join("build").String()) |
| 183 | + |
| 184 | + // Verifies binaries are exported when export binaries env var is set |
| 185 | + fqbn = strings.ReplaceAll(fqbn, ":", ".") |
| 186 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.eep").String()) |
| 187 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.elf").String()) |
| 188 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.hex").String()) |
| 189 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.with_bootloader.bin").String()) |
| 190 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.with_bootloader.hex").String()) |
| 191 | +} |
| 192 | + |
| 193 | +func TestCompileWithExportBinariesConfig(t *testing.T) { |
| 194 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 195 | + defer env.CleanUp() |
| 196 | + |
| 197 | + // Init the environment explicitly |
| 198 | + _, _, err := cli.Run("core", "update-index") |
| 199 | + require.NoError(t, err) |
| 200 | + |
| 201 | + // Download latest AVR |
| 202 | + _, _, err = cli.Run("core", "install", "arduino:avr") |
| 203 | + require.NoError(t, err) |
| 204 | + |
| 205 | + sketchName := "CompileWithExportBinariesEnvVar" |
| 206 | + sketchPath := cli.SketchbookDir().Join(sketchName) |
| 207 | + fqbn := "arduino:avr:uno" |
| 208 | + |
| 209 | + // Create a test sketch |
| 210 | + _, _, err = cli.Run("sketch", "new", sketchPath.String()) |
| 211 | + require.NoError(t, err) |
| 212 | + |
| 213 | + // Create settings with export binaries set to true |
| 214 | + envVar := cli.GetDefaultEnv() |
| 215 | + envVar["ARDUINO_SKETCH_ALWAYS_EXPORT_BINARIES"] = "true" |
| 216 | + _, _, err = cli.RunWithCustomEnv(envVar, "config", "init", "--dest-dir", ".") |
| 217 | + require.NoError(t, err) |
| 218 | + |
| 219 | + // Test if arduino-cli config file written in the previous run has the `always_export_binaries` flag set. |
| 220 | + stdout, _, err := cli.Run("config", "dump", "--format", "json") |
| 221 | + require.NoError(t, err) |
| 222 | + requirejson.Contains(t, stdout, ` |
| 223 | + { |
| 224 | + "sketch": { |
| 225 | + "always_export_binaries": "true" |
| 226 | + } |
| 227 | + }`) |
| 228 | + |
| 229 | + // Test compilation with export binaries env var set |
| 230 | + _, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String()) |
| 231 | + require.NoError(t, err) |
| 232 | + require.DirExists(t, sketchPath.Join("build").String()) |
| 233 | + |
| 234 | + // Verifies binaries are exported when export binaries env var is set |
| 235 | + fqbn = strings.ReplaceAll(fqbn, ":", ".") |
| 236 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.eep").String()) |
| 237 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.elf").String()) |
| 238 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.hex").String()) |
| 239 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.with_bootloader.bin").String()) |
| 240 | + require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.with_bootloader.hex").String()) |
| 241 | +} |
| 242 | + |
| 243 | +func TestCompileWithInvalidUrl(t *testing.T) { |
| 244 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 245 | + defer env.CleanUp() |
| 246 | + |
| 247 | + // Init the environment explicitly |
| 248 | + _, _, err := cli.Run("core", "update-index") |
| 249 | + require.NoError(t, err) |
| 250 | + |
| 251 | + // Download latest AVR |
| 252 | + _, _, err = cli.Run("core", "install", "arduino:avr") |
| 253 | + require.NoError(t, err) |
| 254 | + |
| 255 | + sketchName := "CompileWithInvalidURL" |
| 256 | + sketchPath := cli.SketchbookDir().Join(sketchName) |
| 257 | + fqbn := "arduino:avr:uno" |
| 258 | + |
| 259 | + // Create a test sketch |
| 260 | + _, _, err = cli.Run("sketch", "new", sketchPath.String()) |
| 261 | + require.NoError(t, err) |
| 262 | + |
| 263 | + _, _, err = cli.Run("config", "init", "--dest-dir", ".", "--additional-urls", "https://example.com/package_example_index.json") |
| 264 | + require.NoError(t, err) |
| 265 | + |
| 266 | + _, stderr, err := cli.Run("compile", "-b", fqbn, sketchPath.String()) |
| 267 | + require.NoError(t, err) |
| 268 | + require.Contains(t, string(stderr), "Error initializing instance: Loading index file: loading json index file") |
| 269 | + expectedIndexfile := cli.DataDir().Join("package_example_index.json") |
| 270 | + require.Contains(t, string(stderr), "loading json index file "+expectedIndexfile.String()+": open "+expectedIndexfile.String()+":") |
| 271 | +} |
| 272 | + |
| 273 | +func TestCompileWithCustomLibraries(t *testing.T) { |
| 274 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 275 | + defer env.CleanUp() |
| 276 | + |
| 277 | + // Creates config with additional URL to install necessary core |
| 278 | + url := "http://arduino.esp8266.com/stable/package_esp8266com_index.json" |
| 279 | + _, _, err := cli.Run("config", "init", "--dest-dir", ".", "--additional-urls", url) |
| 280 | + require.NoError(t, err) |
| 281 | + |
| 282 | + // Init the environment explicitly |
| 283 | + _, _, err = cli.Run("update") |
| 284 | + require.NoError(t, err) |
| 285 | + |
| 286 | + _, _, err = cli.Run("core", "install", "esp8266:esp8266") |
| 287 | + require.NoError(t, err) |
| 288 | + |
| 289 | + sketchName := "sketch_with_multiple_custom_libraries" |
| 290 | + sketchPath := cli.CopySketch(sketchName) |
| 291 | + fqbn := "esp8266:esp8266:nodemcu:xtal=80,vt=heap,eesz=4M1M,wipe=none,baud=115200" |
| 292 | + |
| 293 | + firstLib := sketchPath.Join("libraries1") |
| 294 | + secondLib := sketchPath.Join("libraries2") |
| 295 | + _, _, err = cli.Run("compile", "--libraries", firstLib.String(), "--libraries", secondLib.String(), "-b", fqbn, sketchPath.String()) |
| 296 | + require.NoError(t, err) |
| 297 | +} |
| 298 | + |
| 299 | +func TestCompileWithArchivesAndLongPaths(t *testing.T) { |
| 300 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 301 | + defer env.CleanUp() |
| 302 | + |
| 303 | + // Creates config with additional URL to install necessary core |
| 304 | + url := "http://arduino.esp8266.com/stable/package_esp8266com_index.json" |
| 305 | + _, _, err := cli.Run("config", "init", "--dest-dir", ".", "--additional-urls", url) |
| 306 | + require.NoError(t, err) |
| 307 | + |
| 308 | + // Init the environment explicitly |
| 309 | + _, _, err = cli.Run("update") |
| 310 | + require.NoError(t, err) |
| 311 | + |
| 312 | + // Install core to compile |
| 313 | + _, _, err = cli. Run( "core", "install", "esp8266:[email protected]") |
| 314 | + require.NoError(t, err) |
| 315 | + |
| 316 | + // Install test library |
| 317 | + _, _, err = cli.Run("lib", "install", "ArduinoIoTCloud") |
| 318 | + require.NoError(t, err) |
| 319 | + |
| 320 | + stdout, _, err := cli.Run("lib", "examples", "ArduinoIoTCloud", "--format", "json") |
| 321 | + require.NoError(t, err) |
| 322 | + var libOutput []map[string]interface{} |
| 323 | + err = json.Unmarshal(stdout, &libOutput) |
| 324 | + require.NoError(t, err) |
| 325 | + sketchPath := paths.New(libOutput[0]["library"].(map[string]interface{})["install_dir"].(string)) |
| 326 | + sketchPath = sketchPath.Join("examples", "ArduinoIoTCloud-Advanced") |
| 327 | + |
| 328 | + _, _, err = cli.Run("compile", "-b", "esp8266:esp8266:huzzah", sketchPath.String()) |
| 329 | + require.NoError(t, err) |
| 330 | +} |
| 331 | + |
| 332 | +func TestCompileWithPrecompileLibrary(t *testing.T) { |
| 333 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 334 | + defer env.CleanUp() |
| 335 | + |
| 336 | + _, _, err := cli.Run("update") |
| 337 | + require.NoError(t, err) |
| 338 | + |
| 339 | + _, _, err = cli. Run( "core", "install", "arduino:[email protected]") |
| 340 | + require.NoError(t, err) |
| 341 | + fqbn := "arduino:samd:mkrzero" |
| 342 | + |
| 343 | + // Install precompiled library |
| 344 | + // For more information see: |
| 345 | + // https://arduino.github.io/arduino-cli/latest/library-specification/#precompiled-binaries |
| 346 | + _, _, err = cli. Run( "lib", "install", "BSEC Software [email protected]") |
| 347 | + require.NoError(t, err) |
| 348 | + sketchFolder := cli.SketchbookDir().Join("libraries", "BSEC_Software_Library", "examples", "basic") |
| 349 | + |
| 350 | + // Compile and verify dependencies detection for fully precompiled library is not skipped |
| 351 | + stdout, _, err := cli.Run("compile", "-b", fqbn, sketchFolder.String(), "-v") |
| 352 | + require.NoError(t, err) |
| 353 | + require.NotContains(t, string(stdout), "Skipping dependencies detection for precompiled library BSEC Software Library") |
| 354 | +} |
0 commit comments