-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs_test.go
More file actions
219 lines (188 loc) · 9.14 KB
/
fs_test.go
File metadata and controls
219 lines (188 loc) · 9.14 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package testutils_test
import (
"testing"
testutils "github.com/adevinta/go-testutils-toolkit"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAssertFileContents(t *testing.T) {
t.Run("When the file does not exist on the filesystem", func(t *testing.T) {
fs := afero.NewMemMapFs()
fakeT := &testutils.FakeTest{}
assert.False(t, testutils.AssertFileContents(fakeT, fs, "/hello/world", "my-content"))
assert.False(t, fakeT.Failed)
require.Len(t, fakeT.ErrorMessages, 1)
assert.Contains(t, fakeT.ErrorMessages[0], "open /hello/world: file does not exist")
})
t.Run("When the file content differs", func(t *testing.T) {
fs := afero.NewMemMapFs()
testutils.EnsureFileContent(t, fs, "/hello/world", "this is wrong")
fakeT := &testutils.FakeTest{}
assert.False(t, testutils.AssertFileContents(fakeT, fs, "/hello/world", "hello world"))
assert.False(t, fakeT.Failed)
require.Len(t, fakeT.ErrorMessages, 1)
assert.Contains(t, fakeT.ErrorMessages[0], `expected: "hello world"`)
assert.Contains(t, fakeT.ErrorMessages[0], `actual : "this is wrong"`)
})
t.Run("When the file contents matches on the file system", func(t *testing.T) {
fs := afero.NewMemMapFs()
testutils.EnsureFileContent(t, fs, "/hello/world", "hello world")
fakeT := &testutils.FakeTest{}
assert.True(t, testutils.AssertFileContents(fakeT, fs, "/hello/world", "hello world"))
assert.False(t, fakeT.Failed)
assert.Empty(t, fakeT.ErrorMessages)
})
}
func TestAssertFileExists(t *testing.T) {
t.Run("When the file does not exist on the filesystem", func(t *testing.T) {
fs := afero.NewMemMapFs()
fakeT := &testutils.FakeTest{}
assert.False(t, testutils.AssertFileExists(fakeT, fs, "/hello/world"))
assert.False(t, fakeT.Failed)
require.Len(t, fakeT.ErrorMessages, 1)
assert.Contains(t, fakeT.ErrorMessages[0], "Expect file path /hello/world to exist in filesystem MemMapFS")
})
t.Run("When the file exists on the filesystem", func(t *testing.T) {
fs := afero.NewMemMapFs()
testutils.EnsureFileContent(t, fs, "/hello/world", "hello world")
fakeT := &testutils.FakeTest{}
assert.True(t, testutils.AssertFileExists(fakeT, fs, "/hello/world"))
assert.False(t, fakeT.Failed)
assert.Empty(t, fakeT.ErrorMessages)
})
}
func TestRequireFileExists(t *testing.T) {
t.Run("When the file does not exist on the filesystem", func(t *testing.T) {
fs := afero.NewMemMapFs()
fakeT := &testutils.FakeTest{}
testutils.RequireFileExists(fakeT, fs, "/hello/world")
assert.True(t, fakeT.Failed)
require.Len(t, fakeT.ErrorMessages, 1)
assert.Contains(t, fakeT.ErrorMessages[0], "Expect file path /hello/world to exist in filesystem MemMapFS")
})
t.Run("When the file exists on the filesystem", func(t *testing.T) {
fs := afero.NewMemMapFs()
testutils.EnsureFileContent(t, fs, "/hello/world", "hello world")
fakeT := &testutils.FakeTest{}
testutils.RequireFileExists(fakeT, fs, "/hello/world")
assert.False(t, fakeT.Failed)
assert.Empty(t, fakeT.ErrorMessages)
})
}
func TestAssertFileEquivalent(t *testing.T) {
t.Run("When the file does not exist neither on the reference file system nor on the actual one", func(t *testing.T) {
expectedFS := afero.NewMemMapFs()
actualFS := afero.NewMemMapFs()
fakeT := &testutils.FakeTest{}
assert.True(t, testutils.AssertFsFileEquivalent(fakeT, expectedFS, actualFS, "/my/path"))
assert.False(t, fakeT.Failed)
require.Len(t, fakeT.ErrorMessages, 0)
})
t.Run("When the file exists on the reference file system but not on the actual one", func(t *testing.T) {
expectedFS := afero.NewMemMapFs()
actualFS := afero.NewMemMapFs()
testutils.EnsureFileContent(t, expectedFS, "/my/path", "hello-world")
fakeT := &testutils.FakeTest{}
assert.False(t, testutils.AssertFsFileEquivalent(fakeT, expectedFS, actualFS, "/my/path"))
assert.False(t, fakeT.Failed)
require.Len(t, fakeT.ErrorMessages, 1)
assert.Contains(t, fakeT.ErrorMessages[0], "Expecting no error when opening file path /my/path but got:\n")
assert.Contains(t, fakeT.ErrorMessages[0], "open /my/path: file does not exist")
})
t.Run("When the file does not exist on the reference file system but does on the actual one", func(t *testing.T) {
expectedFS := afero.NewMemMapFs()
actualFS := afero.NewMemMapFs()
testutils.EnsureFileContent(t, actualFS, "/my/path", "hello-world")
fakeT := &testutils.FakeTest{}
assert.False(t, testutils.AssertFsFileEquivalent(fakeT, expectedFS, actualFS, "/my/path"))
assert.False(t, fakeT.Failed)
require.Len(t, fakeT.ErrorMessages, 1)
assert.Contains(t, fakeT.ErrorMessages[0], "Expecting an error open /my/path: file does not exist when opening file path /my/path but got nil")
})
t.Run("When the file stat differs on the expected and the actual File Systems", func(t *testing.T) {
expectedFS := afero.NewMemMapFs()
actualFS := afero.NewMemMapFs()
testutils.EnsureFileContent(t, expectedFS, "/my/path", "hello-world")
testutils.EnsureFileContent(t, actualFS, "/my/path", "hello")
fakeT := &testutils.FakeTest{}
assert.False(t, testutils.AssertFsFileEquivalent(fakeT, expectedFS, actualFS, "/my/path"))
assert.False(t, fakeT.Failed)
})
t.Run("When the file has the same stats (size) but contents differs on the expected and the actual File Systems", func(t *testing.T) {
expectedFS := afero.NewMemMapFs()
actualFS := afero.NewMemMapFs()
testutils.EnsureFileContent(t, expectedFS, "/my/path", "hello-world-1")
testutils.EnsureFileContent(t, actualFS, "/my/path", "hello-world-2")
fakeT := &testutils.FakeTest{}
assert.False(t, testutils.AssertFsFileEquivalent(fakeT, expectedFS, actualFS, "/my/path"))
assert.False(t, fakeT.Failed)
})
t.Run("When the file has the same stats (size) and contents differs on the expected and the actual File Systems", func(t *testing.T) {
expectedFS := afero.NewMemMapFs()
actualFS := afero.NewMemMapFs()
testutils.EnsureFileContent(t, expectedFS, "/my/path", "hello-world")
testutils.EnsureFileContent(t, actualFS, "/my/path", "hello-world")
fakeT := &testutils.FakeTest{}
assert.True(t, testutils.AssertFsFileEquivalent(fakeT, expectedFS, actualFS, "/my/path"))
assert.False(t, fakeT.Failed)
})
}
func TestRequireFileEquivalent(t *testing.T) {
t.Run("When the file does not exist neither on the reference file system nor on the actual one", func(t *testing.T) {
expectedFS := afero.NewMemMapFs()
actualFS := afero.NewMemMapFs()
fakeT := &testutils.FakeTest{}
testutils.RequireFsFileEquivalent(fakeT, expectedFS, actualFS, "/my/path")
assert.False(t, fakeT.Failed)
require.Len(t, fakeT.ErrorMessages, 0)
})
t.Run("When the file exists on the reference file system but not on the actual one", func(t *testing.T) {
expectedFS := afero.NewMemMapFs()
actualFS := afero.NewMemMapFs()
testutils.EnsureFileContent(t, expectedFS, "/my/path", "hello-world")
fakeT := &testutils.FakeTest{}
testutils.RequireFsFileEquivalent(fakeT, expectedFS, actualFS, "/my/path")
assert.True(t, fakeT.Failed)
require.Len(t, fakeT.ErrorMessages, 1)
assert.Contains(t, fakeT.ErrorMessages[0], "Expecting no error when opening file path /my/path but got:\n")
assert.Contains(t, fakeT.ErrorMessages[0], "open /my/path: file does not exist")
})
t.Run("When the file does not exist on the reference file system but does on the actual one", func(t *testing.T) {
expectedFS := afero.NewMemMapFs()
actualFS := afero.NewMemMapFs()
testutils.EnsureFileContent(t, actualFS, "/my/path", "hello-world")
fakeT := &testutils.FakeTest{}
testutils.RequireFsFileEquivalent(fakeT, expectedFS, actualFS, "/my/path")
assert.True(t, fakeT.Failed)
require.Len(t, fakeT.ErrorMessages, 1)
assert.Contains(t, fakeT.ErrorMessages[0], "Expecting an error open /my/path: file does not exist when opening file path /my/path but got nil")
})
t.Run("When the file stat differs on the expected and the actual File Systems", func(t *testing.T) {
expectedFS := afero.NewMemMapFs()
actualFS := afero.NewMemMapFs()
testutils.EnsureFileContent(t, expectedFS, "/my/path", "hello-world")
testutils.EnsureFileContent(t, actualFS, "/my/path", "hello")
fakeT := &testutils.FakeTest{}
testutils.RequireFsFileEquivalent(fakeT, expectedFS, actualFS, "/my/path")
assert.True(t, fakeT.Failed)
})
t.Run("When the file has the same stats (size) but contents differs on the expected and the actual File Systems", func(t *testing.T) {
expectedFS := afero.NewMemMapFs()
actualFS := afero.NewMemMapFs()
testutils.EnsureFileContent(t, expectedFS, "/my/path", "hello-world-1")
testutils.EnsureFileContent(t, actualFS, "/my/path", "hello-world-2")
fakeT := &testutils.FakeTest{}
testutils.RequireFsFileEquivalent(fakeT, expectedFS, actualFS, "/my/path")
assert.True(t, fakeT.Failed)
})
t.Run("When the file has the same stats (size) and contents differs on the expected and the actual File Systems", func(t *testing.T) {
expectedFS := afero.NewMemMapFs()
actualFS := afero.NewMemMapFs()
testutils.EnsureFileContent(t, expectedFS, "/my/path", "hello-world")
testutils.EnsureFileContent(t, actualFS, "/my/path", "hello-world")
fakeT := &testutils.FakeTest{}
testutils.RequireFsFileEquivalent(fakeT, expectedFS, actualFS, "/my/path")
assert.False(t, fakeT.Failed)
})
}