github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/compile/internal/gc/reproduciblebuilds_test.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gc_test
     6  
     7  import (
     8  	"bytes"
     9  	"github.com/gagliardetto/golang-go/not-internal/testenv"
    10  	"io/ioutil"
    11  	"os"
    12  	"os/exec"
    13  	"path/filepath"
    14  	"testing"
    15  )
    16  
    17  func TestReproducibleBuilds(t *testing.T) {
    18  	tests := []string{
    19  		"issue20272.go",
    20  		"issue27013.go",
    21  		"issue30202.go",
    22  	}
    23  
    24  	testenv.MustHaveGoBuild(t)
    25  	iters := 10
    26  	if testing.Short() {
    27  		iters = 4
    28  	}
    29  	t.Parallel()
    30  	for _, test := range tests {
    31  		test := test
    32  		t.Run(test, func(t *testing.T) {
    33  			t.Parallel()
    34  			var want []byte
    35  			tmp, err := ioutil.TempFile("", "")
    36  			if err != nil {
    37  				t.Fatalf("temp file creation failed: %v", err)
    38  			}
    39  			defer os.Remove(tmp.Name())
    40  			defer tmp.Close()
    41  			for i := 0; i < iters; i++ {
    42  				// Note: use -c 2 to expose any nondeterminism which is the result
    43  				// of the runtime scheduler.
    44  				out, err := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-c", "2", "-o", tmp.Name(), filepath.Join("testdata", "reproducible", test)).CombinedOutput()
    45  				if err != nil {
    46  					t.Fatalf("failed to compile: %v\n%s", err, out)
    47  				}
    48  				obj, err := ioutil.ReadFile(tmp.Name())
    49  				if err != nil {
    50  					t.Fatalf("failed to read object file: %v", err)
    51  				}
    52  				if i == 0 {
    53  					want = obj
    54  				} else {
    55  					if !bytes.Equal(want, obj) {
    56  						t.Fatalf("builds produced different output after %d iters (%d bytes vs %d bytes)", i, len(want), len(obj))
    57  					}
    58  				}
    59  			}
    60  		})
    61  	}
    62  }