From 60040575a31b606d85ab22dc4bd2244dbe6e0828 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Wed, 13 Sep 2023 11:22:02 +0200 Subject: [PATCH] polish legacy test --- arduino/builder/linker.go | 11 +- arduino/builder/utils/utils_test.go | 133 +++++++++ .../integrationtest/compile_4/compile_test.go | 2 + legacy/builder/test/builder_test.go | 38 +-- legacy/builder/test/builder_utils_test.go | 156 ---------- legacy/builder/test/helper.go | 19 -- legacy/builder/test/libraries_loader_test.go | 275 ------------------ .../test/merge_sketch_with_bootloader_test.go | 1 + .../test/setup_build_properties_test.go | 3 + legacy/builder/test/tools_loader_test.go | 154 ---------- .../try_build_of_problematic_sketch_test.go | 6 +- .../unused_compiled_libraries_remover_test.go | 95 ------ 12 files changed, 156 insertions(+), 737 deletions(-) delete mode 100644 legacy/builder/test/builder_utils_test.go delete mode 100644 legacy/builder/test/libraries_loader_test.go delete mode 100644 legacy/builder/test/unused_compiled_libraries_remover_test.go diff --git a/arduino/builder/linker.go b/arduino/builder/linker.go index b69e71aaefe..a4a2eba3bec 100644 --- a/arduino/builder/linker.go +++ b/arduino/builder/linker.go @@ -33,15 +33,10 @@ func (b *Builder) Link() error { return nil } - // TODO can we remove this multiple assignations? - objectFilesSketch := b.buildArtifacts.sketchObjectFiles - objectFilesLibraries := b.buildArtifacts.librariesObjectFiles - objectFilesCore := b.buildArtifacts.coreObjectsFiles - objectFiles := paths.NewPathList() - objectFiles.AddAll(objectFilesSketch) - objectFiles.AddAll(objectFilesLibraries) - objectFiles.AddAll(objectFilesCore) + objectFiles.AddAll(b.buildArtifacts.sketchObjectFiles) + objectFiles.AddAll(b.buildArtifacts.librariesObjectFiles) + objectFiles.AddAll(b.buildArtifacts.coreObjectsFiles) coreDotARelPath, err := b.buildPath.RelTo(b.buildArtifacts.coreArchiveFilePath) if err != nil { diff --git a/arduino/builder/utils/utils_test.go b/arduino/builder/utils/utils_test.go index 3dd4c1a2142..1c31160f5d0 100644 --- a/arduino/builder/utils/utils_test.go +++ b/arduino/builder/utils/utils_test.go @@ -16,8 +16,11 @@ package utils import ( + "os" "testing" + "time" + "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" ) @@ -39,3 +42,133 @@ func TestPrintableCommand(t *testing.T) { result := printableCommand(parts) require.Equal(t, correct, result) } + +func tempFile(t *testing.T, prefix string) *paths.Path { + file, err := os.CreateTemp("", prefix) + file.Close() + require.NoError(t, err) + return paths.New(file.Name()) +} + +func TestObjFileIsUpToDateObjMissing(t *testing.T) { + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + upToDate, err := ObjFileIsUpToDate(sourceFile, nil, nil) + require.NoError(t, err) + require.False(t, upToDate) +} + +func TestObjFileIsUpToDateDepMissing(t *testing.T) { + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + objFile := tempFile(t, "obj") + defer objFile.RemoveAll() + + upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, nil) + require.NoError(t, err) + require.False(t, upToDate) +} + +func TestObjFileIsUpToDateObjOlder(t *testing.T) { + objFile := tempFile(t, "obj") + defer objFile.RemoveAll() + depFile := tempFile(t, "dep") + defer depFile.RemoveAll() + + time.Sleep(time.Second) + + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile) + require.NoError(t, err) + require.False(t, upToDate) +} + +func TestObjFileIsUpToDateObjNewer(t *testing.T) { + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + time.Sleep(time.Second) + + objFile := tempFile(t, "obj") + defer objFile.RemoveAll() + depFile := tempFile(t, "dep") + defer depFile.RemoveAll() + + upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile) + require.NoError(t, err) + require.True(t, upToDate) +} + +func TestObjFileIsUpToDateDepIsNewer(t *testing.T) { + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + time.Sleep(time.Second) + + objFile := tempFile(t, "obj") + defer objFile.RemoveAll() + depFile := tempFile(t, "dep") + defer depFile.RemoveAll() + + time.Sleep(time.Second) + + headerFile := tempFile(t, "header") + defer headerFile.RemoveAll() + + data := objFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String() + depFile.WriteFile([]byte(data)) + + upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile) + require.NoError(t, err) + require.False(t, upToDate) +} + +func TestObjFileIsUpToDateDepIsOlder(t *testing.T) { + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + headerFile := tempFile(t, "header") + defer headerFile.RemoveAll() + + time.Sleep(time.Second) + + objFile := tempFile(t, "obj") + defer objFile.RemoveAll() + depFile := tempFile(t, "dep") + defer depFile.RemoveAll() + + res := objFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String() + depFile.WriteFile([]byte(res)) + + upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile) + require.NoError(t, err) + require.True(t, upToDate) +} + +func TestObjFileIsUpToDateDepIsWrong(t *testing.T) { + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + time.Sleep(time.Second) + + objFile := tempFile(t, "obj") + defer objFile.RemoveAll() + depFile := tempFile(t, "dep") + defer depFile.RemoveAll() + + time.Sleep(time.Second) + + headerFile := tempFile(t, "header") + defer headerFile.RemoveAll() + + res := sourceFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String() + depFile.WriteFile([]byte(res)) + + upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile) + require.NoError(t, err) + require.False(t, upToDate) +} diff --git a/internal/integrationtest/compile_4/compile_test.go b/internal/integrationtest/compile_4/compile_test.go index 09125b5e2e1..4210eaa931f 100644 --- a/internal/integrationtest/compile_4/compile_test.go +++ b/internal/integrationtest/compile_4/compile_test.go @@ -387,6 +387,7 @@ func testBuilderBridgeExample(t *testing.T, env *integrationtest.Environment, cl // Simulate a library use in libraries build path require.NoError(t, buildPath.Join("libraries", "SPI").MkdirAll()) + require.NoError(t, buildPath.Join("libraries", "dummy_file").WriteFile([]byte{})) // Build again... _, err = tryBuild(t, env, cli, "arduino:avr:leonardo", &buildOptions{NoClean: true}) @@ -394,6 +395,7 @@ func testBuilderBridgeExample(t *testing.T, env *integrationtest.Environment, cl require.False(t, buildPath.Join("libraries", "SPI").Exist()) require.True(t, buildPath.Join("libraries", "Bridge").Exist()) + require.True(t, buildPath.Join("libraries", "dummy_file").Exist()) }) t.Run("Preprocess", func(t *testing.T) { diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index b3819960cac..25df800ff5e 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -38,18 +38,9 @@ func cleanUpBuilderTestContext(t *testing.T, ctx *types.Context) { } } -type skipContextPreparationStepName string - -const skipLibraries = skipContextPreparationStepName("libraries") - -func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *paths.Path, fqbnString string, skips ...skipContextPreparationStepName) *types.Context { +func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *paths.Path, fqbnString string) *types.Context { DownloadCoresAndToolsAndLibraries(t) - stepToSkip := map[skipContextPreparationStepName]bool{} - for _, skip := range skips { - stepToSkip[skip] = true - } - if ctx == nil { ctx = &types.Context{} } @@ -123,7 +114,6 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat _, err = pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform) require.NoError(t, err) - ctx.Builder, err = bldr.NewBuilder( sk, boardBuildProperties, buildPath, false, nil, 0, nil, ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, @@ -133,21 +123,19 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.PackageManager = pme } - if !stepToSkip[skipLibraries] { - lm, libsResolver, _, err := detector.LibrariesLoader( - false, nil, - ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - actualPlatform, targetPlatform, - ) - require.NoError(t, err) + lm, libsResolver, _, err := detector.LibrariesLoader( + false, nil, + ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, + actualPlatform, targetPlatform, + ) + require.NoError(t, err) - ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( - lm, libsResolver, - false, - false, - builderLogger, - ) - } + ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( + lm, libsResolver, + false, + false, + builderLogger, + ) return ctx } diff --git a/legacy/builder/test/builder_utils_test.go b/legacy/builder/test/builder_utils_test.go deleted file mode 100644 index 8d5cb09de2e..00000000000 --- a/legacy/builder/test/builder_utils_test.go +++ /dev/null @@ -1,156 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 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 license@arduino.cc. - -package test - -import ( - "os" - "testing" - "time" - - "github.com/arduino/arduino-cli/arduino/builder/utils" - paths "github.com/arduino/go-paths-helper" - "github.com/stretchr/testify/require" -) - -func tempFile(t *testing.T, prefix string) *paths.Path { - file, err := os.CreateTemp("", prefix) - file.Close() - require.NoError(t, err) - return paths.New(file.Name()) -} - -func TestObjFileIsUpToDateObjMissing(t *testing.T) { - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, nil, nil) - require.NoError(t, err) - require.False(t, upToDate) -} - -func TestObjFileIsUpToDateDepMissing(t *testing.T) { - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - objFile := tempFile(t, "obj") - defer objFile.RemoveAll() - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, nil) - require.NoError(t, err) - require.False(t, upToDate) -} - -func TestObjFileIsUpToDateObjOlder(t *testing.T) { - objFile := tempFile(t, "obj") - defer objFile.RemoveAll() - depFile := tempFile(t, "dep") - defer depFile.RemoveAll() - - time.Sleep(time.Second) - - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - require.NoError(t, err) - require.False(t, upToDate) -} - -func TestObjFileIsUpToDateObjNewer(t *testing.T) { - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - time.Sleep(time.Second) - - objFile := tempFile(t, "obj") - defer objFile.RemoveAll() - depFile := tempFile(t, "dep") - defer depFile.RemoveAll() - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - require.NoError(t, err) - require.True(t, upToDate) -} - -func TestObjFileIsUpToDateDepIsNewer(t *testing.T) { - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - time.Sleep(time.Second) - - objFile := tempFile(t, "obj") - defer objFile.RemoveAll() - depFile := tempFile(t, "dep") - defer depFile.RemoveAll() - - time.Sleep(time.Second) - - headerFile := tempFile(t, "header") - defer headerFile.RemoveAll() - - data := objFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String() - depFile.WriteFile([]byte(data)) - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - require.NoError(t, err) - require.False(t, upToDate) -} - -func TestObjFileIsUpToDateDepIsOlder(t *testing.T) { - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - headerFile := tempFile(t, "header") - defer headerFile.RemoveAll() - - time.Sleep(time.Second) - - objFile := tempFile(t, "obj") - defer objFile.RemoveAll() - depFile := tempFile(t, "dep") - defer depFile.RemoveAll() - - res := objFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String() - depFile.WriteFile([]byte(res)) - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - require.NoError(t, err) - require.True(t, upToDate) -} - -func TestObjFileIsUpToDateDepIsWrong(t *testing.T) { - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - time.Sleep(time.Second) - - objFile := tempFile(t, "obj") - defer objFile.RemoveAll() - depFile := tempFile(t, "dep") - defer depFile.RemoveAll() - - time.Sleep(time.Second) - - headerFile := tempFile(t, "header") - defer headerFile.RemoveAll() - - res := sourceFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String() - depFile.WriteFile([]byte(res)) - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - require.NoError(t, err) - require.False(t, upToDate) -} diff --git a/legacy/builder/test/helper.go b/legacy/builder/test/helper.go index f2d0990b2e9..746096c473f 100644 --- a/legacy/builder/test/helper.go +++ b/legacy/builder/test/helper.go @@ -20,17 +20,10 @@ import ( "testing" "github.com/arduino/arduino-cli/arduino/cores" - "github.com/arduino/arduino-cli/arduino/libraries" paths "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" ) -func Abs(t *testing.T, rel *paths.Path) *paths.Path { - absPath, err := rel.Abs() - require.NoError(t, err) - return absPath -} - func SetupBuildPath(t *testing.T) *paths.Path { buildPath, err := paths.MkTempDir("", "test_build_path") require.NoError(t, err) @@ -42,15 +35,3 @@ func parseFQBN(t *testing.T, fqbnIn string) *cores.FQBN { require.NoError(t, err) return fqbn } - -type ByLibraryName []*libraries.Library - -func (s ByLibraryName) Len() int { - return len(s) -} -func (s ByLibraryName) Swap(i, j int) { - s[i], s[j] = s[j], s[i] -} -func (s ByLibraryName) Less(i, j int) bool { - return s[i].Name < s[j].Name -} diff --git a/legacy/builder/test/libraries_loader_test.go b/legacy/builder/test/libraries_loader_test.go deleted file mode 100644 index e0e0c40f56b..00000000000 --- a/legacy/builder/test/libraries_loader_test.go +++ /dev/null @@ -1,275 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 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 license@arduino.cc. - -package test - -import ( - "path/filepath" - "sort" - "testing" - - "github.com/arduino/arduino-cli/arduino/builder/detector" - "github.com/arduino/arduino-cli/arduino/libraries" - "github.com/arduino/arduino-cli/legacy/builder/types" - paths "github.com/arduino/go-paths-helper" - "github.com/stretchr/testify/require" -) - -func extractLibraries(libs map[string]libraries.List) []*libraries.Library { - res := []*libraries.Library{} - for _, lib := range libs { - for _, libAlternative := range lib { - res = append(res, libAlternative) - } - } - return res -} - -func TestLoadLibrariesAVR(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), - BuiltInLibrariesDirs: paths.New("downloaded_libraries"), - OtherLibrariesDirs: paths.NewPathList("libraries"), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "arduino:avr:leonardo") - defer cleanUpBuilderTestContext(t, ctx) - - _, targetPlatform, _, _, buildPlatform, err := ctx.PackageManager.ResolveFQBN(parseFQBN(t, "arduino:avr:leonardo")) - require.NoError(t, err) - - lm, libsResolver, _, err := detector.LibrariesLoader( - false, nil, - ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - buildPlatform, targetPlatform, - ) - require.NoError(t, err) - - librariesFolders := lm.LibrariesDir - require.Equal(t, 3, len(librariesFolders)) - require.True(t, Abs(t, paths.New("downloaded_libraries")).EquivalentTo(librariesFolders[0].Path)) - require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "avr", "libraries")).EquivalentTo(librariesFolders[1].Path)) - require.True(t, Abs(t, paths.New("libraries")).EquivalentTo(librariesFolders[2].Path)) - - libs := extractLibraries(lm.Libraries) - require.Equal(t, 24, len(libs)) - - sort.Sort(ByLibraryName(libs)) - - idx := 0 - - require.Equal(t, "ANewLibrary-master", libs[idx].Name) - - idx++ - require.Equal(t, "Adafruit PN532", libs[idx].Name) - require.True(t, Abs(t, paths.New("downloaded_libraries/Adafruit_PN532")).EquivalentTo(libs[idx].InstallDir)) - require.True(t, Abs(t, paths.New("downloaded_libraries/Adafruit_PN532")).EquivalentTo(libs[idx].SourceDir)) - require.Equal(t, 1, len(libs[idx].Architectures)) - require.Equal(t, "*", libs[idx].Architectures[0]) - require.False(t, libs[idx].IsLegacy) - - idx++ - require.Equal(t, "Audio", libs[idx].Name) - - idx++ - require.Equal(t, "Balanduino", libs[idx].Name) - require.True(t, libs[idx].IsLegacy) - - idx++ - bridgeLib := libs[idx] - require.Equal(t, "Bridge", bridgeLib.Name) - require.True(t, Abs(t, paths.New("downloaded_libraries/Bridge")).EquivalentTo(bridgeLib.InstallDir)) - require.True(t, Abs(t, paths.New("downloaded_libraries/Bridge/src")).EquivalentTo(bridgeLib.SourceDir)) - require.Equal(t, 1, len(bridgeLib.Architectures)) - require.Equal(t, "*", bridgeLib.Architectures[0]) - require.Equal(t, "Arduino", bridgeLib.Author) - require.Equal(t, "Arduino ", bridgeLib.Maintainer) - - idx++ - require.Equal(t, "CapacitiveSensor", libs[idx].Name) - idx++ - require.Equal(t, "EEPROM", libs[idx].Name) - idx++ - require.Equal(t, "Ethernet", libs[idx].Name) - idx++ - require.Equal(t, "FakeAudio", libs[idx].Name) - idx++ - require.Equal(t, "FastLED", libs[idx].Name) - idx++ - require.Equal(t, "HID", libs[idx].Name) - idx++ - require.Equal(t, "IRremote", libs[idx].Name) - idx++ - require.Equal(t, "Robot IR Remote", libs[idx].Name) - idx++ - require.Equal(t, "SPI", libs[idx].Name) - idx++ - require.Equal(t, "SPI", libs[idx].Name) - idx++ - require.Equal(t, "ShouldNotRecurseWithOldLibs", libs[idx].Name) - idx++ - require.Equal(t, "SoftwareSerial", libs[idx].Name) - idx++ - require.Equal(t, "USBHost", libs[idx].Name) - idx++ - require.Equal(t, "Wire", libs[idx].Name) - - libs = libsResolver.AlternativesFor("Audio.h") - require.Len(t, libs, 2) - sort.Sort(ByLibraryName(libs)) - require.Equal(t, "Audio", libs[0].Name) - require.Equal(t, "FakeAudio", libs[1].Name) - - libs = libsResolver.AlternativesFor("FakeAudio.h") - require.Len(t, libs, 1) - require.Equal(t, "FakeAudio", libs[0].Name) - - libs = libsResolver.AlternativesFor("Adafruit_PN532.h") - require.Len(t, libs, 1) - require.Equal(t, "Adafruit PN532", libs[0].Name) - - libs = libsResolver.AlternativesFor("IRremote.h") - require.Len(t, libs, 1) - require.Equal(t, "IRremote", libs[0].Name) -} - -func TestLoadLibrariesSAM(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), - BuiltInLibrariesDirs: paths.New("downloaded_libraries"), - OtherLibrariesDirs: paths.NewPathList("libraries"), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "arduino:sam:arduino_due_x_dbg") - defer cleanUpBuilderTestContext(t, ctx) - - _, targetPlatform, _, _, buildPlatform, err := ctx.PackageManager.ResolveFQBN(parseFQBN(t, "arduino:sam:arduino_due_x_dbg")) - require.NoError(t, err) - lm, libsResolver, _, err := detector.LibrariesLoader( - false, nil, - ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - buildPlatform, targetPlatform, - ) - require.NoError(t, err) - - librariesFolders := lm.LibrariesDir - require.Equal(t, 3, len(librariesFolders)) - require.True(t, Abs(t, paths.New("downloaded_libraries")).EquivalentTo(librariesFolders[0].Path)) - require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "sam", "libraries")).EquivalentTo(librariesFolders[1].Path)) - require.True(t, Abs(t, paths.New("libraries")).EquivalentTo(librariesFolders[2].Path)) - - libraries := extractLibraries(lm.Libraries) - require.Equal(t, 22, len(libraries)) - - sort.Sort(ByLibraryName(libraries)) - - idx := 0 - require.Equal(t, "ANewLibrary-master", libraries[idx].Name) - idx++ - require.Equal(t, "Adafruit PN532", libraries[idx].Name) - idx++ - require.Equal(t, "Audio", libraries[idx].Name) - idx++ - require.Equal(t, "Balanduino", libraries[idx].Name) - idx++ - require.Equal(t, "Bridge", libraries[idx].Name) - idx++ - require.Equal(t, "CapacitiveSensor", libraries[idx].Name) - idx++ - require.Equal(t, "Ethernet", libraries[idx].Name) - idx++ - require.Equal(t, "FakeAudio", libraries[idx].Name) - idx++ - require.Equal(t, "FastLED", libraries[idx].Name) - idx++ - require.Equal(t, "HID", libraries[idx].Name) - idx++ - require.Equal(t, "IRremote", libraries[idx].Name) - idx++ - require.Equal(t, "Robot IR Remote", libraries[idx].Name) - idx++ - require.Equal(t, "SPI", libraries[idx].Name) - idx++ - require.Equal(t, "SPI", libraries[idx].Name) - idx++ - require.Equal(t, "ShouldNotRecurseWithOldLibs", libraries[idx].Name) - idx++ - require.Equal(t, "USBHost", libraries[idx].Name) - idx++ - require.Equal(t, "Wire", libraries[idx].Name) - - libs := libsResolver.AlternativesFor("Audio.h") - require.Len(t, libs, 2) - sort.Sort(ByLibraryName(libs)) - require.Equal(t, "Audio", libs[0].Name) - require.Equal(t, "FakeAudio", libs[1].Name) - - libs = libsResolver.AlternativesFor("FakeAudio.h") - require.Len(t, libs, 1) - require.Equal(t, "FakeAudio", libs[0].Name) - - libs = libsResolver.AlternativesFor("IRremote.h") - require.Len(t, libs, 1) - require.Equal(t, "IRremote", libs[0].Name) -} - -func TestLoadLibrariesAVRNoDuplicateLibrariesFolders(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), - BuiltInLibrariesDirs: paths.New("downloaded_libraries"), - OtherLibrariesDirs: paths.NewPathList("libraries", filepath.Join("downloaded_hardware", "arduino", "avr", "libraries")), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "arduino:avr:leonardo") - defer cleanUpBuilderTestContext(t, ctx) - - _, targetPlatform, _, _, buildPlatform, err := ctx.PackageManager.ResolveFQBN(parseFQBN(t, "arduino:avr:leonardo")) - require.NoError(t, err) - lm, _, _, err := detector.LibrariesLoader( - false, nil, - ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - buildPlatform, targetPlatform, - ) - require.NoError(t, err) - - librariesFolders := lm.LibrariesDir - require.Equal(t, 3, len(librariesFolders)) - require.True(t, Abs(t, paths.New("downloaded_libraries")).EquivalentTo(librariesFolders[0].Path)) - require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "avr", "libraries")).EquivalentTo(librariesFolders[1].Path)) - require.True(t, Abs(t, paths.New("libraries")).EquivalentTo(librariesFolders[2].Path)) -} - -func TestLoadLibrariesMyAVRPlatform(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "user_hardware", "downloaded_hardware"), - BuiltInLibrariesDirs: paths.New("downloaded_libraries"), - OtherLibrariesDirs: paths.NewPathList("libraries", filepath.Join("downloaded_hardware", "arduino", "avr", "libraries")), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "my_avr_platform:avr:custom_yun") - defer cleanUpBuilderTestContext(t, ctx) - - _, targetPlatform, _, _, buildPlatform, err := ctx.PackageManager.ResolveFQBN(parseFQBN(t, "my_avr_platform:avr:custom_yun")) - require.NoError(t, err) - lm, _, _, err := detector.LibrariesLoader( - false, nil, - ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - buildPlatform, targetPlatform, - ) - require.NoError(t, err) - - librariesFolders := lm.LibrariesDir - require.Equal(t, 4, len(librariesFolders)) - require.True(t, Abs(t, paths.New("downloaded_libraries")).EquivalentTo(librariesFolders[0].Path)) - require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "avr", "libraries")).EquivalentTo(librariesFolders[1].Path)) - require.True(t, Abs(t, paths.New("user_hardware", "my_avr_platform", "avr", "libraries")).EquivalentTo(librariesFolders[2].Path)) - require.True(t, Abs(t, paths.New("libraries")).EquivalentTo(librariesFolders[3].Path)) -} diff --git a/legacy/builder/test/merge_sketch_with_bootloader_test.go b/legacy/builder/test/merge_sketch_with_bootloader_test.go index b1d2a6df7ea..f7c9a24024f 100644 --- a/legacy/builder/test/merge_sketch_with_bootloader_test.go +++ b/legacy/builder/test/merge_sketch_with_bootloader_test.go @@ -148,6 +148,7 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) { require.False(t, exist) } +// TODO convert in a compile test and we check against the real .hex func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware", "user_hardware"), diff --git a/legacy/builder/test/setup_build_properties_test.go b/legacy/builder/test/setup_build_properties_test.go index b4acb7d2ecc..68aaa6b0574 100644 --- a/legacy/builder/test/setup_build_properties_test.go +++ b/legacy/builder/test/setup_build_properties_test.go @@ -25,6 +25,7 @@ import ( "github.com/stretchr/testify/require" ) +// TODO maybe create a test that actually check all the keys func TestSetupBuildProperties(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware", "user_hardware"), @@ -71,6 +72,7 @@ func TestSetupBuildProperties(t *testing.T) { require.True(t, buildProperties.ContainsKey("extra.time.dst")) } +// TODO make this integration tests func TestSetupBuildPropertiesWithSomeCustomOverrides(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), @@ -90,6 +92,7 @@ func TestSetupBuildPropertiesWithSomeCustomOverrides(t *testing.T) { require.Equal(t, "non existent path with space and a =", buildProperties.Get("tools.avrdude.config.path")) } +// TODO go to compile_4 that uses the custom_yum to compile and we also verify this properties func TestSetupBuildPropertiesUserHardware(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware", "user_hardware"), diff --git a/legacy/builder/test/tools_loader_test.go b/legacy/builder/test/tools_loader_test.go index e28f828401d..3460ac82c76 100644 --- a/legacy/builder/test/tools_loader_test.go +++ b/legacy/builder/test/tools_loader_test.go @@ -16,35 +16,12 @@ package test import ( - "path/filepath" - "sort" "testing" - "github.com/arduino/arduino-cli/arduino/cores" - "github.com/arduino/arduino-cli/arduino/cores/packagemanager" - "github.com/arduino/arduino-cli/legacy/builder/types" paths "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" ) -type ByToolIDAndVersion []*cores.ToolRelease - -func (s ByToolIDAndVersion) Len() int { - return len(s) -} -func (s ByToolIDAndVersion) Swap(i, j int) { - s[i], s[j] = s[j], s[i] -} -func (s ByToolIDAndVersion) Less(i, j int) bool { - if s[i].Tool.Name != s[j].Tool.Name { - return s[i].Tool.Name < s[j].Tool.Name - } - if !s[i].Version.Equal(s[j].Version) { - return s[i].Version.LessThan(s[j].Version) - } - return s[i].InstallDir.String() < s[j].InstallDir.String() -} - func requireEquivalentPaths(t *testing.T, actual string, expected ...string) { if len(expected) == 1 { actualAbs, err := paths.New(actual).Abs() @@ -60,134 +37,3 @@ func requireEquivalentPaths(t *testing.T, actual string, expected ...string) { require.Contains(t, expectedAbs.AsStrings(), actualAbs.String()) } } - -func TestLoadTools(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), - BuiltInToolsDirs: paths.NewPathList("downloaded_tools", "tools_builtin"), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "", skipLibraries) - defer cleanUpBuilderTestContext(t, ctx) - - tools := ctx.PackageManager.GetAllInstalledToolsReleases() - require.Equal(t, 9, len(tools)) - - sort.Sort(ByToolIDAndVersion(tools)) - - idx := 0 - require.Equal(t, ":arduino-preprocessor@0.1.5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/arduino-preprocessor/0.1.5") - idx++ - require.Equal(t, ":arm-none-eabi-gcc@4.8.3-2014q1", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/arm-none-eabi-gcc/4.8.3-2014q1") - idx++ - require.Equal(t, ":avr-gcc@4.8.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/avr-gcc/4.8.1-arduino5") - idx++ - require.Equal(t, "arduino:avr-gcc@4.8.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "tools_builtin/avr") - idx++ - require.Equal(t, ":avrdude@6.0.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/avrdude/6.0.1-arduino5") - idx++ - require.Equal(t, "arduino:avrdude@6.0.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "tools_builtin/avr") - idx++ - require.Equal(t, ":bossac@1.5-arduino", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/bossac/1.5-arduino") - idx++ - require.Equal(t, ":bossac@1.6.1-arduino", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/bossac/1.6.1-arduino") - idx++ - require.Equal(t, ":ctags@5.8-arduino11", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/ctags/5.8-arduino11") -} - -func TestLoadToolsWithBoardManagerFolderStructure(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList("downloaded_board_manager_stuff"), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "", skipLibraries) - defer cleanUpBuilderTestContext(t, ctx) - - tools := ctx.PackageManager.GetAllInstalledToolsReleases() - require.Equal(t, 3, len(tools)) - - sort.Sort(ByToolIDAndVersion(tools)) - - idx := 0 - require.Equal(t, "arduino:CMSIS@4.0.0-atmel", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_board_manager_stuff/arduino/tools/CMSIS/4.0.0-atmel") - idx++ - require.Equal(t, "RFduino:arm-none-eabi-gcc@4.8.3-2014q1", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_board_manager_stuff/RFduino/tools/arm-none-eabi-gcc/4.8.3-2014q1") - idx++ - require.Equal(t, "arduino:openocd@0.9.0-arduino", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_board_manager_stuff/arduino/tools/openocd/0.9.0-arduino") -} - -func TestLoadLotsOfTools(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList("downloaded_board_manager_stuff"), - BuiltInToolsDirs: paths.NewPathList("downloaded_tools", "tools_builtin"), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "", skipLibraries) - defer cleanUpBuilderTestContext(t, ctx) - - tools := ctx.PackageManager.GetAllInstalledToolsReleases() - require.Equal(t, 12, len(tools)) - - sort.Sort(ByToolIDAndVersion(tools)) - - idx := 0 - require.Equal(t, "arduino:CMSIS@4.0.0-atmel", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_board_manager_stuff/arduino/tools/CMSIS/4.0.0-atmel") - idx++ - require.Equal(t, ":arduino-preprocessor@0.1.5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/arduino-preprocessor/0.1.5") - idx++ - require.Equal(t, "RFduino:arm-none-eabi-gcc@4.8.3-2014q1", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_board_manager_stuff/RFduino/tools/arm-none-eabi-gcc/4.8.3-2014q1") - idx++ - require.Equal(t, ":arm-none-eabi-gcc@4.8.3-2014q1", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/arm-none-eabi-gcc/4.8.3-2014q1") - idx++ - require.Equal(t, ":avr-gcc@4.8.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/avr-gcc/4.8.1-arduino5") - idx++ - require.Equal(t, "arduino:avr-gcc@4.8.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "tools_builtin/avr") - idx++ - require.Equal(t, ":avrdude@6.0.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/avrdude/6.0.1-arduino5") - idx++ - require.Equal(t, "arduino:avrdude@6.0.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "tools_builtin/avr") - idx++ - require.Equal(t, ":bossac@1.5-arduino", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/bossac/1.5-arduino") - idx++ - require.Equal(t, ":bossac@1.6.1-arduino", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), tools[idx].InstallDir.String(), "downloaded_tools/bossac/1.6.1-arduino") - idx++ - require.Equal(t, ":ctags@5.8-arduino11", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/ctags/5.8-arduino11") - idx++ - require.Equal(t, "arduino:openocd@0.9.0-arduino", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_board_manager_stuff/arduino/tools/openocd/0.9.0-arduino") -} - -func TestAllToolsContextIsPopulated(t *testing.T) { - pmb := packagemanager.NewBuilder(nil, nil, nil, nil, "") - pmb.LoadHardwareFromDirectories(paths.NewPathList("downloaded_board_manager_stuff")) - pmb.LoadToolsFromBundleDirectory(paths.New("downloaded_tools", "tools_builtin")) - pm := pmb.Build() - pme, release := pm.NewExplorer() - defer release() - - ctx := &types.Context{ - PackageManager: pme, - } - - require.NotEmpty(t, ctx.PackageManager.GetAllInstalledToolsReleases()) -} diff --git a/legacy/builder/test/try_build_of_problematic_sketch_test.go b/legacy/builder/test/try_build_of_problematic_sketch_test.go index ab40de05714..6504bcd9b84 100644 --- a/legacy/builder/test/try_build_of_problematic_sketch_test.go +++ b/legacy/builder/test/try_build_of_problematic_sketch_test.go @@ -27,11 +27,7 @@ import ( "github.com/stretchr/testify/require" ) -// This is a sketch that fails to build on purpose -//func TestTryBuild016(t *testing.T) { -// tryBuild(t, paths.New("sketch_that_checks_if_SPI_has_transactions_and_includes_missing_Ethernet", "sketch.ino")) -//} - +// TODO add them in the compile_4 func TestTryBuild033(t *testing.T) { tryBuild(t, paths.New("sketch_that_includes_arduino_h", "sketch_that_includes_arduino_h.ino")) } diff --git a/legacy/builder/test/unused_compiled_libraries_remover_test.go b/legacy/builder/test/unused_compiled_libraries_remover_test.go deleted file mode 100644 index a9e122195ca..00000000000 --- a/legacy/builder/test/unused_compiled_libraries_remover_test.go +++ /dev/null @@ -1,95 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 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 license@arduino.cc. - -package test - -// TODO to be ported in the E2E suite - -//func TestUnusedCompiledLibrariesRemover(t *testing.T) { -// temp, err := paths.MkTempDir("", "test") -// require.NoError(t, err) -// defer temp.RemoveAll() -// -// require.NoError(t, temp.Join("SPI").MkdirAll()) -// require.NoError(t, temp.Join("Bridge").MkdirAll()) -// require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) -// -// librariesBuildPath := temp -// sketchLibrariesDetector := detector.NewSketchLibrariesDetector( -// nil, nil, false, false, logger.New(nil, nil, false, ""), -// ) -// sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) -// -// err = builder.UnusedCompiledLibrariesRemover( -// librariesBuildPath, -// sketchLibrariesDetector.ImportedLibraries(), -// ) -// require.NoError(t, err) -// -// exist, err := temp.Join("SPI").ExistCheck() -// require.NoError(t, err) -// require.False(t, exist) -// exist, err = temp.Join("Bridge").ExistCheck() -// require.NoError(t, err) -// require.True(t, exist) -// exist, err = temp.Join("dummy_file").ExistCheck() -// require.NoError(t, err) -// require.True(t, exist) -//} -// -//func TestUnusedCompiledLibrariesRemoverLibDoesNotExist(t *testing.T) { -// librariesBuildPath := paths.TempDir().Join("test") -// sketchLibrariesDetector := detector.NewSketchLibrariesDetector( -// nil, nil, false, false, logger.New(nil, nil, false, ""), -// ) -// sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) -// -// err := builder.UnusedCompiledLibrariesRemover( -// librariesBuildPath, -// sketchLibrariesDetector.ImportedLibraries(), -// ) -// require.NoError(t, err) -//} -// -//func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) { -// temp, err := paths.MkTempDir("", "test") -// require.NoError(t, err) -// defer temp.RemoveAll() -// -// require.NoError(t, temp.Join("SPI").MkdirAll()) -// require.NoError(t, temp.Join("Bridge").MkdirAll()) -// require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) -// -// sketchLibrariesDetector := detector.NewSketchLibrariesDetector( -// nil, nil, false, false, logger.New(nil, nil, false, ""), -// ) -// librariesBuildPath := temp -// -// err = builder.UnusedCompiledLibrariesRemover( -// librariesBuildPath, -// sketchLibrariesDetector.ImportedLibraries(), -// ) -// require.NoError(t, err) -// -// exist, err := temp.Join("SPI").ExistCheck() -// require.NoError(t, err) -// require.False(t, exist) -// exist, err = temp.Join("Bridge").ExistCheck() -// require.NoError(t, err) -// require.False(t, exist) -// exist, err = temp.Join("dummy_file").ExistCheck() -// require.NoError(t, err) -// require.True(t, exist) -//}