forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile_part_1_test.go
713 lines (609 loc) · 30.3 KB
/
compile_part_1_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// This file is part of arduino-cli.
//
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package compile_test
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"os"
"strings"
"testing"
"github.com/arduino/arduino-cli/internal/integrationtest"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
"go.bug.st/testifyjson/requirejson"
)
func TestCompile(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Init the environment explicitly
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)
// Install Arduino AVR Boards
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
require.NoError(t, err)
integrationtest.CLISubtests{
{"WithoutFqbn", compileWithoutFqbn},
{"ErrorMessage", compileErrorMessage},
{"WithSimpleSketch", compileWithSimpleSketch},
{"OutputFlagDefaultPath", compileOutputFlagDefaultPath},
{"WithSketchWithSymlinkSelfloop", compileWithSketchWithSymlinkSelfloop},
{"BlacklistedSketchname", compileBlacklistedSketchname},
{"WithBuildPropertiesFlag", compileWithBuildPropertiesFlag},
{"WithBuildPropertyContainingQuotes", compileWithBuildPropertyContainingQuotes},
{"WithMultipleBuildPropertyFlags", compileWithMultipleBuildPropertyFlags},
{"WithOutputDirFlag", compileWithOutputDirFlag},
{"WithExportBinariesFlag", compileWithExportBinariesFlag},
{"WithCustomBuildPath", compileWithCustomBuildPath},
{"WithExportBinariesEnvVar", compileWithExportBinariesEnvVar},
{"WithExportBinariesConfig", compileWithExportBinariesConfig},
{"WithInvalidUrl", compileWithInvalidUrl},
{"WithPdeExtension", compileWithPdeExtension},
{"WithMultipleMainFiles", compileWithMultipleMainFiles},
{"CaseMismatchFails", compileCaseMismatchFails},
{"OnlyCompilationDatabaseFlag", compileOnlyCompilationDatabaseFlag},
{"UsingPlatformLocalTxt", compileUsingPlatformLocalTxt},
{"UsingBoardsLocalTxt", compileUsingBoardsLocalTxt},
}.Run(t, env, cli)
}
func compileWithoutFqbn(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
// Build sketch without FQBN
_, _, err := cli.Run("compile")
require.Error(t, err)
}
func compileErrorMessage(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
// Run a batch of bogus compile in a temp dir to check the error messages
tmp, err := paths.MkTempDir("", "tmp_dir")
require.NoError(t, err)
defer tmp.RemoveAll()
abcdef := tmp.Join("ABCDEF")
_, stderr, err := cli.Run("compile", "-b", "arduino:avr:uno", abcdef.String())
require.Error(t, err)
require.Contains(t, string(stderr), "no such file or directory:")
_, stderr, err = cli.Run("compile", "-b", "arduino:avr:uno", abcdef.Join("ABCDEF.ino").String())
require.Error(t, err)
require.Contains(t, string(stderr), "no such file or directory:")
_, stderr, err = cli.Run("compile", "-b", "arduino:avr:uno", abcdef.Join("QWERTY").String())
require.Error(t, err)
require.Contains(t, string(stderr), "no such file or directory:")
err = abcdef.Mkdir()
require.NoError(t, err)
_, stderr, err = cli.Run("compile", "-b", "arduino:avr:uno", abcdef.String())
require.Error(t, err)
require.Contains(t, string(stderr), "main file missing from sketch:")
_, stderr, err = cli.Run("compile", "-b", "arduino:avr:uno", abcdef.Join("ABCDEF.ino").String())
require.Error(t, err)
require.Contains(t, string(stderr), "no such file or directory:")
qwertyIno := abcdef.Join("QWERTY.ino")
f, err := qwertyIno.Create()
require.NoError(t, err)
defer f.Close()
_, stderr, err = cli.Run("compile", "-b", "arduino:avr:uno", qwertyIno.String())
require.Error(t, err)
require.Contains(t, string(stderr), "main file missing from sketch:")
}
func compileWithSimpleSketch(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileIntegrationTest"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Create a test sketch
stdout, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
require.Contains(t, string(stdout), "Sketch created in: "+sketchPath.String())
// Build sketch for arduino:avr:uno
_, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String())
require.NoError(t, err)
// Build sketch for arduino:avr:uno with json output
stdout, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--format", "json")
require.NoError(t, err)
// check is a valid json and contains requested data
var compileOutput map[string]interface{}
err = json.Unmarshal(stdout, &compileOutput)
require.NoError(t, err)
require.NotEmpty(t, compileOutput["compiler_out"])
require.Empty(t, compileOutput["compiler_err"])
// Verifies expected binaries have been built
md5 := md5.Sum(([]byte(sketchPath.String())))
sketchPathMd5 := strings.ToUpper(hex.EncodeToString(md5[:]))
require.NotEmpty(t, sketchPathMd5)
buildDir := paths.TempDir().Join("arduino-sketch-" + sketchPathMd5)
require.FileExists(t, buildDir.Join(sketchName+".ino.eep").String())
require.FileExists(t, buildDir.Join(sketchName+".ino.elf").String())
require.FileExists(t, buildDir.Join(sketchName+".ino.hex").String())
require.FileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.bin").String())
require.FileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.hex").String())
// Verifies binaries are not exported by default to Sketch folder
sketchBuildDir := sketchPath.Join("build" + strings.ReplaceAll(fqbn, ":", "."))
require.NoFileExists(t, sketchBuildDir.Join(sketchName+".ino.eep").String())
require.NoFileExists(t, sketchBuildDir.Join(sketchName+".ino.elf").String())
require.NoFileExists(t, sketchBuildDir.Join(sketchName+".ino.hex").String())
require.NoFileExists(t, sketchBuildDir.Join(sketchName+".ino.with_bootloader.bin").String())
require.NoFileExists(t, sketchBuildDir.Join(sketchName+".ino.with_bootloader.hex").String())
}
func compileOutputFlagDefaultPath(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
// Create a test sketch
sketchPath := cli.SketchbookDir().Join("test_output_flag_default_path")
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
// Test the --output-dir flag defaulting to current working dir
target := cli.WorkingDir().Join("test")
_, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--output-dir", "test")
require.NoError(t, err)
require.DirExists(t, target.String())
}
func compileWithSketchWithSymlinkSelfloop(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
{
sketchName := "CompileIntegrationTestSymlinkSelfLoop"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Create a test sketch
stdout, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
require.Contains(t, string(stdout), "Sketch created in: "+sketchPath.String())
// create a symlink that loops on himself
loopFilePath := sketchPath.Join("loop")
err = os.Symlink(loopFilePath.String(), loopFilePath.String())
require.NoError(t, err)
// Build sketch for arduino:avr:uno
_, stderr, err := cli.Run("compile", "-b", fqbn, sketchPath.String())
// The assertion is a bit relaxed in this case because win behaves differently from macOs and linux
// returning a different error detailed message
require.Contains(t, string(stderr), "Error opening sketch:")
require.Error(t, err)
}
{
sketchName := "CompileIntegrationTestSymlinkDirLoop"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Create a test sketch
stdout, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
require.Contains(t, string(stdout), "Sketch created in: "+sketchPath.String())
// create a symlink that loops on the upper level
loopDirPath := sketchPath.Join("loop_dir")
err = loopDirPath.Mkdir()
require.NoError(t, err)
loopDirSymlinkPath := loopDirPath.Join("loop_dir_symlink")
err = os.Symlink(loopDirPath.String(), loopDirSymlinkPath.String())
require.NoError(t, err)
// Build sketch for arduino:avr:uno
_, stderr, err := cli.Run("compile", "-b", fqbn, sketchPath.String())
// The assertion is a bit relaxed in this case because win behaves differently from macOs and linux
// returning a different error detailed message
require.Contains(t, string(stderr), "Error opening sketch:")
require.Error(t, err)
}
}
func compileBlacklistedSketchname(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
// Compile should ignore folders named `RCS`, `.git` and the likes, but
// it should be ok for a sketch to be named like RCS.ino
sketchName := "RCS"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Create a test sketch
stdout, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
require.Contains(t, string(stdout), "Sketch created in: "+sketchPath.String())
// Build sketch for arduino:avr:uno
_, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String())
require.NoError(t, err)
}
func compileWithBuildPropertiesFlag(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
{
sketchName := "sketch_with_single_string_define"
sketchPath := cli.CopySketch(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Compile using a build property with quotes
_, stderr, err := cli.Run("compile", "-b", fqbn, "--build-properties=\"build.extra_flags=\"-DMY_DEFINE=\"hello world\"\"", sketchPath.String(), "--verbose", "--clean")
require.Error(t, err)
require.NotContains(t, string(stderr), "Flag --build-properties has been deprecated, please use --build-property instead.")
// Try again with quotes
_, stderr, err = cli.Run("compile", "-b", fqbn, "--build-properties=\"build.extra_flags=-DMY_DEFINE=\"hello\"\"", sketchPath.String(), "--verbose", "--clean")
require.Error(t, err)
require.NotContains(t, string(stderr), "Flag --build-properties has been deprecated, please use --build-property instead.")
}
{
sketchName := "sketch_with_single_int_define"
sketchPath := cli.CopySketch(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Try without quotes
stdout, stderr, err := cli.Run("compile", "-b", fqbn, "--build-properties=\"build.extra_flags=-DMY_DEFINE=1\"", sketchPath.String(), "--verbose", "--clean")
require.NoError(t, err)
require.Contains(t, string(stderr), "Flag --build-properties has been deprecated, please use --build-property instead.")
require.Contains(t, string(stdout), "-DMY_DEFINE=1")
}
{
sketchName := "sketch_with_multiple_int_defines"
sketchPath := cli.CopySketch(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
stdout, stderr, err := cli.Run("compile", "-b", fqbn,
"--build-properties", "build.extra_flags=-DFIRST_PIN=1,compiler.cpp.extra_flags=-DSECOND_PIN=2",
sketchPath.String(), "--verbose", "--clean")
require.NoError(t, err)
require.Contains(t, string(stderr), "Flag --build-properties has been deprecated, please use --build-property instead.")
require.Contains(t, string(stdout), "-DFIRST_PIN=1")
require.Contains(t, string(stdout), "-DSECOND_PIN=2")
}
}
func compileWithBuildPropertyContainingQuotes(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "sketch_with_single_string_define"
sketchPath := cli.CopySketch(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Compile using a build property with quotes
stdout, _, err := cli.Run("compile",
"-b",
fqbn,
`--build-property=build.extra_flags="-DMY_DEFINE="hello world""`,
sketchPath.String(),
"--verbose")
require.NoError(t, err)
require.Contains(t, string(stdout), `-DMY_DEFINE=\"hello world\"`)
}
func compileWithMultipleBuildPropertyFlags(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "sketch_with_multiple_defines"
sketchPath := cli.CopySketch(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Compile using multiple build properties separated by a space
_, _, err := cli.Run("compile", "-b", fqbn,
"--build-property=compiler.cpp.extra_flags=\"-DPIN=2 -DSSID=\"This is a String\"\"",
sketchPath.String(), "--verbose", "--clean",
)
require.Error(t, err)
// Compile using multiple build properties separated by a space and properly quoted
stdout, _, err := cli.Run(
"compile", "-b", fqbn,
"--build-property=compiler.cpp.extra_flags=-DPIN=2 \"-DSSID=\"This is a String\"\"",
sketchPath.String(), "--verbose", "--clean",
)
require.NoError(t, err)
require.Contains(t, string(stdout), "-DPIN=2 \"-DSSID=\\\"This is a String\\\"\"")
// Tries compilation using multiple build properties separated by a comma
_, _, err = cli.Run(
"compile", "-b", fqbn,
"--build-property=compiler.cpp.extra_flags=\"-DPIN=2,-DSSID=\"This is a String\"\"",
sketchPath.String(), "--verbose", "--clean",
)
require.Error(t, err)
stdout, _, err = cli.Run(
"compile", "-b", fqbn,
"--build-property=compiler.cpp.extra_flags=\"-DPIN=2\"",
"--build-property=compiler.cpp.extra_flags=\"-DSSID=\"This is a String\"\"",
sketchPath.String(), "--verbose", "--clean",
)
require.Error(t, err)
require.NotContains(t, string(stdout), "-DPIN=2")
require.Contains(t, string(stdout), "-DSSID=\\\"This is a String\\\"")
stdout, _, err = cli.Run(
"compile", "-b", fqbn,
"--build-property=compiler.cpp.extra_flags=\"-DPIN=2\"",
"--build-property=build.extra_flags=\"-DSSID=\"hello world\"\"",
sketchPath.String(), "--verbose", "--clean",
)
require.NoError(t, err)
require.Contains(t, string(stdout), "-DPIN=2")
require.Contains(t, string(stdout), "-DSSID=\\\"hello world\\\"")
}
func compileWithOutputDirFlag(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileWithOutputDir"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Create a test sketch
stdout, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
require.Contains(t, string(stdout), "Sketch created in: "+sketchPath.String())
// Test the --output-dir flag with absolute path
outputDir := cli.SketchbookDir().Join("test_dir", "output_dir")
_, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--output-dir", outputDir.String())
require.NoError(t, err)
// Verifies expected binaries have been built
md5 := md5.Sum(([]byte(sketchPath.String())))
sketchPathMd5 := strings.ToUpper(hex.EncodeToString(md5[:]))
require.NotEmpty(t, sketchPathMd5)
buildDir := paths.TempDir().Join("arduino-sketch-" + sketchPathMd5)
require.FileExists(t, buildDir.Join(sketchName+".ino.eep").String())
require.FileExists(t, buildDir.Join(sketchName+".ino.elf").String())
require.FileExists(t, buildDir.Join(sketchName+".ino.hex").String())
require.FileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.bin").String())
require.FileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.hex").String())
// Verifies binaries are exported when --output-dir flag is specified
require.DirExists(t, outputDir.String())
require.FileExists(t, outputDir.Join(sketchName+".ino.eep").String())
require.FileExists(t, outputDir.Join(sketchName+".ino.elf").String())
require.FileExists(t, outputDir.Join(sketchName+".ino.hex").String())
require.FileExists(t, outputDir.Join(sketchName+".ino.with_bootloader.bin").String())
require.FileExists(t, outputDir.Join(sketchName+".ino.with_bootloader.hex").String())
}
func compileWithExportBinariesFlag(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileWithExportBinariesFlag"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Create a test sketch
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
// Test the --output-dir flag with absolute path
_, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--export-binaries")
require.NoError(t, err)
require.DirExists(t, sketchPath.Join("build").String())
// Verifies binaries are exported when --export-binaries flag is set
fqbn = strings.ReplaceAll(fqbn, ":", ".")
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.eep").String())
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.elf").String())
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.hex").String())
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.with_bootloader.bin").String())
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.with_bootloader.hex").String())
}
func compileWithCustomBuildPath(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileWithBuildPath"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Create a test sketch
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
// Test the --build-path flag with absolute path
buildPath := cli.DataDir().Join("test_dir", "build_dir")
_, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--build-path", buildPath.String())
require.NoError(t, err)
// Verifies expected binaries have been built to build_path
require.DirExists(t, buildPath.String())
require.FileExists(t, buildPath.Join(sketchName+".ino.eep").String())
require.FileExists(t, buildPath.Join(sketchName+".ino.elf").String())
require.FileExists(t, buildPath.Join(sketchName+".ino.hex").String())
require.FileExists(t, buildPath.Join(sketchName+".ino.with_bootloader.bin").String())
require.FileExists(t, buildPath.Join(sketchName+".ino.with_bootloader.hex").String())
// Verifies there are no binaries in temp directory
md5 := md5.Sum(([]byte(sketchPath.String())))
sketchPathMd5 := strings.ToUpper(hex.EncodeToString(md5[:]))
require.NotEmpty(t, sketchPathMd5)
buildDir := paths.TempDir().Join("arduino-sketch-" + sketchPathMd5)
require.NoFileExists(t, buildDir.Join(sketchName+".ino.eep").String())
require.NoFileExists(t, buildDir.Join(sketchName+".ino.elf").String())
require.NoFileExists(t, buildDir.Join(sketchName+".ino.hex").String())
require.NoFileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.bin").String())
require.NoFileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.hex").String())
}
func compileWithExportBinariesEnvVar(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileWithExportBinariesEnvVar"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Create a test sketch
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
envVar := cli.GetDefaultEnv()
envVar["ARDUINO_SKETCH_ALWAYS_EXPORT_BINARIES"] = "true"
// Test compilation with export binaries env var set
_, _, err = cli.RunWithCustomEnv(envVar, "compile", "-b", fqbn, sketchPath.String())
require.NoError(t, err)
require.DirExists(t, sketchPath.Join("build").String())
// Verifies binaries are exported when export binaries env var is set
fqbn = strings.ReplaceAll(fqbn, ":", ".")
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.eep").String())
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.elf").String())
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.hex").String())
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.with_bootloader.bin").String())
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.with_bootloader.hex").String())
}
func compileWithExportBinariesConfig(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileWithExportBinariesEnvVar"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Create a test sketch
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
// Create settings with export binaries set to true
envVar := cli.GetDefaultEnv()
envVar["ARDUINO_SKETCH_ALWAYS_EXPORT_BINARIES"] = "true"
_, _, err = cli.RunWithCustomEnv(envVar, "config", "init", "--dest-dir", ".")
require.NoError(t, err)
defer cli.WorkingDir().Join("arduino-cli.yaml").Remove()
// Test if arduino-cli config file written in the previous run has the `always_export_binaries` flag set.
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Contains(t, stdout, `
{
"sketch": {
"always_export_binaries": "true"
}
}`)
// Test compilation with export binaries env var set
_, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String())
require.NoError(t, err)
require.DirExists(t, sketchPath.Join("build").String())
// Verifies binaries are exported when export binaries env var is set
fqbn = strings.ReplaceAll(fqbn, ":", ".")
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.eep").String())
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.elf").String())
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.hex").String())
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.with_bootloader.bin").String())
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.with_bootloader.hex").String())
}
func compileWithInvalidUrl(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileWithInvalidURL"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Create a test sketch
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
_, _, err = cli.Run("config", "init", "--dest-dir", ".", "--additional-urls", "https://example.com/package_example_index.json")
require.NoError(t, err)
defer cli.WorkingDir().Join("arduino-cli.yaml").Remove()
_, stderr, err := cli.Run("compile", "-b", fqbn, sketchPath.String())
require.NoError(t, err)
require.Contains(t, string(stderr), "Error initializing instance: Loading index file: loading json index file")
expectedIndexfile := cli.DataDir().Join("package_example_index.json")
require.Contains(t, string(stderr), "loading json index file "+expectedIndexfile.String()+": open "+expectedIndexfile.String()+":")
}
func compileWithPdeExtension(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompilePdeSketch"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Create a test sketch
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
// Renames sketch file to pde
sketchFileIno := sketchPath.Join(sketchName + ".ino")
sketchFilePde := sketchPath.Join(sketchName + ".pde")
err = sketchFileIno.Rename(sketchFilePde)
require.NoError(t, err)
// Build sketch from folder
_, stderr, err := cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
require.NoError(t, err)
require.Contains(t, string(stderr), "Sketches with .pde extension are deprecated, please rename the following files to .ino:")
require.Contains(t, string(stderr), sketchFilePde.String())
// Build sketch from file
_, stderr, err = cli.Run("compile", "--clean", "-b", fqbn, sketchFilePde.String())
require.NoError(t, err)
require.Contains(t, string(stderr), "Sketches with .pde extension are deprecated, please rename the following files to .ino:")
require.Contains(t, string(stderr), sketchFilePde.String())
}
func compileWithMultipleMainFiles(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileSketchMultipleMainFiles"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
// Create a test sketch
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
// Copy .ino sketch file to .pde
sketchFileIno := sketchPath.Join(sketchName + ".ino")
sketchFilePde := sketchPath.Join(sketchName + ".pde")
err = sketchFileIno.CopyTo(sketchFilePde)
require.NoError(t, err)
// Build sketch from folder
_, stderr, err := cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
require.Error(t, err)
require.Contains(t, string(stderr), "Error opening sketch: multiple main sketch files found")
// Build sketch from .ino file
_, stderr, err = cli.Run("compile", "--clean", "-b", fqbn, sketchFileIno.String())
require.Error(t, err)
require.Contains(t, string(stderr), "Error opening sketch: multiple main sketch files found")
// Build sketch from .pde file
_, stderr, err = cli.Run("compile", "--clean", "-b", fqbn, sketchFilePde.String())
require.Error(t, err)
require.Contains(t, string(stderr), "Error opening sketch: multiple main sketch files found")
}
func compileCaseMismatchFails(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileSketchCaseMismatch"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
// Rename main .ino file so casing is different from sketch name
sketchFile := sketchPath.Join(sketchName + ".ino")
sketchMainFile := sketchPath.Join(strings.ToLower(sketchName) + ".ino")
err = sketchFile.Rename(sketchMainFile)
require.NoError(t, err)
// Verifies compilation fails when:
// * Compiling with sketch path
_, stderr, err := cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
require.Error(t, err)
require.Contains(t, string(stderr), "Error opening sketch:")
// * Compiling with sketch main file
_, stderr, err = cli.Run("compile", "--clean", "-b", fqbn, sketchMainFile.String())
require.Error(t, err)
require.Contains(t, string(stderr), "Error opening sketch:")
// * Compiling in sketch path
cli.SetWorkingDir(sketchPath)
defer cli.SetWorkingDir(env.RootDir())
_, stderr, err = cli.Run("compile", "--clean", "-b", fqbn)
require.Error(t, err)
require.Contains(t, string(stderr), "Error opening sketch:")
}
func compileOnlyCompilationDatabaseFlag(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileSketchOnlyCompilationDatabaseFlag"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
// Verifies no binaries exist
buildPath := sketchPath.Join("build")
require.NoDirExists(t, buildPath.String())
// Compile with both --export-binaries and --only-compilation-database flags
_, _, err = cli.Run("compile", "--export-binaries", "--only-compilation-database", "--clean", "-b", fqbn, sketchPath.String())
require.NoError(t, err)
// Verifies no binaries are exported
require.NoDirExists(t, buildPath.String())
// Verifies no binaries exist
buildPath = cli.SketchbookDir().Join("export-dir")
require.NoDirExists(t, buildPath.String())
// Compile by setting the --output-dir flag and --only-compilation-database flags
_, _, err = cli.Run("compile", "--output-dir", buildPath.String(), "--only-compilation-database", "--clean", "-b", fqbn, sketchPath.String())
require.NoError(t, err)
// Verifies no binaries are exported
require.NoDirExists(t, buildPath.String())
}
func compileUsingPlatformLocalTxt(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileSketchUsingPlatformLocalTxt"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
// Verifies compilation works without issues
_, _, err = cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
require.NoError(t, err)
// Overrides default platform compiler with an unexisting one
platformLocalTxt := cli.DataDir().Join("packages", "arduino", "hardware", "avr", "1.8.5", "platform.local.txt")
err = platformLocalTxt.WriteFile([]byte("compiler.c.cmd=my-compiler-that-does-not-exist"))
require.NoError(t, err)
// Remove the file at the end of the test to avoid disrupting following tests
defer platformLocalTxt.Remove()
// Verifies compilation now fails because compiler is not found
_, stderr, err := cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
require.Error(t, err)
require.Contains(t, string(stderr), "my-compiler-that-does-not-exist")
}
func compileUsingBoardsLocalTxt(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileSketchUsingBoardsLocalTxt"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
// Usa a made up board
fqbn := "arduino:avr:nessuno"
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
// Verifies compilation fails because board doesn't exist
_, stderr, err := cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
require.Error(t, err)
require.Contains(t, string(stderr), "Error during build: Error resolving FQBN: board arduino:avr:nessuno not found")
// Use custom boards.local.txt with made arduino:avr:nessuno board
boardsLocalTxt := cli.DataDir().Join("packages", "arduino", "hardware", "avr", "1.8.5", "boards.local.txt")
wd, err := paths.Getwd()
require.NoError(t, err)
err = wd.Parent().Join("testdata", "boards.local.txt").CopyTo(boardsLocalTxt)
require.NoError(t, err)
// Remove the file at the end of the test to avoid disrupting following tests
defer boardsLocalTxt.Remove()
_, _, err = cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
require.NoError(t, err)
}