Skip to content

Commit

Permalink
Refactor AddAdditionalEntriesToContext
Browse files Browse the repository at this point in the history
cmaglie#32

* refactor AddAdditionalEntriesToContext in a function

* use the new function in all the tests

* Move the assignaton of LibrariesResolutionResults inside the ResolveLibrary func

The ResolveLibrary func is only called by the ContainerFindIncludes

* rename bPath to buildPath

* cleanup usless tests

* remove shadowed variable
  • Loading branch information
alessio-perugini authored Aug 31, 2023
1 parent 868d325 commit 90994dd
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 67 deletions.
34 changes: 14 additions & 20 deletions legacy/builder/add_additional_entries_to_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,32 @@ package builder

import (
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/go-paths-helper"
"github.com/pkg/errors"
)

type AddAdditionalEntriesToContext struct{}
func AddAdditionalEntriesToContext(buildPath *paths.Path, warningLevel string) (*paths.Path, *paths.Path, *paths.Path, string, error) {
var sketchBuildPath, librariesBuildPath, coreBuildPath *paths.Path
var err error

func (*AddAdditionalEntriesToContext) Run(ctx *types.Context) error {
if ctx.BuildPath != nil {
buildPath := ctx.BuildPath
sketchBuildPath, err := buildPath.Join(constants.FOLDER_SKETCH).Abs()
if buildPath != nil {
sketchBuildPath, err = buildPath.Join(constants.FOLDER_SKETCH).Abs()
if err != nil {
return errors.WithStack(err)
return nil, nil, nil, "", errors.WithStack(err)
}
librariesBuildPath, err := buildPath.Join(constants.FOLDER_LIBRARIES).Abs()
librariesBuildPath, err = buildPath.Join(constants.FOLDER_LIBRARIES).Abs()
if err != nil {
return errors.WithStack(err)
return nil, nil, nil, "", errors.WithStack(err)
}
coreBuildPath, err := buildPath.Join(constants.FOLDER_CORE).Abs()
coreBuildPath, err = buildPath.Join(constants.FOLDER_CORE).Abs()
if err != nil {
return errors.WithStack(err)
return nil, nil, nil, "", errors.WithStack(err)
}

ctx.SketchBuildPath = sketchBuildPath
ctx.LibrariesBuildPath = librariesBuildPath
ctx.CoreBuildPath = coreBuildPath
}

if ctx.WarningsLevel == "" {
ctx.WarningsLevel = DEFAULT_WARNINGS_LEVEL
if warningLevel == "" {
warningLevel = DEFAULT_WARNINGS_LEVEL
}

ctx.LibrariesResolutionResults = map[string]types.LibraryResolutionResult{}

return nil
return sketchBuildPath, librariesBuildPath, coreBuildPath, warningLevel, nil
}
4 changes: 4 additions & 0 deletions legacy/builder/container_find_includes.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ func ResolveLibrary(ctx *types.Context, header string) *libraries.Library {
resolver := ctx.LibrariesResolver
importedLibraries := ctx.ImportedLibraries

if ctx.LibrariesResolutionResults == nil {
ctx.LibrariesResolutionResults = map[string]types.LibraryResolutionResult{}
}

candidates := resolver.AlternativesFor(header)

if ctx.Verbose {
Expand Down
11 changes: 10 additions & 1 deletion legacy/builder/container_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ import (
type ContainerSetupHardwareToolsLibsSketchAndProps struct{}

func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context) error {
sketchBuildPath, librariesBuildPath, coreBuildPath,
warningsLevel, err := AddAdditionalEntriesToContext(ctx.BuildPath, ctx.WarningsLevel)
if err != nil {
return errors.WithStack(err)
}
ctx.SketchBuildPath = sketchBuildPath
ctx.LibrariesBuildPath = librariesBuildPath
ctx.CoreBuildPath = coreBuildPath
ctx.WarningsLevel = warningsLevel

commands := []types.Command{
&AddAdditionalEntriesToContext{},
&FailIfBuildPathEqualsSketchPath{},
&LibrariesLoader{},
}
Expand Down
30 changes: 14 additions & 16 deletions legacy/builder/test/add_additional_entries_to_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,28 @@ import (
func TestAddAdditionalEntriesToContextNoBuildPath(t *testing.T) {
ctx := &types.Context{}

command := builder.AddAdditionalEntriesToContext{}
NoError(t, command.Run(ctx))
sketchBuildPath, librariesBuildPath, coreBuildPath,
warningsLevel, err := builder.AddAdditionalEntriesToContext(ctx.BuildPath, ctx.WarningsLevel)
NoError(t, err)

require.Empty(t, ctx.SketchBuildPath)
require.Empty(t, ctx.LibrariesBuildPath)
require.Empty(t, ctx.CoreBuildPath)
require.Empty(t, sketchBuildPath)
require.Empty(t, librariesBuildPath)
require.Empty(t, coreBuildPath)

require.NotNil(t, ctx.WarningsLevel)

require.Equal(t, 0, len(ctx.LibrariesResolutionResults))
require.NotNil(t, warningsLevel)
}

func TestAddAdditionalEntriesToContextWithBuildPath(t *testing.T) {
ctx := &types.Context{}
ctx.BuildPath = paths.New("folder")

command := builder.AddAdditionalEntriesToContext{}
NoError(t, command.Run(ctx))

require.Equal(t, Abs(t, paths.New("folder", constants.FOLDER_SKETCH)), ctx.SketchBuildPath)
require.Equal(t, Abs(t, paths.New("folder", "libraries")), ctx.LibrariesBuildPath)
require.Equal(t, Abs(t, paths.New("folder", constants.FOLDER_CORE)), ctx.CoreBuildPath)
sketchBuildPath, librariesBuildPath, coreBuildPath,
warningsLevel, err := builder.AddAdditionalEntriesToContext(ctx.BuildPath, ctx.WarningsLevel)
NoError(t, err)

require.NotNil(t, ctx.WarningsLevel)
require.Equal(t, Abs(t, paths.New("folder", constants.FOLDER_SKETCH)), sketchBuildPath)
require.Equal(t, Abs(t, paths.New("folder", "libraries")), librariesBuildPath)
require.Equal(t, Abs(t, paths.New("folder", constants.FOLDER_CORE)), coreBuildPath)

require.Equal(t, 0, len(ctx.LibrariesResolutionResults))
require.NotNil(t, warningsLevel)
}
9 changes: 0 additions & 9 deletions legacy/builder/test/hardware_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"runtime"
"testing"

"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"
Expand Down Expand Up @@ -69,14 +68,6 @@ func TestLoadHardwareMixingUserHardwareFolder(t *testing.T) {
ctx = prepareBuilderTestContext(t, ctx, nil, "")
defer cleanUpBuilderTestContext(t, ctx)

commands := []types.Command{
&builder.AddAdditionalEntriesToContext{},
}
for _, command := range commands {
err := command.Run(ctx)
NoError(t, err)
}

packages := ctx.PackageManager.GetPackages()

if runtime.GOOS == "windows" {
Expand Down
4 changes: 0 additions & 4 deletions legacy/builder/test/libraries_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func TestLoadLibrariesAVR(t *testing.T) {
defer cleanUpBuilderTestContext(t, ctx)

commands := []types.Command{
&builder.AddAdditionalEntriesToContext{},
&builder.LibrariesLoader{},
}
for _, command := range commands {
Expand Down Expand Up @@ -154,7 +153,6 @@ func TestLoadLibrariesSAM(t *testing.T) {
defer cleanUpBuilderTestContext(t, ctx)

commands := []types.Command{
&builder.AddAdditionalEntriesToContext{},
&builder.LibrariesLoader{},
}
for _, command := range commands {
Expand Down Expand Up @@ -233,7 +231,6 @@ func TestLoadLibrariesAVRNoDuplicateLibrariesFolders(t *testing.T) {
defer cleanUpBuilderTestContext(t, ctx)

commands := []types.Command{
&builder.AddAdditionalEntriesToContext{},
&builder.LibrariesLoader{},
}
for _, command := range commands {
Expand All @@ -258,7 +255,6 @@ func TestLoadLibrariesMyAVRPlatform(t *testing.T) {
defer cleanUpBuilderTestContext(t, ctx)

commands := []types.Command{
&builder.AddAdditionalEntriesToContext{},
&builder.LibrariesLoader{},
}
for _, command := range commands {
Expand Down
1 change: 0 additions & 1 deletion legacy/builder/test/recipe_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func TestRecipeRunner(t *testing.T) {
buildProperties.Set("recipe.hooks.prebuild.1.pattern", "echo")

commands := []types.Command{
&builder.AddAdditionalEntriesToContext{},
&builder.RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.prebuild", Suffix: ".pattern"},
}

Expand Down
16 changes: 0 additions & 16 deletions legacy/builder/test/setup_build_properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ func TestSetupBuildProperties(t *testing.T) {
ctx = prepareBuilderTestContext(t, ctx, paths.New("sketch1", "sketch1.ino"), "arduino:avr:uno")
defer cleanUpBuilderTestContext(t, ctx)

commands := []types.Command{
&builder.AddAdditionalEntriesToContext{},
}
for _, command := range commands {
err := command.Run(ctx)
NoError(t, err)
}

buildProperties := ctx.BuildProperties

require.Equal(t, "ARDUINO", buildProperties.Get("software"))
Expand Down Expand Up @@ -108,14 +100,6 @@ func TestSetupBuildPropertiesUserHardware(t *testing.T) {
ctx = prepareBuilderTestContext(t, ctx, paths.New("sketch1", "sketch1.ino"), "my_avr_platform:avr:custom_yun")
defer cleanUpBuilderTestContext(t, ctx)

commands := []types.Command{
&builder.AddAdditionalEntriesToContext{},
}
for _, command := range commands {
err := command.Run(ctx)
NoError(t, err)
}

buildProperties := ctx.BuildProperties

require.Equal(t, "ARDUINO", buildProperties.Get("software"))
Expand Down

0 comments on commit 90994dd

Please sign in to comment.