Skip to content

Commit 67a7d5d

Browse files
committed
misc/cgo/testshared: don't assume mtimes trigger rebuilds
The upcoming CL 73212 will see through mtime modifications. Change the underlying file too. Change-Id: Ib23b4136a62ee87bce408b76bb0385451ae7dcd2 Reviewed-on: https://go-review.googlesource.com/74130 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 4d0151e commit 67a7d5d

File tree

1 file changed

+78
-25
lines changed

1 file changed

+78
-25
lines changed

misc/cgo/testshared/shared_test.go

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -702,33 +702,72 @@ func resetFileStamps() {
702702
reset(gorootInstallDir)
703703
}
704704

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+
}
707739
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+
}
709746
}
710747
}
711748

712749
// 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 {
714751
fi, err := os.Stat(path)
715752
if err != nil {
716-
log.Fatalf("os.Stat failed: %v", err)
753+
t.Fatal(err)
717754
}
718755
return fi.ModTime().After(stampTime)
719756
}
720757

721758
// Fail unless path has been rebuilt (i.e. is newer than the time stamp used by
722759
// isNew)
723760
func AssertRebuilt(t *testing.T, msg, path string) {
724-
if !isNew(path) {
761+
t.Helper()
762+
if !isNew(t, path) {
725763
t.Errorf("%s was not rebuilt (%s)", msg, path)
726764
}
727765
}
728766

729767
// Fail if path has been rebuilt (i.e. is newer than the time stamp used by isNew)
730768
func AssertNotRebuilt(t *testing.T, msg, path string) {
731-
if isNew(path) {
769+
t.Helper()
770+
if isNew(t, path) {
732771
t.Errorf("%s was rebuilt (%s)", msg, path)
733772
}
734773
}
@@ -738,41 +777,55 @@ func TestRebuilding(t *testing.T) {
738777
goCmd(t, "install", "-linkshared", "exe")
739778

740779
// 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+
})
746791

747792
// 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+
})
753806
}
754807

755-
func appendFile(path, content string) {
808+
func appendFile(t *testing.T, path, content string) {
756809
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0660)
757810
if err != nil {
758-
log.Fatalf("os.OpenFile failed: %v", err)
811+
t.Fatalf("os.OpenFile failed: %v", err)
759812
}
760813
defer func() {
761814
err := f.Close()
762815
if err != nil {
763-
log.Fatalf("f.Close failed: %v", err)
816+
t.Fatalf("f.Close failed: %v", err)
764817
}
765818
}()
766819
_, err = f.WriteString(content)
767820
if err != nil {
768-
log.Fatalf("f.WriteString failed: %v", err)
821+
t.Fatalf("f.WriteString failed: %v", err)
769822
}
770823
}
771824

772-
func writeFile(path, content string) {
825+
func writeFile(t *testing.T, path, content string) {
773826
err := ioutil.WriteFile(path, []byte(content), 0644)
774827
if err != nil {
775-
log.Fatalf("ioutil.WriteFile failed: %v", err)
828+
t.Fatalf("ioutil.WriteFile failed: %v", err)
776829
}
777830
}
778831

@@ -786,7 +839,7 @@ func TestABIChecking(t *testing.T) {
786839
// some senses but suffices for the narrow definition of ABI compatibility the
787840
// toolchain uses today.
788841
resetFileStamps()
789-
appendFile("src/depBase/dep.go", "func ABIBreak() {}\n")
842+
appendFile(t, "src/depBase/dep.go", "func ABIBreak() {}\n")
790843
goCmd(t, "install", "-buildmode=shared", "-linkshared", "depBase")
791844
c := exec.Command("./bin/exe")
792845
output, err := c.CombinedOutput()
@@ -817,7 +870,7 @@ func TestABIChecking(t *testing.T) {
817870
// function) and rebuild libdepBase.so, exe still works, even if new function
818871
// is in a file by itself.
819872
resetFileStamps()
820-
writeFile("src/depBase/dep2.go", "package depBase\nfunc noABIBreak() {}\n")
873+
writeFile(t, "src/depBase/dep2.go", "package depBase\nfunc noABIBreak() {}\n")
821874
goCmd(t, "install", "-buildmode=shared", "-linkshared", "depBase")
822875
run(t, "after non-ABI breaking change", "./bin/exe")
823876
}

0 commit comments

Comments
 (0)