-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathtoolsnaps_test.go
More file actions
124 lines (96 loc) · 3.75 KB
/
toolsnaps_test.go
File metadata and controls
124 lines (96 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package toolsnaps
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type dummyTool struct {
Name string `json:"name"`
Value int `json:"value"`
}
// withIsolatedWorkingDir creates a temp dir, changes to it, and restores the original working dir after the test.
func withIsolatedWorkingDir(t *testing.T) {
dir := t.TempDir()
origDir, err := os.Getwd()
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, os.Chdir(origDir)) })
require.NoError(t, os.Chdir(dir))
}
func TestSnapshotDoesNotExistNotInCI(t *testing.T) {
withIsolatedWorkingDir(t)
// Given we are not running in CI
t.Setenv("GITHUB_ACTIONS", "false") // This REALLY is required because the tests run in CI
tool := dummyTool{"foo", 42}
// When we test the snapshot
err := Test("dummy", tool)
// Then it should succeed and write the snapshot file
require.NoError(t, err)
path := filepath.Join("__toolsnaps__", "dummy.snap")
_, statErr := os.Stat(path)
assert.NoError(t, statErr, "expected snapshot file to be written")
}
func TestSnapshotDoesNotExistInCI(t *testing.T) {
withIsolatedWorkingDir(t)
// Given we are running in CI
t.Setenv("GITHUB_ACTIONS", "true")
tool := dummyTool{"foo", 42}
// When we test the snapshot
err := Test("dummy", tool)
// Then it should error about missing snapshot in CI
require.Error(t, err)
assert.Contains(t, err.Error(), "tool snapshot does not exist", "expected error about missing snapshot in CI")
}
func TestSnapshotExistsMatch(t *testing.T) {
withIsolatedWorkingDir(t)
// Given a matching snapshot file exists
tool := dummyTool{"foo", 42}
b, _ := json.MarshalIndent(tool, "", " ")
require.NoError(t, os.MkdirAll("__toolsnaps__", 0700))
require.NoError(t, os.WriteFile(filepath.Join("__toolsnaps__", "dummy.snap"), b, 0600))
// When we test the snapshot
err := Test("dummy", tool)
// Then it should succeed (no error)
require.NoError(t, err)
}
func TestSnapshotExistsDiff(t *testing.T) {
withIsolatedWorkingDir(t)
// Given a non-matching snapshot file exists
require.NoError(t, os.MkdirAll("__toolsnaps__", 0700))
require.NoError(t, os.WriteFile(filepath.Join("__toolsnaps__", "dummy.snap"), []byte(`{"name":"foo","value":1}`), 0600))
tool := dummyTool{"foo", 2}
// When we test the snapshot
err := Test("dummy", tool)
// Then it should error about the schema diff
require.Error(t, err)
assert.Contains(t, err.Error(), "tool schema for dummy has changed unexpectedly", "expected error about diff")
}
func TestUpdateToolsnaps(t *testing.T) {
withIsolatedWorkingDir(t)
// Given UPDATE_TOOLSNAPS is set, regardless of whether a matching snapshot file exists
t.Setenv("UPDATE_TOOLSNAPS", "true")
require.NoError(t, os.MkdirAll("__toolsnaps__", 0700))
require.NoError(t, os.WriteFile(filepath.Join("__toolsnaps__", "dummy.snap"), []byte(`{"name":"foo","value":1}`), 0600))
tool := dummyTool{"foo", 42}
// When we test the snapshot
err := Test("dummy", tool)
// Then it should succeed and write the snapshot file
require.NoError(t, err)
path := filepath.Join("__toolsnaps__", "dummy.snap")
_, statErr := os.Stat(path)
assert.NoError(t, statErr, "expected snapshot file to be written")
}
func TestMalformedSnapshotJSON(t *testing.T) {
withIsolatedWorkingDir(t)
// Given a malformed snapshot file exists
require.NoError(t, os.MkdirAll("__toolsnaps__", 0700))
require.NoError(t, os.WriteFile(filepath.Join("__toolsnaps__", "dummy.snap"), []byte(`not-json`), 0600))
tool := dummyTool{"foo", 42}
// When we test the snapshot
err := Test("dummy", tool)
// Then it should error about malformed snapshot JSON
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to parse snapshot JSON for dummy", "expected error about malformed snapshot JSON")
}