@@ -22,6 +22,7 @@ import (
22
22
"strings"
23
23
"testing"
24
24
"text/template"
25
+ "time"
25
26
26
27
"github.com/arduino/arduino-cli/arduino/builder/cpp"
27
28
"github.com/arduino/arduino-cli/internal/integrationtest"
@@ -710,3 +711,55 @@ func comparePreprocessGoldenFile(t *testing.T, sketchDir *paths.Path, preprocess
710
711
711
712
require .Equal (t , buf .String (), strings .Replace (preprocessedSketch , "\r \n " , "\n " , - 1 ))
712
713
}
714
+
715
+ func TestCoreCaching (t * testing.T ) {
716
+ env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
717
+ defer env .CleanUp ()
718
+
719
+ sketchPath , err := paths .New (".." , "testdata" , "bare_minimum" ).Abs ()
720
+ require .NoError (t , err )
721
+
722
+ // Install Arduino AVR Boards
723
+ _ ,
_ ,
err = cli .
Run (
"core" ,
"install" ,
"arduino:[email protected] " )
724
+ require .NoError (t , err )
725
+
726
+ // Create temporary cache dir
727
+ buildCachePath , err := paths .MkTempDir ("" , "test_build_cache" )
728
+ require .NoError (t , err )
729
+ defer buildCachePath .RemoveAll ()
730
+
731
+ // Build first time
732
+ _ , _ , err = cli .Run ("compile" , "-b" , "arduino:avr:uno" , "--build-cache-path" , buildCachePath .String (), sketchPath .String ())
733
+ require .NoError (t , err )
734
+
735
+ // Find cached core and save timestamp
736
+ pathList , err := buildCachePath .ReadDirRecursiveFiltered (nil , paths .FilterPrefixes ("core.a" ))
737
+ require .NoError (t , err )
738
+ require .Len (t , pathList , 1 )
739
+ cachedCoreFile := pathList [0 ]
740
+ lastUsedPath := cachedCoreFile .Parent ().Join (".last-used" )
741
+ require .True (t , lastUsedPath .Exist ())
742
+ coreStatBefore , err := cachedCoreFile .Stat ()
743
+ require .NoError (t , err )
744
+
745
+ // Run build again and check timestamp is unchanged
746
+ _ , _ , err = cli .Run ("compile" , "-b" , "arduino:avr:uno" , "--build-cache-path" , buildCachePath .String (), sketchPath .String ())
747
+ require .NoError (t , err )
748
+ coreStatAfterRebuild , err := cachedCoreFile .Stat ()
749
+ require .NoError (t , err )
750
+ require .Equal (t , coreStatBefore .ModTime (), coreStatAfterRebuild .ModTime ())
751
+
752
+ // Touch a file of the core and check if the builder invalidate the cache
753
+ time .Sleep (time .Second )
754
+ now := time .Now ().Local ()
755
+ coreFolder := cli .DataDir ().Join ("packages" , "arduino" , "hardware" , "avr" , "1.8.6" )
756
+ err = coreFolder .Join ("cores" , "arduino" , "Arduino.h" ).Chtimes (now , now )
757
+ require .NoError (t , err )
758
+
759
+ // Run build again, to verify that the builder rebuilds core.a
760
+ _ , _ , err = cli .Run ("compile" , "-b" , "arduino:avr:uno" , "--build-cache-path" , buildCachePath .String (), sketchPath .String ())
761
+ require .NoError (t , err )
762
+ coreStatAfterTouch , err := cachedCoreFile .Stat ()
763
+ require .NoError (t , err )
764
+ require .NotEqual (t , coreStatBefore .ModTime (), coreStatAfterTouch .ModTime ())
765
+ }
0 commit comments