Skip to content

Commit c48844b

Browse files
[skip-changelog] Migrate tests from test_compile_part_3.py to compile_part_3_test.go (#1906)
* Migrate TestCompileSketchWithPdeExtension from test_compile_part_3.py to compile_part_3_test.go * Migrate TestCompileSketchWithMultipleMainFiles from test_compile_part_3.py to compile_part_3_test.go * Migrate TestCompileSketchCaseMismatchFails from test_compile_part_3.py to compile_part_3_test.go A new function that sets the working directory to a specific path has been added in order to run all the tests as intended. * Migrate TestCompileWithOnlyCompilationDatabaseFlag from test_compile_part_3.py to compile_part_3_test.go * Migrate TestCompileUsingPlatformLocalTxt from test_compile_part_3.py to compile_part_3_test.go * Migrate TestCompileUsingBoardsLocalTxt from test_compile_part_3.py to compile_part_3_test.go * Migrate TestCompileManuallyInstalledPlatform from test_compile_part_3.py to compile_part_3_test.go * Migrate TestCompileManuallyInstalledPlatformUsingPlatformLocalTxt from test_compile_part_3.py to compile_part_3_test.go * Migrate TestCompileWithFullyPrecompiledLibrary to compile_part_3_test.go and delete test_compile_part_3.py * Rearrange tests to have them share the same environment
1 parent 520ec91 commit c48844b

File tree

6 files changed

+349
-268
lines changed

6 files changed

+349
-268
lines changed

Diff for: internal/integrationtest/arduino-cli.go

+5
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ func (cli *ArduinoCLI) DownloadDir() *paths.Path {
146146
return cli.stagingDir
147147
}
148148

149+
// SetWorkingDir sets a new working directory
150+
func (cli *ArduinoCLI) SetWorkingDir(p *paths.Path) {
151+
cli.workingDir = p
152+
}
153+
149154
// CopySketch copies a sketch inside the testing environment and returns its path
150155
func (cli *ArduinoCLI) CopySketch(sketchName string) *paths.Path {
151156
p, err := paths.Getwd()

Diff for: internal/integrationtest/compile/compile_part_1_test.go

+185
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ func TestCompile(t *testing.T) {
5757
{"WithExportBinariesEnvVar", compileWithExportBinariesEnvVar},
5858
{"WithExportBinariesConfig", compileWithExportBinariesConfig},
5959
{"WithInvalidUrl", compileWithInvalidUrl},
60+
{"WithPdeExtension", compileWithPdeExtension},
61+
{"WithMultipleMainFiles", compileWithMultipleMainFiles},
62+
{"CaseMismatchFails", compileCaseMismatchFails},
63+
{"OnlyCompilationDatabaseFlag", compileOnlyCompilationDatabaseFlag},
64+
{"UsingPlatformLocalTxt", compileUsingPlatformLocalTxt},
65+
{"UsingBoardsLocalTxt", compileUsingBoardsLocalTxt},
6066
}.Run(t, env, cli)
6167
}
6268

@@ -526,3 +532,182 @@ func compileWithInvalidUrl(t *testing.T, env *integrationtest.Environment, cli *
526532
expectedIndexfile := cli.DataDir().Join("package_example_index.json")
527533
require.Contains(t, string(stderr), "loading json index file "+expectedIndexfile.String()+": open "+expectedIndexfile.String()+":")
528534
}
535+
536+
func compileWithPdeExtension(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
537+
sketchName := "CompilePdeSketch"
538+
sketchPath := cli.SketchbookDir().Join(sketchName)
539+
defer sketchPath.RemoveAll()
540+
fqbn := "arduino:avr:uno"
541+
542+
// Create a test sketch
543+
_, _, err := cli.Run("sketch", "new", sketchPath.String())
544+
require.NoError(t, err)
545+
546+
// Renames sketch file to pde
547+
sketchFileIno := sketchPath.Join(sketchName + ".ino")
548+
sketchFilePde := sketchPath.Join(sketchName + ".pde")
549+
err = sketchFileIno.Rename(sketchFilePde)
550+
require.NoError(t, err)
551+
552+
// Build sketch from folder
553+
_, stderr, err := cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
554+
require.NoError(t, err)
555+
require.Contains(t, string(stderr), "Sketches with .pde extension are deprecated, please rename the following files to .ino:")
556+
require.Contains(t, string(stderr), sketchFilePde.String())
557+
558+
// Build sketch from file
559+
_, stderr, err = cli.Run("compile", "--clean", "-b", fqbn, sketchFilePde.String())
560+
require.NoError(t, err)
561+
require.Contains(t, string(stderr), "Sketches with .pde extension are deprecated, please rename the following files to .ino:")
562+
require.Contains(t, string(stderr), sketchFilePde.String())
563+
}
564+
565+
func compileWithMultipleMainFiles(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
566+
sketchName := "CompileSketchMultipleMainFiles"
567+
sketchPath := cli.SketchbookDir().Join(sketchName)
568+
defer sketchPath.RemoveAll()
569+
fqbn := "arduino:avr:uno"
570+
571+
// Create a test sketch
572+
_, _, err := cli.Run("sketch", "new", sketchPath.String())
573+
require.NoError(t, err)
574+
575+
// Copy .ino sketch file to .pde
576+
sketchFileIno := sketchPath.Join(sketchName + ".ino")
577+
sketchFilePde := sketchPath.Join(sketchName + ".pde")
578+
err = sketchFileIno.CopyTo(sketchFilePde)
579+
require.NoError(t, err)
580+
581+
// Build sketch from folder
582+
_, stderr, err := cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
583+
require.Error(t, err)
584+
require.Contains(t, string(stderr), "Error opening sketch: multiple main sketch files found")
585+
586+
// Build sketch from .ino file
587+
_, stderr, err = cli.Run("compile", "--clean", "-b", fqbn, sketchFileIno.String())
588+
require.Error(t, err)
589+
require.Contains(t, string(stderr), "Error opening sketch: multiple main sketch files found")
590+
591+
// Build sketch from .pde file
592+
_, stderr, err = cli.Run("compile", "--clean", "-b", fqbn, sketchFilePde.String())
593+
require.Error(t, err)
594+
require.Contains(t, string(stderr), "Error opening sketch: multiple main sketch files found")
595+
}
596+
597+
func compileCaseMismatchFails(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
598+
sketchName := "CompileSketchCaseMismatch"
599+
sketchPath := cli.SketchbookDir().Join(sketchName)
600+
defer sketchPath.RemoveAll()
601+
fqbn := "arduino:avr:uno"
602+
603+
_, _, err := cli.Run("sketch", "new", sketchPath.String())
604+
require.NoError(t, err)
605+
606+
// Rename main .ino file so casing is different from sketch name
607+
sketchFile := sketchPath.Join(sketchName + ".ino")
608+
sketchMainFile := sketchPath.Join(strings.ToLower(sketchName) + ".ino")
609+
err = sketchFile.Rename(sketchMainFile)
610+
require.NoError(t, err)
611+
612+
// Verifies compilation fails when:
613+
// * Compiling with sketch path
614+
_, stderr, err := cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
615+
require.Error(t, err)
616+
require.Contains(t, string(stderr), "Error opening sketch:")
617+
// * Compiling with sketch main file
618+
_, stderr, err = cli.Run("compile", "--clean", "-b", fqbn, sketchMainFile.String())
619+
require.Error(t, err)
620+
require.Contains(t, string(stderr), "Error opening sketch:")
621+
// * Compiling in sketch path
622+
cli.SetWorkingDir(sketchPath)
623+
defer cli.SetWorkingDir(env.RootDir())
624+
_, stderr, err = cli.Run("compile", "--clean", "-b", fqbn)
625+
require.Error(t, err)
626+
require.Contains(t, string(stderr), "Error opening sketch:")
627+
}
628+
629+
func compileOnlyCompilationDatabaseFlag(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
630+
sketchName := "CompileSketchOnlyCompilationDatabaseFlag"
631+
sketchPath := cli.SketchbookDir().Join(sketchName)
632+
defer sketchPath.RemoveAll()
633+
fqbn := "arduino:avr:uno"
634+
635+
_, _, err := cli.Run("sketch", "new", sketchPath.String())
636+
require.NoError(t, err)
637+
638+
// Verifies no binaries exist
639+
buildPath := sketchPath.Join("build")
640+
require.NoDirExists(t, buildPath.String())
641+
642+
// Compile with both --export-binaries and --only-compilation-database flags
643+
_, _, err = cli.Run("compile", "--export-binaries", "--only-compilation-database", "--clean", "-b", fqbn, sketchPath.String())
644+
require.NoError(t, err)
645+
646+
// Verifies no binaries are exported
647+
require.NoDirExists(t, buildPath.String())
648+
649+
// Verifies no binaries exist
650+
buildPath = cli.SketchbookDir().Join("export-dir")
651+
require.NoDirExists(t, buildPath.String())
652+
653+
// Compile by setting the --output-dir flag and --only-compilation-database flags
654+
_, _, err = cli.Run("compile", "--output-dir", buildPath.String(), "--only-compilation-database", "--clean", "-b", fqbn, sketchPath.String())
655+
require.NoError(t, err)
656+
657+
// Verifies no binaries are exported
658+
require.NoDirExists(t, buildPath.String())
659+
}
660+
661+
func compileUsingPlatformLocalTxt(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
662+
sketchName := "CompileSketchUsingPlatformLocalTxt"
663+
sketchPath := cli.SketchbookDir().Join(sketchName)
664+
defer sketchPath.RemoveAll()
665+
fqbn := "arduino:avr:uno"
666+
667+
_, _, err := cli.Run("sketch", "new", sketchPath.String())
668+
require.NoError(t, err)
669+
670+
// Verifies compilation works without issues
671+
_, _, err = cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
672+
require.NoError(t, err)
673+
674+
// Overrides default platform compiler with an unexisting one
675+
platformLocalTxt := cli.DataDir().Join("packages", "arduino", "hardware", "avr", "1.8.5", "platform.local.txt")
676+
err = platformLocalTxt.WriteFile([]byte("compiler.c.cmd=my-compiler-that-does-not-exist"))
677+
require.NoError(t, err)
678+
// Remove the file at the end of the test to avoid disrupting following tests
679+
defer platformLocalTxt.Remove()
680+
681+
// Verifies compilation now fails because compiler is not found
682+
_, stderr, err := cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
683+
require.Error(t, err)
684+
require.Contains(t, string(stderr), "my-compiler-that-does-not-exist")
685+
}
686+
687+
func compileUsingBoardsLocalTxt(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
688+
sketchName := "CompileSketchUsingBoardsLocalTxt"
689+
sketchPath := cli.SketchbookDir().Join(sketchName)
690+
defer sketchPath.RemoveAll()
691+
// Usa a made up board
692+
fqbn := "arduino:avr:nessuno"
693+
694+
_, _, err := cli.Run("sketch", "new", sketchPath.String())
695+
require.NoError(t, err)
696+
697+
// Verifies compilation fails because board doesn't exist
698+
_, stderr, err := cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
699+
require.Error(t, err)
700+
require.Contains(t, string(stderr), "Error during build: Error resolving FQBN: board arduino:avr:nessuno not found")
701+
702+
// Use custom boards.local.txt with made arduino:avr:nessuno board
703+
boardsLocalTxt := cli.DataDir().Join("packages", "arduino", "hardware", "avr", "1.8.5", "boards.local.txt")
704+
wd, err := paths.Getwd()
705+
require.NoError(t, err)
706+
err = wd.Parent().Join("testdata", "boards.local.txt").CopyTo(boardsLocalTxt)
707+
require.NoError(t, err)
708+
// Remove the file at the end of the test to avoid disrupting following tests
709+
defer boardsLocalTxt.Remove()
710+
711+
_, _, err = cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
712+
require.NoError(t, err)
713+
}
+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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_test
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/arduino-cli/internal/integrationtest"
22+
"github.com/arduino/go-paths-helper"
23+
"github.com/stretchr/testify/require"
24+
"gopkg.in/src-d/go-git.v4"
25+
"gopkg.in/src-d/go-git.v4/plumbing"
26+
)
27+
28+
func TestCompileWithFullyPrecompiledLibrary(t *testing.T) {
29+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
30+
defer env.CleanUp()
31+
32+
_, _, err := cli.Run("update")
33+
require.NoError(t, err)
34+
35+
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
36+
require.NoError(t, err)
37+
fqbn := "arduino:mbed:nano33ble"
38+
39+
// Create settings with library unsafe install set to true
40+
envVar := cli.GetDefaultEnv()
41+
envVar["ARDUINO_LIBRARY_ENABLE_UNSAFE_INSTALL"] = "true"
42+
_, _, err = cli.RunWithCustomEnv(envVar, "config", "init", "--dest-dir", ".")
43+
require.NoError(t, err)
44+
45+
// Install fully precompiled library
46+
// For more information see:
47+
// https://arduino.github.io/arduino-cli/latest/library-specification/#precompiled-binaries
48+
wd, err := paths.Getwd()
49+
require.NoError(t, err)
50+
_, _, err = cli.Run("lib", "install", "--zip-path", wd.Parent().Join("testdata", "Arduino_TensorFlowLite-2.1.0-ALPHA-precompiled.zip").String())
51+
require.NoError(t, err)
52+
sketchFolder := cli.SketchbookDir().Join("libraries", "Arduino_TensorFlowLite-2.1.0-ALPHA-precompiled", "examples", "hello_world")
53+
54+
// Install example dependency
55+
_, _, err = cli.Run("lib", "install", "Arduino_LSM9DS1")
56+
require.NoError(t, err)
57+
58+
// Compile and verify dependencies detection for fully precompiled library is skipped
59+
stdout, _, err := cli.Run("compile", "-b", fqbn, sketchFolder.String(), "-v")
60+
require.NoError(t, err)
61+
require.Contains(t, string(stdout), "Skipping dependencies detection for precompiled library Arduino_TensorFlowLite")
62+
}
63+
64+
func TestCompileManuallyInstalledPlatform(t *testing.T) {
65+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
66+
defer env.CleanUp()
67+
68+
_, _, err := cli.Run("update")
69+
require.NoError(t, err)
70+
71+
sketchName := "CompileSketchManuallyInstalledPlatformUsingPlatformLocalTxt"
72+
sketchPath := cli.SketchbookDir().Join(sketchName)
73+
fqbn := "arduino-beta-development:avr:uno"
74+
_, _, err = cli.Run("sketch", "new", sketchPath.String())
75+
require.NoError(t, err)
76+
77+
// Manually installs a core in sketchbooks hardware folder
78+
gitUrl := "https://github.com/arduino/ArduinoCore-avr.git"
79+
repoDir := cli.SketchbookDir().Join("hardware", "arduino-beta-development", "avr")
80+
_, err = git.PlainClone(repoDir.String(), false, &git.CloneOptions{
81+
URL: gitUrl,
82+
ReferenceName: plumbing.NewTagReferenceName("1.8.3"),
83+
})
84+
require.NoError(t, err)
85+
86+
// Installs also the same core via CLI so all the necessary tools are installed
87+
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
88+
require.NoError(t, err)
89+
90+
// Verifies compilation works without issues
91+
_, _, err = cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
92+
require.NoError(t, err)
93+
}
94+
95+
func TestCompileManuallyInstalledPlatformUsingPlatformLocalTxt(t *testing.T) {
96+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
97+
defer env.CleanUp()
98+
99+
_, _, err := cli.Run("update")
100+
require.NoError(t, err)
101+
102+
sketchName := "CompileSketchManuallyInstalledPlatformUsingPlatformLocalTxt"
103+
sketchPath := cli.SketchbookDir().Join(sketchName)
104+
fqbn := "arduino-beta-development:avr:uno"
105+
_, _, err = cli.Run("sketch", "new", sketchPath.String())
106+
require.NoError(t, err)
107+
108+
// Manually installs a core in sketchbooks hardware folder
109+
gitUrl := "https://github.com/arduino/ArduinoCore-avr.git"
110+
repoDir := cli.SketchbookDir().Join("hardware", "arduino-beta-development", "avr")
111+
_, err = git.PlainClone(repoDir.String(), false, &git.CloneOptions{
112+
URL: gitUrl,
113+
ReferenceName: plumbing.NewTagReferenceName("1.8.3"),
114+
})
115+
require.NoError(t, err)
116+
117+
// Installs also the same core via CLI so all the necessary tools are installed
118+
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
119+
require.NoError(t, err)
120+
121+
// Verifies compilation works without issues
122+
_, _, err = cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
123+
require.NoError(t, err)
124+
125+
// Overrides default platform compiler with an unexisting one
126+
platformLocalTxt := repoDir.Join("platform.local.txt")
127+
platformLocalTxt.WriteFile([]byte("compiler.c.cmd=my-compiler-that-does-not-exist"))
128+
129+
// Verifies compilation now fails because compiler is not found
130+
_, stderr, err := cli.Run("compile", "--clean", "-b", fqbn, sketchPath.String())
131+
require.Error(t, err)
132+
require.Contains(t, string(stderr), "my-compiler-that-does-not-exist")
133+
}
Binary file not shown.

