github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/pkg/testutil/golden/golden.go (about)

     1  // Package golden provides function and helpers to use golden file for
     2  // testing purpose.
     3  package golden
     4  
     5  import (
     6  	"flag"
     7  	"io/ioutil"
     8  	"path/filepath"
     9  	"testing"
    10  )
    11  
    12  var update = flag.Bool("test.update", false, "update golden file")
    13  
    14  // Get returns the golden file content. If the `test.update` is specified, it updates the
    15  // file with the current output and returns it.
    16  func Get(t *testing.T, actual []byte, filename string) []byte {
    17  	golden := filepath.Join("testdata", filename)
    18  	if *update {
    19  		if err := ioutil.WriteFile(golden, actual, 0644); err != nil {
    20  			t.Fatal(err)
    21  		}
    22  	}
    23  	expected, err := ioutil.ReadFile(golden)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	return expected
    28  }