diff --git a/arduino/sketch/sketch.go b/arduino/sketch/sketch.go index 337aa904d57..5de5ffda9be 100644 --- a/arduino/sketch/sketch.go +++ b/arduino/sketch/sketch.go @@ -32,8 +32,7 @@ import ( type Sketch struct { Name string MainFile *paths.Path - FullPath *paths.Path // FullPath is the path to the Sketch folder - BuildPath *paths.Path + FullPath *paths.Path // FullPath is the path to the Sketch folder OtherSketchFiles paths.PathList // Sketch files that end in .ino other than main file AdditionalFiles paths.PathList RootFolderFiles paths.PathList // All files that are in the Sketch root @@ -81,7 +80,6 @@ func New(path *paths.Path) (*Sketch, error) { Name: path.Base(), MainFile: mainFile, FullPath: path, - BuildPath: GenBuildPath(path), OtherSketchFiles: paths.PathList{}, AdditionalFiles: paths.PathList{}, RootFolderFiles: paths.PathList{}, @@ -293,14 +291,15 @@ func CheckForPdeFiles(sketch *paths.Path) []*paths.Path { return files } -// GenBuildPath generates a suitable name for the build folder. -// The sketchPath, if not nil, is also used to furhter differentiate build paths. -func GenBuildPath(sketchPath *paths.Path) *paths.Path { - path := "" - if sketchPath != nil { - path = sketchPath.String() - } +// DefaultBuildPath generates the default build directory for a given sketch. +// The build path is in a temporary directory and is unique for each sketch. +func (s *Sketch) DefaultBuildPath() *paths.Path { + return paths.TempDir().Join("arduino", "sketch-"+s.Hash()) +} + +// Hash generate a unique hash for the given sketch. +func (s *Sketch) Hash() string { + path := s.FullPath.String() md5SumBytes := md5.Sum([]byte(path)) - md5Sum := strings.ToUpper(hex.EncodeToString(md5SumBytes[:])) - return paths.TempDir().Join("arduino", "sketch-"+md5Sum) + return strings.ToUpper(hex.EncodeToString(md5SumBytes[:])) } diff --git a/arduino/sketch/sketch_test.go b/arduino/sketch/sketch_test.go index cb7b624ab12..dabace257a9 100644 --- a/arduino/sketch/sketch_test.go +++ b/arduino/sketch/sketch_test.go @@ -287,10 +287,8 @@ func TestNewSketchFolderSymlink(t *testing.T) { func TestGenBuildPath(t *testing.T) { want := paths.TempDir().Join("arduino", "sketch-ACBD18DB4CC2F85CEDEF654FCCC4A4D8") - assert.True(t, GenBuildPath(paths.New("foo")).EquivalentTo(want)) - - want = paths.TempDir().Join("arduino", "sketch-D41D8CD98F00B204E9800998ECF8427E") - assert.True(t, GenBuildPath(nil).EquivalentTo(want)) + assert.True(t, (&Sketch{FullPath: paths.New("foo")}).DefaultBuildPath().EquivalentTo(want)) + assert.Equal(t, "ACBD18DB4CC2F85CEDEF654FCCC4A4D8", (&Sketch{FullPath: paths.New("foo")}).Hash()) } func TestCheckForPdeFiles(t *testing.T) { diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 2a881f23829..092b69c4238 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -116,7 +116,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream } builderCtx.UseCachedLibrariesResolution = req.GetSkipLibrariesDiscovery() builderCtx.FQBN = fqbn - builderCtx.SketchLocation = sk.FullPath + builderCtx.Sketch = sk builderCtx.ProgressCB = progressCB // FIXME: This will be redundant when arduino-builder will be part of the cli @@ -128,7 +128,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.OtherLibrariesDirs.Add(configuration.LibrariesDir(configuration.Settings)) builderCtx.LibraryDirs = paths.NewPathList(req.Library...) if req.GetBuildPath() == "" { - builderCtx.BuildPath = sk.BuildPath + builderCtx.BuildPath = sk.DefaultBuildPath() } else { builderCtx.BuildPath = paths.New(req.GetBuildPath()).Canonical() } @@ -144,8 +144,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream // Optimize for debug builderCtx.OptimizeForDebug = req.GetOptimizeForDebug() - builderCtx.CoreBuildCachePath = paths.TempDir().Join("arduino", "core-cache") - builderCtx.Jobs = int(req.GetJobs()) builderCtx.USBVidPid = req.GetVidPid() @@ -154,12 +152,17 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.CustomBuildProperties = append(req.GetBuildProperties(), "build.warn_data_percentage=75") builderCtx.CustomBuildProperties = append(req.GetBuildProperties(), securityKeysOverride...) - if req.GetBuildCachePath() != "" { - builderCtx.BuildCachePath = paths.New(req.GetBuildCachePath()) - err = builderCtx.BuildCachePath.MkdirAll() + if req.GetBuildCachePath() == "" { + builderCtx.CoreBuildCachePath = paths.TempDir().Join("arduino", "core-cache") + } else { + buildCachePath, err := paths.New(req.GetBuildCachePath()).Abs() if err != nil { return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build cache directory"), Cause: err} } + if err := buildCachePath.MkdirAll(); err != nil { + return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build cache directory"), Cause: err} + } + builderCtx.CoreBuildCachePath = buildCachePath.Join("core") } builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings) diff --git a/commands/debug/debug_info.go b/commands/debug/debug_info.go index 4e3d931636f..9b5a1ff920a 100644 --- a/commands/debug/debug_info.go +++ b/commands/debug/debug_info.go @@ -115,9 +115,11 @@ func getDebugProperties(req *debug.DebugConfigRequest, pme *packagemanager.Explo } } - importPath := sk.BuildPath + var importPath *paths.Path if importDir := req.GetImportDir(); importDir != "" { importPath = paths.New(importDir) + } else { + importPath = sk.DefaultBuildPath() } if !importPath.Exist() { return nil, &arduino.NotFoundError{Message: tr("Compiled sketch not found in %s", importPath)} diff --git a/commands/upload/upload.go b/commands/upload/upload.go index cfa0d72d4d7..c3b9e86e0a0 100644 --- a/commands/upload/upload.go +++ b/commands/upload/upload.go @@ -575,7 +575,7 @@ func determineBuildPathAndSketchName(importFile, importDir string, sk *sketch.Sk // Case 4: only sketch specified. In this case we use the generated build path // and the given sketch name. - return sk.BuildPath, sk.Name + sk.MainFile.Ext(), nil + return sk.DefaultBuildPath(), sk.Name + sk.MainFile.Ext(), nil } func detectSketchNameFromBuildPath(buildPath *paths.Path) (string, error) { diff --git a/commands/upload/upload_test.go b/commands/upload/upload_test.go index 5ddd4898373..d3f69fd1baf 100644 --- a/commands/upload/upload_test.go +++ b/commands/upload/upload_test.go @@ -79,7 +79,7 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) { // 03: error: used both importPath and importFile {"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, nil, "", ""}, // 04: only sketch without FQBN - {"", "", blonk, nil, sketch.GenBuildPath(blonk.FullPath).String(), "Blonk.ino"}, + {"", "", blonk, nil, blonk.DefaultBuildPath().String(), "Blonk.ino"}, // 05: use importFile to detect build.path and project_name, sketch is ignored. {"testdata/build_path_2/Blink.ino.hex", "", blonk, nil, "testdata/build_path_2", "Blink.ino"}, // 06: use importPath as build.path and Blink as project name, ignore the sketch Blonk @@ -95,7 +95,7 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) { // 11: error: used both importPath and importFile {"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, fqbn, "", ""}, // 12: use sketch to determine project name and sketch+fqbn to determine build path - {"", "", blonk, fqbn, sketch.GenBuildPath(blonk.FullPath).String(), "Blonk.ino"}, + {"", "", blonk, fqbn, blonk.DefaultBuildPath().String(), "Blonk.ino"}, // 13: use importFile to detect build.path and project_name, sketch+fqbn is ignored. {"testdata/build_path_2/Blink.ino.hex", "", blonk, fqbn, "testdata/build_path_2", "Blink.ino"}, // 14: use importPath as build.path and Blink as project name, ignore the sketch Blonk, ignore fqbn diff --git a/legacy/builder/add_additional_entries_to_context.go b/legacy/builder/add_additional_entries_to_context.go index 775b6cdc63e..984b686a04c 100644 --- a/legacy/builder/add_additional_entries_to_context.go +++ b/legacy/builder/add_additional_entries_to_context.go @@ -50,15 +50,6 @@ func (*AddAdditionalEntriesToContext) Run(ctx *types.Context) error { ctx.CoreBuildPath = coreBuildPath } - if ctx.BuildCachePath != nil { - coreBuildCachePath, err := ctx.BuildCachePath.Join(constants.FOLDER_CORE).Abs() - if err != nil { - return errors.WithStack(err) - } - - ctx.CoreBuildCachePath = coreBuildCachePath - } - if ctx.WarningsLevel == "" { ctx.WarningsLevel = DEFAULT_WARNINGS_LEVEL } diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 00af87a6931..68ed660639e 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -19,7 +19,6 @@ import ( "reflect" "time" - "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/i18n" "github.com/arduino/arduino-cli/legacy/builder/phases" "github.com/arduino/arduino-cli/legacy/builder/types" @@ -129,10 +128,6 @@ func (s *PreprocessSketch) Run(ctx *types.Context) error { type Preprocess struct{} func (s *Preprocess) Run(ctx *types.Context) error { - if ctx.BuildPath == nil { - ctx.BuildPath = sketch.GenBuildPath(ctx.SketchLocation) - } - if err := ctx.BuildPath.MkdirAll(); err != nil { return err } @@ -188,9 +183,6 @@ func RunBuilder(ctx *types.Context) error { } func RunParseHardware(ctx *types.Context) error { - if ctx.BuildPath == nil { - ctx.BuildPath = sketch.GenBuildPath(ctx.SketchLocation) - } commands := []types.Command{ &ContainerSetupHardwareToolsLibsSketchAndProps{}, } diff --git a/legacy/builder/container_setup.go b/legacy/builder/container_setup.go index bc6398117a6..c48e3a1d0bf 100644 --- a/legacy/builder/container_setup.go +++ b/legacy/builder/container_setup.go @@ -16,7 +16,6 @@ package builder import ( - sk "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/legacy/builder/types" "github.com/pkg/errors" ) @@ -48,22 +47,6 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context) ctx.PushProgress() } - if ctx.SketchLocation != nil { - // get abs path to sketch - sketchLocation, err := ctx.SketchLocation.Abs() - if err != nil { - return errors.WithStack(err) - } - - // load sketch - sketch, err := sk.New(sketchLocation) - if err != nil { - return errors.WithStack(err) - } - sketch.BuildPath = ctx.BuildPath - ctx.SketchLocation = sketch.MainFile - ctx.Sketch = sketch - } ctx.Progress.CompleteStep() ctx.PushProgress() diff --git a/legacy/builder/fail_if_buildpath_equals_sketchpath.go b/legacy/builder/fail_if_buildpath_equals_sketchpath.go index 0cfe7073a77..2ec5c0da662 100644 --- a/legacy/builder/fail_if_buildpath_equals_sketchpath.go +++ b/legacy/builder/fail_if_buildpath_equals_sketchpath.go @@ -23,21 +23,8 @@ import ( type FailIfBuildPathEqualsSketchPath struct{} func (s *FailIfBuildPathEqualsSketchPath) Run(ctx *types.Context) error { - if ctx.BuildPath == nil || ctx.SketchLocation == nil { - return nil - } - - buildPath, err := ctx.BuildPath.Abs() - if err != nil { - return errors.WithStack(err) - } - - sketchPath, err := ctx.SketchLocation.Abs() - if err != nil { - return errors.WithStack(err) - } - sketchPath = sketchPath.Parent() - + buildPath := ctx.BuildPath.Canonical() + sketchPath := ctx.Sketch.FullPath.Canonical() if buildPath.EqualsTo(sketchPath) { return errors.New(tr("Sketch cannot be located in build path. Please specify a different build path")) } diff --git a/legacy/builder/setup_build_properties.go b/legacy/builder/setup_build_properties.go index 8b8e2cd799c..917da83faf7 100644 --- a/legacy/builder/setup_build_properties.go +++ b/legacy/builder/setup_build_properties.go @@ -109,14 +109,7 @@ func (s *SetupBuildProperties) Run(ctx *types.Context) error { buildProperties.Set("software", DEFAULT_SOFTWARE) } - if ctx.SketchLocation != nil { - sourcePath, err := ctx.SketchLocation.Abs() - if err != nil { - return err - } - sourcePath = sourcePath.Parent() - buildProperties.SetPath("build.source.path", sourcePath) - } + buildProperties.SetPath("build.source.path", ctx.Sketch.FullPath) now := time.Now() buildProperties.Set("extra.time.utc", strconv.FormatInt(now.Unix(), 10)) diff --git a/legacy/builder/sketch_loader.go b/legacy/builder/sketch_loader.go deleted file mode 100644 index 42a5ce1a0e4..00000000000 --- a/legacy/builder/sketch_loader.go +++ /dev/null @@ -1,55 +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 builder - -import ( - sk "github.com/arduino/arduino-cli/arduino/sketch" - "github.com/arduino/arduino-cli/legacy/builder/types" - "github.com/pkg/errors" -) - -type SketchLoader struct{} - -func (s *SketchLoader) Run(ctx *types.Context) error { - if ctx.SketchLocation == nil { - return nil - } - - sketchLocation := ctx.SketchLocation - - sketchLocation, err := sketchLocation.Abs() - if err != nil { - return errors.WithStack(err) - } - mainSketchStat, err := sketchLocation.Stat() - if err != nil { - return errors.WithStack(err) - } - if mainSketchStat.IsDir() { - sketchLocation = sketchLocation.Join(mainSketchStat.Name() + ".ino") - } - - ctx.SketchLocation = sketchLocation - - sketch, err := sk.New(sketchLocation) - if err != nil { - return errors.WithStack(err) - } - ctx.SketchLocation = sketchLocation - ctx.Sketch = sketch - - return nil -} diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 21107425fd5..c7b91a18473 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -21,19 +21,21 @@ import ( "testing" "time" - "github.com/arduino/go-paths-helper" - "github.com/sirupsen/logrus" - + "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/sirupsen/logrus" "github.com/stretchr/testify/require" ) func prepareBuilderTestContext(t *testing.T, sketchPath *paths.Path, fqbn string) *types.Context { + sk, err := sketch.New(sketchPath) + require.NoError(t, err) return &types.Context{ - SketchLocation: sketchPath, + Sketch: sk, FQBN: parseFQBN(t, fqbn), HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), @@ -261,7 +263,7 @@ func TestBuilderBridgeRedBearLab(t *testing.T) { func TestBuilderSketchNoFunctions(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - ctx := prepareBuilderTestContext(t, paths.New("sketch_no_functions", "main.ino"), "RedBearLab:avr:blend") + ctx := prepareBuilderTestContext(t, paths.New("sketch_no_functions", "sketch_no_functions.ino"), "RedBearLab:avr:blend") ctx.HardwareDirs = append(ctx.HardwareDirs, paths.New("downloaded_board_manager_stuff")) ctx.BuiltInToolsDirs = append(ctx.BuiltInToolsDirs, paths.New("downloaded_board_manager_stuff")) @@ -371,7 +373,7 @@ func TestBuilderCacheCoreAFile(t *testing.T) { SetupBuildPath(t, ctx) defer ctx.BuildPath.RemoveAll() SetupBuildCachePath(t, ctx) - defer ctx.BuildCachePath.RemoveAll() + defer ctx.CoreBuildCachePath.RemoveAll() // Run build bldr := builder.Builder{} diff --git a/legacy/builder/test/builder_utils_test.go b/legacy/builder/test/builder_utils_test.go index d3c8e5a360e..b20e7e2555a 100644 --- a/legacy/builder/test/builder_utils_test.go +++ b/legacy/builder/test/builder_utils_test.go @@ -16,7 +16,7 @@ package test import ( - "io/ioutil" + "os" "testing" "time" @@ -32,7 +32,7 @@ func sleep(t *testing.T) { } func tempFile(t *testing.T, prefix string) *paths.Path { - file, err := ioutil.TempFile("", prefix) + file, err := os.CreateTemp("", prefix) file.Close() NoError(t, err) return paths.New(file.Name()) diff --git a/legacy/builder/test/create_build_options_map_test.go b/legacy/builder/test/create_build_options_map_test.go index 966d86a7205..e3ba6ca71d6 100644 --- a/legacy/builder/test/create_build_options_map_test.go +++ b/legacy/builder/test/create_build_options_map_test.go @@ -18,6 +18,7 @@ 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" @@ -29,7 +30,7 @@ func TestCreateBuildOptionsMap(t *testing.T) { HardwareDirs: paths.NewPathList("hardware", "hardware2"), BuiltInToolsDirs: paths.NewPathList("tools"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("sketchLocation"), + Sketch: &sketch.Sketch{FullPath: paths.New("sketchLocation")}, FQBN: parseFQBN(t, "my:nice:fqbn"), ArduinoAPIVersion: "ideVersion", Verbose: true, diff --git a/legacy/builder/test/ctags_runner_test.go b/legacy/builder/test/ctags_runner_test.go index 7aa70a58478..77d52f5c916 100644 --- a/legacy/builder/test/ctags_runner_test.go +++ b/legacy/builder/test/ctags_runner_test.go @@ -36,7 +36,7 @@ func TestCTagsRunner(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -86,7 +86,7 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -134,7 +134,7 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -181,7 +181,7 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -227,7 +227,7 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, 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 76617e58fcc..d24b9d65ee1 100644 --- a/legacy/builder/test/fail_if_buildpath_equals_sketchpath_test.go +++ b/legacy/builder/test/fail_if_buildpath_equals_sketchpath_test.go @@ -18,6 +18,7 @@ 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" @@ -26,8 +27,8 @@ import ( func TestFailIfBuildPathEqualsSketchPath(t *testing.T) { ctx := &types.Context{ - SketchLocation: paths.New("buildPath/sketch.ino"), - BuildPath: paths.New("buildPath"), + Sketch: &sketch.Sketch{FullPath: paths.New("buildPath")}, + BuildPath: paths.New("buildPath"), } command := builder.FailIfBuildPathEqualsSketchPath{} @@ -36,8 +37,8 @@ func TestFailIfBuildPathEqualsSketchPath(t *testing.T) { func TestFailIfBuildPathEqualsSketchPathSketchPathDiffers(t *testing.T) { ctx := &types.Context{ - SketchLocation: paths.New("sketchPath/sketch.ino"), - BuildPath: paths.New("buildPath"), + Sketch: &sketch.Sketch{FullPath: paths.New("sketchPath")}, + BuildPath: paths.New("buildPath"), } command := builder.FailIfBuildPathEqualsSketchPath{} diff --git a/legacy/builder/test/helper.go b/legacy/builder/test/helper.go index 16cbe3d044b..38144840f1f 100644 --- a/legacy/builder/test/helper.go +++ b/legacy/builder/test/helper.go @@ -24,11 +24,13 @@ import ( "text/template" "github.com/arduino/arduino-cli/arduino/libraries" + "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/legacy/builder/constants" "github.com/arduino/arduino-cli/legacy/builder/types" "github.com/arduino/arduino-cli/legacy/builder/utils" paths "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func LoadAndInterpolate(t *testing.T, filename string, ctx *types.Context) string { @@ -71,10 +73,16 @@ func SetupBuildPath(t *testing.T, ctx *types.Context) *paths.Path { func SetupBuildCachePath(t *testing.T, ctx *types.Context) *paths.Path { buildCachePath, err := paths.MkTempDir(constants.EMPTY_STRING, "test_build_cache") NoError(t, err) - ctx.BuildCachePath = buildCachePath + ctx.CoreBuildCachePath = buildCachePath return buildCachePath } +func OpenSketch(t *testing.T, sketchPath *paths.Path) *sketch.Sketch { + sketch, err := sketch.New(sketchPath) + require.NoError(t, err) + return sketch +} + type ByLibraryName []*libraries.Library func (s ByLibraryName) Len() int { diff --git a/legacy/builder/test/helper_tools_downloader.go b/legacy/builder/test/helper_tools_downloader.go index a4be3843655..6e79e4d3aac 100644 --- a/legacy/builder/test/helper_tools_downloader.go +++ b/legacy/builder/test/helper_tools_downloader.go @@ -19,7 +19,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "os" "os/exec" @@ -216,7 +215,7 @@ func downloadIndex(url string) (map[string]interface{}, error) { return nil, err } - bytes, err := ioutil.ReadAll(res.Body) + bytes, err := io.ReadAll(res.Body) if err != nil { return nil, err } @@ -632,7 +631,7 @@ func downloadAndUnpack(url string) (*paths.Path, []os.FileInfo, error) { return nil, nil, errors.WithStack(err) } - bytes, err := ioutil.ReadAll(res.Body) + bytes, err := io.ReadAll(res.Body) if err != nil { return nil, nil, errors.WithStack(err) } diff --git a/legacy/builder/test/includes_to_include_folders_test.go b/legacy/builder/test/includes_to_include_folders_test.go index 7049a06ef03..0e8a513b740 100644 --- a/legacy/builder/test/includes_to_include_folders_test.go +++ b/legacy/builder/test/includes_to_include_folders_test.go @@ -34,7 +34,7 @@ func TestIncludesToIncludeFolders(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), + Sketch: OpenSketch(t, paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino")), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -70,7 +70,7 @@ func TestIncludesToIncludeFoldersSketchWithIfDef(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("SketchWithIfDef", "SketchWithIfDef.ino"), + Sketch: OpenSketch(t, paths.New("SketchWithIfDef", "SketchWithIfDef.ino")), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -105,7 +105,7 @@ func TestIncludesToIncludeFoldersIRremoteLibrary(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("sketch9", "sketch9.ino"), + Sketch: OpenSketch(t, paths.New("sketch9", "sketch9.ino")), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -143,7 +143,7 @@ func TestIncludesToIncludeFoldersANewLibrary(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("sketch10", "sketch10.ino"), + Sketch: OpenSketch(t, paths.New("sketch10", "sketch10.ino")), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -177,7 +177,7 @@ func TestIncludesToIncludeFoldersDuplicateLibs(t *testing.T) { HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware", "user_hardware"), BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), - SketchLocation: paths.New("user_hardware", "my_avr_platform", "avr", "libraries", "SPI", "examples", "BarometricPressureSensor", "BarometricPressureSensor.ino"), + Sketch: OpenSketch(t, paths.New("user_hardware", "my_avr_platform", "avr", "libraries", "SPI", "examples", "BarometricPressureSensor", "BarometricPressureSensor.ino")), FQBN: parseFQBN(t, "my_avr_platform:avr:custom_yun"), ArduinoAPIVersion: "10600", Verbose: true, @@ -215,7 +215,7 @@ func TestIncludesToIncludeFoldersDuplicateLibsWithConflictingLibsOutsideOfPlatfo BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("user_hardware", "my_avr_platform", "avr", "libraries", "SPI", "examples", "BarometricPressureSensor", "BarometricPressureSensor.ino"), + Sketch: OpenSketch(t, paths.New("user_hardware", "my_avr_platform", "avr", "libraries", "SPI", "examples", "BarometricPressureSensor", "BarometricPressureSensor.ino")), FQBN: parseFQBN(t, "my_avr_platform:avr:custom_yun"), ArduinoAPIVersion: "10600", Verbose: true, @@ -253,7 +253,7 @@ func TestIncludesToIncludeFoldersDuplicateLibs2(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("sketch_usbhost", "sketch_usbhost.ino"), + Sketch: OpenSketch(t, paths.New("sketch_usbhost", "sketch_usbhost.ino")), FQBN: parseFQBN(t, "arduino:samd:arduino_zero_native"), ArduinoAPIVersion: "10600", Verbose: true, @@ -291,7 +291,7 @@ func TestIncludesToIncludeFoldersSubfolders(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("sketch_with_subfolders", "sketch_with_subfolders.ino"), + Sketch: OpenSketch(t, paths.New("sketch_with_subfolders", "sketch_with_subfolders.ino")), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, diff --git a/legacy/builder/test/load_vid_pid_specific_properties_test.go b/legacy/builder/test/load_vid_pid_specific_properties_test.go index a9bb9b89f3a..b80f16b3d44 100644 --- a/legacy/builder/test/load_vid_pid_specific_properties_test.go +++ b/legacy/builder/test/load_vid_pid_specific_properties_test.go @@ -31,7 +31,7 @@ func TestLoadVIDPIDSpecificPropertiesWhenNoVIDPIDAreProvided(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), BuiltInToolsDirs: paths.NewPathList("downloaded_tools", "./tools_builtin"), - SketchLocation: paths.New("sketch1", "sketch1.ino"), + Sketch: OpenSketch(t, paths.New("sketch1", "sketch1.ino")), FQBN: parseFQBN(t, "arduino:avr:micro"), ArduinoAPIVersion: "10600", } @@ -61,7 +61,7 @@ func TestLoadVIDPIDSpecificProperties(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), BuiltInToolsDirs: paths.NewPathList("downloaded_tools", "./tools_builtin"), - SketchLocation: paths.New("sketch1", "sketch1.ino"), + Sketch: OpenSketch(t, paths.New("sketch1", "sketch1.ino")), FQBN: parseFQBN(t, "arduino:avr:micro"), ArduinoAPIVersion: "10600", } diff --git a/legacy/builder/test/merge_sketch_with_bootloader_test.go b/legacy/builder/test/merge_sketch_with_bootloader_test.go index cea8964c31b..fcce8942ad0 100644 --- a/legacy/builder/test/merge_sketch_with_bootloader_test.go +++ b/legacy/builder/test/merge_sketch_with_bootloader_test.go @@ -36,7 +36,7 @@ func TestMergeSketchWithBootloader(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("sketch1", "sketch1.ino"), + Sketch: OpenSketch(t, paths.New("sketch1", "sketch1.ino")), FQBN: parseFQBN(t, "arduino:avr:uno"), ArduinoAPIVersion: "10600", } @@ -106,7 +106,7 @@ func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("sketch1", "sketch1.ino"), + Sketch: OpenSketch(t, paths.New("sketch1", "sketch1.ino")), FQBN: parseFQBN(t, "arduino:avr:uno"), ArduinoAPIVersion: "10600", } @@ -177,7 +177,7 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("sketch1", "sketch1.ino"), + Sketch: OpenSketch(t, paths.New("sketch1", "sketch1.ino")), FQBN: parseFQBN(t, "arduino:avr:uno"), ArduinoAPIVersion: "10600", } @@ -215,7 +215,7 @@ func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("sketch1", "sketch1.ino"), + Sketch: OpenSketch(t, paths.New("sketch1", "sketch1.ino")), FQBN: parseFQBN(t, "my_avr_platform:avr:mymega:cpu=atmega2560"), ArduinoAPIVersion: "10600", } diff --git a/legacy/builder/test/prototypes_adder_test.go b/legacy/builder/test/prototypes_adder_test.go index 9e94f4dd4b5..871536def19 100644 --- a/legacy/builder/test/prototypes_adder_test.go +++ b/legacy/builder/test/prototypes_adder_test.go @@ -39,7 +39,7 @@ func TestPrototypesAdderBridgeExample(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -79,7 +79,7 @@ func TestPrototypesAdderSketchWithIfDef(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("SketchWithIfDef", "SketchWithIfDef.ino"), + Sketch: OpenSketch(t, paths.New("SketchWithIfDef", "SketchWithIfDef.ino")), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -119,7 +119,7 @@ func TestPrototypesAdderBaladuino(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("Baladuino", "Baladuino.ino"), + Sketch: OpenSketch(t, paths.New("Baladuino", "Baladuino.ino")), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -159,7 +159,7 @@ func TestPrototypesAdderCharWithEscapedDoubleQuote(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("CharWithEscapedDoubleQuote", "CharWithEscapedDoubleQuote.ino"), + Sketch: OpenSketch(t, paths.New("CharWithEscapedDoubleQuote", "CharWithEscapedDoubleQuote.ino")), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -199,7 +199,7 @@ func TestPrototypesAdderIncludeBetweenMultilineComment(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("IncludeBetweenMultilineComment", "IncludeBetweenMultilineComment.ino"), + Sketch: OpenSketch(t, paths.New("IncludeBetweenMultilineComment", "IncludeBetweenMultilineComment.ino")), FQBN: parseFQBN(t, "arduino:sam:arduino_due_x_dbg"), ArduinoAPIVersion: "10600", Verbose: true, @@ -239,7 +239,7 @@ func TestPrototypesAdderLineContinuations(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("LineContinuations", "LineContinuations.ino"), + Sketch: OpenSketch(t, paths.New("LineContinuations", "LineContinuations.ino")), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -279,7 +279,7 @@ func TestPrototypesAdderStringWithComment(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("StringWithComment", "StringWithComment.ino"), + Sketch: OpenSketch(t, paths.New("StringWithComment", "StringWithComment.ino")), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -319,7 +319,7 @@ func TestPrototypesAdderSketchWithStruct(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("SketchWithStruct", "SketchWithStruct.ino"), + Sketch: OpenSketch(t, paths.New("SketchWithStruct", "SketchWithStruct.ino")), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -367,7 +367,7 @@ func TestPrototypesAdderSketchWithConfig(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -413,7 +413,7 @@ func TestPrototypesAdderSketchNoFunctionsTwoFiles(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("sketch_no_functions_two_files", "sketch_no_functions_two_files.ino"), + Sketch: OpenSketch(t, paths.New("sketch_no_functions_two_files", "sketch_no_functions_two_files.ino")), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -453,7 +453,7 @@ func TestPrototypesAdderSketchNoFunctions(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("sketch_no_functions", "sketch_no_functions.ino"), + Sketch: OpenSketch(t, paths.New("sketch_no_functions", "sketch_no_functions.ino")), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -499,7 +499,7 @@ func TestPrototypesAdderSketchWithDefaultArgs(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -542,7 +542,7 @@ func TestPrototypesAdderSketchWithInlineFunction(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -596,7 +596,7 @@ func TestPrototypesAdderSketchWithFunctionSignatureInsideIFDEF(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -639,7 +639,7 @@ func TestPrototypesAdderSketchWithUSBCON(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), OtherLibrariesDirs: paths.NewPathList("libraries"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -681,7 +681,7 @@ func TestPrototypesAdderSketchWithTypename(t *testing.T) { HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), BuiltInLibrariesDirs: paths.New("libraries", "downloaded_libraries"), BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:leonardo"), ArduinoAPIVersion: "10600", Verbose: true, @@ -730,7 +730,7 @@ func TestPrototypesAdderSketchWithIfDef2(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:yun"), ArduinoAPIVersion: "10600", Verbose: true, @@ -776,7 +776,7 @@ func TestPrototypesAdderSketchWithIfDef2SAM(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:sam:arduino_due_x_dbg"), ArduinoAPIVersion: "10600", Verbose: true, @@ -822,7 +822,7 @@ func TestPrototypesAdderSketchWithConst(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:uno"), ArduinoAPIVersion: "10600", Verbose: true, @@ -862,7 +862,7 @@ func TestPrototypesAdderSketchWithDosEol(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("eol_processing", "eol_processing.ino"), + Sketch: OpenSketch(t, paths.New("eol_processing", "eol_processing.ino")), FQBN: parseFQBN(t, "arduino:avr:uno"), ArduinoAPIVersion: "10600", Verbose: true, @@ -902,7 +902,7 @@ func TestPrototypesAdderSketchWithSubstringFunctionMember(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: sketchLocation, + Sketch: OpenSketch(t, sketchLocation), FQBN: parseFQBN(t, "arduino:avr:uno"), ArduinoAPIVersion: "10600", Verbose: true, diff --git a/legacy/builder/test/read_file_and_store_in_context_test.go b/legacy/builder/test/read_file_and_store_in_context_test.go index c746c056d64..c4e299e2c60 100644 --- a/legacy/builder/test/read_file_and_store_in_context_test.go +++ b/legacy/builder/test/read_file_and_store_in_context_test.go @@ -16,7 +16,7 @@ package test import ( - "io/ioutil" + "os" "testing" "github.com/arduino/arduino-cli/legacy/builder" @@ -26,7 +26,7 @@ import ( ) func TestReadFileAndStoreInContext(t *testing.T) { - filePath, err := ioutil.TempFile("", "test") + filePath, err := os.CreateTemp("", "test") NoError(t, err) file := paths.New(filePath.Name()) diff --git a/legacy/builder/test/setup_build_properties_test.go b/legacy/builder/test/setup_build_properties_test.go index 44d64eeba3a..815d78c0398 100644 --- a/legacy/builder/test/setup_build_properties_test.go +++ b/legacy/builder/test/setup_build_properties_test.go @@ -31,7 +31,7 @@ func TestSetupBuildProperties(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware", "user_hardware"), BuiltInToolsDirs: paths.NewPathList("downloaded_tools", "tools_builtin"), - SketchLocation: paths.New("sketch1", "sketch1.ino"), + Sketch: OpenSketch(t, paths.New("sketch1", "sketch1.ino")), FQBN: parseFQBN(t, "arduino:avr:uno"), ArduinoAPIVersion: "10600", } @@ -43,7 +43,6 @@ func TestSetupBuildProperties(t *testing.T) { &builder.AddAdditionalEntriesToContext{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, - &builder.SketchLoader{}, &builder.SetupBuildProperties{}, } @@ -96,7 +95,7 @@ func TestSetupBuildPropertiesWithSomeCustomOverrides(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), BuiltInToolsDirs: paths.NewPathList("downloaded_tools", "tools_builtin"), - SketchLocation: paths.New("sketch1", "sketch1.ino"), + Sketch: OpenSketch(t, paths.New("sketch1", "sketch1.ino")), FQBN: parseFQBN(t, "arduino:avr:uno"), ArduinoAPIVersion: "10600", @@ -110,7 +109,6 @@ func TestSetupBuildPropertiesWithSomeCustomOverrides(t *testing.T) { &builder.AddAdditionalEntriesToContext{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, - &builder.SketchLoader{}, &builder.SetupBuildProperties{}, &builder.SetCustomBuildProperties{}, } @@ -136,7 +134,7 @@ func TestSetupBuildPropertiesUserHardware(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware", "user_hardware"), BuiltInToolsDirs: paths.NewPathList("downloaded_tools", "tools_builtin"), - SketchLocation: paths.New("sketch1", "sketch1.ino"), + Sketch: OpenSketch(t, paths.New("sketch1", "sketch1.ino")), FQBN: parseFQBN(t, "my_avr_platform:avr:custom_yun"), ArduinoAPIVersion: "10600", } @@ -148,7 +146,6 @@ func TestSetupBuildPropertiesUserHardware(t *testing.T) { &builder.AddAdditionalEntriesToContext{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, - &builder.SketchLoader{}, &builder.SetupBuildProperties{}, } @@ -173,7 +170,7 @@ func TestSetupBuildPropertiesWithMissingPropsFromParentPlatformTxtFiles(t *testi ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware", "user_hardware"), BuiltInToolsDirs: paths.NewPathList("downloaded_tools", "tools_builtin"), - SketchLocation: paths.New("sketch1", "sketch1.ino"), + Sketch: OpenSketch(t, paths.New("sketch1", "sketch1.ino")), FQBN: parseFQBN(t, "my_avr_platform:avr:custom_yun"), ArduinoAPIVersion: "10600", } diff --git a/legacy/builder/test/store_build_options_map_test.go b/legacy/builder/test/store_build_options_map_test.go index 9610d6f5a53..24f757708a0 100644 --- a/legacy/builder/test/store_build_options_map_test.go +++ b/legacy/builder/test/store_build_options_map_test.go @@ -18,6 +18,7 @@ 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/constants" "github.com/arduino/arduino-cli/legacy/builder/types" @@ -31,7 +32,7 @@ func TestStoreBuildOptionsMap(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("tools"), BuiltInLibrariesDirs: paths.New("built-in libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - SketchLocation: paths.New("sketchLocation"), + Sketch: &sketch.Sketch{FullPath: paths.New("sketchLocation")}, FQBN: parseFQBN(t, "my:nice:fqbn"), ArduinoAPIVersion: "ideVersion", CustomBuildProperties: []string{"custom=prop"}, 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 2e9fb66c007..3227d3fa381 100644 --- a/legacy/builder/test/try_build_of_problematic_sketch_test.go +++ b/legacy/builder/test/try_build_of_problematic_sketch_test.go @@ -230,7 +230,7 @@ func tryBuild(t *testing.T, sketchPath ...string) { func tryBuildWithContext(t *testing.T, ctx *types.Context, sketchPath ...string) { sketchLocation := paths.New(sketchPath...) - ctx.SketchLocation = sketchLocation + ctx.Sketch = OpenSketch(t, sketchLocation) err := builder.RunBuilder(ctx) NoError(t, err, "Build error for "+sketchLocation.String()) @@ -243,7 +243,7 @@ func tryPreprocess(t *testing.T, sketchPath ...string) { func tryPreprocessWithContext(t *testing.T, ctx *types.Context, sketchPath ...string) { sketchLocation := paths.New(sketchPath...) - ctx.SketchLocation = sketchLocation + ctx.Sketch = OpenSketch(t, sketchLocation) err := builder.RunPreprocess(ctx) NoError(t, err, "Build error for "+sketchLocation.String()) diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 980f1f410cc..3286416fbb8 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -22,7 +22,6 @@ import ( "strings" "sync" - "github.com/arduino/arduino-cli/arduino" "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" @@ -71,7 +70,6 @@ type Context struct { BuiltInLibrariesDirs *paths.Path OtherLibrariesDirs paths.PathList LibraryDirs paths.PathList // List of paths pointing to individual library root folders - SketchLocation *paths.Path // SketchLocation points to the main Sketch file WatchedLocations paths.PathList ArduinoAPIVersion string FQBN *cores.FQBN @@ -99,7 +97,6 @@ type Context struct { BuildProperties *properties.Map BuildCore string BuildPath *paths.Path - BuildCachePath *paths.Path SketchBuildPath *paths.Path CoreBuildPath *paths.Path CoreBuildCachePath *paths.Path @@ -212,17 +209,15 @@ func (ctx *Context) ExtractBuildOptions() *properties.Map { opts.Set("builtInLibrariesFolders", ctx.BuiltInLibrariesDirs.String()) } opts.Set("otherLibrariesFolders", strings.Join(ctx.OtherLibrariesDirs.AsStrings(), ",")) - opts.SetPath("sketchLocation", ctx.SketchLocation) + opts.SetPath("sketchLocation", ctx.Sketch.FullPath) var additionalFilesRelative []string - if ctx.Sketch != nil { - for _, f := range ctx.Sketch.AdditionalFiles { - absPath := ctx.SketchLocation.Parent() - relPath, err := f.RelTo(absPath) - if err != nil { - continue // ignore - } - additionalFilesRelative = append(additionalFilesRelative, relPath.String()) + absPath := ctx.Sketch.FullPath.Parent() + for _, f := range ctx.Sketch.AdditionalFiles { + relPath, err := f.RelTo(absPath) + if err != nil { + continue // ignore } + additionalFilesRelative = append(additionalFilesRelative, relPath.String()) } opts.Set("fqbn", ctx.FQBN.String()) opts.Set("runtime.ide.version", ctx.ArduinoAPIVersion) @@ -232,22 +227,6 @@ func (ctx *Context) ExtractBuildOptions() *properties.Map { return opts } -func (ctx *Context) InjectBuildOptions(opts *properties.Map) { - ctx.HardwareDirs = paths.NewPathList(strings.Split(opts.Get("hardwareFolders"), ",")...) - ctx.BuiltInToolsDirs = paths.NewPathList(strings.Split(opts.Get("builtInToolsFolders"), ",")...) - ctx.BuiltInLibrariesDirs = paths.New(opts.Get("builtInLibrariesFolders")) - ctx.OtherLibrariesDirs = paths.NewPathList(strings.Split(opts.Get("otherLibrariesFolders"), ",")...) - ctx.SketchLocation = opts.GetPath("sketchLocation") - fqbn, err := cores.ParseFQBN(opts.Get("fqbn")) - if err != nil { - fmt.Fprintln(ctx.Stderr, &arduino.InvalidFQBNError{Cause: err}) - } - ctx.FQBN = fqbn - ctx.ArduinoAPIVersion = opts.Get("runtime.ide.version") - ctx.CustomBuildProperties = strings.Split(opts.Get("customBuildProperties"), ",") - ctx.OptimizationFlags = opts.Get("compiler.optimization_flags") -} - func (ctx *Context) PushProgress() { if ctx.ProgressCB != nil { ctx.ProgressCB(&rpc.TaskProgress{Percent: ctx.Progress.Progress}) diff --git a/legacy/builder/types/context_test.go b/legacy/builder/types/context_test.go index c10ea71dc33..55af78c506c 100644 --- a/legacy/builder/types/context_test.go +++ b/legacy/builder/types/context_test.go @@ -19,6 +19,7 @@ import ( "testing" "github.com/arduino/arduino-cli/arduino/cores" + "github.com/arduino/arduino-cli/arduino/sketch" paths "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" ) @@ -33,29 +34,24 @@ func TestInjectBuildOption(t *testing.T) { BuiltInToolsDirs: paths.NewPathList("ccc", "ddd"), BuiltInLibrariesDirs: paths.New("eee"), OtherLibrariesDirs: paths.NewPathList("fff", "ggg"), - SketchLocation: paths.New("hhh"), + Sketch: &sketch.Sketch{FullPath: paths.New("hhh")}, FQBN: fqbn, ArduinoAPIVersion: "iii", CustomBuildProperties: []string{"jjj", "kkk"}, OptimizationFlags: "lll", } - newCtx := &Context{} - newCtx.InjectBuildOptions(ctx.ExtractBuildOptions()) - require.Equal(t, ctx, newCtx) - } - { - ctx := &Context{ - HardwareDirs: paths.NewPathList("aaa", "bbb"), - BuiltInToolsDirs: paths.NewPathList("ccc", "ddd"), - OtherLibrariesDirs: paths.NewPathList("fff", "ggg"), - SketchLocation: paths.New("hhh"), - FQBN: fqbn, - ArduinoAPIVersion: "iii", - CustomBuildProperties: []string{"jjj", "kkk"}, - OptimizationFlags: "lll", - } - newCtx := &Context{} - newCtx.InjectBuildOptions(ctx.ExtractBuildOptions()) - require.Equal(t, ctx, newCtx) + opts := ctx.ExtractBuildOptions() + require.Equal(t, `properties.Map{ + "hardwareFolders": "aaa,bbb", + "builtInToolsFolders": "ccc,ddd", + "builtInLibrariesFolders": "eee", + "otherLibrariesFolders": "fff,ggg", + "sketchLocation": "hhh", + "fqbn": "aaa:bbb:ccc", + "runtime.ide.version": "iii", + "customBuildProperties": "jjj,kkk", + "additionalFiles": "", + "compiler.optimization_flags": "lll", +}`, opts.Dump()) } }