From 5bfa17186b7e4b1d69786a01152322d3277421a4 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Sun, 29 May 2022 16:53:20 -0400 Subject: [PATCH] assert: tests for golden variables --- assert/assert_ext_test.go | 50 +++++++++++++++++++++++++++++---------- internal/source/update.go | 22 ++++++++++++++--- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/assert/assert_ext_test.go b/assert/assert_ext_test.go index 88b0421..2ef1725 100644 --- a/assert/assert_ext_test.go +++ b/assert/assert_ext_test.go @@ -1,7 +1,11 @@ package assert_test import ( - "fmt" + "go/parser" + "go/token" + "io/ioutil" + "runtime" + "strings" "testing" "gotest.tools/v3/assert" @@ -9,7 +13,7 @@ import ( ) 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) @@ -18,23 +22,25 @@ func TestEqual_WithGoldenUpdate(t *testing.T) { t.Run("value 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) + + raw, err := ioutil.ReadFile(fileName(t)) + assert.NilError(t, err) - // reset - fmt.Println("WHHHHHHHHHHY") - assert.Equal(ft, "\n\n\n", 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)) }) } -var expectedOne = ` - - -` +var expectedOne = `` func patchUpdate(t *testing.T) { source.Update = true @@ -43,6 +49,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 diff --git a/internal/source/update.go b/internal/source/update.go index 1b669f6..02e2347 100644 --- a/internal/source/update.go +++ b/internal/source/update.go @@ -68,6 +68,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 @@ -89,9 +106,8 @@ func UpdateExpectedValue(stackIndex int, x, y interface{}) error { // 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})