-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_replace_test.go
366 lines (304 loc) · 10.3 KB
/
find_replace_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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"errors"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
/*
* Testing utilities
*/
// newTestFile creates a file in the given directory path, with the given name
// and content. If a directory path is not provided, a temp directory is used.
// If a baseName is not provided, a random file name is generated. Returns the
// directory where the file was created, the file's directory entry, and the
// actual name of the file.
func newTestFile(path string, baseName string, content string) *File {
f, err := os.CreateTemp(path, baseName)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write([]byte(content)); err != nil {
defer os.Remove(f.Name())
log.Fatal(err)
}
if err := f.Close(); err != nil {
defer os.Remove(f.Name())
log.Fatal(err)
}
return NewFile(f.Name())
}
// newTestDir creates a directory in the given directory path, with the given
// base name. If a directory path is not provided, a temp directory is used. If
// a baseName is not provided, a random file name is generated. Returns the
// directory where the file was created, the file's directory entry, and the
// actual name of the file.
func newTestDir(path string, baseName string) *File {
dirPath, err := os.MkdirTemp(path, baseName)
if err != nil {
log.Fatal(err)
}
return NewFile(dirPath)
}
func expectedPathAfterRename(f *File, fr *findReplace) string {
return filepath.Join(f.Dir(), strings.Replace(f.Base(), fr.find, fr.replace, -1))
}
/*
* Assertions
*/
// assertFileExists ensures that the given File exists
func assertFileExists(t *testing.T, f *File) {
if _, err := os.Stat(f.Path); errors.Is(err, os.ErrNotExist) {
t.Errorf("test file %v does not exist", f.Path)
}
}
// assertFileNonexistent ensures that the File does not exist
func assertFileNonexistent(t *testing.T, f *File) {
if _, err := os.Stat(f.Path); !errors.Is(err, os.ErrNotExist) {
if err == nil {
t.Errorf("test file %v exists", f.Path)
} else {
t.Errorf("test file %v exists (got %v)", f.Path, err)
}
}
}
func assertPathExistsAfterRename(t *testing.T, f *File, expectedPath string) *File {
assertFileNonexistent(t, f)
newFile := NewFile(expectedPath)
assertFileExists(t, newFile)
return newFile
}
/*
* Tests
*/
// TestWalkDir is the most important test of the entire suite, because it
// exercises all the basic functionality of the app. It builds a directory tree
// of temporary files and directories, walks the entire tree, and ensures that
// all files and directories are appropriately renamed at at the end, and all
// files contain the correct contents.
func TestWalkDir(t *testing.T) {
find := "wh"
replace := "f"
d := newTestDir("", "*")
defer os.Remove(d.Path)
// d1: who/
d1 := newTestDir(d.Path, "who")
defer os.Remove(d1.Path)
// d1d1: who/what/
d1d1 := newTestDir(d1.Path, "what")
defer os.Remove(d1d1.Path)
// d1d1f1: who/what/when (contains "where")
d1d1f1Contents := "where"
d1d1f1 := newTestFile(d1d1.Path, "when", d1d1f1Contents)
defer os.Remove(d1d1f1.Path)
// d2: what/
d2 := newTestDir(d.Path, "what")
defer os.Remove(d2.Path)
// d2d1: what/when/
d2d1 := newTestDir(d2.Path, "when")
defer os.Remove(d2d1.Path)
// d2d1d1: what/when/where (directories with no files)
d2d1d1 := newTestDir(d2d1.Path, "where")
defer os.Remove(d2d1d1.Path)
// d3: when/
d3 := newTestDir(d.Path, "when")
defer os.Remove(d3.Path)
// d3f1: when/where (contains "why")
d3f1Contents := "why"
d3f1 := newTestFile(d3.Path, "where", d3f1Contents)
defer os.Remove(d3f1.Path)
// d4: where/ (empty directory in base dir)
d4 := newTestDir(d.Path, "where")
defer os.Remove(d4.Path)
// f1: why (file in base dir contains "wh")
f1Contents := "wh\nwh\nwh\n"
f1 := newTestFile(d.Path, "why", f1Contents)
defer os.Remove(f1.Path)
fr := findReplace{find: find, replace: replace}
fr.WalkDir(d)
// d1: who/ > fo/
d1ExpectedPath := expectedPathAfterRename(d1, &fr)
assertPathExistsAfterRename(t, d1, d1ExpectedPath)
// d1d1: who/what/ > fo/foat/
d1d1ExpectedPath := filepath.Join(d1ExpectedPath, strings.Replace(d1d1.Base(), fr.find, fr.replace, -1))
assertPathExistsAfterRename(t, d1d1, d1d1ExpectedPath)
// d1d1f1: who/what/when > fo/fat/fen (contains "fere")
d1d1f1ExpectedPath := filepath.Join(d1d1ExpectedPath, strings.Replace(d1d1f1.Base(), fr.find, fr.replace, -1))
assertPathExistsAfterRename(t, d1d1f1, d1d1f1ExpectedPath)
assertNewContentsOfFile(t, d1d1f1ExpectedPath, d1d1f1Contents, find, replace, "fere")
// d2: what/ > fat/
d2ExpectedPath := expectedPathAfterRename(d2, &fr)
assertPathExistsAfterRename(t, d2, d2ExpectedPath)
// d2d1: what/when/
d2d1ExpectedPath := filepath.Join(d2ExpectedPath, strings.Replace(d2d1.Base(), fr.find, fr.replace, -1))
assertPathExistsAfterRename(t, d2d1, d2d1ExpectedPath)
// d2d1d1: what/when/where (directories with no files)
d2d1d1ExpectedPath := filepath.Join(d2d1ExpectedPath, strings.Replace(d2d1d1.Base(), fr.find, fr.replace, -1))
assertPathExistsAfterRename(t, d2d1d1, d2d1d1ExpectedPath)
// d3: when/
d3ExpectedPath := expectedPathAfterRename(d3, &fr)
assertPathExistsAfterRename(t, d3, d3ExpectedPath)
// d3f1: when/where (contains "why")
d3f1ExpectedPath := filepath.Join(d3ExpectedPath, strings.Replace(d3f1.Base(), fr.find, fr.replace, -1))
assertPathExistsAfterRename(t, d3f1, d3f1ExpectedPath)
assertNewContentsOfFile(t, d3f1ExpectedPath, d3f1Contents, find, replace, "fy")
// d4: where/ (empty directory in base dir)
d4ExpectedPath := expectedPathAfterRename(d4, &fr)
assertPathExistsAfterRename(t, d4, d4ExpectedPath)
// f1: why (file in base dir contains "wh\nwh\nwh\n")
f1ExpectedPath := expectedPathAfterRename(f1, &fr)
assertPathExistsAfterRename(t, f1, f1ExpectedPath)
assertNewContentsOfFile(t, f1ExpectedPath, f1Contents, find, replace, "f\nf\nf\n")
}
func TestHandleFileWithDir(t *testing.T) {
initial := "alpha"
find := "ph"
replace := "f"
f := newTestDir("", initial)
defer os.Remove(f.Path)
expectedPath := filepath.Join(f.Dir(), strings.Replace(f.Base(), find, replace, -1))
defer os.Remove(expectedPath)
fr := findReplace{find: find, replace: replace}
assertFileExists(t, f)
fr.HandleFile(f)
assertPathExistsAfterRename(t, f, expectedPath)
}
func TestHandleFileWithIgnoredDir(t *testing.T) {
initial := ".git"
find := "git"
replace := "got"
dirPath := filepath.Join(os.TempDir(), initial)
if err := os.Mkdir(dirPath, 0700); err != nil {
log.Fatal(err)
}
f := NewFile(dirPath)
defer os.Remove(f.Path)
// Just in case it's unexpectedly renamed, let's make sure we cleanup the
// anticipated name.
unexpectedName := strings.Replace(f.Base(), find, replace, -1)
unexpectedPath := filepath.Join(f.Dir(), unexpectedName)
defer os.Remove(unexpectedPath)
fr := findReplace{find: find, replace: replace}
assertFileExists(t, f)
fr.HandleFile(f)
assertFileExists(t, f)
}
func TestHandleFileWithFile(t *testing.T) {
initial := "alpha"
find := "ph"
replace := "f"
want := "alfa"
f := newTestFile("", initial, initial)
defer os.Remove(f.Path)
expectedName := strings.Replace(f.Base(), find, replace, -1)
expectedPath := filepath.Join(f.Dir(), expectedName)
defer os.Remove(expectedPath)
fr := findReplace{find: find, replace: replace}
assertFileExists(t, f)
fr.HandleFile(f)
assertPathExistsAfterRename(t, f, expectedPath)
got := NewFile(expectedPath).Read()
if got != want {
t.Errorf("replace %v with %v in %v, but got %v; want %v", find, replace, initial, got, want)
}
}
func TestRenameFile(t *testing.T) {
initial := "alpha"
find := "ph"
replace := "f"
f := newTestFile("", initial, "")
defer os.Remove(f.Path)
expectedName := strings.Replace(f.Base(), find, replace, -1)
expectedPath := filepath.Join(f.Dir(), expectedName)
defer os.Remove(expectedPath)
fr := findReplace{find: find, replace: replace}
assertFileExists(t, f)
fr.RenameFile(f)
assertPathExistsAfterRename(t, f, expectedPath)
}
// assertNewContentsOfFile ensures that the contents of the file at the given
// path exactly match the desired string.
func assertNewContentsOfFile(t *testing.T, path string, initial string, find string, replace string, want string) {
got := NewFile(path).Read()
if got != want {
t.Errorf("replace %v with %v in %v, but got %v; want %v", find, replace, initial, got, want)
}
}
func TestReplaceContents(t *testing.T) {
initial := "alpha"
find := "ph"
replace := "f"
want := "alfa"
f := newTestFile("", "*", initial)
defer os.Remove(f.Path)
fr := findReplace{find: find, replace: replace}
fr.ReplaceContents(f)
assertNewContentsOfFile(t, f.Path, initial, find, replace, want)
}
func TestReplaceContentsEntireFile(t *testing.T) {
initial := "alpha"
find := "alpha"
replace := "beta"
want := "beta"
f := newTestFile("", "*", initial)
defer os.Remove(f.Path)
fr := findReplace{find: find, replace: replace}
fr.ReplaceContents(f)
assertNewContentsOfFile(t, f.Path, initial, find, replace, want)
}
func TestReplaceContentsMultipleMatchesSingleLine(t *testing.T) {
initial := "alphaalpha"
find := "ph"
replace := "f"
want := "alfaalfa"
f := newTestFile("", "*", initial)
defer os.Remove(f.Path)
fr := findReplace{find: find, replace: replace}
fr.ReplaceContents(f)
assertNewContentsOfFile(t, f.Path, initial, find, replace, want)
}
func TestReplaceContentsMultipleMatchesMultipleLines(t *testing.T) {
initial := "alpha\nalpha"
find := "ph"
replace := "f"
want := "alfa\nalfa"
f := newTestFile("", "*", initial)
defer os.Remove(f.Path)
fr := findReplace{find: find, replace: replace}
fr.ReplaceContents(f)
assertNewContentsOfFile(t, f.Path, initial, find, replace, want)
}
func TestReplaceContentsNoMatches(t *testing.T) {
initial := "alpha"
find := "abc"
replace := "xyz"
want := "alpha"
f := newTestFile("", "*", initial)
defer os.Remove(f.Path)
fr := findReplace{find: find, replace: replace}
fr.ReplaceContents(f)
assertNewContentsOfFile(t, f.Path, initial, find, replace, want)
}
func CloneRepoToTestDir(b *testing.B, repoUrl string) *File {
d := newTestDir("", "*")
defer os.Remove(d.Path)
cmd := exec.Command("git", "clone", "--depth=1", "--single-branch", repoUrl, ".")
cmd.Dir = d.Path
out, err := cmd.CombinedOutput()
if err != nil {
b.Errorf("failed to clone repo: %s", out)
}
return d
}
func BenchmarkNova(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
d := CloneRepoToTestDir(b, "[email protected]:openstack/nova.git")
fr := findReplace{find: RandomString(2), replace: RandomString(2)}
b.StartTimer()
fr.WalkDir(d)
}
}