Home | History | Annotate | Download | only in gc
      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 	testenv.MustHaveGoBuild(t)
     19 	iters := 10
     20 	if testing.Short() {
     21 		iters = 4
     22 	}
     23 	t.Parallel()
     24 	var want []byte
     25 	tmp, err := ioutil.TempFile("", "")
     26 	if err != nil {
     27 		t.Fatalf("temp file creation failed: %v", err)
     28 	}
     29 	defer os.Remove(tmp.Name())
     30 	defer tmp.Close()
     31 	for i := 0; i < iters; i++ {
     32 		out, err := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", tmp.Name(), filepath.Join("testdata", "reproducible", "issue20272.go")).CombinedOutput()
     33 		if err != nil {
     34 			t.Fatalf("failed to compile: %v\n%s", err, out)
     35 		}
     36 		obj, err := ioutil.ReadFile(tmp.Name())
     37 		if err != nil {
     38 			t.Fatalf("failed to read object file: %v", err)
     39 		}
     40 		if i == 0 {
     41 			want = obj
     42 		} else {
     43 			if !bytes.Equal(want, obj) {
     44 				t.Fatalf("builds produced different output after %d iters (%d bytes vs %d bytes)", i, len(want), len(obj))
     45 			}
     46 		}
     47 	}
     48 }
     49