...

Text file src/cmd/go/testdata/script/build_issue6480.txt

Documentation: cmd/go/testdata/script

     1# "go test -c -test.bench=XXX errors" should not hang.
     2# "go test -c" should also produce reproducible binaries.
     3# "go test -c" should also appear to write a new binary every time,
     4# even if it's really just updating the mtime on an existing up-to-date binary.
     5
     6[compiler:gccgo] skip
     7[short] skip
     8
     9# Install some commands to compare mtimes
    10env GOBIN=$WORK/tmp/bin
    11go install m/now m/mtime m/before
    12
    13# Initial builds
    14go test -c -test.bench=XXX errors
    15go test -c -o errors2.test errors
    16cmp errors.test$GOEXE errors2.test # // errors2.test has no exeSuffix because -o above doesn't have it
    17
    18# Check errors.test mtime is updated
    19exec $GOBIN/now
    20cp stdout start_time.txt
    21go test -x -c -test.bench=XXX errors
    22! stderr '[\\/]link|gccgo' # make sure up-to-date test binary is not relinked
    23exec $GOBIN/mtime errors.test$GOEXE
    24cp stdout errors1_mod_time.txt
    25exec $GOBIN/before start_time.txt errors1_mod_time.txt
    26rm start_time.txt errors1_mod_time.txt
    27
    28# Check errors2.test mtime is updated
    29exec $GOBIN/now
    30cp stdout start_time.txt
    31go test -x -c -o errors2.test errors
    32! stderr '[\\/]link|gccgo' # make sure up-to-date test binary is not relinked
    33exec $GOBIN/mtime errors2.test
    34cp stdout errors2_mod_time.txt
    35exec $GOBIN/before start_time.txt errors2_mod_time.txt
    36
    37-- go.mod --
    38module m
    39
    40go 1.16
    41-- now/now.go --
    42// Writes time.Now() to a file
    43package main
    44
    45import (
    46	"encoding/json"
    47	"fmt"
    48	"os"
    49	"time"
    50)
    51
    52func main() {
    53	if err := json.NewEncoder(os.Stdout).Encode(time.Now()); err != nil {
    54		fmt.Fprintln(os.Stderr, err)
    55		os.Exit(1)
    56	}
    57}
    58-- mtime/mtime.go --
    59package main
    60
    61import (
    62	"encoding/json"
    63	"fmt"
    64	"os"
    65)
    66
    67func main() {
    68	info, err := os.Stat(os.Args[1])
    69	if err != nil {
    70		fmt.Fprintln(os.Stderr, err)
    71		os.Exit(1)
    72	}
    73	if err := json.NewEncoder(os.Stdout).Encode(info.ModTime()); err != nil {
    74		fmt.Fprintln(os.Stderr, err)
    75		os.Exit(1)
    76	}
    77}
    78-- before/before.go --
    79package main
    80
    81import (
    82	"encoding/json"
    83	"fmt"
    84	"os"
    85	"time"
    86)
    87
    88func truncateLike(t, p time.Time) time.Time {
    89	nano := p.UnixNano()
    90	d := 1 * time.Nanosecond
    91	for nano%int64(d) == 0 && d < 1*time.Second {
    92		d *= 10
    93	}
    94	for nano%int64(d) == 0 && d < 2*time.Second {
    95		d *= 2
    96	}
    97	return t.Truncate(d)
    98}
    99
   100func main() {
   101	var t1 time.Time
   102	b1, err := os.ReadFile(os.Args[1])
   103	if err != nil {
   104		fmt.Fprintln(os.Stderr, err)
   105		os.Exit(1)
   106	}
   107	if err := json.Unmarshal(b1, &t1); err != nil  {
   108		fmt.Fprintln(os.Stderr, err)
   109		os.Exit(1)
   110	}
   111
   112	var t2 time.Time
   113	b2, err := os.ReadFile(os.Args[2])
   114	if err != nil {
   115		fmt.Fprintln(os.Stderr, err)
   116		os.Exit(1)
   117	}
   118	if err := json.Unmarshal(b2, &t2); err != nil  {
   119		fmt.Fprintln(os.Stderr, err)
   120		os.Exit(1)
   121	}
   122
   123	t1 = truncateLike(t1, t2)
   124	if !t1.Before(t2) {
   125		fmt.Fprintf(os.Stderr, "time in %v (%v) is not before time in %v (%v)", os.Args[1], t1, os.Args[2], t2)
   126		os.Exit(1)
   127	}
   128}

View as plain text