From ca89ce703f5ee3c1a59e65a36b4a409057c8f3e5 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 Sep 2023 11:07:58 +0200 Subject: [PATCH 01/12] Ported TestCoreCaching to current integration test infra --- .../integrationtest/compile_4/compile_test.go | 53 +++++++++++++++++++ legacy/builder/test/builder_test.go | 47 ---------------- 2 files changed, 53 insertions(+), 47 deletions(-) diff --git a/internal/integrationtest/compile_4/compile_test.go b/internal/integrationtest/compile_4/compile_test.go index ad9a5b7b9e1..41ae6bd856a 100644 --- a/internal/integrationtest/compile_4/compile_test.go +++ b/internal/integrationtest/compile_4/compile_test.go @@ -22,6 +22,7 @@ import ( "strings" "testing" "text/template" + "time" "github.com/arduino/arduino-cli/arduino/builder/cpp" "github.com/arduino/arduino-cli/internal/integrationtest" @@ -710,3 +711,55 @@ func comparePreprocessGoldenFile(t *testing.T, sketchDir *paths.Path, preprocess require.Equal(t, buf.String(), strings.Replace(preprocessedSketch, "\r\n", "\n", -1)) } + +func TestCoreCaching(t *testing.T) { + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) + defer env.CleanUp() + + sketchPath, err := paths.New("..", "testdata", "bare_minimum").Abs() + require.NoError(t, err) + + // Install Arduino AVR Boards + _, _, err = cli.Run("core", "install", "arduino:avr@1.8.6") + require.NoError(t, err) + + // Create temporary cache dir + buildCachePath, err := paths.MkTempDir("", "test_build_cache") + require.NoError(t, err) + defer buildCachePath.RemoveAll() + + // Build first time + _, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-cache-path", buildCachePath.String(), sketchPath.String()) + require.NoError(t, err) + + // Find cached core and save timestamp + pathList, err := buildCachePath.ReadDirRecursiveFiltered(nil, paths.FilterPrefixes("core.a")) + require.NoError(t, err) + require.Len(t, pathList, 1) + cachedCoreFile := pathList[0] + lastUsedPath := cachedCoreFile.Parent().Join(".last-used") + require.True(t, lastUsedPath.Exist()) + coreStatBefore, err := cachedCoreFile.Stat() + require.NoError(t, err) + + // Run build again and check timestamp is unchanged + _, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-cache-path", buildCachePath.String(), sketchPath.String()) + require.NoError(t, err) + coreStatAfterRebuild, err := cachedCoreFile.Stat() + require.NoError(t, err) + require.Equal(t, coreStatBefore.ModTime(), coreStatAfterRebuild.ModTime()) + + // Touch a file of the core and check if the builder invalidate the cache + time.Sleep(time.Second) + now := time.Now().Local() + coreFolder := cli.DataDir().Join("packages", "arduino", "hardware", "avr", "1.8.6") + err = coreFolder.Join("cores", "arduino", "Arduino.h").Chtimes(now, now) + require.NoError(t, err) + + // Run build again, to verify that the builder rebuilds core.a + _, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-cache-path", buildCachePath.String(), sketchPath.String()) + require.NoError(t, err) + coreStatAfterTouch, err := cachedCoreFile.Stat() + require.NoError(t, err) + require.NotEqual(t, coreStatBefore.ModTime(), coreStatAfterTouch.ModTime()) +} diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 696bcfad957..92302ede563 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -19,7 +19,6 @@ import ( "fmt" "path/filepath" "testing" - "time" bldr "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/arduino/builder/detector" @@ -27,7 +26,6 @@ import ( "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/legacy/builder" "github.com/arduino/arduino-cli/legacy/builder/constants" - "github.com/arduino/arduino-cli/legacy/builder/phases" "github.com/arduino/arduino-cli/legacy/builder/types" "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" @@ -188,48 +186,3 @@ func TestBuilderWithBuildPathInSketchDir(t *testing.T) { err = command.Run(ctx) NoError(t, err) } - -func TestBuilderCacheCoreAFile(t *testing.T) { - ctx := prepareBuilderTestContext(t, nil, paths.New("sketch1", "sketch1.ino"), "arduino:avr:uno") - defer cleanUpBuilderTestContext(t, ctx) - - SetupBuildCachePath(t, ctx) - defer ctx.CoreBuildCachePath.RemoveAll() - - // Run build - bldr := builder.Builder{} - err := bldr.Run(ctx) - NoError(t, err) - - // Pick timestamp of cached core - coreFolder := paths.New("downloaded_hardware", "arduino", "avr") - coreFileName := phases.GetCachedCoreArchiveDirName(ctx.FQBN.String(), ctx.BuildProperties.Get("compiler.optimization_flags"), coreFolder) - cachedCoreFile := ctx.CoreBuildCachePath.Join(coreFileName, "core.a") - coreStatBefore, err := cachedCoreFile.Stat() - require.NoError(t, err) - lastUsedFile := ctx.CoreBuildCachePath.Join(coreFileName, ".last-used") - _, err = lastUsedFile.Stat() - require.NoError(t, err) - - // Run build again, to verify that the builder skips rebuilding core.a - err = bldr.Run(ctx) - NoError(t, err) - - coreStatAfterRebuild, err := cachedCoreFile.Stat() - require.NoError(t, err) - require.Equal(t, coreStatBefore.ModTime(), coreStatAfterRebuild.ModTime()) - - // Touch a file of the core and check if the builder invalidate the cache - time.Sleep(time.Second) - now := time.Now().Local() - err = coreFolder.Join("cores", "arduino", "Arduino.h").Chtimes(now, now) - require.NoError(t, err) - - // Run build again, to verify that the builder rebuilds core.a - err = bldr.Run(ctx) - NoError(t, err) - - coreStatAfterTouch, err := cachedCoreFile.Stat() - require.NoError(t, err) - require.NotEqual(t, coreStatBefore.ModTime(), coreStatAfterTouch.ModTime()) -} From 180d06f1f42aa4446cc1011b76d08680f4b40d2b Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 Sep 2023 11:11:44 +0200 Subject: [PATCH 02/12] Removed useless TestBuilderEmptySketch The same features are already tested in a number of other integration tests. --- legacy/builder/test/builder_test.go | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 92302ede563..99ac7eefa41 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -146,30 +146,6 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat return ctx } -func TestBuilderEmptySketch(t *testing.T) { - ctx := prepareBuilderTestContext(t, nil, paths.New("sketch1", "sketch1.ino"), "arduino:avr:uno") - defer cleanUpBuilderTestContext(t, ctx) - - // Run builder - command := builder.Builder{} - err := command.Run(ctx) - NoError(t, err) - - buildPath := ctx.BuildPath - exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck() - NoError(t, err) - require.True(t, exist) - exist, err = buildPath.Join(constants.FOLDER_SKETCH, "sketch1.ino.cpp.o").ExistCheck() - NoError(t, err) - require.True(t, exist) - exist, err = buildPath.Join("sketch1.ino.elf").ExistCheck() - NoError(t, err) - require.True(t, exist) - exist, err = buildPath.Join("sketch1.ino.hex").ExistCheck() - NoError(t, err) - require.True(t, exist) -} - func TestBuilderWithBuildPathInSketchDir(t *testing.T) { buildPath, err := paths.New("sketch1", "build").Abs() NoError(t, err) From f0025bbea3531977406a157004b2f157fbf2f1f9 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 Sep 2023 11:31:46 +0200 Subject: [PATCH 03/12] Ported TestBuilderWithBuildPathInSketchDir to current integration test infra --- .../integrationtest/compile_1/compile_test.go | 16 ++++++++++++++++ legacy/builder/test/builder_test.go | 18 ------------------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/internal/integrationtest/compile_1/compile_test.go b/internal/integrationtest/compile_1/compile_test.go index 56501c88dec..0be102cbc0c 100644 --- a/internal/integrationtest/compile_1/compile_test.go +++ b/internal/integrationtest/compile_1/compile_test.go @@ -73,6 +73,7 @@ func TestCompile(t *testing.T) { {"WithRelativeBuildPath", compileWithRelativeBuildPath}, {"WithFakeSecureBootCore", compileWithFakeSecureBootCore}, {"PreprocessFlagDoNotMessUpWithOutput", preprocessFlagDoNotMessUpWithOutput}, + {"BuildWithBuildPathInSketchDir", buildWithBuildPathInSketchDir}, }.Run(t, env, cli) } @@ -1204,3 +1205,18 @@ void loop() { require.NoError(t, err) require.Equal(t, expected, string(output)) } + +func buildWithBuildPathInSketchDir(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) { + sketchName := "bare_minimum" + sketchPath := cli.CopySketch(sketchName) + defer sketchPath.RemoveAll() + buildPath := sketchPath.Join("build") + + // Run build + _, _, err := cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", buildPath.String(), sketchPath.String()) + require.NoError(t, err) + + // Run build twice, to verify the build still works when the build directory is present at the start + _, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", buildPath.String(), sketchPath.String()) + require.NoError(t, err) +} diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 99ac7eefa41..f8f59a0ff55 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -24,7 +24,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/detector" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" "github.com/arduino/arduino-cli/arduino/sketch" - "github.com/arduino/arduino-cli/legacy/builder" "github.com/arduino/arduino-cli/legacy/builder/constants" "github.com/arduino/arduino-cli/legacy/builder/types" "github.com/arduino/go-paths-helper" @@ -145,20 +144,3 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat return ctx } - -func TestBuilderWithBuildPathInSketchDir(t *testing.T) { - buildPath, err := paths.New("sketch1", "build").Abs() - NoError(t, err) - ctx := prepareBuilderTestContext(t, &types.Context{BuildPath: buildPath}, paths.New("sketch1", "sketch1.ino"), "arduino:avr:uno") - defer cleanUpBuilderTestContext(t, ctx) - - // Run build - command := builder.Builder{} - err = command.Run(ctx) - NoError(t, err) - - // Run build twice, to verify the build still works when the - // build directory is present at the start - err = command.Run(ctx) - NoError(t, err) -} From 18dfe0da96c4d842524c1a0b22cbbc0b81962d1a Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 Sep 2023 13:21:26 +0200 Subject: [PATCH 04/12] Ported TestLoadHardware to current integration test infra --- .../board/hardware_loading_test.go | 139 ++++++++++++++++++ .../custom_local_txts/boards.local.txt | 0 .../custom_local_txts/platform.local.txt | 0 legacy/builder/test/hardware_loader_test.go | 37 ----- 4 files changed, 139 insertions(+), 37 deletions(-) create mode 100644 internal/integrationtest/board/hardware_loading_test.go rename {legacy/builder/test => internal/integrationtest/board/testdata}/custom_local_txts/boards.local.txt (100%) rename {legacy/builder/test => internal/integrationtest/board/testdata}/custom_local_txts/platform.local.txt (100%) diff --git a/internal/integrationtest/board/hardware_loading_test.go b/internal/integrationtest/board/hardware_loading_test.go new file mode 100644 index 00000000000..d770a906b86 --- /dev/null +++ b/internal/integrationtest/board/hardware_loading_test.go @@ -0,0 +1,139 @@ +// 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 license@arduino.cc. + +package board_test + +import ( + "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 TestHardwareLoading(t *testing.T) { + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) + defer env.CleanUp() + + // install two cores, boards must be ordered by package name and platform name + _, _, err := cli.Run("core", "install", "arduino:avr@1.8.6") + require.NoError(t, err) + // _, _, err = cli.Run("core", "install", "arduino:sam") + // require.NoError(t, err) + + localTxt, err := paths.New("testdata", "custom_local_txts").Abs() + require.NoError(t, err) + downloadedHardwareAvr := cli.DataDir().Join("packages", "arduino", "hardware", "avr", "1.8.6") + localTxt.Join("boards.local.txt").CopyTo(downloadedHardwareAvr.Join("boards.local.txt")) + localTxt.Join("platform.local.txt").CopyTo(downloadedHardwareAvr.Join("platform.local.txt")) + + { + out, _, err := cli.Run("core", "list", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, out) + jsonOut.LengthMustEqualTo(1) + jsonOut.MustContain(`[ + { + "id": "arduino:avr", + "installed": "1.8.6", + "name": "Arduino AVR Boards", + "boards": [ + { + "name": "Arduino Uno", + "fqbn": "arduino:avr:uno" + }, + { + "name": "Arduino Yún", + "fqbn": "arduino:avr:yun" + } + ] + } + ]`) + } + + { + // Also test local platform.txt properties override + out, _, err := cli.Run("board", "details", "-b", "arduino:avr:uno", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, out) + jsonOut.MustContain(`{ + "version": "1.8.6", + "properties_id": "uno", + "build_properties": [ + "_id=uno", + "tools.avrdude.bootloader.params.verbose=-v", + "tools.avrdude.cmd.path=/my/personal/avrdude" + ], + "programmers": [ + { + "platform": "Arduino AVR Boards", + "id": "usbasp", + "name": "USBasp" + }, + { + "platform": "Arduino AVR Boards", + "id": "avrispmkii", + "name": "AVRISP mkII" + } + ] + }`) + } + + { + out, _, err := cli.Run("board", "details", "-b", "arduino:avr:yun", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, out) + jsonOut.MustContain(`{ + "version": "1.8.6", + "properties_id": "yun", + "build_properties": [ + "_id=yun", + "upload.wait_for_upload_port=true" + ] + }`) + } + + { + // Check un-expansion of board_properties + out, _, err := cli.Run("board", "details", "-b", "arduino:avr:robotMotor", "--show-properties=unexpanded", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, out) + jsonOut.MustContain(`{ + "version": "1.8.6", + "properties_id": "robotMotor", + "build_properties": [ + "_id=robotMotor", + "build.extra_flags={build.usb_flags}", + "upload.wait_for_upload_port=true" + ] + }`) + } + + { + // Also test local boards.txt properties override + out, _, err := cli.Run("board", "details", "-b", "arduino:avr:diecimila", "--show-properties=unexpanded", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, out) + jsonOut.MustContain(`{ + "version": "1.8.6", + "properties_id": "diecimila", + "build_properties": [ + "_id=diecimila", + "menu.cpu.atmega123=ATmega123" + ] + }`) + } +} diff --git a/legacy/builder/test/custom_local_txts/boards.local.txt b/internal/integrationtest/board/testdata/custom_local_txts/boards.local.txt similarity index 100% rename from legacy/builder/test/custom_local_txts/boards.local.txt rename to internal/integrationtest/board/testdata/custom_local_txts/boards.local.txt diff --git a/legacy/builder/test/custom_local_txts/platform.local.txt b/internal/integrationtest/board/testdata/custom_local_txts/platform.local.txt similarity index 100% rename from legacy/builder/test/custom_local_txts/platform.local.txt rename to internal/integrationtest/board/testdata/custom_local_txts/platform.local.txt diff --git a/legacy/builder/test/hardware_loader_test.go b/legacy/builder/test/hardware_loader_test.go index ae87e1c41c7..03bdc75b77f 100644 --- a/legacy/builder/test/hardware_loader_test.go +++ b/legacy/builder/test/hardware_loader_test.go @@ -25,43 +25,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestLoadHardware(t *testing.T) { - DownloadCoresAndToolsAndLibraries(t) - downloadedHardwareAvr := paths.New("downloaded_hardware", "arduino", "avr") - paths.New("custom_local_txts", "boards.local.txt").CopyTo(downloadedHardwareAvr.Join("boards.local.txt")) - paths.New("custom_local_txts", "platform.local.txt").CopyTo(downloadedHardwareAvr.Join("platform.local.txt")) - ctx := &types.Context{ - HardwareDirs: paths.NewPathList("downloaded_hardware", filepath.Join("..", "hardware")), - } - - ctx = prepareBuilderTestContext(t, ctx, nil, "", skipLibraries) - defer cleanUpBuilderTestContext(t, ctx) - - packages := ctx.PackageManager.GetPackages() - require.Equal(t, 1, len(packages)) - require.NotNil(t, packages["arduino"]) - require.Equal(t, 2, len(packages["arduino"].Platforms)) - - require.Equal(t, "uno", packages["arduino"].Platforms["avr"].Releases["1.6.10"].Boards["uno"].BoardID) - require.Equal(t, "uno", packages["arduino"].Platforms["avr"].Releases["1.6.10"].Boards["uno"].Properties.Get("_id")) - - require.Equal(t, "yun", packages["arduino"].Platforms["avr"].Releases["1.6.10"].Boards["yun"].BoardID) - require.Equal(t, "true", packages["arduino"].Platforms["avr"].Releases["1.6.10"].Boards["yun"].Properties.Get("upload.wait_for_upload_port")) - - require.Equal(t, "{build.usb_flags}", packages["arduino"].Platforms["avr"].Releases["1.6.10"].Boards["robotMotor"].Properties.Get("build.extra_flags")) - - require.Equal(t, "arduino_due_x", packages["arduino"].Platforms["sam"].Releases["1.6.7"].Boards["arduino_due_x"].BoardID) - - require.Equal(t, "ATmega123", packages["arduino"].Platforms["avr"].Releases["1.6.10"].Boards["diecimila"].Properties.Get("menu.cpu.atmega123")) - - avrPlatform := packages["arduino"].Platforms["avr"] - require.Equal(t, "Arduino AVR Boards", avrPlatform.Releases["1.6.10"].Properties.Get("name")) - require.Equal(t, "-v", avrPlatform.Releases["1.6.10"].Properties.Get("tools.avrdude.bootloader.params.verbose")) - require.Equal(t, "/my/personal/avrdude", avrPlatform.Releases["1.6.10"].Properties.Get("tools.avrdude.cmd.path")) - - require.Equal(t, "AVRISP mkII", avrPlatform.Releases["1.6.10"].Programmers["avrispmkii"].Name) -} - func TestLoadHardwareMixingUserHardwareFolder(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList("downloaded_hardware", filepath.Join("..", "hardware"), "user_hardware"), From 8727f67d66638451ba06a4c4912ea0a515f43808 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 Sep 2023 14:46:57 +0200 Subject: [PATCH 05/12] Moved user_hardware testdata resources --- internal/integrationtest/compile_4/compile_test.go | 2 +- .../{compile_4 => }/testdata/user_hardware/arduino/avr/.gitkeep | 0 .../testdata/user_hardware/my_avr_platform/avr/boards.txt | 0 .../avr/bootloaders/stk500v2/stk500boot_v2_mega2560.hex | 0 .../user_hardware/my_avr_platform/avr/libraries/SPI/SPI.cpp | 0 .../user_hardware/my_avr_platform/avr/libraries/SPI/SPI.h | 0 .../BarometricPressureSensor/BarometricPressureSensor.ino | 0 .../SPI/examples/DigitalPotControl/DigitalPotControl.ino | 0 .../my_avr_platform/avr/libraries/SPI/keywords.txt | 0 .../my_avr_platform/avr/libraries/SPI/library.properties | 0 .../testdata/user_hardware/my_avr_platform/avr/platform.txt | 0 .../testdata/user_hardware/my_symlinked_avr_platform | 0 12 files changed, 1 insertion(+), 1 deletion(-) rename internal/integrationtest/{compile_4 => }/testdata/user_hardware/arduino/avr/.gitkeep (100%) rename internal/integrationtest/{compile_4 => }/testdata/user_hardware/my_avr_platform/avr/boards.txt (100%) rename internal/integrationtest/{compile_4 => }/testdata/user_hardware/my_avr_platform/avr/bootloaders/stk500v2/stk500boot_v2_mega2560.hex (100%) rename internal/integrationtest/{compile_4 => }/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/SPI.cpp (100%) rename internal/integrationtest/{compile_4 => }/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/SPI.h (100%) rename internal/integrationtest/{compile_4 => }/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/examples/BarometricPressureSensor/BarometricPressureSensor.ino (100%) rename internal/integrationtest/{compile_4 => }/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/examples/DigitalPotControl/DigitalPotControl.ino (100%) rename internal/integrationtest/{compile_4 => }/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/keywords.txt (100%) rename internal/integrationtest/{compile_4 => }/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/library.properties (100%) rename internal/integrationtest/{compile_4 => }/testdata/user_hardware/my_avr_platform/avr/platform.txt (100%) rename internal/integrationtest/{compile_4 => }/testdata/user_hardware/my_symlinked_avr_platform (100%) diff --git a/internal/integrationtest/compile_4/compile_test.go b/internal/integrationtest/compile_4/compile_test.go index 41ae6bd856a..09125b5e2e1 100644 --- a/internal/integrationtest/compile_4/compile_test.go +++ b/internal/integrationtest/compile_4/compile_test.go @@ -65,7 +65,7 @@ func TestCompileOfProblematicSketches(t *testing.T) { require.NoError(t, err) // Install custom hardware required for tests - customHwDir, err := paths.New("testdata", "user_hardware").Abs() + customHwDir, err := paths.New("..", "testdata", "user_hardware").Abs() require.NoError(t, err) require.NoError(t, customHwDir.CopyDirTo(cli.SketchbookDir().Join("hardware"))) diff --git a/internal/integrationtest/compile_4/testdata/user_hardware/arduino/avr/.gitkeep b/internal/integrationtest/testdata/user_hardware/arduino/avr/.gitkeep similarity index 100% rename from internal/integrationtest/compile_4/testdata/user_hardware/arduino/avr/.gitkeep rename to internal/integrationtest/testdata/user_hardware/arduino/avr/.gitkeep diff --git a/internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/boards.txt b/internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/boards.txt similarity index 100% rename from internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/boards.txt rename to internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/boards.txt diff --git a/internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/bootloaders/stk500v2/stk500boot_v2_mega2560.hex b/internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/bootloaders/stk500v2/stk500boot_v2_mega2560.hex similarity index 100% rename from internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/bootloaders/stk500v2/stk500boot_v2_mega2560.hex rename to internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/bootloaders/stk500v2/stk500boot_v2_mega2560.hex diff --git a/internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/SPI.cpp b/internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/SPI.cpp similarity index 100% rename from internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/SPI.cpp rename to internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/SPI.cpp diff --git a/internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/SPI.h b/internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/SPI.h similarity index 100% rename from internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/SPI.h rename to internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/SPI.h diff --git a/internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/examples/BarometricPressureSensor/BarometricPressureSensor.ino b/internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/examples/BarometricPressureSensor/BarometricPressureSensor.ino similarity index 100% rename from internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/examples/BarometricPressureSensor/BarometricPressureSensor.ino rename to internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/examples/BarometricPressureSensor/BarometricPressureSensor.ino diff --git a/internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/examples/DigitalPotControl/DigitalPotControl.ino b/internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/examples/DigitalPotControl/DigitalPotControl.ino similarity index 100% rename from internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/examples/DigitalPotControl/DigitalPotControl.ino rename to internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/examples/DigitalPotControl/DigitalPotControl.ino diff --git a/internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/keywords.txt b/internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/keywords.txt similarity index 100% rename from internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/keywords.txt rename to internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/keywords.txt diff --git a/internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/library.properties b/internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/library.properties similarity index 100% rename from internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/library.properties rename to internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/libraries/SPI/library.properties diff --git a/internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/platform.txt b/internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/platform.txt similarity index 100% rename from internal/integrationtest/compile_4/testdata/user_hardware/my_avr_platform/avr/platform.txt rename to internal/integrationtest/testdata/user_hardware/my_avr_platform/avr/platform.txt diff --git a/internal/integrationtest/compile_4/testdata/user_hardware/my_symlinked_avr_platform b/internal/integrationtest/testdata/user_hardware/my_symlinked_avr_platform similarity index 100% rename from internal/integrationtest/compile_4/testdata/user_hardware/my_symlinked_avr_platform rename to internal/integrationtest/testdata/user_hardware/my_symlinked_avr_platform From 0deba1c2d4da06e031cd238f0f6a9a084acb73a4 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 Sep 2023 18:51:38 +0200 Subject: [PATCH 06/12] Ported TestLoadHardwareMixingUserHardwareFolder to current integration test infra --- .../board/hardware_loading_test.go | 282 ++++++++++++------ legacy/builder/test/hardware_loader_test.go | 60 ---- 2 files changed, 197 insertions(+), 145 deletions(-) diff --git a/internal/integrationtest/board/hardware_loading_test.go b/internal/integrationtest/board/hardware_loading_test.go index d770a906b86..05c650c7f0b 100644 --- a/internal/integrationtest/board/hardware_loading_test.go +++ b/internal/integrationtest/board/hardware_loading_test.go @@ -16,6 +16,7 @@ package board_test import ( + "runtime" "testing" "github.com/arduino/arduino-cli/internal/integrationtest" @@ -40,100 +41,211 @@ func TestHardwareLoading(t *testing.T) { localTxt.Join("boards.local.txt").CopyTo(downloadedHardwareAvr.Join("boards.local.txt")) localTxt.Join("platform.local.txt").CopyTo(downloadedHardwareAvr.Join("platform.local.txt")) - { - out, _, err := cli.Run("core", "list", "--format", "json") - require.NoError(t, err) - jsonOut := requirejson.Parse(t, out) - jsonOut.LengthMustEqualTo(1) - jsonOut.MustContain(`[ - { - "id": "arduino:avr", - "installed": "1.8.6", - "name": "Arduino AVR Boards", - "boards": [ + t.Run("Simple", func(t *testing.T) { + { + out, _, err := cli.Run("core", "list", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, out) + jsonOut.LengthMustEqualTo(1) + jsonOut.MustContain(`[ + { + "id": "arduino:avr", + "installed": "1.8.6", + "name": "Arduino AVR Boards", + "boards": [ + { + "name": "Arduino Uno", + "fqbn": "arduino:avr:uno" + }, + { + "name": "Arduino Yún", + "fqbn": "arduino:avr:yun" + } + ] + } + ]`) + } + + { + // Also test local platform.txt properties override + out, _, err := cli.Run("board", "details", "-b", "arduino:avr:uno", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, out) + jsonOut.MustContain(`{ + "version": "1.8.6", + "properties_id": "uno", + "build_properties": [ + "_id=uno", + "tools.avrdude.bootloader.params.verbose=-v", + "tools.avrdude.cmd.path=/my/personal/avrdude" + ], + "programmers": [ { - "name": "Arduino Uno", - "fqbn": "arduino:avr:uno" + "platform": "Arduino AVR Boards", + "id": "usbasp", + "name": "USBasp" }, { - "name": "Arduino Yún", - "fqbn": "arduino:avr:yun" + "platform": "Arduino AVR Boards", + "id": "avrispmkii", + "name": "AVRISP mkII" } + ] + }`) + } + + { + out, _, err := cli.Run("board", "details", "-b", "arduino:avr:yun", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, out) + jsonOut.MustContain(`{ + "version": "1.8.6", + "properties_id": "yun", + "build_properties": [ + "_id=yun", + "upload.wait_for_upload_port=true" ] - } - ]`) - } + }`) + } + + { + // Check un-expansion of board_properties + out, _, err := cli.Run("board", "details", "-b", "arduino:avr:robotMotor", "--show-properties=unexpanded", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, out) + jsonOut.MustContain(`{ + "version": "1.8.6", + "properties_id": "robotMotor", + "build_properties": [ + "_id=robotMotor", + "build.extra_flags={build.usb_flags}", + "upload.wait_for_upload_port=true" + ] + }`) + } + + { + // Also test local boards.txt properties override + out, _, err := cli.Run("board", "details", "-b", "arduino:avr:diecimila", "--show-properties=unexpanded", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, out) + jsonOut.MustContain(`{ + "version": "1.8.6", + "properties_id": "diecimila", + "build_properties": [ + "_id=diecimila", + "menu.cpu.atmega123=ATmega123" + ] + }`) + } + }) - { - // Also test local platform.txt properties override - out, _, err := cli.Run("board", "details", "-b", "arduino:avr:uno", "--format", "json") + t.Run("MixingUserHardware", func(t *testing.T) { + // Install custom hardware required for tests + customHwDir, err := paths.New("..", "testdata", "user_hardware").Abs() require.NoError(t, err) - jsonOut := requirejson.Parse(t, out) - jsonOut.MustContain(`{ - "version": "1.8.6", - "properties_id": "uno", - "build_properties": [ - "_id=uno", - "tools.avrdude.bootloader.params.verbose=-v", - "tools.avrdude.cmd.path=/my/personal/avrdude" - ], - "programmers": [ + require.NoError(t, customHwDir.CopyDirTo(cli.SketchbookDir().Join("hardware"))) + + { + out, _, err := cli.Run("core", "list", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, out) + if runtime.GOOS == "windows" { + //a package is a symlink, and windows does not support them + jsonOut.LengthMustEqualTo(2) + } else { + jsonOut.LengthMustEqualTo(3) + } + jsonOut.MustContain(`[ { - "platform": "Arduino AVR Boards", - "id": "usbasp", - "name": "USBasp" - }, + "id": "arduino:avr", + "installed": "1.8.6", + "name": "Arduino AVR Boards", + "boards": [ + { + "name": "Arduino Uno", + "fqbn": "arduino:avr:uno" + }, + { + "name": "Arduino Yún", + "fqbn": "arduino:avr:yun" + } + ] + } + ]`) + jsonOut.MustContain(`[ { - "platform": "Arduino AVR Boards", - "id": "avrispmkii", - "name": "AVRISP mkII" + "id": "my_avr_platform:avr", + "installed": "9.9.9", + "name": "My AVR Boards", + "boards": [ + { + "name": "Arduino Yún", + "fqbn": "my_avr_platform:avr:custom_yun" + } + ], + "manually_installed": true, + "missing_metadata": true } - ] - }`) - } + ]`) - { - out, _, err := cli.Run("board", "details", "-b", "arduino:avr:yun", "--format", "json") - require.NoError(t, err) - jsonOut := requirejson.Parse(t, out) - jsonOut.MustContain(`{ - "version": "1.8.6", - "properties_id": "yun", - "build_properties": [ - "_id=yun", - "upload.wait_for_upload_port=true" - ] - }`) - } - - { - // Check un-expansion of board_properties - out, _, err := cli.Run("board", "details", "-b", "arduino:avr:robotMotor", "--show-properties=unexpanded", "--format", "json") - require.NoError(t, err) - jsonOut := requirejson.Parse(t, out) - jsonOut.MustContain(`{ - "version": "1.8.6", - "properties_id": "robotMotor", - "build_properties": [ - "_id=robotMotor", - "build.extra_flags={build.usb_flags}", - "upload.wait_for_upload_port=true" - ] - }`) - } - - { - // Also test local boards.txt properties override - out, _, err := cli.Run("board", "details", "-b", "arduino:avr:diecimila", "--show-properties=unexpanded", "--format", "json") - require.NoError(t, err) - jsonOut := requirejson.Parse(t, out) - jsonOut.MustContain(`{ - "version": "1.8.6", - "properties_id": "diecimila", - "build_properties": [ - "_id=diecimila", - "menu.cpu.atmega123=ATmega123" - ] - }`) - } + // require.False(t, myAVRPlatformAvrArch.Properties.ContainsKey("preproc.includes.flags")) + + if runtime.GOOS != "windows" { + jsonOut.MustContain(`[ + { + "id": "my_symlinked_avr_platform:avr", + "manually_installed": true, + "missing_metadata": true + } + ]`) + } + } + + { + // Also test local platform.txt properties override + out, _, err := cli.Run("board", "details", "-b", "arduino:avr:uno", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, out) + jsonOut.MustContain(`{ + "version": "1.8.6", + "properties_id": "uno", + "build_properties": [ + "_id=uno", + "tools.avrdude.bootloader.params.verbose=-v", + "tools.avrdude.cmd.path=/my/personal/avrdude" + ], + "programmers": [ + { + "platform": "Arduino AVR Boards", + "id": "usbasp", + "name": "USBasp" + }, + { + "platform": "Arduino AVR Boards", + "id": "avrispmkii", + "name": "AVRISP mkII" + } + ] + }`) + } + + { + out, _, err := cli.Run("board", "details", "-b", "arduino:avr:yun", "--show-properties=unexpanded", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, out) + jsonOut.MustContain(`{ + "version": "1.8.6", + "properties_id": "yun", + "build_properties": [ + "_id=yun", + "upload.wait_for_upload_port=true", + "preproc.includes.flags=-w -x c++ -M -MG -MP", + "preproc.macros.flags=-w -x c++ -E -CC", + "recipe.preproc.includes=\"{compiler.path}{compiler.cpp.cmd}\" {compiler.cpp.flags} {preproc.includes.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} \"{source_file}\"" + ] + }`) + jsonOut.Query(`isempty( .build_properties[] | select(startswith("preproc.macros.compatibility_flags")) )`).MustEqual("true") + } + }) } diff --git a/legacy/builder/test/hardware_loader_test.go b/legacy/builder/test/hardware_loader_test.go index 03bdc75b77f..28228ac8e4c 100644 --- a/legacy/builder/test/hardware_loader_test.go +++ b/legacy/builder/test/hardware_loader_test.go @@ -25,66 +25,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestLoadHardwareMixingUserHardwareFolder(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList("downloaded_hardware", filepath.Join("..", "hardware"), "user_hardware"), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "", skipLibraries) - defer cleanUpBuilderTestContext(t, ctx) - - packages := ctx.PackageManager.GetPackages() - - if runtime.GOOS == "windows" { - //a package is a symlink, and windows does not support them - require.Equal(t, 2, len(packages)) - } else { - require.Equal(t, 3, len(packages)) - } - - require.NotNil(t, packages["arduino"]) - require.Equal(t, 2, len(packages["arduino"].Platforms)) - - require.Equal(t, "uno", packages["arduino"].Platforms["avr"].Releases["1.6.10"].Boards["uno"].BoardID) - require.Equal(t, "uno", packages["arduino"].Platforms["avr"].Releases["1.6.10"].Boards["uno"].Properties.Get("_id")) - - require.Equal(t, "yun", packages["arduino"].Platforms["avr"].Releases["1.6.10"].Boards["yun"].BoardID) - require.Equal(t, "true", packages["arduino"].Platforms["avr"].Releases["1.6.10"].Boards["yun"].Properties.Get("upload.wait_for_upload_port")) - - require.Equal(t, "{build.usb_flags}", packages["arduino"].Platforms["avr"].Releases["1.6.10"].Boards["robotMotor"].Properties.Get("build.extra_flags")) - - require.Equal(t, "arduino_due_x", packages["arduino"].Platforms["sam"].Releases["1.6.7"].Boards["arduino_due_x"].BoardID) - - avrPlatform := packages["arduino"].Platforms["avr"].Releases["1.6.10"] - require.Equal(t, "Arduino AVR Boards", avrPlatform.Properties.Get("name")) - require.Equal(t, "-v", avrPlatform.Properties.Get("tools.avrdude.bootloader.params.verbose")) - require.Equal(t, "/my/personal/avrdude", avrPlatform.Properties.Get("tools.avrdude.cmd.path")) - - require.Equal(t, "AVRISP mkII", avrPlatform.Programmers["avrispmkii"].Name) - - require.Equal(t, "-w -x c++ -M -MG -MP", avrPlatform.Properties.Get("preproc.includes.flags")) - require.Equal(t, "-w -x c++ -E -CC", avrPlatform.Properties.Get("preproc.macros.flags")) - require.Equal(t, "\"{compiler.path}{compiler.cpp.cmd}\" {compiler.cpp.flags} {preproc.includes.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} \"{source_file}\"", avrPlatform.Properties.Get("recipe.preproc.includes")) - require.False(t, avrPlatform.Properties.ContainsKey("preproc.macros.compatibility_flags")) - - require.NotNil(t, packages["my_avr_platform"]) - myAVRPlatform := packages["my_avr_platform"] - //require.Equal(t, "hello world", myAVRPlatform.Properties.Get("example")) - myAVRPlatformAvrArch := myAVRPlatform.Platforms["avr"].Releases["9.9.9"] - require.Equal(t, "custom_yun", myAVRPlatformAvrArch.Boards["custom_yun"].BoardID) - - require.False(t, myAVRPlatformAvrArch.Properties.ContainsKey("preproc.includes.flags")) - - //require.Equal(t, "{runtime.tools.ctags.path}", packages.Properties.Get("tools.ctags.path")) - //require.Equal(t, "\"{cmd.path}\" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives \"{source_file}\"", packages.Properties.Get("tools.ctags.pattern")) - //require.Equal(t, "{runtime.tools.avrdude.path}", packages.Properties.Get("tools.avrdude.path")) - //require.Equal(t, "-w -x c++ -E -CC", packages.Properties.Get("preproc.macros.flags")) - - if runtime.GOOS != "windows" { - require.NotNil(t, packages["my_symlinked_avr_platform"]) - require.NotNil(t, packages["my_symlinked_avr_platform"].Platforms["avr"]) - } -} - func TestLoadHardwareWithBoardManagerFolderStructure(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList("downloaded_board_manager_stuff"), From 2fd7651a435206820067fa88422b93d40ff0066e Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 Sep 2023 18:53:35 +0200 Subject: [PATCH 07/12] Removed redundant legacy tests --- legacy/builder/test/hardware_loader_test.go | 100 -------------------- 1 file changed, 100 deletions(-) delete mode 100644 legacy/builder/test/hardware_loader_test.go diff --git a/legacy/builder/test/hardware_loader_test.go b/legacy/builder/test/hardware_loader_test.go deleted file mode 100644 index 28228ac8e4c..00000000000 --- a/legacy/builder/test/hardware_loader_test.go +++ /dev/null @@ -1,100 +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" - "runtime" - "testing" - - "github.com/arduino/arduino-cli/legacy/builder/types" - paths "github.com/arduino/go-paths-helper" - "github.com/stretchr/testify/require" -) - -func TestLoadHardwareWithBoardManagerFolderStructure(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList("downloaded_board_manager_stuff"), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "", skipLibraries) - defer cleanUpBuilderTestContext(t, ctx) - - packages := ctx.PackageManager.GetPackages() - require.Equal(t, 3, len(packages)) - require.NotNil(t, packages["arduino"]) - require.Equal(t, 1, len(packages["arduino"].Platforms)) - require.NotNil(t, packages["RedBearLab"]) - require.Equal(t, 1, len(packages["RedBearLab"].Platforms)) - require.NotNil(t, packages["RFduino"]) - require.Equal(t, 0, len(packages["RFduino"].Platforms)) - - samdPlatform := packages["arduino"].Platforms["samd"].Releases["1.6.5"] - require.Equal(t, 3, len(samdPlatform.Boards)) - - require.Equal(t, "arduino_zero_edbg", samdPlatform.Boards["arduino_zero_edbg"].BoardID) - require.Equal(t, "arduino_zero_edbg", samdPlatform.Boards["arduino_zero_edbg"].Properties.Get("_id")) - - require.Equal(t, "arduino_zero", samdPlatform.Boards["arduino_zero_native"].Properties.Get("build.variant")) - require.Equal(t, "-D__SAMD21G18A__ {build.usb_flags}", samdPlatform.Boards["arduino_zero_native"].Properties.Get("build.extra_flags")) - - require.Equal(t, "Arduino SAMD (32-bits ARM Cortex-M0+) Boards", samdPlatform.Properties.Get("name")) - require.Equal(t, "-d3", samdPlatform.Properties.Get("tools.openocd.erase.params.verbose")) - - require.Equal(t, 3, len(samdPlatform.Programmers)) - - require.Equal(t, "Atmel EDBG", samdPlatform.Programmers["edbg"].Name) - require.Equal(t, "openocd", samdPlatform.Programmers["edbg"].Properties.Get("program.tool")) - - avrRedBearPlatform := packages["RedBearLab"].Platforms["avr"].Releases["1.0.0"] - require.Equal(t, 3, len(avrRedBearPlatform.Boards)) - - require.Equal(t, "blend", avrRedBearPlatform.Boards["blend"].BoardID) - require.Equal(t, "blend", avrRedBearPlatform.Boards["blend"].Properties.Get("_id")) - require.Equal(t, "arduino:arduino", avrRedBearPlatform.Boards["blend"].Properties.Get("build.core")) -} - -func TestLoadLotsOfHardware(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList("downloaded_board_manager_stuff", "downloaded_hardware", filepath.Join("..", "hardware"), "user_hardware"), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "", skipLibraries) - defer cleanUpBuilderTestContext(t, ctx) - - packages := ctx.PackageManager.GetPackages() - - if runtime.GOOS == "windows" { - //a package is a symlink, and windows does not support them - require.Equal(t, 4, len(packages)) - } else { - require.Equal(t, 5, len(packages)) - } - - require.NotNil(t, packages["arduino"]) - require.NotNil(t, packages["my_avr_platform"]) - - require.Equal(t, 3, len(packages["arduino"].Platforms)) - require.Equal(t, 20, len(packages["arduino"].Platforms["avr"].Releases["1.6.10"].Boards)) - require.Equal(t, 2, len(packages["arduino"].Platforms["sam"].Releases["1.6.7"].Boards)) - require.Equal(t, 3, len(packages["arduino"].Platforms["samd"].Releases["1.6.5"].Boards)) - - require.Equal(t, 1, len(packages["my_avr_platform"].Platforms)) - require.Equal(t, 2, len(packages["my_avr_platform"].Platforms["avr"].Releases["9.9.9"].Boards)) - - if runtime.GOOS != "windows" { - require.Equal(t, 1, len(packages["my_symlinked_avr_platform"].Platforms)) - require.Equal(t, 2, len(packages["my_symlinked_avr_platform"].Platforms["avr"].Releases["9.9.9"].Boards)) - } -} From a040517e7608dd2c167cdb11e0078f234ebf0362 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 Sep 2023 18:56:15 +0200 Subject: [PATCH 08/12] Removed useless 'sleep' helper function --- legacy/builder/test/builder_utils_test.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/legacy/builder/test/builder_utils_test.go b/legacy/builder/test/builder_utils_test.go index f032eabeddc..b147d183c44 100644 --- a/legacy/builder/test/builder_utils_test.go +++ b/legacy/builder/test/builder_utils_test.go @@ -25,12 +25,6 @@ import ( "github.com/stretchr/testify/require" ) -func sleep(t *testing.T) { - dur, err := time.ParseDuration("1s") - NoError(t, err) - time.Sleep(dur) -} - func tempFile(t *testing.T, prefix string) *paths.Path { file, err := os.CreateTemp("", prefix) file.Close() @@ -65,7 +59,7 @@ func TestObjFileIsUpToDateObjOlder(t *testing.T) { depFile := tempFile(t, "dep") defer depFile.RemoveAll() - sleep(t) + time.Sleep(time.Second) sourceFile := tempFile(t, "source") defer sourceFile.RemoveAll() @@ -79,7 +73,7 @@ func TestObjFileIsUpToDateObjNewer(t *testing.T) { sourceFile := tempFile(t, "source") defer sourceFile.RemoveAll() - sleep(t) + time.Sleep(time.Second) objFile := tempFile(t, "obj") defer objFile.RemoveAll() @@ -95,14 +89,14 @@ func TestObjFileIsUpToDateDepIsNewer(t *testing.T) { sourceFile := tempFile(t, "source") defer sourceFile.RemoveAll() - sleep(t) + time.Sleep(time.Second) objFile := tempFile(t, "obj") defer objFile.RemoveAll() depFile := tempFile(t, "dep") defer depFile.RemoveAll() - sleep(t) + time.Sleep(time.Second) headerFile := tempFile(t, "header") defer headerFile.RemoveAll() @@ -122,7 +116,7 @@ func TestObjFileIsUpToDateDepIsOlder(t *testing.T) { headerFile := tempFile(t, "header") defer headerFile.RemoveAll() - sleep(t) + time.Sleep(time.Second) objFile := tempFile(t, "obj") defer objFile.RemoveAll() @@ -141,14 +135,14 @@ func TestObjFileIsUpToDateDepIsWrong(t *testing.T) { sourceFile := tempFile(t, "source") defer sourceFile.RemoveAll() - sleep(t) + time.Sleep(time.Second) objFile := tempFile(t, "obj") defer objFile.RemoveAll() depFile := tempFile(t, "dep") defer depFile.RemoveAll() - sleep(t) + time.Sleep(time.Second) headerFile := tempFile(t, "header") defer headerFile.RemoveAll() From 45aa68b0d17f587cc3597fb251375429c6b69f73 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 Sep 2023 18:56:56 +0200 Subject: [PATCH 09/12] Removed unused LoadAndInterpolate function --- legacy/builder/test/helper.go | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/legacy/builder/test/helper.go b/legacy/builder/test/helper.go index 88432e5fc76..4e6f05ec3c8 100644 --- a/legacy/builder/test/helper.go +++ b/legacy/builder/test/helper.go @@ -17,13 +17,9 @@ package test import ( - "bytes" "fmt" - "path/filepath" "testing" - "text/template" - "github.com/arduino/arduino-cli/arduino/builder/cpp" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/libraries" "github.com/arduino/arduino-cli/legacy/builder/constants" @@ -33,23 +29,6 @@ import ( "github.com/stretchr/testify/require" ) -func LoadAndInterpolate(t *testing.T, filename string, ctx *types.Context) string { - funcsMap := template.FuncMap{ - "QuoteCppString": func(p *paths.Path) string { return cpp.QuoteString(p.String()) }, - } - - tpl, err := template.New(filepath.Base(filename)).Funcs(funcsMap).ParseFiles(filename) - NoError(t, err) - - var buf bytes.Buffer - data := make(map[string]interface{}) - data["sketch"] = ctx.Sketch - err = tpl.Execute(&buf, data) - NoError(t, err) - - return buf.String() -} - func Abs(t *testing.T, rel *paths.Path) *paths.Path { absPath, err := rel.Abs() NoError(t, err) From 3d979d3c569fe1d598ae1e6f4832fd44a6c18ded Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 Sep 2023 19:00:22 +0200 Subject: [PATCH 10/12] Removed useless NoError helper function --- legacy/builder/test/builder_test.go | 10 +++--- legacy/builder/test/builder_utils_test.go | 16 ++++----- .../test/create_build_options_map_test.go | 2 +- ...ail_if_buildpath_equals_sketchpath_test.go | 2 +- legacy/builder/test/helper.go | 15 ++------- .../builder/test/helper_tools_downloader.go | 33 ++++++++++--------- legacy/builder/test/libraries_loader_test.go | 8 ++--- .../load_previous_build_options_map_test.go | 6 ++-- .../test/merge_sketch_with_bootloader_test.go | 26 +++++++-------- legacy/builder/test/recipe_runner_test.go | 3 +- .../test/setup_build_properties_test.go | 2 +- .../test/store_build_options_map_test.go | 6 ++-- .../try_build_of_problematic_sketch_test.go | 3 +- .../unused_compiled_libraries_remover_test.go | 28 ++++++++-------- ...uild_path_if_build_options_changed_test.go | 14 ++++---- 15 files changed, 84 insertions(+), 90 deletions(-) diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index f8f59a0ff55..c4f61212cc6 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -60,17 +60,17 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat } if ctx.BuildPath == nil { buildPath, err := paths.MkTempDir("", "test_build_path") - NoError(t, err) + require.NoError(t, err) ctx.BuildPath = buildPath } buildPath := ctx.BuildPath sketchBuildPath, err := buildPath.Join(constants.FOLDER_SKETCH).Abs() - NoError(t, err) + require.NoError(t, err) librariesBuildPath, err := buildPath.Join(constants.FOLDER_LIBRARIES).Abs() - NoError(t, err) + require.NoError(t, err) coreBuildPath, err := buildPath.Join(constants.FOLDER_CORE).Abs() - NoError(t, err) + require.NoError(t, err) ctx.SketchBuildPath = sketchBuildPath ctx.LibrariesBuildPath = librariesBuildPath @@ -128,7 +128,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.BuiltInLibrariesDirs, ctx.LibraryDirs, ctx.OtherLibrariesDirs, ctx.ActualPlatform, ctx.TargetPlatform, ) - NoError(t, err) + require.NoError(t, err) ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( lm, libsResolver, diff --git a/legacy/builder/test/builder_utils_test.go b/legacy/builder/test/builder_utils_test.go index b147d183c44..8d5cb09de2e 100644 --- a/legacy/builder/test/builder_utils_test.go +++ b/legacy/builder/test/builder_utils_test.go @@ -28,7 +28,7 @@ import ( func tempFile(t *testing.T, prefix string) *paths.Path { file, err := os.CreateTemp("", prefix) file.Close() - NoError(t, err) + require.NoError(t, err) return paths.New(file.Name()) } @@ -37,7 +37,7 @@ func TestObjFileIsUpToDateObjMissing(t *testing.T) { defer sourceFile.RemoveAll() upToDate, err := utils.ObjFileIsUpToDate(sourceFile, nil, nil) - NoError(t, err) + require.NoError(t, err) require.False(t, upToDate) } @@ -49,7 +49,7 @@ func TestObjFileIsUpToDateDepMissing(t *testing.T) { defer objFile.RemoveAll() upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, nil) - NoError(t, err) + require.NoError(t, err) require.False(t, upToDate) } @@ -65,7 +65,7 @@ func TestObjFileIsUpToDateObjOlder(t *testing.T) { defer sourceFile.RemoveAll() upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - NoError(t, err) + require.NoError(t, err) require.False(t, upToDate) } @@ -81,7 +81,7 @@ func TestObjFileIsUpToDateObjNewer(t *testing.T) { defer depFile.RemoveAll() upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - NoError(t, err) + require.NoError(t, err) require.True(t, upToDate) } @@ -105,7 +105,7 @@ func TestObjFileIsUpToDateDepIsNewer(t *testing.T) { depFile.WriteFile([]byte(data)) upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - NoError(t, err) + require.NoError(t, err) require.False(t, upToDate) } @@ -127,7 +127,7 @@ func TestObjFileIsUpToDateDepIsOlder(t *testing.T) { depFile.WriteFile([]byte(res)) upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - NoError(t, err) + require.NoError(t, err) require.True(t, upToDate) } @@ -151,6 +151,6 @@ func TestObjFileIsUpToDateDepIsWrong(t *testing.T) { depFile.WriteFile([]byte(res)) upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - NoError(t, err) + require.NoError(t, err) require.False(t, upToDate) } diff --git a/legacy/builder/test/create_build_options_map_test.go b/legacy/builder/test/create_build_options_map_test.go index 4e250b07d9b..4cd1986f55d 100644 --- a/legacy/builder/test/create_build_options_map_test.go +++ b/legacy/builder/test/create_build_options_map_test.go @@ -40,7 +40,7 @@ func TestCreateBuildOptionsMap(t *testing.T) { create := builder.CreateBuildOptionsMap{} err := create.Run(ctx) - NoError(t, err) + require.NoError(t, err) require.Equal(t, `{ "additionalFiles": "", diff --git a/legacy/builder/test/fail_if_buildpath_equals_sketchpath_test.go b/legacy/builder/test/fail_if_buildpath_equals_sketchpath_test.go index d24b9d65ee1..6cdeb21e6ae 100644 --- a/legacy/builder/test/fail_if_buildpath_equals_sketchpath_test.go +++ b/legacy/builder/test/fail_if_buildpath_equals_sketchpath_test.go @@ -42,5 +42,5 @@ func TestFailIfBuildPathEqualsSketchPathSketchPathDiffers(t *testing.T) { } command := builder.FailIfBuildPathEqualsSketchPath{} - NoError(t, command.Run(ctx)) + require.NoError(t, command.Run(ctx)) } diff --git a/legacy/builder/test/helper.go b/legacy/builder/test/helper.go index 4e6f05ec3c8..4ca6f318157 100644 --- a/legacy/builder/test/helper.go +++ b/legacy/builder/test/helper.go @@ -17,7 +17,6 @@ package test import ( - "fmt" "testing" "github.com/arduino/arduino-cli/arduino/cores" @@ -25,33 +24,25 @@ import ( "github.com/arduino/arduino-cli/legacy/builder/constants" "github.com/arduino/arduino-cli/legacy/builder/types" paths "github.com/arduino/go-paths-helper" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func Abs(t *testing.T, rel *paths.Path) *paths.Path { absPath, err := rel.Abs() - NoError(t, err) + require.NoError(t, err) return absPath } -func NoError(t *testing.T, err error, msgAndArgs ...interface{}) { - if !assert.NoError(t, err, msgAndArgs...) { - fmt.Printf("%+v\n", err) // Outputs stack trace in case of wrapped error - t.FailNow() - } -} - func SetupBuildPath(t *testing.T, ctx *types.Context) *paths.Path { buildPath, err := paths.MkTempDir("", "test_build_path") - NoError(t, err) + require.NoError(t, err) ctx.BuildPath = buildPath return buildPath } func SetupBuildCachePath(t *testing.T, ctx *types.Context) *paths.Path { buildCachePath, err := paths.MkTempDir(constants.EMPTY_STRING, "test_build_cache") - NoError(t, err) + require.NoError(t, err) ctx.CoreBuildCachePath = buildCachePath return buildCachePath } diff --git a/legacy/builder/test/helper_tools_downloader.go b/legacy/builder/test/helper_tools_downloader.go index c32042c0c15..f28c29c1c5e 100644 --- a/legacy/builder/test/helper_tools_downloader.go +++ b/legacy/builder/test/helper_tools_downloader.go @@ -30,6 +30,7 @@ import ( "github.com/arduino/go-paths-helper" "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" + "github.com/stretchr/testify/require" "golang.org/x/exp/slices" ) @@ -136,9 +137,9 @@ func DownloadCoresAndToolsAndLibraries(t *testing.T) { func patchFiles(t *testing.T) { err := patchesFolder.MkdirAll() - NoError(t, err) + require.NoError(t, err) files, err := patchesFolder.ReadDir() - NoError(t, err) + require.NoError(t, err) for _, file := range files { if file.Ext() == ".patch" { @@ -147,9 +148,9 @@ func patchFiles(t *testing.T) { // https://github.com/arduino/arduino-builder/issues/147 /* data, err := ioutil.ReadFile(Abs(t, filepath.Join(PATCHES_FOLDER, file.Name()))) - NoError(t, err) + require.NoError(t, err) patchSet, err := patch.Parse(data) - NoError(t, err) + require.NoError(t, err) operations, err := patchSet.Apply(ioutil.ReadFile) for _, op := range operations { utils.WriteFileBytes(op.Dst, op.Data) @@ -161,7 +162,7 @@ func patchFiles(t *testing.T) { func download(t *testing.T, cores, boardsManagerCores, boardsManagerRedBearCores []Core, tools, toolsMultipleVersions, boardsManagerTools, boardsManagerRFduinoTools []Tool, libraries []Library) { allCoresDownloaded, err := allCoresAlreadyDownloadedAndUnpacked(hardwareFolder, cores) - NoError(t, err) + require.NoError(t, err) if allCoresDownloaded && allBoardsManagerCoresAlreadyDownloadedAndUnpacked(boardManagerFolder, boardsManagerCores) && allBoardsManagerCoresAlreadyDownloadedAndUnpacked(boardManagerFolder, boardsManagerRedBearCores) && @@ -174,37 +175,37 @@ func download(t *testing.T, cores, boardsManagerCores, boardsManagerRedBearCores } index, err := downloadIndex("http://downloads.arduino.cc/packages/package_index.json") - NoError(t, err) + require.NoError(t, err) err = downloadCores(cores, index) - NoError(t, err) + require.NoError(t, err) err = downloadBoardManagerCores(boardsManagerCores, index) - NoError(t, err) + require.NoError(t, err) err = downloadTools(tools, index) - NoError(t, err) + require.NoError(t, err) err = downloadToolsMultipleVersions(toolsMultipleVersions, index) - NoError(t, err) + require.NoError(t, err) err = downloadBoardsManagerTools(boardsManagerTools, index) - NoError(t, err) + require.NoError(t, err) rfduinoIndex, err := downloadIndex("http://downloads.arduino.cc/packages/test_package_rfduino_index.json") - NoError(t, err) + require.NoError(t, err) err = downloadBoardsManagerTools(boardsManagerRFduinoTools, rfduinoIndex) - NoError(t, err) + require.NoError(t, err) err = downloadBoardManagerCores(boardsManagerRedBearCores, nil) - NoError(t, err) + require.NoError(t, err) librariesIndex, err := downloadIndex("http://downloads.arduino.cc/libraries/library_index.json") - NoError(t, err) + require.NoError(t, err) err = downloadLibraries(libraries, librariesIndex) - NoError(t, err) + require.NoError(t, err) } func downloadIndex(url string) (map[string]interface{}, error) { diff --git a/legacy/builder/test/libraries_loader_test.go b/legacy/builder/test/libraries_loader_test.go index e4e1cacfa59..b265ca848a2 100644 --- a/legacy/builder/test/libraries_loader_test.go +++ b/legacy/builder/test/libraries_loader_test.go @@ -52,7 +52,7 @@ func TestLoadLibrariesAVR(t *testing.T) { ctx.BuiltInLibrariesDirs, ctx.LibraryDirs, ctx.OtherLibrariesDirs, ctx.ActualPlatform, ctx.TargetPlatform, ) - NoError(t, err) + require.NoError(t, err) librariesFolders := lm.LibrariesDir require.Equal(t, 3, len(librariesFolders)) @@ -156,7 +156,7 @@ func TestLoadLibrariesSAM(t *testing.T) { ctx.BuiltInLibrariesDirs, ctx.LibraryDirs, ctx.OtherLibrariesDirs, ctx.ActualPlatform, ctx.TargetPlatform, ) - NoError(t, err) + require.NoError(t, err) librariesFolders := lm.LibrariesDir require.Equal(t, 3, len(librariesFolders)) @@ -233,7 +233,7 @@ func TestLoadLibrariesAVRNoDuplicateLibrariesFolders(t *testing.T) { ctx.BuiltInLibrariesDirs, ctx.LibraryDirs, ctx.OtherLibrariesDirs, ctx.ActualPlatform, ctx.TargetPlatform, ) - NoError(t, err) + require.NoError(t, err) librariesFolders := lm.LibrariesDir require.Equal(t, 3, len(librariesFolders)) @@ -256,7 +256,7 @@ func TestLoadLibrariesMyAVRPlatform(t *testing.T) { ctx.BuiltInLibrariesDirs, ctx.LibraryDirs, ctx.OtherLibrariesDirs, ctx.ActualPlatform, ctx.TargetPlatform, ) - NoError(t, err) + require.NoError(t, err) librariesFolders := lm.LibrariesDir require.Equal(t, 4, len(librariesFolders)) diff --git a/legacy/builder/test/load_previous_build_options_map_test.go b/legacy/builder/test/load_previous_build_options_map_test.go index 0ff05a1b973..b137b6e4b45 100644 --- a/legacy/builder/test/load_previous_build_options_map_test.go +++ b/legacy/builder/test/load_previous_build_options_map_test.go @@ -31,11 +31,11 @@ func TestLoadPreviousBuildOptionsMap(t *testing.T) { defer buildPath.RemoveAll() err := buildPath.Join(constants.BUILD_OPTIONS_FILE).WriteFile([]byte("test")) - NoError(t, err) + require.NoError(t, err) command := builder.LoadPreviousBuildOptionsMap{} err = command.Run(ctx) - NoError(t, err) + require.NoError(t, err) require.Equal(t, "test", ctx.BuildOptionsJsonPrevious) } @@ -48,7 +48,7 @@ func TestLoadPreviousBuildOptionsMapMissingFile(t *testing.T) { command := builder.LoadPreviousBuildOptionsMap{} err := command.Run(ctx) - NoError(t, err) + require.NoError(t, err) require.Empty(t, ctx.BuildOptionsJsonPrevious) } diff --git a/legacy/builder/test/merge_sketch_with_bootloader_test.go b/legacy/builder/test/merge_sketch_with_bootloader_test.go index 43387eeaa8d..6dac87c05b3 100644 --- a/legacy/builder/test/merge_sketch_with_bootloader_test.go +++ b/legacy/builder/test/merge_sketch_with_bootloader_test.go @@ -34,7 +34,7 @@ func TestMergeSketchWithBootloader(t *testing.T) { buildPath := ctx.BuildPath err := buildPath.Join("sketch").MkdirAll() - NoError(t, err) + require.NoError(t, err) fakeSketchHex := `:100000000C9434000C9446000C9446000C9446006A :100010000C9446000C9446000C9446000C94460048 @@ -67,7 +67,7 @@ func TestMergeSketchWithBootloader(t *testing.T) { :00000001FF ` err = buildPath.Join("sketch", "sketch1.ino.hex").WriteFile([]byte(fakeSketchHex)) - NoError(t, err) + require.NoError(t, err) commands := []types.Command{ &builder.MergeSketchWithBootloader{}, @@ -75,11 +75,11 @@ func TestMergeSketchWithBootloader(t *testing.T) { for _, command := range commands { err := command.Run(ctx) - NoError(t, err) + require.NoError(t, err) } bytes, err := buildPath.Join("sketch", "sketch1.ino.with_bootloader.hex").ReadFile() - NoError(t, err) + require.NoError(t, err) mergedSketchHex := string(bytes) require.Contains(t, mergedSketchHex, ":100000000C9434000C9446000C9446000C9446006A\n") @@ -92,7 +92,7 @@ func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) { buildPath := ctx.BuildPath err := buildPath.Join("sketch").MkdirAll() - NoError(t, err) + require.NoError(t, err) fakeSketchHex := `:100000000C9434000C9446000C9446000C9446006A :100010000C9446000C9446000C9446000C94460048 @@ -125,7 +125,7 @@ func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) { :00000001FF ` err = buildPath.Join("sketch1.ino.hex").WriteFile([]byte(fakeSketchHex)) - NoError(t, err) + require.NoError(t, err) commands := []types.Command{ &builder.MergeSketchWithBootloader{}, @@ -133,11 +133,11 @@ func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) { for _, command := range commands { err := command.Run(ctx) - NoError(t, err) + require.NoError(t, err) } bytes, err := buildPath.Join("sketch1.ino.with_bootloader.hex").ReadFile() - NoError(t, err) + require.NoError(t, err) mergedSketchHex := string(bytes) fmt.Println(string(mergedSketchHex)) @@ -156,7 +156,7 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) { command := &builder.MergeSketchWithBootloader{} err := command.Run(ctx) - NoError(t, err) + require.NoError(t, err) exist, err := buildPath.Join("sketch.ino.with_bootloader.hex").ExistCheck() require.NoError(t, err) @@ -175,7 +175,7 @@ func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) { buildPath := ctx.BuildPath err := buildPath.Join("sketch").MkdirAll() - NoError(t, err) + require.NoError(t, err) fakeSketchHex := `:100000000C9434000C9446000C9446000C9446006A :100010000C9446000C9446000C9446000C94460048 @@ -208,7 +208,7 @@ func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) { :00000001FF ` err = buildPath.Join("sketch", "sketch1.ino.hex").WriteFile([]byte(fakeSketchHex)) - NoError(t, err) + require.NoError(t, err) commands := []types.Command{ &builder.MergeSketchWithBootloader{}, @@ -216,11 +216,11 @@ func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) { for _, command := range commands { err := command.Run(ctx) - NoError(t, err) + require.NoError(t, err) } bytes, err := buildPath.Join("sketch", "sketch1.ino.with_bootloader.hex").ReadFile() - NoError(t, err) + require.NoError(t, err) mergedSketchHex := string(bytes) require.Contains(t, mergedSketchHex, ":100000000C9434000C9446000C9446000C9446006A\n") diff --git a/legacy/builder/test/recipe_runner_test.go b/legacy/builder/test/recipe_runner_test.go index 5d9a53ef68e..72a23353faf 100644 --- a/legacy/builder/test/recipe_runner_test.go +++ b/legacy/builder/test/recipe_runner_test.go @@ -21,6 +21,7 @@ import ( "github.com/arduino/arduino-cli/legacy/builder" "github.com/arduino/arduino-cli/legacy/builder/types" "github.com/arduino/go-properties-orderedmap" + "github.com/stretchr/testify/require" ) // TODO @@ -40,6 +41,6 @@ func TestRecipeRunner(t *testing.T) { for _, command := range commands { err := command.Run(ctx) - NoError(t, err) + require.NoError(t, err) } } diff --git a/legacy/builder/test/setup_build_properties_test.go b/legacy/builder/test/setup_build_properties_test.go index 2c0344812a2..9b8d7236a31 100644 --- a/legacy/builder/test/setup_build_properties_test.go +++ b/legacy/builder/test/setup_build_properties_test.go @@ -80,7 +80,7 @@ func TestSetupBuildPropertiesWithSomeCustomOverrides(t *testing.T) { ctx = prepareBuilderTestContext(t, ctx, paths.New("sketch1", "sketch1.ino"), "arduino:avr:uno") defer cleanUpBuilderTestContext(t, ctx) customProps, err := properties.LoadFromSlice(ctx.CustomBuildProperties) - NoError(t, err) + require.NoError(t, err) ctx.BuildProperties.Merge(customProps) buildProperties := ctx.BuildProperties diff --git a/legacy/builder/test/store_build_options_map_test.go b/legacy/builder/test/store_build_options_map_test.go index 47d040f9dac..25f5866a9af 100644 --- a/legacy/builder/test/store_build_options_map_test.go +++ b/legacy/builder/test/store_build_options_map_test.go @@ -50,15 +50,15 @@ func TestStoreBuildOptionsMap(t *testing.T) { for _, command := range commands { err := command.Run(ctx) - NoError(t, err) + require.NoError(t, err) } exist, err := buildPath.Join(constants.BUILD_OPTIONS_FILE).ExistCheck() - NoError(t, err) + require.NoError(t, err) require.True(t, exist) bytes, err := buildPath.Join(constants.BUILD_OPTIONS_FILE).ReadFile() - NoError(t, err) + require.NoError(t, err) require.Equal(t, `{ "additionalFiles": "", 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 a5dc81d308a..9a5b43a0e9f 100644 --- a/legacy/builder/test/try_build_of_problematic_sketch_test.go +++ b/legacy/builder/test/try_build_of_problematic_sketch_test.go @@ -24,6 +24,7 @@ import ( "github.com/arduino/arduino-cli/legacy/builder" "github.com/arduino/arduino-cli/legacy/builder/types" paths "github.com/arduino/go-paths-helper" + "github.com/stretchr/testify/require" ) // This is a sketch that fails to build on purpose @@ -93,5 +94,5 @@ func tryBuildWithContext(t *testing.T, ctx *types.Context, fqbn string, sketchLo defer cleanUpBuilderTestContext(t, ctx) err := builder.RunBuilder(ctx) - NoError(t, err, "Build error for "+sketchLocation.String()) + require.NoError(t, err, "Build error for "+sketchLocation.String()) } diff --git a/legacy/builder/test/unused_compiled_libraries_remover_test.go b/legacy/builder/test/unused_compiled_libraries_remover_test.go index 24001e674ab..d8586eb7996 100644 --- a/legacy/builder/test/unused_compiled_libraries_remover_test.go +++ b/legacy/builder/test/unused_compiled_libraries_remover_test.go @@ -28,12 +28,12 @@ import ( func TestUnusedCompiledLibrariesRemover(t *testing.T) { temp, err := paths.MkTempDir("", "test") - NoError(t, err) + require.NoError(t, err) defer temp.RemoveAll() - NoError(t, temp.Join("SPI").MkdirAll()) - NoError(t, temp.Join("Bridge").MkdirAll()) - NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) + require.NoError(t, temp.Join("SPI").MkdirAll()) + require.NoError(t, temp.Join("Bridge").MkdirAll()) + require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) ctx := &types.Context{} ctx.LibrariesBuildPath = temp @@ -44,16 +44,16 @@ func TestUnusedCompiledLibrariesRemover(t *testing.T) { cmd := builder.UnusedCompiledLibrariesRemover{} err = cmd.Run(ctx) - NoError(t, err) + require.NoError(t, err) exist, err := temp.Join("SPI").ExistCheck() require.NoError(t, err) require.False(t, exist) exist, err = temp.Join("Bridge").ExistCheck() - NoError(t, err) + require.NoError(t, err) require.True(t, exist) exist, err = temp.Join("dummy_file").ExistCheck() - NoError(t, err) + require.NoError(t, err) require.True(t, exist) } @@ -67,17 +67,17 @@ func TestUnusedCompiledLibrariesRemoverLibDoesNotExist(t *testing.T) { cmd := builder.UnusedCompiledLibrariesRemover{} err := cmd.Run(ctx) - NoError(t, err) + require.NoError(t, err) } func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) { temp, err := paths.MkTempDir("", "test") - NoError(t, err) + require.NoError(t, err) defer temp.RemoveAll() - NoError(t, temp.Join("SPI").MkdirAll()) - NoError(t, temp.Join("Bridge").MkdirAll()) - NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) + require.NoError(t, temp.Join("SPI").MkdirAll()) + require.NoError(t, temp.Join("Bridge").MkdirAll()) + require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) ctx := &types.Context{} ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( @@ -87,7 +87,7 @@ func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) { cmd := builder.UnusedCompiledLibrariesRemover{} err = cmd.Run(ctx) - NoError(t, err) + require.NoError(t, err) exist, err := temp.Join("SPI").ExistCheck() require.NoError(t, err) @@ -96,6 +96,6 @@ func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) { require.NoError(t, err) require.False(t, exist) exist, err = temp.Join("dummy_file").ExistCheck() - NoError(t, err) + require.NoError(t, err) require.True(t, exist) } diff --git a/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go b/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go index e46a66a7dbb..dd72c1926e9 100644 --- a/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go +++ b/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go @@ -40,15 +40,15 @@ func TestWipeoutBuildPathIfBuildOptionsChanged(t *testing.T) { for _, command := range commands { err := command.Run(ctx) - NoError(t, err) + require.NoError(t, err) } exist, err := buildPath.ExistCheck() - NoError(t, err) + require.NoError(t, err) require.True(t, exist) files, err := buildPath.ReadDir() - NoError(t, err) + require.NoError(t, err) require.Equal(t, 0, len(files)) exist, err = buildPath.Join("should_be_deleted.txt").ExistCheck() @@ -72,18 +72,18 @@ func TestWipeoutBuildPathIfBuildOptionsChangedNoPreviousBuildOptions(t *testing. for _, command := range commands { err := command.Run(ctx) - NoError(t, err) + require.NoError(t, err) } exist, err := buildPath.ExistCheck() - NoError(t, err) + require.NoError(t, err) require.True(t, exist) files, err := buildPath.ReadDir() - NoError(t, err) + require.NoError(t, err) require.Equal(t, 1, len(files)) exist, err = buildPath.Join("should_not_be_deleted.txt").ExistCheck() - NoError(t, err) + require.NoError(t, err) require.True(t, exist) } From aa0e293745acd552c73b7fd0efdeb02f4f048c67 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 Sep 2023 19:56:08 +0200 Subject: [PATCH 11/12] Grouped tests inside the same correct sub-group --- .../integrationtest/compile_1/compile_test.go | 91 ++++++++++--------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/internal/integrationtest/compile_1/compile_test.go b/internal/integrationtest/compile_1/compile_test.go index 0be102cbc0c..c90d2727e45 100644 --- a/internal/integrationtest/compile_1/compile_test.go +++ b/internal/integrationtest/compile_1/compile_test.go @@ -59,7 +59,6 @@ func TestCompile(t *testing.T) { {"WithMultipleBuildPropertyFlags", compileWithMultipleBuildPropertyFlags}, {"WithOutputDirFlag", compileWithOutputDirFlag}, {"WithExportBinariesFlag", compileWithExportBinariesFlag}, - {"WithCustomBuildPath", compileWithCustomBuildPath}, {"WithExportBinariesEnvVar", compileWithExportBinariesEnvVar}, {"WithExportBinariesConfig", compileWithExportBinariesConfig}, {"WithInvalidUrl", compileWithInvalidUrl}, @@ -73,7 +72,7 @@ func TestCompile(t *testing.T) { {"WithRelativeBuildPath", compileWithRelativeBuildPath}, {"WithFakeSecureBootCore", compileWithFakeSecureBootCore}, {"PreprocessFlagDoNotMessUpWithOutput", preprocessFlagDoNotMessUpWithOutput}, - {"BuildWithBuildPathInSketchDir", buildWithBuildPathInSketchDir}, + {"WithCustomBuildPath", buildWithCustomBuildPath}, }.Run(t, env, cli) } @@ -449,41 +448,6 @@ func compileWithExportBinariesFlag(t *testing.T, env *integrationtest.Environmen 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", "sketches", 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) @@ -1206,17 +1170,54 @@ void loop() { require.Equal(t, expected, string(output)) } -func buildWithBuildPathInSketchDir(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) { +func buildWithCustomBuildPath(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) { sketchName := "bare_minimum" sketchPath := cli.CopySketch(sketchName) defer sketchPath.RemoveAll() - buildPath := sketchPath.Join("build") - // Run build - _, _, err := cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", buildPath.String(), sketchPath.String()) - require.NoError(t, err) + t.Run("OutsideSketch", func(t *testing.T) { + sketchName := "CompileWithBuildPath" + sketchPath := cli.SketchbookDir().Join(sketchName) + defer sketchPath.RemoveAll() + fqbn := "arduino:avr:uno" - // Run build twice, to verify the build still works when the build directory is present at the start - _, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", buildPath.String(), sketchPath.String()) - require.NoError(t, err) + // 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", "sketches", 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()) + }) + + t.Run("InsideSketch", func(t *testing.T) { + buildPath := sketchPath.Join("build") + + // Run build + _, _, err := cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", buildPath.String(), sketchPath.String()) + require.NoError(t, err) + // Run build twice, to verify the build still works when the build directory is present at the start + _, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", buildPath.String(), sketchPath.String()) + require.NoError(t, err) + }) } From 3d8d37d727bbfade870c9fd800687cf034c57743 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 Sep 2023 19:57:03 +0200 Subject: [PATCH 12/12] Ported TestFailIfBuildPathEqualsSketchPath and TestFailIfBuildPathEqualsSketchPathSketchPathDiffers to current integration test infra --- .../integrationtest/compile_1/compile_test.go | 6 +++ ...ail_if_buildpath_equals_sketchpath_test.go | 46 ------------------- 2 files changed, 6 insertions(+), 46 deletions(-) delete mode 100644 legacy/builder/test/fail_if_buildpath_equals_sketchpath_test.go diff --git a/internal/integrationtest/compile_1/compile_test.go b/internal/integrationtest/compile_1/compile_test.go index c90d2727e45..2815afaabe6 100644 --- a/internal/integrationtest/compile_1/compile_test.go +++ b/internal/integrationtest/compile_1/compile_test.go @@ -1220,4 +1220,10 @@ func buildWithCustomBuildPath(t *testing.T, env *integrationtest.Environment, cl _, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", buildPath.String(), sketchPath.String()) require.NoError(t, err) }) + + t.Run("SameAsSektch", func(t *testing.T) { + // Run build + _, _, err := cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", sketchPath.String(), sketchPath.String()) + require.Error(t, err) + }) } diff --git a/legacy/builder/test/fail_if_buildpath_equals_sketchpath_test.go b/legacy/builder/test/fail_if_buildpath_equals_sketchpath_test.go deleted file mode 100644 index 6cdeb21e6ae..00000000000 --- a/legacy/builder/test/fail_if_buildpath_equals_sketchpath_test.go +++ /dev/null @@ -1,46 +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 ( - "testing" - - "github.com/arduino/arduino-cli/arduino/sketch" - "github.com/arduino/arduino-cli/legacy/builder" - "github.com/arduino/arduino-cli/legacy/builder/types" - "github.com/arduino/go-paths-helper" - "github.com/stretchr/testify/require" -) - -func TestFailIfBuildPathEqualsSketchPath(t *testing.T) { - ctx := &types.Context{ - Sketch: &sketch.Sketch{FullPath: paths.New("buildPath")}, - BuildPath: paths.New("buildPath"), - } - - command := builder.FailIfBuildPathEqualsSketchPath{} - require.Error(t, command.Run(ctx)) -} - -func TestFailIfBuildPathEqualsSketchPathSketchPathDiffers(t *testing.T) { - ctx := &types.Context{ - Sketch: &sketch.Sketch{FullPath: paths.New("sketchPath")}, - BuildPath: paths.New("buildPath"), - } - - command := builder.FailIfBuildPathEqualsSketchPath{} - require.NoError(t, command.Run(ctx)) -}