@@ -702,33 +702,72 @@ func resetFileStamps() {
702
702
reset (gorootInstallDir )
703
703
}
704
704
705
- // touch makes path newer than the "old" time stamp used by resetFileStamps.
706
- func touch (path string ) {
705
+ // touch changes path and returns a function that changes it back.
706
+ // It also sets the time of the file, so that we can see if it is rewritten.
707
+ func touch (t * testing.T , path string ) (cleanup func ()) {
708
+ data , err := ioutil .ReadFile (path )
709
+ if err != nil {
710
+ t .Fatal (err )
711
+ }
712
+ old := make ([]byte , len (data ))
713
+ copy (old , data )
714
+ if bytes .HasPrefix (data , []byte ("!<arch>\n " )) {
715
+ // Change last digit of build ID.
716
+ // (Content ID in the new content-based build IDs.)
717
+ const marker = `build id "`
718
+ i := bytes .Index (data , []byte (marker ))
719
+ if i < 0 {
720
+ t .Fatal ("cannot find build id in archive" )
721
+ }
722
+ j := bytes .IndexByte (data [i + len (marker ):], '"' )
723
+ if j < 0 {
724
+ t .Fatal ("cannot find build id in archive" )
725
+ }
726
+ i += len (marker ) + j - 1
727
+ if data [i ] == 'a' {
728
+ data [i ] = 'b'
729
+ } else {
730
+ data [i ] = 'a'
731
+ }
732
+ } else {
733
+ // assume it's a text file
734
+ data = append (data , '\n' )
735
+ }
736
+ if err := ioutil .WriteFile (path , data , 0666 ); err != nil {
737
+ t .Fatal (err )
738
+ }
707
739
if err := os .Chtimes (path , nearlyNew , nearlyNew ); err != nil {
708
- log .Fatalf ("os.Chtimes failed: %v" , err )
740
+ t .Fatal (err )
741
+ }
742
+ return func () {
743
+ if err := ioutil .WriteFile (path , old , 0666 ); err != nil {
744
+ t .Fatal (err )
745
+ }
709
746
}
710
747
}
711
748
712
749
// isNew returns if the path is newer than the time stamp used by touch.
713
- func isNew (path string ) bool {
750
+ func isNew (t * testing. T , path string ) bool {
714
751
fi , err := os .Stat (path )
715
752
if err != nil {
716
- log . Fatalf ( "os.Stat failed: %v" , err )
753
+ t . Fatal ( err )
717
754
}
718
755
return fi .ModTime ().After (stampTime )
719
756
}
720
757
721
758
// Fail unless path has been rebuilt (i.e. is newer than the time stamp used by
722
759
// isNew)
723
760
func AssertRebuilt (t * testing.T , msg , path string ) {
724
- if ! isNew (path ) {
761
+ t .Helper ()
762
+ if ! isNew (t , path ) {
725
763
t .Errorf ("%s was not rebuilt (%s)" , msg , path )
726
764
}
727
765
}
728
766
729
767
// Fail if path has been rebuilt (i.e. is newer than the time stamp used by isNew)
730
768
func AssertNotRebuilt (t * testing.T , msg , path string ) {
731
- if isNew (path ) {
769
+ t .Helper ()
770
+ if isNew (t , path ) {
732
771
t .Errorf ("%s was rebuilt (%s)" , msg , path )
733
772
}
734
773
}
@@ -738,41 +777,55 @@ func TestRebuilding(t *testing.T) {
738
777
goCmd (t , "install" , "-linkshared" , "exe" )
739
778
740
779
// If the source is newer than both the .a file and the .so, both are rebuilt.
741
- resetFileStamps ()
742
- touch ("src/depBase/dep.go" )
743
- goCmd (t , "install" , "-linkshared" , "exe" )
744
- AssertRebuilt (t , "new source" , filepath .Join (gopathInstallDir , "depBase.a" ))
745
- AssertRebuilt (t , "new source" , filepath .Join (gopathInstallDir , "libdepBase.so" ))
780
+ t .Run ("newsource" , func (t * testing.T ) {
781
+ resetFileStamps ()
782
+ cleanup := touch (t , "src/depBase/dep.go" )
783
+ defer func () {
784
+ cleanup ()
785
+ goCmd (t , "install" , "-linkshared" , "exe" )
786
+ }()
787
+ goCmd (t , "install" , "-linkshared" , "exe" )
788
+ AssertRebuilt (t , "new source" , filepath .Join (gopathInstallDir , "depBase.a" ))
789
+ AssertRebuilt (t , "new source" , filepath .Join (gopathInstallDir , "libdepBase.so" ))
790
+ })
746
791
747
792
// If the .a file is newer than the .so, the .so is rebuilt (but not the .a)
748
- resetFileStamps ()
749
- touch (filepath .Join (gopathInstallDir , "depBase.a" ))
750
- goCmd (t , "install" , "-linkshared" , "exe" )
751
- AssertNotRebuilt (t , "new .a file" , filepath .Join (gopathInstallDir , "depBase.a" ))
752
- AssertRebuilt (t , "new .a file" , filepath .Join (gopathInstallDir , "libdepBase.so" ))
793
+ t .Run ("newarchive" , func (t * testing.T ) {
794
+ resetFileStamps ()
795
+ goCmd (t , "list" , "-linkshared" , "-f={{.ImportPath}} {{.Stale}} {{.StaleReason}} {{.Target}}" , "depBase" )
796
+ AssertNotRebuilt (t , "new .a file before build" , filepath .Join (gopathInstallDir , "depBase.a" ))
797
+ cleanup := touch (t , filepath .Join (gopathInstallDir , "depBase.a" ))
798
+ defer func () {
799
+ cleanup ()
800
+ goCmd (t , "install" , "-v" , "-linkshared" , "exe" )
801
+ }()
802
+ goCmd (t , "install" , "-v" , "-linkshared" , "exe" )
803
+ AssertNotRebuilt (t , "new .a file" , filepath .Join (gopathInstallDir , "depBase.a" ))
804
+ AssertRebuilt (t , "new .a file" , filepath .Join (gopathInstallDir , "libdepBase.so" ))
805
+ })
753
806
}
754
807
755
- func appendFile (path , content string ) {
808
+ func appendFile (t * testing. T , path , content string ) {
756
809
f , err := os .OpenFile (path , os .O_WRONLY | os .O_APPEND , 0660 )
757
810
if err != nil {
758
- log .Fatalf ("os.OpenFile failed: %v" , err )
811
+ t .Fatalf ("os.OpenFile failed: %v" , err )
759
812
}
760
813
defer func () {
761
814
err := f .Close ()
762
815
if err != nil {
763
- log .Fatalf ("f.Close failed: %v" , err )
816
+ t .Fatalf ("f.Close failed: %v" , err )
764
817
}
765
818
}()
766
819
_ , err = f .WriteString (content )
767
820
if err != nil {
768
- log .Fatalf ("f.WriteString failed: %v" , err )
821
+ t .Fatalf ("f.WriteString failed: %v" , err )
769
822
}
770
823
}
771
824
772
- func writeFile (path , content string ) {
825
+ func writeFile (t * testing. T , path , content string ) {
773
826
err := ioutil .WriteFile (path , []byte (content ), 0644 )
774
827
if err != nil {
775
- log .Fatalf ("ioutil.WriteFile failed: %v" , err )
828
+ t .Fatalf ("ioutil.WriteFile failed: %v" , err )
776
829
}
777
830
}
778
831
@@ -786,7 +839,7 @@ func TestABIChecking(t *testing.T) {
786
839
// some senses but suffices for the narrow definition of ABI compatibility the
787
840
// toolchain uses today.
788
841
resetFileStamps ()
789
- appendFile ("src/depBase/dep.go" , "func ABIBreak() {}\n " )
842
+ appendFile (t , "src/depBase/dep.go" , "func ABIBreak() {}\n " )
790
843
goCmd (t , "install" , "-buildmode=shared" , "-linkshared" , "depBase" )
791
844
c := exec .Command ("./bin/exe" )
792
845
output , err := c .CombinedOutput ()
@@ -817,7 +870,7 @@ func TestABIChecking(t *testing.T) {
817
870
// function) and rebuild libdepBase.so, exe still works, even if new function
818
871
// is in a file by itself.
819
872
resetFileStamps ()
820
- writeFile ("src/depBase/dep2.go" , "package depBase\n func noABIBreak() {}\n " )
873
+ writeFile (t , "src/depBase/dep2.go" , "package depBase\n func noABIBreak() {}\n " )
821
874
goCmd (t , "install" , "-buildmode=shared" , "-linkshared" , "depBase" )
822
875
run (t , "after non-ABI breaking change" , "./bin/exe" )
823
876
}
0 commit comments