Diff for: internal/integrationtest/testdata/boards.local.txt

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
nessuno.name=Arduino Nessuno
2+
nessuno.vid.0=0x2341
3+
nessuno.pid.0=0x0043
4+
nessuno.vid.1=0x2341
5+
nessuno.pid.1=0x0001
6+
nessuno.vid.2=0x2A03
7+
nessuno.pid.2=0x0043
8+
nessuno.vid.3=0x2341
9+
nessuno.pid.3=0x0243
10+
nessuno.upload.tool=avrdude
11+
nessuno.upload.protocol=arduino
12+
nessuno.upload.maximum_size=32256
13+
nessuno.upload.maximum_data_size=2048
14+
nessuno.upload.speed=115200
15+
nessuno.bootloader.tool=avrdude
16+
nessuno.bootloader.low_fuses=0xFF
17+
nessuno.bootloader.high_fuses=0xDE
18+
nessuno.bootloader.extended_fuses=0xFD
19+
nessuno.bootloader.unlock_bits=0x3F
20+
nessuno.bootloader.lock_bits=0x0F
21+
nessuno.bootloader.file=optiboot/optiboot_atmega328.hex
22+
nessuno.build.mcu=atmega328p
23+
nessuno.build.f_cpu=16000000L
24+
nessuno.build.board=AVR_NESSUNO
25+
nessuno.build.core=arduino
26+
nessuno.build.variant=standard

0 commit comments

Comments
 (0)