sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/testutil/fixtures.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes 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 testutil
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/google/go-cmp/cmp"
    26  	"sigs.k8s.io/yaml"
    27  )
    28  
    29  // CompareWithFixtureDir will compare all files in a directory with a corresponding test fixture directory.
    30  func CompareWithFixtureDir(t *testing.T, golden, output string) {
    31  	if walkErr := filepath.Walk(output, func(path string, info os.FileInfo, err error) error {
    32  		if err != nil {
    33  			return err
    34  		}
    35  		if info.IsDir() || info.Mode()&os.ModeSymlink == os.ModeSymlink {
    36  			return nil
    37  		}
    38  		relPath, err := filepath.Rel(output, path)
    39  		if err != nil {
    40  			// this should not happen
    41  			t.Errorf("bug: could not compute relative path in fixture dir: %v", err)
    42  		}
    43  		CompareWithFixture(t, filepath.Join(golden, relPath), path)
    44  		return nil
    45  	}); walkErr != nil {
    46  		t.Errorf("failed to walk fixture tree for comparison: %v", walkErr)
    47  	}
    48  }
    49  
    50  // CompareWithFixture will compare output files with a test fixture and allows to automatically update them
    51  // by setting the UPDATE env var. The output and golden paths are relative to the test's directory.
    52  func CompareWithFixture(t *testing.T, golden, output string) {
    53  	actual, err := os.ReadFile(output)
    54  	if err != nil {
    55  		t.Fatalf("failed to read testdata file: %v", err)
    56  	}
    57  	if os.Getenv("UPDATE") != "" {
    58  		if err := os.MkdirAll(filepath.Dir(golden), 0755); err != nil {
    59  			t.Fatalf("failed to create fixture directory: %v", err)
    60  		}
    61  		if err := os.WriteFile(golden, actual, 0644); err != nil {
    62  			t.Fatalf("failed to write updated fixture: %v", err)
    63  		}
    64  	}
    65  	expected, err := os.ReadFile(golden)
    66  	if err != nil {
    67  		t.Fatalf("failed to read testdata file: %v", err)
    68  	}
    69  
    70  	if diff := cmp.Diff(string(expected), string(actual)); diff != "" {
    71  		t.Errorf("got diff between expected and actual result: \n%s\n\nIf this is expected, re-run the test with `UPDATE=true go test ./...` to update the fixtures.", diff)
    72  	}
    73  }
    74  
    75  func sanitizeFilename(s string) string {
    76  	result := strings.Builder{}
    77  	for _, r := range s {
    78  		if (r >= 'a' && r < 'z') || (r >= 'A' && r < 'Z') || r == '_' || r == '.' || (r >= '0' && r <= '9') {
    79  			// The thing is documented as returning a nil error so lets just drop it
    80  			_, _ = result.WriteRune(r)
    81  			continue
    82  		}
    83  		if !strings.HasSuffix(result.String(), "_") {
    84  			result.WriteRune('_')
    85  		}
    86  	}
    87  	return "zz_fixture_" + result.String()
    88  }
    89  
    90  // CompareWithSerializedFixture compares an object that can be marshalled with a golden file containing the
    91  // serialized version of the data.
    92  func CompareWithSerializedFixture(t *testing.T, data interface{}) {
    93  	t.Helper()
    94  	tempFile, err := os.CreateTemp("", "tmp-serialized")
    95  	if err != nil {
    96  		t.Fatalf("could not create temporary file to hold serialized data: %v", err)
    97  	}
    98  	defer func() {
    99  		if err := os.Remove(tempFile.Name()); err != nil {
   100  			t.Errorf("could not remove temporary file: %v", err)
   101  		}
   102  	}()
   103  
   104  	serialized, err := yaml.Marshal(data)
   105  	if err != nil {
   106  		t.Fatalf("failed to yaml marshal data of type %T: %v", data, err)
   107  	}
   108  	if _, err := tempFile.Write(serialized); err != nil {
   109  		t.Fatalf("could not write serialized data: %v", err)
   110  	}
   111  	if err := tempFile.Close(); err != nil {
   112  		t.Errorf("could not close temporary file: %v", err)
   113  	}
   114  
   115  	goldenFile, err := filepath.Abs(filepath.Join("testdata", sanitizeFilename(t.Name())+".yaml"))
   116  	if err != nil {
   117  		t.Fatalf("could not determine path to golden file: %v", err)
   118  	}
   119  	CompareWithFixture(t, goldenFile, tempFile.Name())
   120  }