Skip to content

Commit

Permalink
Tests: Test TESTFILE_UPDATE and general improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Oct 18, 2023
1 parent a91e47a commit d8fa6f5
Showing 1 changed file with 61 additions and 12 deletions.
73 changes: 61 additions & 12 deletions testfile/testfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,46 @@ package testfile_test
import (
"math"
"path/filepath"
"strings"
"testing"

"github.com/carlmjohnson/be"
"github.com/carlmjohnson/be/testfile"
)

func TestRunEqualJSON(t *testing.T) {
testfile.Run(t, "testdata/*.json", func(t *testing.T, path string) {
testfile.EqualJSON(t, path, struct {
Data any `json:"data"`
func runPaths(t *testing.T, inpath string) []string {
var paths []string
testfile.Run(t, inpath, func(t *testing.T, path string) {
paths = append(paths, path)
})
return paths
}

func TestRun(t *testing.T) {
cases := []struct {
InPath string
WantFound string
}{
{"testdata/*.glorp", ""},
{"testdata/*.json", "testdata/example.json"},
}
for _, tc := range cases {
got := runPaths(t, tc.InPath)
be.Equal(t, tc.WantFound, strings.Join(got, ","))
}
}

func TestEqualJSON(t *testing.T) {
testfile.EqualJSON(t, "testdata/example.json", struct {
Data any `json:"data"`
}{
Data: []struct {
Field string `json:"field"`
Value int `json:"value"`
}{
Data: []struct {
Field string `json:"field"`
Value int `json:"value"`
}{
{"foo", 1},
{"bar", 2},
},
})
{"foo", 1},
{"bar", 2},
},
})
}

Expand Down Expand Up @@ -80,3 +101,31 @@ func TestCases(t *testing.T) {
})
}
}

func TestSetEnv(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "example.txt")
failedPath := filepath.Join(dir, "-failed-example.txt")

// If testfile.Equal fails
testfile.Write(t, path, "1")
be.False(t, runTest(func(t *testing.T) {
testfile.Equal(t, path, "2")
}))
// it writes a -failed file.
testfile.Equal(t, failedPath, "2")
// If testfile.Equal succeeds,
testfile.Equal(t, path, "1")
// it erases the -failed file.
testfile.Equal(t, failedPath, "")
// If TESTFILE_UPDATE is set,
t.Setenv("TESTFILE_UPDATE", "ON")
// the test still fails
be.False(t, runTest(func(t *testing.T) {
testfile.Equal(t, path, "3")
}))
// but it doesn't write a failed path,
testfile.Equal(t, failedPath, "")
// and does update the file.
testfile.Equal(t, path, "3")
}

0 comments on commit d8fa6f5

Please sign in to comment.