...

Source file src/oss.terrastruct.com/util-go/diff/diff_test.go

Documentation: oss.terrastruct.com/util-go/diff

     1  package diff_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"oss.terrastruct.com/util-go/assert"
     9  	"oss.terrastruct.com/util-go/diff"
    10  )
    11  
    12  func TestTestData(t *testing.T) {
    13  	t.Run("TESTDATA_ACCEPT", testTestDataAccept)
    14  
    15  	os.Unsetenv("TESTDATA_ACCEPT")
    16  	os.Unsetenv("TA")
    17  
    18  	m1 := map[string]interface{}{
    19  		"one":   1,
    20  		"two":   2,
    21  		"three": 3,
    22  		"four":  4,
    23  		"five": map[string]interface{}{
    24  			"yes": "yes",
    25  			"no":  "yes",
    26  			"five": map[string]interface{}{
    27  				"yes": "no",
    28  				"no":  "yes",
    29  			},
    30  		},
    31  	}
    32  
    33  	err := os.Remove("testdata/TestTestData.exp.json")
    34  	if err != nil && !os.IsNotExist(err) {
    35  		t.Fatalf("unexpected error: %v", err)
    36  	}
    37  
    38  	err = diff.TestdataJSON(filepath.Join("testdata", t.Name()), m1)
    39  	assert.Error(t, err)
    40  	exp := `diff (rerun with $TESTDATA_ACCEPT=1 or $TA=1 to accept):
    41  --- /dev/null
    42  +++ b/testdata/TestTestData.got.json
    43  @@ -0,0 +1,14 @@
    44  +{
    45  +  "five": {
    46  +    "five": {
    47  +      "no": "yes",
    48  +      "yes": "no"
    49  +    },
    50  +    "no": "yes",
    51  +    "yes": "yes"
    52  +  },
    53  +  "four": 4,
    54  +  "one": 1,
    55  +  "three": 3,
    56  +  "two": 2
    57  +}`
    58  	got := err.Error()
    59  	ds, err := diff.Strings(exp, got)
    60  	if err != nil {
    61  		t.Fatalf("unable to generate exp diff: %v", err)
    62  	}
    63  	if ds != "" {
    64  		t.Fatalf("expected no diff:\n%s", ds)
    65  	}
    66  	err = diff.Runes(exp, got)
    67  	if err != nil {
    68  		t.Fatalf("expected no rune diff: %v", err)
    69  	}
    70  
    71  	err = os.Rename("testdata/TestTestData.got.json", "testdata/TestTestData.exp.json")
    72  	assert.Success(t, err)
    73  
    74  	m1["five"].(map[string]interface{})["five"].(map[string]interface{})["no"] = "ys"
    75  
    76  	err = diff.TestdataJSON(filepath.Join("testdata", t.Name()), m1)
    77  	if err == nil {
    78  		t.Fatalf("expected err: %#v", err)
    79  	}
    80  	exp = `diff (rerun with $TESTDATA_ACCEPT=1 or $TA=1 to accept):
    81  --- a/testdata/TestTestData.exp.json
    82  +++ b/testdata/TestTestData.got.json
    83  @@ -1,7 +1,7 @@
    84   {
    85     "five": {
    86       "five": {
    87  -      "no": "yes",
    88  +      "no": "ys",
    89         "yes": "no"
    90       },
    91       "no": "yes",`
    92  	got = err.Error()
    93  	ds, err = diff.Strings(exp, got)
    94  	assert.Success(t, err)
    95  	if ds != "" {
    96  		t.Fatalf("expected no diff:\n%s", ds)
    97  	}
    98  
    99  	exp += "a"
   100  	ds, err = diff.Strings(exp, got)
   101  	assert.Success(t, err)
   102  	if ds == "" {
   103  		t.Fatalf("expected incorrect diff:\n%s", ds)
   104  	}
   105  	err = diff.Runes(exp, got)
   106  	assert.Error(t, err)
   107  }
   108  
   109  func testTestDataAccept(t *testing.T) {
   110  	m1 := map[string]interface{}{
   111  		"one": 1,
   112  	}
   113  
   114  	os.Setenv("TESTDATA_ACCEPT", "1")
   115  	os.Setenv("TA", "1")
   116  	err := diff.TestdataJSON(filepath.Join("testdata", t.Name()), m1)
   117  	assert.Success(t, err)
   118  
   119  	m1["one"] = 2
   120  
   121  	os.Setenv("TESTDATA_ACCEPT", "")
   122  	os.Setenv("TA", "")
   123  	err = diff.TestdataJSON(filepath.Join("testdata", t.Name()), m1)
   124  	assert.Error(t, err)
   125  	exp := `diff (rerun with $TESTDATA_ACCEPT=1 or $TA=1 to accept):
   126  --- a/testdata/TestTestData/TESTDATA_ACCEPT.exp.json
   127  +++ b/testdata/TestTestData/TESTDATA_ACCEPT.got.json
   128  @@ -1,3 +1,3 @@
   129   {
   130  -  "one": 1
   131  +  "one": 2
   132   }`
   133  	ds, err := diff.Strings(exp, err.Error())
   134  	assert.Success(t, err)
   135  	if ds != "" {
   136  		t.Fatalf("expected no diff:\n%s", ds)
   137  	}
   138  
   139  	err = os.Remove(filepath.Join("testdata", t.Name()) + ".got.json")
   140  	assert.Success(t, err)
   141  }
   142  

View as plain text