Commit 77222ecd authored by Martino Facchin's avatar Martino Facchin Committed by Cristian Maglie

Fix caching for libraries when installation folder differents from Name (#2446)

* Fix caching for libraries when installation folder differents from Name

* Prepare infra to add integration test

* Added integration test

---------
Co-authored-by: default avatarCristian Maglie <c.maglie@arduino.cc>
parent ce6bb989
...@@ -245,7 +245,7 @@ func (b *Builder) removeUnusedCompiledLibraries(importedLibraries libraries.List ...@@ -245,7 +245,7 @@ func (b *Builder) removeUnusedCompiledLibraries(importedLibraries libraries.List
toLibraryNames := func(libraries []*libraries.Library) []string { toLibraryNames := func(libraries []*libraries.Library) []string {
libraryNames := []string{} libraryNames := []string{}
for _, library := range libraries { for _, library := range libraries {
libraryNames = append(libraryNames, library.Name) libraryNames = append(libraryNames, library.DirName)
} }
return libraryNames return libraryNames
} }
......
...@@ -20,6 +20,7 @@ import ( ...@@ -20,6 +20,7 @@ import (
"cmp" "cmp"
"encoding/json" "encoding/json"
"os/exec" "os/exec"
"regexp"
"slices" "slices"
"strings" "strings"
"testing" "testing"
...@@ -904,56 +905,75 @@ func comparePreprocessGoldenFile(t *testing.T, sketchDir *paths.Path, preprocess ...@@ -904,56 +905,75 @@ func comparePreprocessGoldenFile(t *testing.T, sketchDir *paths.Path, preprocess
require.Equal(t, buf.String(), strings.ReplaceAll(preprocessedSketch, "\r\n", "\n")) require.Equal(t, buf.String(), strings.ReplaceAll(preprocessedSketch, "\r\n", "\n"))
} }
func TestCoreCaching(t *testing.T) { func TestBuildCaching(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp() defer env.CleanUp()
sketchPath, err := paths.New("..", "testdata", "bare_minimum").Abs()
require.NoError(t, err)
// Install Arduino AVR Boards // Install Arduino AVR Boards
_, _, err = cli.Run("core", "install", "arduino:avr@1.8.6") _, _, err := cli.Run("core", "install", "arduino:avr@1.8.6")
require.NoError(t, err) require.NoError(t, err)
// Create temporary cache dir t.Run("CoreCaching", func(t *testing.T) {
buildCachePath, err := paths.MkTempDir("", "test_build_cache") sketchPath, err := paths.New("..", "testdata", "bare_minimum").Abs()
require.NoError(t, err) require.NoError(t, err)
defer buildCachePath.RemoveAll()
// Build first time // Create temporary cache dir
_, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-cache-path", buildCachePath.String(), sketchPath.String()) buildCachePath, err := paths.MkTempDir("", "test_build_cache")
require.NoError(t, err) require.NoError(t, err)
defer buildCachePath.RemoveAll()
// Find cached core and save timestamp // Build first time
pathList, err := buildCachePath.ReadDirRecursiveFiltered(nil, paths.FilterPrefixes("core.a")) _, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-cache-path", buildCachePath.String(), sketchPath.String())
require.NoError(t, err) 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 // Find cached core and save timestamp
_, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-cache-path", buildCachePath.String(), sketchPath.String()) pathList, err := buildCachePath.ReadDirRecursiveFiltered(nil, paths.FilterPrefixes("core.a"))
require.NoError(t, err) require.NoError(t, err)
coreStatAfterRebuild, err := cachedCoreFile.Stat() require.Len(t, pathList, 1)
require.NoError(t, err) cachedCoreFile := pathList[0]
require.Equal(t, coreStatBefore.ModTime(), coreStatAfterRebuild.ModTime()) lastUsedPath := cachedCoreFile.Parent().Join(".last-used")
require.True(t, lastUsedPath.Exist())
coreStatBefore, err := cachedCoreFile.Stat()
require.NoError(t, err)
// Touch a file of the core and check if the builder invalidate the cache // Run build again and check timestamp is unchanged
time.Sleep(time.Second) _, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-cache-path", buildCachePath.String(), sketchPath.String())
now := time.Now().Local() require.NoError(t, err)
coreFolder := cli.DataDir().Join("packages", "arduino", "hardware", "avr", "1.8.6") coreStatAfterRebuild, err := cachedCoreFile.Stat()
err = coreFolder.Join("cores", "arduino", "Arduino.h").Chtimes(now, now) require.NoError(t, err)
require.NoError(t, err) require.Equal(t, coreStatBefore.ModTime(), coreStatAfterRebuild.ModTime())
// Run build again, to verify that the builder rebuilds core.a // Touch a file of the core and check if the builder invalidate the cache
_, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-cache-path", buildCachePath.String(), sketchPath.String()) time.Sleep(time.Second)
require.NoError(t, err) now := time.Now().Local()
coreStatAfterTouch, err := cachedCoreFile.Stat() coreFolder := cli.DataDir().Join("packages", "arduino", "hardware", "avr", "1.8.6")
require.NoError(t, err) err = coreFolder.Join("cores", "arduino", "Arduino.h").Chtimes(now, now)
require.NotEqual(t, coreStatBefore.ModTime(), coreStatAfterTouch.ModTime()) 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())
})
t.Run("LibraryCacheWithDifferentDirname", func(t *testing.T) {
_, _, err = cli.Run("lib", "install", "Robot IR Remote")
require.NoError(t, err)
// Run first compile
sketchPath, err := paths.New("testdata", "SketchUsingRobotIRRemote").Abs()
require.NoError(t, err)
_, _, err = cli.Run("compile", "-b", "arduino:avr:robotControl", "-v", sketchPath.String())
require.NoError(t, err)
// Run second compile and check that previous build is re-used
out, _, err := cli.Run("compile", "-b", "arduino:avr:robotControl", "-v", sketchPath.String())
require.NoError(t, err)
check := regexp.MustCompile(`(?m)^Using previously compiled file:.*IRremoteTools\.cpp\.o$`)
require.True(t, check.Match(out))
})
} }
func TestMergeSketchWithBootloader(t *testing.T) { func TestMergeSketchWithBootloader(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment