From fe86e78eb1a4c536d3ae5ee7097b10f6f7ca48be Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Thu, 14 Sep 2023 10:37:56 +0200 Subject: [PATCH] ported CheckBuildOptionsFile from legacy into integration test --- .../integrationtest/compile_4/compile_test.go | 59 ++++++++ .../test/store_build_options_map_test.go | 131 ------------------ 2 files changed, 59 insertions(+), 131 deletions(-) delete mode 100644 legacy/builder/test/store_build_options_map_test.go diff --git a/internal/integrationtest/compile_4/compile_test.go b/internal/integrationtest/compile_4/compile_test.go index a645c87fafe..b84cd54ff1c 100644 --- a/internal/integrationtest/compile_4/compile_test.go +++ b/internal/integrationtest/compile_4/compile_test.go @@ -28,6 +28,7 @@ import ( "github.com/arduino/arduino-cli/internal/integrationtest" "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" + "go.bug.st/testifyjson/requirejson" "golang.org/x/exp/slices" ) @@ -954,3 +955,61 @@ func TestMergeSketchWithBootloader(t *testing.T) { require.Contains(t, mergedSketchHex, ":100000000C9434000C9446000C9446000C9446006A\n") require.True(t, strings.HasSuffix(mergedSketchHex, ":00000001FF\n")) } + +func TestBuildOptionsFile(t *testing.T) { + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) + defer env.CleanUp() + + sketchPath := cli.CopySketch("sketch_simple") + defer sketchPath.RemoveAll() + + _, _, err := cli.Run("core", "install", "arduino:avr@1.8.6") + require.NoError(t, err) + + buildPath, err := paths.MkTempDir("", "arduino-integration-test") + require.NoError(t, err) + defer buildPath.RemoveAll() + + // Build first time + _, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", buildPath.String(), sketchPath.String()) + require.NoError(t, err) + + exist, err := buildPath.Join("build.options.json").ExistCheck() + require.NoError(t, err) + require.True(t, exist) + + buildOptionsBytes, err := buildPath.Join("build.options.json").ReadFile() + require.NoError(t, err) + + requirejson.Query(t, buildOptionsBytes, "keys", `[ + "additionalFiles", + "builtInToolsFolders", + "compiler.optimization_flags", + "customBuildProperties", + "fqbn", + "hardwareFolders", + "otherLibrariesFolders", + "sketchLocation" + ]`) + requirejson.Query(t, buildOptionsBytes, ".fqbn", `"arduino:avr:uno"`) + requirejson.Query(t, buildOptionsBytes, ".customBuildProperties", `"build.warn_data_percentage=75"`) + + // Recompiling a second time should provide the same result + _, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", buildPath.String(), sketchPath.String()) + require.NoError(t, err) + + buildOptionsBytes, err = buildPath.Join("build.options.json").ReadFile() + require.NoError(t, err) + requirejson.Query(t, buildOptionsBytes, ".customBuildProperties", `"build.warn_data_percentage=75"`) + + // Recompiling with a new build option must produce a new `build.options.json` + _, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", buildPath.String(), + "--build-property", "custom=prop", + sketchPath.String(), + ) + require.NoError(t, err) + + buildOptionsBytes, err = buildPath.Join("build.options.json").ReadFile() + require.NoError(t, err) + requirejson.Query(t, buildOptionsBytes, ".customBuildProperties", `"custom=prop,build.warn_data_percentage=75"`) +} diff --git a/legacy/builder/test/store_build_options_map_test.go b/legacy/builder/test/store_build_options_map_test.go deleted file mode 100644 index ad087a0c48b..00000000000 --- a/legacy/builder/test/store_build_options_map_test.go +++ /dev/null @@ -1,131 +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/builder" - "github.com/arduino/arduino-cli/arduino/sketch" - paths "github.com/arduino/go-paths-helper" - "github.com/arduino/go-properties-orderedmap" - "github.com/stretchr/testify/require" -) - -func TestCheckIfBuildOptionsChanged(t *testing.T) { - hardwareDirs := paths.NewPathList("hardware") - builtInToolsDirs := paths.NewPathList("tools") - builtInLibrariesDirs := paths.New("built-in libraries") - otherLibrariesDirs := paths.NewPathList("libraries") - fqbn := parseFQBN(t, "my:nice:fqbn") - - buildPath := SetupBuildPath(t) - defer buildPath.RemoveAll() - - buildProperties := properties.NewFromHashmap(map[string]string{"compiler.optimization_flags": "-Os"}) - buildOptionsManager := builder.NewBuildOptionsManager( - hardwareDirs, builtInToolsDirs, otherLibrariesDirs, - builtInLibrariesDirs, buildPath, &sketch.Sketch{FullPath: paths.New("sketchLocation")}, []string{"custom=prop"}, - fqbn, false, - buildProperties.Get("compiler.optimization_flags"), - buildProperties.GetPath("runtime.platform.path"), - buildProperties.GetPath("build.core.path"), - nil, - ) - - err := buildOptionsManager.WipeBuildPath() - require.NoError(t, err) - - exist, err := buildPath.Join("build.options.json").ExistCheck() - require.NoError(t, err) - require.True(t, exist) - - bytes, err := buildPath.Join("build.options.json").ReadFile() - require.NoError(t, err) - - require.Equal(t, `{ - "additionalFiles": "", - "builtInLibrariesFolders": "built-in libraries", - "builtInToolsFolders": "tools", - "compiler.optimization_flags": "-Os", - "customBuildProperties": "custom=prop", - "fqbn": "my:nice:fqbn", - "hardwareFolders": "hardware", - "otherLibrariesFolders": "libraries", - "sketchLocation": "sketchLocation" -}`, string(bytes)) -} - -//func TestWipeoutBuildPathIfBuildOptionsChanged(t *testing.T) { -// buildPath := SetupBuildPath(t) -// defer buildPath.RemoveAll() -// -// buildOptionsJsonPrevious := "{ \"old\":\"old\" }" -// buildOptionsJson := "{ \"new\":\"new\" }" -// -// buildPath.Join("should_be_deleted.txt").Truncate() -// -// _, err := builder.WipeoutBuildPathIfBuildOptionsChanged( -// false, -// buildPath, -// buildOptionsJson, -// buildOptionsJsonPrevious, -// nil, -// ) -// require.NoError(t, err) -// -// exist, err := buildPath.ExistCheck() -// require.NoError(t, err) -// require.True(t, exist) -// -// files, err := buildPath.ReadDir() -// require.NoError(t, err) -// require.Equal(t, 0, len(files)) -// -// exist, err = buildPath.Join("should_be_deleted.txt").ExistCheck() -// require.NoError(t, err) -// require.False(t, exist) -//} -// -//func TestWipeoutBuildPathIfBuildOptionsChangedNoPreviousBuildOptions(t *testing.T) { -// buildPath := SetupBuildPath(t) -// defer buildPath.RemoveAll() -// -// buildOptionsJson := "{ \"new\":\"new\" }" -// -// require.NoError(t, buildPath.Join("should_not_be_deleted.txt").Truncate()) -// -// _, err := builder.WipeoutBuildPathIfBuildOptionsChanged( -// false, -// buildPath, -// buildOptionsJson, -// "", -// nil, -// ) -// require.NoError(t, err) -// -// exist, err := buildPath.ExistCheck() -// require.NoError(t, err) -// require.True(t, exist) -// -// files, err := buildPath.ReadDir() -// require.NoError(t, err) -// require.Equal(t, 1, len(files)) -// -// exist, err = buildPath.Join("should_not_be_deleted.txt").ExistCheck() -// require.NoError(t, err) -// require.True(t, exist) -//}