88 "regexp"
99 "testing"
1010
11- "github.com/stretchr/testify/require"
12-
1311 "github.com/CodeLieutenant/utils"
12+ "github.com/stretchr/testify/require"
1413)
1514
1615func TestCreateLogFile (t * testing.T ) {
@@ -19,14 +18,14 @@ func TestCreateLogFile(t *testing.T) {
1918
2019 path := "./test-logs/log.json"
2120
22- t .Cleanup (func () { _ = os .RemoveAll ("./test-logs" ) })
23-
2421 file , err := utils .CreateLogFile (path )
25- defer func () {
22+ t . Cleanup ( func () {
2623 if file != nil {
2724 _ = file .Close ()
2825 }
29- }()
26+
27+ _ = os .RemoveAll ("./test-logs" )
28+ })
3029
3130 req .NoError (err )
3231 req .NotNil (file )
@@ -39,13 +38,11 @@ func TestFileExistsSuccess(t *testing.T) {
3938 req := require .New (t )
4039 path := "./log.json"
4140 file , err := utils .CreateLogFile (path )
42- defer func () {
41+ t . Cleanup ( func () {
4342 if file != nil {
4443 _ = file .Close ()
4544 }
46- }()
4745
48- t .Cleanup (func () {
4946 _ = os .Remove (path )
5047 })
5148
@@ -75,12 +72,7 @@ func TestCreateDirectory(t *testing.T) {
7572 t .Parallel ()
7673 req := require .New (t )
7774
78- // Use a temporary directory to avoid conflicts
79- tmp , err := os .MkdirTemp ("" , "test-create-dir-*" )
80- req .NoError (err )
81- defer func () { _ = os .RemoveAll (tmp ) }()
82-
83- testDir := filepath .Join (tmp , "test-dir" )
75+ testDir := filepath .Join (t .TempDir (), "test-dir" )
8476 path , err := utils .CreateDirectory (testDir , 0o744 )
8577
8678 req .NoError (err )
@@ -94,14 +86,11 @@ func TestGetAbsolutePath_RelativeAbsoluteAndError(t *testing.T) {
9486
9587 origWD , err := os .Getwd ()
9688 req .NoError (err )
97- defer func () { _ = os .Chdir (origWD ) }( )
89+ t . Cleanup ( func () { t .Chdir (origWD ) })
9890
99- tmp , err := os .MkdirTemp ("" , "abs-test-*" )
100- req .NoError (err )
101- defer func () { _ = os .RemoveAll (tmp ) }()
91+ tmp := t .TempDir ()
92+ t .Chdir (tmp )
10293
103- // Change into tmp and test relative path success
104- req .NoError (os .Chdir (tmp ))
10594 rel := "sub/dir/file.txt"
10695 got , err := utils .GetAbsolutePath (rel )
10796 req .NoError (err )
@@ -113,14 +102,10 @@ func TestGetAbsolutePath_RelativeAbsoluteAndError(t *testing.T) {
113102 req .NoError (err )
114103 req .Equal (tmp , got2 )
115104
116- // Induce error by removing the current directory before calling
117- req .NoError (os .Chdir (tmp ))
105+ t .Chdir (tmp )
118106 req .NoError (os .RemoveAll (tmp ))
119107 _ , err = utils .GetAbsolutePath ("anything" )
120108 req .Error (err )
121-
122- // Restore WD
123- req .NoError (os .Chdir (origWD ))
124109}
125110
126111func TestCreateDirectoryFromFile_SuccessAndError (t * testing.T ) {
@@ -179,6 +164,7 @@ func TestCreateFile_CreateReopenAndError(t *testing.T) {
179164 b , err := io .ReadAll (f2 )
180165 req .NoError (err )
181166 req .Equal ("hello" , string (b ))
167+ req .NoError (f2 .Close ())
182168
183169 // Error path via invalid CWD so CreateDirectoryFromFile fails
184170 req .NoError (os .Chdir (tmp ))
@@ -187,7 +173,7 @@ func TestCreateFile_CreateReopenAndError(t *testing.T) {
187173 req .Error (err )
188174
189175 // Restore
190- req .NoError (os .Chdir (origWD ))
176+ req .NoError (os .Chdir (origWD )) //nolint:usetesting
191177}
192178
193179func TestGenerateRandomPassword (t * testing.T ) {
@@ -406,71 +392,6 @@ func TestWorkingDir_ErrorHandling(t *testing.T) {
406392 req .Equal (tmp , wd )
407393}
408394
409- func TestProjectRootDir_ErrorCases (t * testing.T ) {
410- // This test manipulates the working directory, so it can't be parallel
411- req := require .New (t )
412-
413- // Save original working directory
414- origWD , err := os .Getwd ()
415- req .NoError (err )
416- defer func () { _ = os .Chdir (origWD ) }()
417-
418- // Test normal operation first - should work and populate cache
419- root := utils .ProjectRootDir (t )
420- req .NotEmpty (root )
421- req .True (filepath .IsAbs (root ))
422-
423- // Test cache hit by calling again
424- root2 := utils .ProjectRootDir (t )
425- req .Equal (root , root2 )
426-
427- // Test from a subdirectory of the project
428- tmp , err := os .MkdirTemp (root , "test-subdir-*" )
429- req .NoError (err )
430- defer func () { _ = os .RemoveAll (tmp ) }()
431-
432- // Change to subdirectory
433- req .NoError (os .Chdir (tmp ))
434-
435- // Should still find the project root
436- root3 := utils .ProjectRootDir (t )
437- req .Equal (root , root3 )
438- }
439-
440- func TestFindFile_ErrorCases (t * testing.T ) {
441- // This test manipulates the working directory
442- req := require .New (t )
443-
444- // Save original working directory
445- origWD , err := os .Getwd ()
446- req .NoError (err )
447- defer func () { _ = os .Chdir (origWD ) }()
448-
449- // Test normal case - finding go.mod should work
450- dir := utils .FindFile (t , "go.mod" )
451- req .NotEmpty (dir )
452- req .True (filepath .IsAbs (dir ))
453-
454- // Test finding a file in current directory
455- projectRoot := utils .ProjectRootDir (t )
456- testFile := filepath .Join (projectRoot , "test-find-file.tmp" )
457- f , err := os .Create (testFile )
458- req .NoError (err )
459- _ = f .Close ()
460- defer func () { _ = os .Remove (testFile ) }()
461-
462- // Change to a subdirectory
463- tmp , err := os .MkdirTemp (projectRoot , "test-find-subdir-*" )
464- req .NoError (err )
465- defer func () { _ = os .RemoveAll (tmp ) }()
466-
467- req .NoError (os .Chdir (tmp ))
468-
469- // Should find the file by walking up
470- foundDir := utils .FindFile (t , "test-find-file.tmp" )
471- req .Equal (projectRoot , foundDir )
472- }
473-
474395// Test that demonstrates the GetLocalIP and GetLocalIPs error handling
475396func TestGetLocalIP_ErrorPath (t * testing.T ) {
476397 t .Parallel ()
@@ -576,25 +497,6 @@ func TestFindFile_ReadDirError(t *testing.T) {
576497 req .Equal (projectRoot , found )
577498}
578499
579- func TestProjectRootDir_CacheEdgeCases (t * testing.T ) {
580- t .Parallel ()
581- req := require .New (t )
582-
583- // Test multiple calls to ensure cache works properly
584- root1 := utils .ProjectRootDir (t )
585- root2 := utils .ProjectRootDir (t )
586- root3 := utils .ProjectRootDir (t )
587-
588- req .Equal (root1 , root2 )
589- req .Equal (root2 , root3 )
590- req .NotEmpty (root1 )
591- req .True (filepath .IsAbs (root1 ))
592-
593- // Verify go.mod exists in the root
594- goModPath := filepath .Join (root1 , "go.mod" )
595- req .FileExists (goModPath )
596- }
597-
598500func TestWorkingDir_Success (t * testing.T ) {
599501 t .Parallel ()
600502 req := require .New (t )
0 commit comments