github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/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  	"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  	}
    22  
    23  	testenv.MustHaveGoBuild(t)
    24  	iters := 10
    25  	if testing.Short() {
    26  		iters = 4
    27  	}
    28  	t.Parallel()
    29  	for _, test := range tests {
    30  		test := test
    31  		t.Run(test, func(t *testing.T) {
    32  			t.Parallel()
    33  			var want []byte
    34  			tmp, err := ioutil.TempFile("", "")
    35  			if err != nil {
    36  				t.Fatalf("temp file creation failed: %v", err)
    37  			}
    38  			defer os.Remove(tmp.Name())
    39  			defer tmp.Close()
    40  			for i := 0; i < iters; i++ {
    41  				out, err := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", tmp.Name(), filepath.Join("testdata", "reproducible", test)).CombinedOutput()
    42  				if err != nil {
    43  					t.Fatalf("failed to compile: %v\n%s", err, out)
    44  				}
    45  				obj, err := ioutil.ReadFile(tmp.Name())
    46  				if err != nil {
    47  					t.Fatalf("failed to read object file: %v", err)
    48  				}
    49  				if i == 0 {
    50  					want = obj
    51  				} else {
    52  					if !bytes.Equal(want, obj) {
    53  						t.Fatalf("builds produced different output after %d iters (%d bytes vs %d bytes)", i, len(want), len(obj))
    54  					}
    55  				}
    56  			}
    57  		})
    58  	}
    59  }