Skip to content

Commit 82e8930

Browse files
committed
assert: tests for golden variables
1 parent a0e2cd3 commit 82e8930

File tree

3 files changed

+80
-21
lines changed

3 files changed

+80
-21
lines changed

assert/assert_ext_test.go

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,68 @@
11
package assert_test
22

33
import (
4-
"fmt"
4+
"go/parser"
5+
"go/token"
6+
"io/ioutil"
7+
"runtime"
8+
"strings"
59
"testing"
610

711
"gotest.tools/v3/assert"
812
"gotest.tools/v3/internal/source"
913
)
1014

1115
func TestEqual_WithGoldenUpdate(t *testing.T) {
12-
t.Run("assert failed with update=false", func(t *testing.T) {
16+
t.Run("assert failed with -update=false", func(t *testing.T) {
1317
ft := &fakeTestingT{}
1418
actual := `not this value`
1519
assert.Equal(ft, actual, expectedOne)
1620
assert.Assert(t, ft.failNowed)
1721
})
1822

19-
t.Run("value is updated when -update=true", func(t *testing.T) {
23+
t.Run("var is updated when -update=true", func(t *testing.T) {
2024
patchUpdate(t)
21-
ft := &fakeTestingT{}
25+
t.Cleanup(func() {
26+
resetVariable(t, "expectedOne", "")
27+
})
2228

2329
actual := `this is the
2430
actual value
25-
that we are testing against`
26-
assert.Equal(ft, actual, expectedOne)
31+
that we are testing
32+
`
33+
assert.Equal(t, actual, expectedOne)
2734

28-
// reset
29-
fmt.Println("WHHHHHHHHHHY")
30-
assert.Equal(ft, "\n\n\n", expectedOne)
31-
})
32-
}
35+
raw, err := ioutil.ReadFile(fileName(t))
36+
assert.NilError(t, err)
3337

34-
var expectedOne = `
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+
})
3541

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+
})
3647

48+
actual := `this is the new
49+
expected value
3750
`
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 = ``
3866

3967
func patchUpdate(t *testing.T) {
4068
source.Update = true
@@ -43,6 +71,26 @@ func patchUpdate(t *testing.T) {
4371
})
4472
}
4573

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+
4694
type fakeTestingT struct {
4795
failNowed bool
4896
failed bool

internal/source/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func scanToLine(fileset *token.FileSet, node ast.Node, lineNum int) ast.Node {
7575
return matchedNode
7676
}
7777

78-
func getCallExprArgs(fileset *token.FileSet, astFile *ast.File, line int) ([]ast.Expr, error) {
78+
func getCallExprArgs(fileset *token.FileSet, astFile ast.Node, line int) ([]ast.Expr, error) {
7979
node, err := getNodeAtLine(fileset, astFile, line)
8080
switch {
8181
case err != nil:

internal/source/update.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ func UpdateExpectedValue(stackIndex int, x, y interface{}) error {
4343
return fmt.Errorf("failed to parse source file %s: %w", filename, err)
4444
}
4545

46-
debug("before modification: %v", debugFormatNode{astFile})
47-
4846
expr, err := getCallExprArgs(fileset, astFile, line)
4947
if err != nil {
5048
return fmt.Errorf("call from %s:%d: %w", filename, line, err)
@@ -68,6 +66,23 @@ func UpdateExpectedValue(stackIndex int, x, y interface{}) error {
6866
value = y
6967
}
7068

69+
strValue, ok := value.(string)
70+
if !ok {
71+
debug("value must be type string, got %T", value)
72+
return ErrNotFound
73+
}
74+
return UpdateVariable(filename, fileset, astFile, varName, strValue)
75+
}
76+
77+
// UpdateVariable writes to filename the contents of astFile with the value of
78+
// the variable updated to value.
79+
func UpdateVariable(
80+
filename string,
81+
fileset *token.FileSet,
82+
astFile *ast.File,
83+
varName string,
84+
value string,
85+
) error {
7186
obj := astFile.Scope.Objects[varName]
7287
if obj == nil {
7388
return ErrNotFound
@@ -87,15 +102,11 @@ func UpdateExpectedValue(stackIndex int, x, y interface{}) error {
87102
return ErrNotFound
88103
}
89104

90-
// TODO: allow a function to wrap the string literal
91105
spec.Values[0] = &ast.BasicLit{
92-
Kind: token.STRING,
93-
// TODO: safer
94-
Value: "`" + value.(string) + "`",
106+
Kind: token.STRING,
107+
Value: "`" + value + "`",
95108
}
96109

97-
debug("after modification: %v", debugFormatNode{astFile})
98-
99110
var buf bytes.Buffer
100111
if err := format.Node(&buf, fileset, astFile); err != nil {
101112
return fmt.Errorf("failed to format file after update: %w", err)

0 commit comments

Comments
 (0)