github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/internal/test/test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package test
    18  
    19  import (
    20  	"bytes"
    21  	"flag"
    22  	"io/ioutil"
    23  	"path/filepath"
    24  
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  // UpdateGolden writes out the golden files with the latest values, rather than failing the test.
    29  var updateGolden = flag.Bool("update", false, "update golden files")
    30  
    31  // TestingT describes a testing object compatible with the critical functions from the testing.T type
    32  type TestingT interface {
    33  	Fatal(...interface{})
    34  	Fatalf(string, ...interface{})
    35  	HelperT
    36  }
    37  
    38  // HelperT describes a test with a helper function
    39  type HelperT interface {
    40  	Helper()
    41  }
    42  
    43  // AssertGoldenBytes asserts that the give actual content matches the contents of the given filename
    44  func AssertGoldenBytes(t TestingT, actual []byte, filename string) {
    45  	t.Helper()
    46  
    47  	if err := compare(actual, path(filename)); err != nil {
    48  		t.Fatalf("%v", err)
    49  	}
    50  }
    51  
    52  // AssertGoldenString asserts that the given string matches the contents of the given file.
    53  func AssertGoldenString(t TestingT, actual, filename string) {
    54  	t.Helper()
    55  
    56  	if err := compare([]byte(actual), path(filename)); err != nil {
    57  		t.Fatalf("%v", err)
    58  	}
    59  }
    60  
    61  func path(filename string) string {
    62  	if filepath.IsAbs(filename) {
    63  		return filename
    64  	}
    65  	return filepath.Join("testdata", filename)
    66  }
    67  
    68  func compare(actual []byte, filename string) error {
    69  	if err := update(filename, actual); err != nil {
    70  		return err
    71  	}
    72  
    73  	expected, err := ioutil.ReadFile(filename)
    74  	if err != nil {
    75  		return errors.Wrapf(err, "unable to read testdata %s", filename)
    76  	}
    77  	if !bytes.Equal(expected, actual) {
    78  		return errors.Errorf("does not match golden file %s\n\nWANT:\n'%s'\n\nGOT:\n'%s'\n", filename, expected, actual)
    79  	}
    80  	return nil
    81  }
    82  
    83  func update(filename string, in []byte) error {
    84  	if !*updateGolden {
    85  		return nil
    86  	}
    87  	return ioutil.WriteFile(filename, normalize(in), 0666)
    88  }
    89  
    90  func normalize(in []byte) []byte {
    91  	return bytes.Replace(in, []byte("\r\n"), []byte("\n"), -1)
    92  }