...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/testutils/tesutils.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/testutils

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testutils
    16  
    17  import (
    18  	"flag"
    19  	"io/ioutil"
    20  	"testing"
    21  
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  )
    26  
    27  var update = flag.Bool("update", false, "update .golden files")
    28  
    29  func VerifyContentsMatch(t *testing.T, actualBytes []byte, expectedFilePath string) {
    30  	if *update {
    31  		ioutil.WriteFile(expectedFilePath, actualBytes, 0644)
    32  	}
    33  	expectedBytes, err := ioutil.ReadFile(expectedFilePath)
    34  	if err != nil {
    35  		t.Fatalf("error reading file '%v': %v", expectedFilePath, err)
    36  	}
    37  	expectedBytes = []byte(test.TrimLicenseHeaderFromYaml(string(expectedBytes)))
    38  	diff := cmp.Diff(actualBytes, expectedBytes)
    39  	if diff != "" {
    40  		t.Logf("actual value:\n%v", string(actualBytes))
    41  		t.Logf("expected value:\n%v", string(expectedBytes))
    42  		t.Logf("actual value does not match expected value in '%v'", expectedFilePath)
    43  		t.Fatalf("diff: %v", diff)
    44  	}
    45  }
    46  

View as plain text