@@ -2,10 +2,8 @@ package main
22
33import (
44 "errors"
5- "io/fs"
65 "log"
76 "os"
8- "path/filepath"
97 "strings"
108 "testing"
119)
@@ -15,18 +13,11 @@ import (
1513// used. If a baseName is not provided, a random file name is generated.
1614// Returns the directory where the file was created, the file's directory
1715// entry, and the actual name of the file.
18- func createTestFile (path string , baseName string , content string ) ( string , fs. DirEntry , string ) {
16+ func createTestFile (path string , baseName string , content string ) * File {
1917 f , err := os .CreateTemp (path , baseName )
2018 if err != nil {
2119 log .Fatal (err )
2220 }
23-
24- fileInfo , err := f .Stat ()
25- if err != nil {
26- defer os .Remove (f .Name ())
27- log .Fatal (err )
28- }
29-
3021 if _ , err := f .Write ([]byte (content )); err != nil {
3122 defer os .Remove (f .Name ())
3223 log .Fatal (err )
@@ -36,23 +27,7 @@ func createTestFile(path string, baseName string, content string) (string, fs.Di
3627 log .Fatal (err )
3728 }
3829
39- dirName := filepath .Dir (f .Name ())
40-
41- // There has to be a better way to get `fInfo` directly for `f`?
42- files , err := os .ReadDir (dirName )
43- if err != nil {
44- defer os .Remove (f .Name ())
45- log .Fatal (err )
46- }
47- fInfo := files [0 ]
48- for _ , file := range files {
49- if file .Name () == fileInfo .Name () {
50- fInfo = file
51- break
52- }
53- }
54-
55- return dirName , fInfo , f .Name ()
30+ return NewFile (f .Name ())
5631}
5732
5833// assertPathExistsBeforeRename ensures that the file at the given path exists
@@ -81,16 +56,16 @@ func TestHandleFileWithFile(t *testing.T) {
8156 replace := "f"
8257 want := "alfa"
8358
84- dirName , fInfo , path := createTestFile ("" , initial , initial )
85- defer os .Remove (path )
86- expectedName := strings .Replace (fInfo . Name (), find , replace , - 1 )
87- expectedPath := dirName + string (os .PathSeparator ) + expectedName
59+ f := createTestFile ("" , initial , initial )
60+ defer os .Remove (f . Path )
61+ expectedName := strings .Replace (f . Base (), find , replace , - 1 )
62+ expectedPath := f . Dir () + string (os .PathSeparator ) + expectedName
8863 defer os .Remove (expectedPath )
8964 fr := findReplace {find : find , replace : replace }
9065
91- assertPathExistsBeforeRename (t , path )
92- fr .HandleFile (dirName , fInfo )
93- assertPathExistsAfterRename (t , path , expectedPath )
66+ assertPathExistsBeforeRename (t , f . Path )
67+ fr .HandleFile (f )
68+ assertPathExistsAfterRename (t , f . Path , expectedPath )
9469
9570 got := readFile (expectedPath )
9671 if got != want {
@@ -103,16 +78,16 @@ func TestRenameFile(t *testing.T) {
10378 find := "ph"
10479 replace := "f"
10580
106- dirName , fInfo , path := createTestFile ("" , initial , "" )
107- defer os .Remove (path )
108- expectedName := strings .Replace (fInfo . Name (), find , replace , - 1 )
109- expectedPath := dirName + string (os .PathSeparator ) + expectedName
81+ f := createTestFile ("" , initial , "" )
82+ defer os .Remove (f . Path )
83+ expectedName := strings .Replace (f . Base (), find , replace , - 1 )
84+ expectedPath := f . Dir () + string (os .PathSeparator ) + expectedName
11085 defer os .Remove (expectedPath )
11186 fr := findReplace {find : find , replace : replace }
11287
113- assertPathExistsBeforeRename (t , path )
114- fr .RenameFile (dirName , fInfo )
115- assertPathExistsAfterRename (t , path , expectedPath )
88+ assertPathExistsBeforeRename (t , f . Path )
89+ fr .RenameFile (f )
90+ assertPathExistsAfterRename (t , f . Path , expectedPath )
11691}
11792
11893// assertNewContentsOfFile ensures that the contents of the file at the given
@@ -130,11 +105,11 @@ func TestReplaceContents(t *testing.T) {
130105 replace := "f"
131106 want := "alfa"
132107
133- dirName , fInfo , path := createTestFile ("" , "*" , initial )
134- defer os .Remove (path )
108+ f := createTestFile ("" , "*" , initial )
109+ defer os .Remove (f . Path )
135110 fr := findReplace {find : find , replace : replace }
136- fr .ReplaceContents (dirName , fInfo )
137- assertNewContentsOfFile (t , path , initial , find , replace , want )
111+ fr .ReplaceContents (f )
112+ assertNewContentsOfFile (t , f . Path , initial , find , replace , want )
138113}
139114
140115func TestReplaceContentsEntireFile (t * testing.T ) {
@@ -143,11 +118,11 @@ func TestReplaceContentsEntireFile(t *testing.T) {
143118 replace := "beta"
144119 want := "beta"
145120
146- dirName , fInfo , path := createTestFile ("" , "*" , initial )
147- defer os .Remove (path )
121+ f := createTestFile ("" , "*" , initial )
122+ defer os .Remove (f . Path )
148123 fr := findReplace {find : find , replace : replace }
149- fr .ReplaceContents (dirName , fInfo )
150- assertNewContentsOfFile (t , path , initial , find , replace , want )
124+ fr .ReplaceContents (f )
125+ assertNewContentsOfFile (t , f . Path , initial , find , replace , want )
151126}
152127
153128func TestReplaceContentsMultipleMatchesSingleLine (t * testing.T ) {
@@ -156,11 +131,11 @@ func TestReplaceContentsMultipleMatchesSingleLine(t *testing.T) {
156131 replace := "f"
157132 want := "alfaalfa"
158133
159- dirName , fInfo , path := createTestFile ("" , "*" , initial )
160- defer os .Remove (path )
134+ f := createTestFile ("" , "*" , initial )
135+ defer os .Remove (f . Path )
161136 fr := findReplace {find : find , replace : replace }
162- fr .ReplaceContents (dirName , fInfo )
163- assertNewContentsOfFile (t , path , initial , find , replace , want )
137+ fr .ReplaceContents (f )
138+ assertNewContentsOfFile (t , f . Path , initial , find , replace , want )
164139}
165140
166141func TestReplaceContentsMultipleMatchesMultipleLines (t * testing.T ) {
@@ -169,11 +144,11 @@ func TestReplaceContentsMultipleMatchesMultipleLines(t *testing.T) {
169144 replace := "f"
170145 want := "alfa\n alfa"
171146
172- dirName , fInfo , path := createTestFile ("" , "*" , initial )
173- defer os .Remove (path )
147+ f := createTestFile ("" , "*" , initial )
148+ defer os .Remove (f . Path )
174149 fr := findReplace {find : find , replace : replace }
175- fr .ReplaceContents (dirName , fInfo )
176- assertNewContentsOfFile (t , path , initial , find , replace , want )
150+ fr .ReplaceContents (f )
151+ assertNewContentsOfFile (t , f . Path , initial , find , replace , want )
177152}
178153
179154func TestReplaceContentsNoMatches (t * testing.T ) {
@@ -182,11 +157,11 @@ func TestReplaceContentsNoMatches(t *testing.T) {
182157 replace := "xyz"
183158 want := "alpha"
184159
185- dirName , fInfo , path := createTestFile ("" , "*" , initial )
186- defer os .Remove (path )
160+ f := createTestFile ("" , "*" , initial )
161+ defer os .Remove (f . Path )
187162 fr := findReplace {find : find , replace : replace }
188- fr .ReplaceContents (dirName , fInfo )
189- assertNewContentsOfFile (t , path , initial , find , replace , want )
163+ fr .ReplaceContents (f )
164+ assertNewContentsOfFile (t , f . Path , initial , find , replace , want )
190165}
191166
192167// assertRandomStringLength ensures that the generated string matches the
0 commit comments