@@ -129,69 +129,15 @@ func AssertDirectoriesEqualWithIgnore(t *testing.T, expectedDir, actualDir strin
129129 }
130130}
131131
132- // AssertDirectoryContains checks if the actual directory contains all files from the expected directory
133- // and their content matches. It allows extra files in the actual directory that are not in expected.
134- func AssertDirectoryContains (t * testing.T , expectedDir , actualDir string ) {
135- t .Helper ()
136-
137- // Collect all files from expected directory
138- expectedFiles := make (map [string ][]byte )
139-
140- // Walk expected directory
141- err := filepath .Walk (expectedDir , func (path string , info os.FileInfo , err error ) error {
142- if err != nil {
143- return err
144- }
145- if ! info .IsDir () {
146- relPath , err := filepath .Rel (expectedDir , path )
147- if err != nil {
148- return err
149- }
150- content , err := os .ReadFile (path )
151- if err != nil {
152- return err
153- }
154- expectedFiles [relPath ] = content
155- }
156- return nil
157- })
158- assert .NoError (t , err , "Failed to walk expected directory: %s" , expectedDir )
159-
160- // Check each expected file exists in actual directory
161- for relPath , expectedContent := range expectedFiles {
162- actualPath := filepath .Join (actualDir , relPath )
163- actualContent , err := os .ReadFile (actualPath )
164- assert .NoError (t , err , "File %s should exist in actual directory" , relPath )
165- if err == nil {
166- assert .Equal (t , string (expectedContent ), string (actualContent ), "Content of file %s should match" , relPath )
167- }
168- }
169- }
170-
171- // AssertFileExists checks if a file exists at the specified path within a directory
172- func AssertFileExists (t * testing.T , dir , relativeFilePath string ) {
173- t .Helper ()
174-
175- fullPath := filepath .Join (dir , relativeFilePath )
176- _ , err := os .Stat (fullPath )
177- assert .NoError (t , err , "File %s should exist in directory %s" , relativeFilePath , dir )
178- }
179-
180- // AssertFileContent checks if a file exists and has the expected content
181- func AssertFileContent (t * testing.T , dir , relativeFilePath , expectedContent string ) {
182- t .Helper ()
183-
184- fullPath := filepath .Join (dir , relativeFilePath )
185- actualContent , err := os .ReadFile (fullPath )
186- assert .NoError (t , err , "Should be able to read file %s" , relativeFilePath )
187- if err == nil {
188- assert .Equal (t , expectedContent , string (actualContent ), "Content of file %s should match" , relativeFilePath )
189- }
190- }
191-
192132// AssertDirectoriesEqualWithNormalization compares two directories recursively with content normalization
193133// for handling dynamic values like temporary paths, timestamps, etc.
194134func AssertDirectoriesEqualWithNormalization (t * testing.T , expectedDir , actualDir string , normalizer func (string , string ) string ) {
135+ AssertDirectoriesEqualWithNormalizationWithIgnore (t , expectedDir , actualDir , defaultIgnoredDirs , normalizer )
136+ }
137+
138+ // AssertDirectoriesEqualWithNormalizationWithIgnore compares two directories recursively with content normalization
139+ // and a custom ignore list
140+ func AssertDirectoriesEqualWithNormalizationWithIgnore (t * testing.T , expectedDir , actualDir string , ignoredDirs []string , normalizer func (string , string ) string ) {
195141 t .Helper ()
196142
197143 // Collect all files from both directories
@@ -240,7 +186,7 @@ func AssertDirectoriesEqualWithNormalization(t *testing.T, expectedDir, actualDi
240186 }
241187
242188 // Check if we should ignore this directory
243- if info .IsDir () && shouldIgnoreDir (info .Name (), defaultIgnoredDirs ) {
189+ if info .IsDir () && shouldIgnoreDir (info .Name (), ignoredDirs ) {
244190 return filepath .SkipDir
245191 }
246192
@@ -253,7 +199,7 @@ func AssertDirectoriesEqualWithNormalization(t *testing.T, expectedDir, actualDi
253199 // Skip files in ignored directories
254200 pathParts := strings .Split (relPath , string (filepath .Separator ))
255201 for _ , part := range pathParts {
256- if shouldIgnoreDir (part , defaultIgnoredDirs ) {
202+ if shouldIgnoreDir (part , ignoredDirs ) {
257203 return nil
258204 }
259205 }
@@ -288,3 +234,80 @@ func AssertDirectoriesEqualWithNormalization(t *testing.T, expectedDir, actualDi
288234 assert .True (t , exists , "Unexpected file %s found in actual directory" , relPath )
289235 }
290236}
237+
238+ // AssertDirectoryContains checks if the actual directory contains all files from the expected directory
239+ // and their content matches. It allows extra files in the actual directory that are not in expected.
240+ func AssertDirectoryContains (t * testing.T , expectedDir , actualDir string ) {
241+ t .Helper ()
242+
243+ // Collect all files from expected directory
244+ expectedFiles := make (map [string ][]byte )
245+
246+ // Walk expected directory
247+ err := filepath .Walk (expectedDir , func (path string , info os.FileInfo , err error ) error {
248+ if err != nil {
249+ return err
250+ }
251+ if ! info .IsDir () {
252+ relPath , err := filepath .Rel (expectedDir , path )
253+ if err != nil {
254+ return err
255+ }
256+ content , err := os .ReadFile (path )
257+ if err != nil {
258+ return err
259+ }
260+ expectedFiles [relPath ] = content
261+ }
262+ return nil
263+ })
264+ assert .NoError (t , err , "Failed to walk expected directory: %s" , expectedDir )
265+
266+ // Check each expected file exists in actual directory
267+ for relPath , expectedContent := range expectedFiles {
268+ actualPath := filepath .Join (actualDir , relPath )
269+ actualContent , err := os .ReadFile (actualPath )
270+ assert .NoError (t , err , "File %s should exist in actual directory" , relPath )
271+ if err == nil {
272+ assert .Equal (t , string (expectedContent ), string (actualContent ), "Content of file %s should match" , relPath )
273+ }
274+ }
275+ }
276+
277+ // AssertFileExists checks if a file exists at the specified path within a directory
278+ func AssertFileExists (t * testing.T , dir , relativeFilePath string ) {
279+ t .Helper ()
280+
281+ fullPath := filepath .Join (dir , relativeFilePath )
282+ _ , err := os .Stat (fullPath )
283+ assert .NoError (t , err , "File %s should exist in directory %s" , relativeFilePath , dir )
284+ }
285+
286+ // AssertFileContent checks if a file exists and has the expected content
287+ func AssertFileContent (t * testing.T , dir , relativeFilePath , expectedContent string ) {
288+ t .Helper ()
289+
290+ fullPath := filepath .Join (dir , relativeFilePath )
291+ actualContent , err := os .ReadFile (fullPath )
292+ assert .NoError (t , err , "Should be able to read file %s" , relativeFilePath )
293+ if err == nil {
294+ assert .Equal (t , expectedContent , string (actualContent ), "Content of file %s should match" , relativeFilePath )
295+ }
296+ }
297+
298+ // WriteToFile writes content to a file at the specified path
299+ func WriteToFile (t * testing.T , filePath , content string ) {
300+ err := os .WriteFile (filePath , []byte (content ), 0644 )
301+ if err != nil {
302+ t .Fatalf ("failed to write to file %s: %v" , filePath , err )
303+ }
304+ }
305+
306+ // ReadFromFile reads content from a file at the specified path
307+ func ReadFromFile (t * testing.T , filePath string ) string {
308+ data , err := os .ReadFile (filePath )
309+ if err != nil {
310+ t .Fatalf ("failed to read from file %s: %v" , filePath , err )
311+ }
312+ return string (data )
313+ }
0 commit comments