Skip to content

Commit

Permalink
assert: tests for golden variables
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed May 30, 2022
1 parent a0e2cd3 commit 82e8930
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 21 deletions.
72 changes: 60 additions & 12 deletions assert/assert_ext_test.go
@@ -1,40 +1,68 @@
package assert_test

import (
"fmt"
"go/parser"
"go/token"
"io/ioutil"
"runtime"
"strings"
"testing"

"gotest.tools/v3/assert"
"gotest.tools/v3/internal/source"
)

func TestEqual_WithGoldenUpdate(t *testing.T) {
t.Run("assert failed with update=false", func(t *testing.T) {
t.Run("assert failed with -update=false", func(t *testing.T) {
ft := &fakeTestingT{}
actual := `not this value`
assert.Equal(ft, actual, expectedOne)
assert.Assert(t, ft.failNowed)
})

t.Run("value is updated when -update=true", func(t *testing.T) {
t.Run("var is updated when -update=true", func(t *testing.T) {
patchUpdate(t)
ft := &fakeTestingT{}
t.Cleanup(func() {
resetVariable(t, "expectedOne", "")
})

actual := `this is the
actual value
that we are testing against`
assert.Equal(ft, actual, expectedOne)
that we are testing
`
assert.Equal(t, actual, expectedOne)

// reset
fmt.Println("WHHHHHHHHHHY")
assert.Equal(ft, "\n\n\n", expectedOne)
})
}
raw, err := ioutil.ReadFile(fileName(t))
assert.NilError(t, err)

var expectedOne = `
expected := "var expectedOne = `this is the\nactual value\nthat we are testing\n`"
assert.Assert(t, strings.Contains(string(raw), expected), "actual=%v", string(raw))
})

t.Run("const is updated when -update=true", func(t *testing.T) {
patchUpdate(t)
t.Cleanup(func() {
resetVariable(t, "expectedTwo", "")
})

actual := `this is the new
expected value
`
assert.Equal(t, actual, expectedTwo)

raw, err := ioutil.ReadFile(fileName(t))
assert.NilError(t, err)

expected := "const expectedTwo = `this is the new\nexpected value\n`"
assert.Assert(t, strings.Contains(string(raw), expected), "actual=%v", string(raw))
})
}

// expectedOne is updated by running the tests with -update
var expectedOne = ``

// expectedTwo is updated by running the tests with -update
const expectedTwo = ``

func patchUpdate(t *testing.T) {
source.Update = true
Expand All @@ -43,6 +71,26 @@ func patchUpdate(t *testing.T) {
})
}

func fileName(t *testing.T) string {
t.Helper()
_, filename, _, ok := runtime.Caller(1)
assert.Assert(t, ok, "failed to get call stack")
return filename
}

func resetVariable(t *testing.T, varName string, value string) {
t.Helper()
_, filename, _, ok := runtime.Caller(1)
assert.Assert(t, ok, "failed to get call stack")

fileset := token.NewFileSet()
astFile, err := parser.ParseFile(fileset, filename, nil, parser.AllErrors|parser.ParseComments)
assert.NilError(t, err)

err = source.UpdateVariable(filename, fileset, astFile, varName, value)
assert.NilError(t, err, "failed to reset file")
}

type fakeTestingT struct {
failNowed bool
failed bool
Expand Down
2 changes: 1 addition & 1 deletion internal/source/source.go
Expand Up @@ -75,7 +75,7 @@ func scanToLine(fileset *token.FileSet, node ast.Node, lineNum int) ast.Node {
return matchedNode
}

func getCallExprArgs(fileset *token.FileSet, astFile *ast.File, line int) ([]ast.Expr, error) {
func getCallExprArgs(fileset *token.FileSet, astFile ast.Node, line int) ([]ast.Expr, error) {
node, err := getNodeAtLine(fileset, astFile, line)
switch {
case err != nil:
Expand Down
27 changes: 19 additions & 8 deletions internal/source/update.go
Expand Up @@ -43,8 +43,6 @@ func UpdateExpectedValue(stackIndex int, x, y interface{}) error {
return fmt.Errorf("failed to parse source file %s: %w", filename, err)
}

debug("before modification: %v", debugFormatNode{astFile})

expr, err := getCallExprArgs(fileset, astFile, line)
if err != nil {
return fmt.Errorf("call from %s:%d: %w", filename, line, err)
Expand All @@ -68,6 +66,23 @@ func UpdateExpectedValue(stackIndex int, x, y interface{}) error {
value = y
}

strValue, ok := value.(string)
if !ok {
debug("value must be type string, got %T", value)
return ErrNotFound
}
return UpdateVariable(filename, fileset, astFile, varName, strValue)
}

// UpdateVariable writes to filename the contents of astFile with the value of
// the variable updated to value.
func UpdateVariable(
filename string,
fileset *token.FileSet,
astFile *ast.File,
varName string,
value string,
) error {
obj := astFile.Scope.Objects[varName]
if obj == nil {
return ErrNotFound
Expand All @@ -87,15 +102,11 @@ func UpdateExpectedValue(stackIndex int, x, y interface{}) error {
return ErrNotFound
}

// TODO: allow a function to wrap the string literal
spec.Values[0] = &ast.BasicLit{
Kind: token.STRING,
// TODO: safer
Value: "`" + value.(string) + "`",
Kind: token.STRING,
Value: "`" + value + "`",
}

debug("after modification: %v", debugFormatNode{astFile})

var buf bytes.Buffer
if err := format.Node(&buf, fileset, astFile); err != nil {
return fmt.Errorf("failed to format file after update: %w", err)
Expand Down

0 comments on commit 82e8930

Please sign in to comment.