Skip to content

Commit

Permalink
use json as wire format for snapshot for better cross platform support
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Jan 4, 2024
1 parent 5aa61cb commit aa23f7b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 53 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,9 +21,14 @@ jobs:
- uses: actions/checkout@v3

- name: lint
if: matrix.os == 'ubuntu-latest'
run: go run github.com/ysmood/golangci-lint@latest

- name: test
env:
TERM: xterm-256color
run: go test -race -coverprofile=coverage.out ./... && go run ./cmd/check-cov
run: go test -coverprofile="coverage.out" ./...

- name: coverage
if: matrix.os == 'ubuntu-latest'
run: go run ./cmd/check-cov
1 change: 0 additions & 1 deletion .got/snapshots/TestSnapshots/a.gop

This file was deleted.

1 change: 0 additions & 1 deletion .got/snapshots/TestSnapshots/b.gop

This file was deleted.

3 changes: 0 additions & 3 deletions .got/snapshots/TestSnapshots/c.gop

This file was deleted.

3 changes: 0 additions & 3 deletions .got/snapshots/TestSnapshots/d.json

This file was deleted.

44 changes: 6 additions & 38 deletions snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import (
"path/filepath"
"regexp"
"strings"

"github.com/ysmood/gop"
)

const snapshotGopExt = ".gop"
const snapshotJSONExt = ".json"

type snapshot struct {
Expand All @@ -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})
}
Expand All @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -31,7 +30,7 @@ func TestSnapshots(t *testing.T) {
gm.Snapshot("a", "no")
m.check(`"no" ⦗not ==⦘ "ok"`)

gm.Snapshot("a", "no\nno")
gm.Snapshot("a", map[int]int{1: 2})
g.Has(m.msg, "diff chunk")
m.reset()

Expand All @@ -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)
Expand All @@ -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`))
Expand All @@ -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`))
Expand Down

0 comments on commit aa23f7b

Please sign in to comment.