|
| 1 | +package assert_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "go/parser" |
| 5 | + "go/token" |
| 6 | + "io/ioutil" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "gotest.tools/v3/assert" |
| 12 | + "gotest.tools/v3/internal/source" |
| 13 | +) |
| 14 | + |
| 15 | +func TestEqual_WithGoldenUpdate(t *testing.T) { |
| 16 | + t.Run("assert failed with -update=false", func(t *testing.T) { |
| 17 | + ft := &fakeTestingT{} |
| 18 | + actual := `not this value` |
| 19 | + assert.Equal(ft, actual, expectedOne) |
| 20 | + assert.Assert(t, ft.failNowed) |
| 21 | + }) |
| 22 | + |
| 23 | + t.Run("var is updated when -update=true", func(t *testing.T) { |
| 24 | + patchUpdate(t) |
| 25 | + t.Cleanup(func() { |
| 26 | + resetVariable(t, "expectedOne", "") |
| 27 | + }) |
| 28 | + |
| 29 | + actual := `this is the |
| 30 | +actual value |
| 31 | +that we are testing |
| 32 | +` |
| 33 | + assert.Equal(t, actual, expectedOne) |
| 34 | + |
| 35 | + raw, err := ioutil.ReadFile(fileName(t)) |
| 36 | + assert.NilError(t, err) |
| 37 | + |
| 38 | + expected := "var expectedOne = `this is the\nactual value\nthat we are testing\n`" |
| 39 | + assert.Assert(t, strings.Contains(string(raw), expected), "actual=%v", string(raw)) |
| 40 | + }) |
| 41 | + |
| 42 | + t.Run("const is updated when -update=true", func(t *testing.T) { |
| 43 | + patchUpdate(t) |
| 44 | + t.Cleanup(func() { |
| 45 | + resetVariable(t, "expectedTwo", "") |
| 46 | + }) |
| 47 | + |
| 48 | + actual := `this is the new |
| 49 | +expected value |
| 50 | +` |
| 51 | + assert.Equal(t, actual, expectedTwo) |
| 52 | + |
| 53 | + raw, err := ioutil.ReadFile(fileName(t)) |
| 54 | + assert.NilError(t, err) |
| 55 | + |
| 56 | + expected := "const expectedTwo = `this is the new\nexpected value\n`" |
| 57 | + assert.Assert(t, strings.Contains(string(raw), expected), "actual=%v", string(raw)) |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +// expectedOne is updated by running the tests with -update |
| 62 | +var expectedOne = `` |
| 63 | + |
| 64 | +// expectedTwo is updated by running the tests with -update |
| 65 | +const expectedTwo = `` |
| 66 | + |
| 67 | +func patchUpdate(t *testing.T) { |
| 68 | + source.Update = true |
| 69 | + t.Cleanup(func() { |
| 70 | + source.Update = false |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +func fileName(t *testing.T) string { |
| 75 | + t.Helper() |
| 76 | + _, filename, _, ok := runtime.Caller(1) |
| 77 | + assert.Assert(t, ok, "failed to get call stack") |
| 78 | + return filename |
| 79 | +} |
| 80 | + |
| 81 | +func resetVariable(t *testing.T, varName string, value string) { |
| 82 | + t.Helper() |
| 83 | + _, filename, _, ok := runtime.Caller(1) |
| 84 | + assert.Assert(t, ok, "failed to get call stack") |
| 85 | + |
| 86 | + fileset := token.NewFileSet() |
| 87 | + astFile, err := parser.ParseFile(fileset, filename, nil, parser.AllErrors|parser.ParseComments) |
| 88 | + assert.NilError(t, err) |
| 89 | + |
| 90 | + err = source.UpdateVariable(filename, fileset, astFile, varName, value) |
| 91 | + assert.NilError(t, err, "failed to reset file") |
| 92 | +} |
| 93 | + |
| 94 | +type fakeTestingT struct { |
| 95 | + failNowed bool |
| 96 | + failed bool |
| 97 | + msgs []string |
| 98 | +} |
| 99 | + |
| 100 | +func (f *fakeTestingT) FailNow() { |
| 101 | + f.failNowed = true |
| 102 | +} |
| 103 | + |
| 104 | +func (f *fakeTestingT) Fail() { |
| 105 | + f.failed = true |
| 106 | +} |
| 107 | + |
| 108 | +func (f *fakeTestingT) Log(args ...interface{}) { |
| 109 | + f.msgs = append(f.msgs, args[0].(string)) |
| 110 | +} |
| 111 | + |
| 112 | +func (f *fakeTestingT) Helper() {} |
0 commit comments