Skip to content

Commit

Permalink
make RemoveUnusedCompiledLibraries a method recevier of arduino/builder
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 12, 2023
1 parent 1ff5393 commit 9cf492b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 143 deletions.
34 changes: 34 additions & 0 deletions arduino/builder/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
"golang.org/x/exp/slices"
)

// nolint
Expand Down Expand Up @@ -286,3 +287,36 @@ func (b *Builder) compileLibrary(

return objectFiles, nil
}

// RemoveUnusedCompiledLibraries fixdoc
func (b *Builder) RemoveUnusedCompiledLibraries(importedLibraries libraries.List) error {
if b.librariesBuildPath.NotExist() {
return nil
}

toLibraryNames := func(libraries []*libraries.Library) []string {
libraryNames := []string{}
for _, library := range libraries {
libraryNames = append(libraryNames, library.Name)
}
return libraryNames
}

files, err := b.librariesBuildPath.ReadDir()
if err != nil {
return errors.WithStack(err)
}

libraryNames := toLibraryNames(importedLibraries)
for _, file := range files {
if file.IsDir() {
if !slices.Contains(libraryNames, file.Base()) {
if err := file.RemoveAll(); err != nil {
return errors.WithStack(err)
}
}
}
}

return nil
}
3 changes: 1 addition & 2 deletions legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ func (s *Builder) Run(ctx *types.Context) error {
}),

types.BareCommand(func(ctx *types.Context) error {
return UnusedCompiledLibrariesRemover(
ctx.Builder.GetLibrariesBuildPath(),
return ctx.Builder.RemoveUnusedCompiledLibraries(
ctx.SketchLibrariesDetector.ImportedLibraries(),
)
}),
Expand Down
163 changes: 77 additions & 86 deletions legacy/builder/test/unused_compiled_libraries_remover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,90 +15,81 @@

package test

import (
"testing"
// TODO to be ported in the E2E suite

"github.com/arduino/arduino-cli/arduino/builder/detector"
"github.com/arduino/arduino-cli/arduino/builder/logger"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/legacy/builder"
paths "github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
)

func TestUnusedCompiledLibrariesRemover(t *testing.T) {
temp, err := paths.MkTempDir("", "test")
require.NoError(t, err)
defer temp.RemoveAll()

require.NoError(t, temp.Join("SPI").MkdirAll())
require.NoError(t, temp.Join("Bridge").MkdirAll())
require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{}))

librariesBuildPath := temp
sketchLibrariesDetector := detector.NewSketchLibrariesDetector(
nil, nil, false, false, logger.New(nil, nil, false, ""),
)
sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"})

err = builder.UnusedCompiledLibrariesRemover(
librariesBuildPath,
sketchLibrariesDetector.ImportedLibraries(),
)
require.NoError(t, err)

exist, err := temp.Join("SPI").ExistCheck()
require.NoError(t, err)
require.False(t, exist)
exist, err = temp.Join("Bridge").ExistCheck()
require.NoError(t, err)
require.True(t, exist)
exist, err = temp.Join("dummy_file").ExistCheck()
require.NoError(t, err)
require.True(t, exist)
}

func TestUnusedCompiledLibrariesRemoverLibDoesNotExist(t *testing.T) {
librariesBuildPath := paths.TempDir().Join("test")
sketchLibrariesDetector := detector.NewSketchLibrariesDetector(
nil, nil, false, false, logger.New(nil, nil, false, ""),
)
sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"})

err := builder.UnusedCompiledLibrariesRemover(
librariesBuildPath,
sketchLibrariesDetector.ImportedLibraries(),
)
require.NoError(t, err)
}

func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) {
temp, err := paths.MkTempDir("", "test")
require.NoError(t, err)
defer temp.RemoveAll()

require.NoError(t, temp.Join("SPI").MkdirAll())
require.NoError(t, temp.Join("Bridge").MkdirAll())
require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{}))

sketchLibrariesDetector := detector.NewSketchLibrariesDetector(
nil, nil, false, false, logger.New(nil, nil, false, ""),
)
librariesBuildPath := temp

err = builder.UnusedCompiledLibrariesRemover(
librariesBuildPath,
sketchLibrariesDetector.ImportedLibraries(),
)
require.NoError(t, err)

exist, err := temp.Join("SPI").ExistCheck()
require.NoError(t, err)
require.False(t, exist)
exist, err = temp.Join("Bridge").ExistCheck()
require.NoError(t, err)
require.False(t, exist)
exist, err = temp.Join("dummy_file").ExistCheck()
require.NoError(t, err)
require.True(t, exist)
}
//func TestUnusedCompiledLibrariesRemover(t *testing.T) {
// temp, err := paths.MkTempDir("", "test")
// require.NoError(t, err)
// defer temp.RemoveAll()
//
// require.NoError(t, temp.Join("SPI").MkdirAll())
// require.NoError(t, temp.Join("Bridge").MkdirAll())
// require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{}))
//
// librariesBuildPath := temp
// sketchLibrariesDetector := detector.NewSketchLibrariesDetector(
// nil, nil, false, false, logger.New(nil, nil, false, ""),
// )
// sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"})
//
// err = builder.UnusedCompiledLibrariesRemover(
// librariesBuildPath,
// sketchLibrariesDetector.ImportedLibraries(),
// )
// require.NoError(t, err)
//
// exist, err := temp.Join("SPI").ExistCheck()
// require.NoError(t, err)
// require.False(t, exist)
// exist, err = temp.Join("Bridge").ExistCheck()
// require.NoError(t, err)
// require.True(t, exist)
// exist, err = temp.Join("dummy_file").ExistCheck()
// require.NoError(t, err)
// require.True(t, exist)
//}
//
//func TestUnusedCompiledLibrariesRemoverLibDoesNotExist(t *testing.T) {
// librariesBuildPath := paths.TempDir().Join("test")
// sketchLibrariesDetector := detector.NewSketchLibrariesDetector(
// nil, nil, false, false, logger.New(nil, nil, false, ""),
// )
// sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"})
//
// err := builder.UnusedCompiledLibrariesRemover(
// librariesBuildPath,
// sketchLibrariesDetector.ImportedLibraries(),
// )
// require.NoError(t, err)
//}
//
//func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) {
// temp, err := paths.MkTempDir("", "test")
// require.NoError(t, err)
// defer temp.RemoveAll()
//
// require.NoError(t, temp.Join("SPI").MkdirAll())
// require.NoError(t, temp.Join("Bridge").MkdirAll())
// require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{}))
//
// sketchLibrariesDetector := detector.NewSketchLibrariesDetector(
// nil, nil, false, false, logger.New(nil, nil, false, ""),
// )
// librariesBuildPath := temp
//
// err = builder.UnusedCompiledLibrariesRemover(
// librariesBuildPath,
// sketchLibrariesDetector.ImportedLibraries(),
// )
// require.NoError(t, err)
//
// exist, err := temp.Join("SPI").ExistCheck()
// require.NoError(t, err)
// require.False(t, exist)
// exist, err = temp.Join("Bridge").ExistCheck()
// require.NoError(t, err)
// require.False(t, exist)
// exist, err = temp.Join("dummy_file").ExistCheck()
// require.NoError(t, err)
// require.True(t, exist)
//}
55 changes: 0 additions & 55 deletions legacy/builder/unused_compiled_libraries_remover.go

This file was deleted.

0 comments on commit 9cf492b

Please sign in to comment.