github.com/Mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/tests/gm/gm.go (about)

     1  /*
     2  Copyright 2017 Mirantis
     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 gm
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  // VerifyNamed generates a file name based on current test name and
    24  // the 'name' argument and compares its contents in git index (i.e.
    25  // staged or committed if the changes are staged for the file) to
    26  // that of of 'data' argument. The test will fail if the data
    27  // differs. In this case the target file is updated and the user
    28  // must stage or commit the changes to make the test pass.
    29  // If data is string or []byte, it's compared directly to the contents
    30  // of the file (the data are supposed to be human-readable and
    31  // easily diffable).
    32  // If data implements Verifier interface, the comparison is done
    33  // by invoking it's Verify method on the contents of the file.
    34  // Otherwise, the comparison is done based on JSON representation
    35  // of the data ignoring any changes in JSON formatting and any
    36  // changes introduced by the encoding/json marshalling mechanism.
    37  // If data implements Verifier interface, the updated contents of the
    38  // data file is generated using its Marshal() method.
    39  // The suffix of the data file name is generated based on the
    40  // data argument, too. For text content, ".out.txt" suffix is used.
    41  // For json content, the suffix is ".out.json". For Verifier type,
    42  // the suffix is ".out" concatenated with the value of the Suffix()
    43  // method.
    44  func VerifyNamed(t *testing.T, name string, data interface{}) {
    45  	testName := t.Name()
    46  	if name != "" {
    47  		testName += "__" + name
    48  	}
    49  	filename, err := GetFilenameForTest(testName, data)
    50  	if err != nil {
    51  		t.Errorf("can't get filename for test %q: %v", testName, err)
    52  		return
    53  	}
    54  	hasDiff, err := DataFileDiffers(filename, data)
    55  	if err != nil {
    56  		t.Errorf("failed to diff data file %q: %v", filename, err)
    57  		return
    58  	}
    59  	if hasDiff {
    60  		if err := WriteDataFile(filename, data); err != nil {
    61  			t.Errorf("failed to write file %q: %v", filename, data)
    62  		}
    63  	}
    64  
    65  	gitDiff, err := GitDiff(filename)
    66  	switch {
    67  	case err != nil:
    68  		t.Errorf("git diff failed on %q: %v", filename, err)
    69  	case gitDiff == "":
    70  		// no difference
    71  	default:
    72  		t.Errorf("got difference for %q (%q):\n%s", testName, filename, gitDiff)
    73  	}
    74  }
    75  
    76  // Verify invokes VerifyNamed with empty 'name' argument
    77  func Verify(t *testing.T, data interface{}) {
    78  	VerifyNamed(t, "", data)
    79  }