-
Notifications
You must be signed in to change notification settings - Fork 3
/
snapshot_test.go
150 lines (143 loc) Β· 4.32 KB
/
snapshot_test.go
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"os"
"path"
"testing"
"github.com/falzm/fsdiff/internal/snapshot"
)
func (ts *testSuite) TestSnapshotCmd_run() {
tests := []struct {
name string
cmd *snapshotCmd
setupFunc func(*testSuite, *snapshotCmd)
testFunc func(*testSuite, *snapshotCmd)
wantErr bool
}{
{
name: "with --output-file",
cmd: &snapshotCmd{
Root: ts.rootDir,
OutputFile: path.Join(ts.testDir, ts.randomString(10)+".snap"),
},
setupFunc: func(t *testSuite, _ *snapshotCmd) { ts.createDummyFile("x", []byte("x"), 0o644) },
testFunc: func(ts *testSuite, cmd *snapshotCmd) {
ts.Require().FileExists(cmd.OutputFile)
snap, err := snapshot.Open(cmd.OutputFile)
ts.Require().NoError(err)
defer snap.Close()
filesByPath, err := snap.FilesByPath()
ts.Require().NoError(err)
ts.Require().Len(filesByPath, 1)
},
},
{
name: "with --shallow",
cmd: &snapshotCmd{
Root: ts.rootDir,
OutputFile: path.Join(ts.testDir, ts.randomString(10)+".snap"),
Shallow: true,
},
setupFunc: func(t *testSuite, _ *snapshotCmd) { ts.createDummyFile("x", []byte("x"), 0o644) },
testFunc: func(ts *testSuite, cmd *snapshotCmd) {
ts.Require().FileExists(cmd.OutputFile)
snap, err := snapshot.Open(cmd.OutputFile)
ts.Require().NoError(err)
defer snap.Close()
ts.True(snap.Metadata().Shallow)
},
},
{
name: "with --exclude",
cmd: &snapshotCmd{
Root: ts.rootDir,
OutputFile: path.Join(ts.testDir, ts.randomString(10)+".snap"),
Exclude: []string{"b"},
},
setupFunc: func(t *testSuite, _ *snapshotCmd) {
ts.createDummyFile("a", []byte("a"), 0o644)
ts.createDummyFile("b", []byte("b"), 0o644)
},
testFunc: func(ts *testSuite, cmd *snapshotCmd) {
ts.Require().FileExists(cmd.OutputFile)
snap, err := snapshot.Open(cmd.OutputFile)
ts.Require().NoError(err)
defer snap.Close()
filesByPath, err := snap.FilesByPath()
ts.Require().NoError(err)
ts.Require().Len(filesByPath, 1)
ts.Require().Equal("a", filesByPath[0].Path)
},
},
{
name: "with --exclude-from",
cmd: &snapshotCmd{
Root: ts.rootDir,
OutputFile: path.Join(ts.testDir, ts.randomString(10)+".snap"),
ExcludeFrom: path.Join(ts.testDir, ts.randomString(10)+".excludes"),
},
setupFunc: func(t *testSuite, cmd *snapshotCmd) {
ts.createDummyFile("a", []byte("a"), 0o644)
ts.createDummyFile("b", []byte("b"), 0o644)
ts.Require().NoError(os.WriteFile(cmd.ExcludeFrom, []byte("b"), 0o644))
},
testFunc: func(ts *testSuite, cmd *snapshotCmd) {
ts.Require().FileExists(cmd.OutputFile)
snap, err := snapshot.Open(cmd.OutputFile)
ts.Require().NoError(err)
defer snap.Close()
filesByPath, err := snap.FilesByPath()
ts.Require().NoError(err)
ts.Require().Len(filesByPath, 1)
ts.Require().Equal("a", filesByPath[0].Path)
},
},
{
name: "filesystem error without --carry-on",
cmd: &snapshotCmd{
Root: ts.rootDir,
OutputFile: path.Join(ts.testDir, ts.randomString(10)+".snap"),
},
setupFunc: func(t *testSuite, _ *snapshotCmd) { ts.createDummyFile("x", []byte("x"), 0o000) },
wantErr: true,
},
{
name: "filesystem error with --carry-on",
cmd: &snapshotCmd{
Root: ts.rootDir,
OutputFile: path.Join(ts.testDir, ts.randomString(10)+".snap"),
CarryOn: true,
},
setupFunc: func(t *testSuite, _ *snapshotCmd) { ts.createDummyFile("x", []byte("x"), 0o000) },
testFunc: func(ts *testSuite, cmd *snapshotCmd) {
ts.Require().FileExists(cmd.OutputFile)
snap, err := snapshot.Open(cmd.OutputFile)
ts.Require().NoError(err)
defer snap.Close()
filesByPath, err := snap.FilesByPath()
ts.Require().NoError(err)
ts.Require().Len(filesByPath, 0)
},
},
}
for _, tt := range tests {
ts.T().Run(tt.name, func(t *testing.T) {
// Clean up root dir between test cases.
dir, err := os.ReadDir(ts.rootDir)
ts.Require().NoError(err)
for _, de := range dir {
ts.Require().NoError(os.RemoveAll(path.Join(ts.rootDir, de.Name())))
}
if tt.setupFunc != nil {
tt.setupFunc(ts, tt.cmd)
}
err = tt.cmd.Run()
if (err != nil) != tt.wantErr {
t.Errorf("snapshotCmd.Run() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr {
return
}
tt.testFunc(ts, tt.cmd)
})
}
}