Skip to content

Commit 8a8df2b

Browse files
Rearrange tests to have them share the same environment
1 parent 885aa40 commit 8a8df2b

File tree

2 files changed

+185
-228
lines changed

2 files changed

+185
-228
lines changed

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+
}

0 commit comments

Comments
 (0)