@@ -2,24 +2,70 @@ package testutils
22
33import (
44 "os"
5+ "path/filepath"
56 "testing"
6-
7+
78 "github.com/stretchr/testify/require"
89)
910
1011func TestWriteTestFile (t * testing.T ) {
11- // test creating a file with content
12- content := "test content"
13- filePath := WriteTestFile (t , content )
14-
15- // check if file exists
16- _ , err := os .Stat (filePath )
17- require .False (t , os .IsNotExist (err ), "WriteTestFile did not create file at %s" , filePath )
18-
19- // check content
20- data , err := os .ReadFile (filePath )
21- require .NoError (t , err , "Failed to read test file" )
22- require .Equal (t , content , string (data ), "File content doesn't match expected" )
23-
24- // file should be cleaned up automatically at the end of the test
12+ t .Run ("standard file creation" , func (t * testing.T ) {
13+ // test creating a file with content
14+ content := "test content"
15+ filePath := WriteTestFile (t , content )
16+
17+ // check if file exists
18+ _ , err := os .Stat (filePath )
19+ require .False (t , os .IsNotExist (err ), "WriteTestFile did not create file at %s" , filePath )
20+
21+ // check content
22+ data , err := os .ReadFile (filePath )
23+ require .NoError (t , err , "Failed to read test file" )
24+ require .Equal (t , content , string (data ), "File content doesn't match expected" )
25+
26+ // verify directory structure
27+ dir := filepath .Dir (filePath )
28+ require .Contains (t , dir , "testutils-" , "Temp directory should contain expected prefix" )
29+
30+ // file should be cleaned up automatically at the end of the test
31+ })
32+
33+ t .Run ("with empty content" , func (t * testing.T ) {
34+ filePath := WriteTestFile (t , "" )
35+
36+ // check empty file was created
37+ info , err := os .Stat (filePath )
38+ require .NoError (t , err , "File should exist" )
39+ require .Zero (t , info .Size (), "File should be empty" )
40+ })
41+
42+ t .Run ("with multi-line content" , func (t * testing.T ) {
43+ content := "line 1\n line 2\n line 3"
44+ filePath := WriteTestFile (t , content )
45+
46+ data , err := os .ReadFile (filePath )
47+ require .NoError (t , err )
48+ require .Equal (t , content , string (data ))
49+ })
50+
51+ t .Run ("cleanup by direct call" , func (t * testing.T ) {
52+ // create a test file
53+ content := "test cleanup"
54+ filePath := WriteTestFile (t , content )
55+
56+ // get the directory to be cleaned up
57+ dir := filepath .Dir (filePath )
58+
59+ // verify directory and file exist
60+ require .DirExists (t , dir )
61+ require .FileExists (t , filePath )
62+
63+ // manually clean up (simulating what t.Cleanup would do)
64+ err := os .RemoveAll (dir )
65+ require .NoError (t , err )
66+
67+ // after manual cleanup, the file should no longer exist
68+ _ , err = os .Stat (filePath )
69+ require .True (t , os .IsNotExist (err ), "File should be removed after cleanup" )
70+ })
2571}
0 commit comments