From 1be208578a5827a30709fb57dba41b9b7bf23053 Mon Sep 17 00:00:00 2001 From: Yad Smood Date: Thu, 4 Jan 2024 11:57:29 +0800 Subject: [PATCH] use json as wire format for snapshot for better cross platform support --- .github/workflows/test.yml | 7 ++++- .got/snapshots/TestSnapshots/a.gop | 1 - .got/snapshots/TestSnapshots/b.gop | 1 - .got/snapshots/TestSnapshots/c.gop | 3 -- .got/snapshots/TestSnapshots/d.json | 3 -- snapshots.go | 44 ++++------------------------- snapshots_test.go | 9 +++--- 7 files changed, 16 insertions(+), 52 deletions(-) delete mode 100644 .got/snapshots/TestSnapshots/a.gop delete mode 100644 .got/snapshots/TestSnapshots/b.gop delete mode 100644 .got/snapshots/TestSnapshots/c.gop delete mode 100644 .got/snapshots/TestSnapshots/d.json diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ca1bad9..39c1ebd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,7 +7,11 @@ env: jobs: test: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: [macos-latest, windows-latest, ubuntu-latest] steps: - uses: actions/setup-go@v4 @@ -18,6 +22,7 @@ jobs: - name: lint run: go run github.com/ysmood/golangci-lint@latest + if: matrix.os == 'ubuntu-latest' - name: test env: diff --git a/.got/snapshots/TestSnapshots/a.gop b/.got/snapshots/TestSnapshots/a.gop deleted file mode 100644 index 1fe57ef..0000000 --- a/.got/snapshots/TestSnapshots/a.gop +++ /dev/null @@ -1 +0,0 @@ -"ok" \ No newline at end of file diff --git a/.got/snapshots/TestSnapshots/b.gop b/.got/snapshots/TestSnapshots/b.gop deleted file mode 100644 index 56a6051..0000000 --- a/.got/snapshots/TestSnapshots/b.gop +++ /dev/null @@ -1 +0,0 @@ -1 \ No newline at end of file diff --git a/.got/snapshots/TestSnapshots/c.gop b/.got/snapshots/TestSnapshots/c.gop deleted file mode 100644 index a8973ca..0000000 --- a/.got/snapshots/TestSnapshots/c.gop +++ /dev/null @@ -1,3 +0,0 @@ -got_test.C{ - Val: 10, -} \ No newline at end of file diff --git a/.got/snapshots/TestSnapshots/d.json b/.got/snapshots/TestSnapshots/d.json deleted file mode 100644 index 3872a8b..0000000 --- a/.got/snapshots/TestSnapshots/d.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "Val": 20 -} \ No newline at end of file diff --git a/snapshots.go b/snapshots.go index 78fab3b..1764ffb 100644 --- a/snapshots.go +++ b/snapshots.go @@ -6,11 +6,8 @@ import ( "path/filepath" "regexp" "strings" - - "github.com/ysmood/gop" ) -const snapshotGopExt = ".gop" const snapshotJSONExt = ".json" type snapshot struct { @@ -23,14 +20,9 @@ func (g G) snapshotsDir() string { } func (g G) loadSnapshots() { - paths, err := filepath.Glob(filepath.Join(g.snapshotsDir(), "*"+snapshotGopExt)) - g.E(err) - - jsonPaths, err := filepath.Glob(filepath.Join(g.snapshotsDir(), "*"+snapshotJSONExt)) + paths, err := filepath.Glob(filepath.Join(g.snapshotsDir(), "*"+snapshotJSONExt)) g.E(err) - paths = append(paths, jsonPaths...) - for _, path := range paths { g.snapshots.Store(path, snapshot{g.Read(path).String(), false}) } @@ -56,39 +48,15 @@ func (g G) loadSnapshots() { // To update the snapshot, just change the name of the snapshot or remove the corresponding snapshot file. // It will auto-remove the unused snapshot files after the test. // The snapshot files should be version controlled. -// The format of the snapshot file is the output of [gop.Plain]. -func (g G) Snapshot(name string, x interface{}) { - g.Helper() - g.snapshot(name, x, false) -} - -// SnapshotJSON is similar to [G.Snapshot], but it will convert x to JSON string before comparing. // The format of the snapshot file is json. -func (g G) SnapshotJSON(name string, x interface{}) { - g.Helper() - g.snapshot(name, x, true) -} - -func (g G) snapshot(name string, x interface{}, jsonType bool) { +func (g G) Snapshot(name string, x interface{}) { g.Helper() - var ext string - if jsonType { - ext = snapshotJSONExt - } else { - ext = snapshotGopExt - } - - path := filepath.Join(g.snapshotsDir(), escapeFileName(name)+ext) + path := filepath.Join(g.snapshotsDir(), escapeFileName(name)+snapshotJSONExt) - var xs string - if jsonType { - b, err := json.MarshalIndent(x, "", " ") - g.E(err) - xs = string(b) - } else { - xs = gop.Plain(x) - } + b, err := json.MarshalIndent(x, "", " ") + g.E(err) + xs := string(b) if data, ok := g.snapshots.Load(path); ok { s := data.(snapshot) diff --git a/snapshots_test.go b/snapshots_test.go index f763dac..2de57d9 100644 --- a/snapshots_test.go +++ b/snapshots_test.go @@ -19,7 +19,6 @@ func TestSnapshots(t *testing.T) { g.Snapshot("a", "ok") g.Snapshot("b", 1) g.Snapshot("c", C{10}) - g.SnapshotJSON("d", C{20}) g.Run("sub", func(g got.G) { g.Snapshot("d", "ok") @@ -32,7 +31,7 @@ func TestSnapshots(t *testing.T) { m.check(`"no" ⦗not ==⦘ "ok"`) gm.Snapshot("a", "no\nno") - g.Has(m.msg, "diff chunk") + gm.Snapshot("a", `{"a": 1}`) m.reset() gm.ErrorHandler = got.NewDefaultAssertionError(gop.ThemeNone, nil) @@ -41,7 +40,7 @@ func TestSnapshots(t *testing.T) { } func TestSnapshotsCreate(t *testing.T) { - path := filepath.FromSlash(".got/snapshots/TestSnapshotsCreate/a.gop") + path := filepath.FromSlash(".got/snapshots/TestSnapshotsCreate/a.json") err := os.RemoveAll(path) if err != nil { panic(err) @@ -57,7 +56,7 @@ func TestSnapshotsCreate(t *testing.T) { } func TestSnapshotsNotUsed(t *testing.T) { - path := filepath.FromSlash(".got/snapshots/TestSnapshotsNotUsed/a.gop") + path := filepath.FromSlash(".got/snapshots/TestSnapshotsNotUsed/a.json") g := got.T(t) g.WriteFile(path, []byte(`1`)) @@ -70,7 +69,7 @@ func TestSnapshotsNotUsed(t *testing.T) { } func TestSnapshotsNotUsedWhenFailure(t *testing.T) { - path := filepath.FromSlash(".got/snapshots/TestSnapshotsNotUsedWhenFailure/a.gop") + path := filepath.FromSlash(".got/snapshots/TestSnapshotsNotUsedWhenFailure/a.json") g := got.T(t) g.WriteFile(path, []byte(`1`))