@@ -20,6 +20,7 @@ import (
2020 "cmp"
2121 "encoding/json"
2222 "os/exec"
23+ "regexp"
2324 "slices"
2425 "strings"
2526 "testing"
@@ -904,56 +905,75 @@ func comparePreprocessGoldenFile(t *testing.T, sketchDir *paths.Path, preprocess
904905 require .Equal (t , buf .String (), strings .ReplaceAll (preprocessedSketch , "\r \n " , "\n " ))
905906}
906907
907- func TestCoreCaching (t * testing.T ) {
908+ func TestBuildCaching (t * testing.T ) {
908909 env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
909910 defer env .CleanUp ()
910911
911- sketchPath , err := paths .New (".." , "testdata" , "bare_minimum" ).Abs ()
912- require .NoError (t , err )
913-
914912 // Install Arduino AVR Boards
915- _ , _ , err = cli .Run ("core" , "install" , "arduino:avr@1.8.6" )
913+ _ , _ , err : = cli .Run ("core" , "install" , "arduino:avr@1.8.6" )
916914 require .NoError (t , err )
917915
918- // Create temporary cache dir
919- buildCachePath , err := paths .MkTempDir ("" , "test_build_cache" )
920- require .NoError (t , err )
921- defer buildCachePath .RemoveAll ()
916+ t .Run ("CoreCaching" , func (t * testing.T ) {
917+ sketchPath , err := paths .New (".." , "testdata" , "bare_minimum" ).Abs ()
918+ require .NoError (t , err )
922919
923- // Build first time
924- _ , _ , err = cli .Run ("compile" , "-b" , "arduino:avr:uno" , "--build-cache-path" , buildCachePath .String (), sketchPath .String ())
925- require .NoError (t , err )
920+ // Create temporary cache dir
921+ buildCachePath , err := paths .MkTempDir ("" , "test_build_cache" )
922+ require .NoError (t , err )
923+ defer buildCachePath .RemoveAll ()
926924
927- // Find cached core and save timestamp
928- pathList , err := buildCachePath .ReadDirRecursiveFiltered (nil , paths .FilterPrefixes ("core.a" ))
929- require .NoError (t , err )
930- require .Len (t , pathList , 1 )
931- cachedCoreFile := pathList [0 ]
932- lastUsedPath := cachedCoreFile .Parent ().Join (".last-used" )
933- require .True (t , lastUsedPath .Exist ())
934- coreStatBefore , err := cachedCoreFile .Stat ()
935- require .NoError (t , err )
925+ // Build first time
926+ _ , _ , err = cli .Run ("compile" , "-b" , "arduino:avr:uno" , "--build-cache-path" , buildCachePath .String (), sketchPath .String ())
927+ require .NoError (t , err )
936928
937- // Run build again and check timestamp is unchanged
938- _ , _ , err = cli .Run ("compile" , "-b" , "arduino:avr:uno" , "--build-cache-path" , buildCachePath .String (), sketchPath .String ())
939- require .NoError (t , err )
940- coreStatAfterRebuild , err := cachedCoreFile .Stat ()
941- require .NoError (t , err )
942- require .Equal (t , coreStatBefore .ModTime (), coreStatAfterRebuild .ModTime ())
929+ // Find cached core and save timestamp
930+ pathList , err := buildCachePath .ReadDirRecursiveFiltered (nil , paths .FilterPrefixes ("core.a" ))
931+ require .NoError (t , err )
932+ require .Len (t , pathList , 1 )
933+ cachedCoreFile := pathList [0 ]
934+ lastUsedPath := cachedCoreFile .Parent ().Join (".last-used" )
935+ require .True (t , lastUsedPath .Exist ())
936+ coreStatBefore , err := cachedCoreFile .Stat ()
937+ require .NoError (t , err )
943938
944- // Touch a file of the core and check if the builder invalidate the cache
945- time . Sleep ( time . Second )
946- now := time . Now (). Local ( )
947- coreFolder := cli . DataDir (). Join ( "packages" , "arduino" , "hardware" , "avr" , "1.8.6" )
948- err = coreFolder . Join ( "cores" , "arduino" , "Arduino.h" ). Chtimes ( now , now )
949- require .NoError (t , err )
939+ // Run build again and check timestamp is unchanged
940+ _ , _ , err = cli . Run ( "compile" , "-b" , "arduino:avr:uno" , "--build-cache-path" , buildCachePath . String (), sketchPath . String () )
941+ require . NoError ( t , err )
942+ coreStatAfterRebuild , err := cachedCoreFile . Stat ( )
943+ require . NoError ( t , err )
944+ require .Equal (t , coreStatBefore . ModTime (), coreStatAfterRebuild . ModTime () )
950945
951- // Run build again, to verify that the builder rebuilds core.a
952- _ , _ , err = cli .Run ("compile" , "-b" , "arduino:avr:uno" , "--build-cache-path" , buildCachePath .String (), sketchPath .String ())
953- require .NoError (t , err )
954- coreStatAfterTouch , err := cachedCoreFile .Stat ()
955- require .NoError (t , err )
956- require .NotEqual (t , coreStatBefore .ModTime (), coreStatAfterTouch .ModTime ())
946+ // Touch a file of the core and check if the builder invalidate the cache
947+ time .Sleep (time .Second )
948+ now := time .Now ().Local ()
949+ coreFolder := cli .DataDir ().Join ("packages" , "arduino" , "hardware" , "avr" , "1.8.6" )
950+ err = coreFolder .Join ("cores" , "arduino" , "Arduino.h" ).Chtimes (now , now )
951+ require .NoError (t , err )
952+
953+ // Run build again, to verify that the builder rebuilds core.a
954+ _ , _ , err = cli .Run ("compile" , "-b" , "arduino:avr:uno" , "--build-cache-path" , buildCachePath .String (), sketchPath .String ())
955+ require .NoError (t , err )
956+ coreStatAfterTouch , err := cachedCoreFile .Stat ()
957+ require .NoError (t , err )
958+ require .NotEqual (t , coreStatBefore .ModTime (), coreStatAfterTouch .ModTime ())
959+ })
960+
961+ t .Run ("LibraryCacheWithDifferentDirname" , func (t * testing.T ) {
962+ _ , _ , err = cli .Run ("lib" , "install" , "Robot IR Remote" )
963+ require .NoError (t , err )
964+
965+ // Run first compile
966+ sketchPath , err := paths .New ("testdata" , "SketchUsingRobotIRRemote" ).Abs ()
967+ require .NoError (t , err )
968+ _ , _ , err = cli .Run ("compile" , "-b" , "arduino:avr:robotControl" , "-v" , sketchPath .String ())
969+ require .NoError (t , err )
970+
971+ // Run second compile and check that previous build is re-used
972+ out , _ , err := cli .Run ("compile" , "-b" , "arduino:avr:robotControl" , "-v" , sketchPath .String ())
973+ require .NoError (t , err )
974+ check := regexp .MustCompile (`(?m)^Using previously compiled file:.*IRremoteTools\.cpp\.o$` )
975+ require .True (t , check .Match (out ))
976+ })
957977}
958978
959979func TestMergeSketchWithBootloader (t * testing.T ) {
0 commit comments