aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/testdata/script/build_issue6480.txt
blob: cf1e9ea6c24dff535e9372c8dd334a7eba478a14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# "go test -c -test.bench=XXX errors" should not hang.
# "go test -c" should also produce reproducible binaries.
# "go test -c" should also appear to write a new binary every time,
# even if it's really just updating the mtime on an existing up-to-date binary.

[gccgo] skip
[short] skip

# Install some commands to compare mtimes
env GOBIN=$WORK/tmp/bin
go install m/now m/mtime m/before

# Initial builds
go test -c -test.bench=XXX errors
go test -c -o errors2.test errors
cmp errors.test$GOEXE errors2.test # // errors2.test has no exeSuffix because -o above doesn't have it

# Check errors.test mtime is updated
exec $GOBIN/now
cp stdout start_time.txt
go test -x -c -test.bench=XXX errors
! stderr '[\\/]link|gccgo' # make sure up-to-date test binary is not relinked
exec $GOBIN/mtime errors.test$GOEXE
cp stdout errors1_mod_time.txt
exec $GOBIN/before start_time.txt errors1_mod_time.txt
rm start_time.txt errors1_mod_time.txt

# Check errors2.test mtime is updated
exec $GOBIN/now
cp stdout start_time.txt
go test -x -c -o errors2.test errors
! stderr '[\\/]link|gccgo' # make sure up-to-date test binary is not relinked
exec $GOBIN/mtime errors2.test
cp stdout errors2_mod_time.txt
exec $GOBIN/before start_time.txt errors2_mod_time.txt

-- go.mod --
module m

go 1.16
-- now/now.go --
// Writes time.Now() to a file
package main

import (
	"encoding/json"
	"fmt"
	"os"
	"time"
)

func main() {
	if err := json.NewEncoder(os.Stdout).Encode(time.Now()); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
-- mtime/mtime.go --
package main

import (
	"encoding/json"
	"fmt"
	"os"
)

func main() {
	info, err := os.Stat(os.Args[1])
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	if err := json.NewEncoder(os.Stdout).Encode(info.ModTime()); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
-- before/before.go --
package main

import (
	"encoding/json"
	"fmt"
	"os"
	"time"
)

func truncateLike(t, p time.Time) time.Time {
	nano := p.UnixNano()
	d := 1 * time.Nanosecond
	for nano%int64(d) == 0 && d < 1*time.Second {
		d *= 10
	}
	for nano%int64(d) == 0 && d < 2*time.Second {
		d *= 2
	}
	return t.Truncate(d)
}

func main() {
	var t1 time.Time
	b1, err := os.ReadFile(os.Args[1])
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	if err := json.Unmarshal(b1, &t1); err != nil  {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	var t2 time.Time
	b2, err := os.ReadFile(os.Args[2])
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	if err := json.Unmarshal(b2, &t2); err != nil  {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	t1 = truncateLike(t1, t2)
	if !t1.Before(t2) {
		fmt.Fprintf(os.Stderr, "time in %v (%v) is not before time in %v (%v)", os.Args[1], t1, os.Args[2], t2)
		os.Exit(1)
	}
}