11package utils
22
33import (
4+ "os"
45 "path/filepath"
56 "testing"
67)
@@ -19,6 +20,19 @@ func TestDirExists(t *testing.T) {
1920 }
2021}
2122
23+ func TestDirExists_File (t * testing.T ) {
24+ tmpDir := t .TempDir ()
25+ filePath := filepath .Join (tmpDir , "file.txt" )
26+
27+ if err := os .WriteFile (filePath , []byte ("data" ), DefaultFileMod ); err != nil {
28+ t .Fatal (err )
29+ }
30+
31+ if DirExists (filePath ) {
32+ t .Errorf ("expected false for a file path, got true" )
33+ }
34+ }
35+
2236func TestCreateFolderIfMissing (t * testing.T ) {
2337 tmpDir := t .TempDir ()
2438 newDir := filepath .Join (tmpDir , "new_folder" )
@@ -39,3 +53,74 @@ func TestCreateFolderIfMissing(t *testing.T) {
3953 t .Fatalf ("expected no error, got %v" , err )
4054 }
4155}
56+
57+ // defaultFileSystem tests
58+
59+ func TestNewFileSystem_ReturnsNonNil (t * testing.T ) {
60+ fs := NewFileSystem ()
61+ if fs == nil {
62+ t .Fatal ("expected non-nil FileSystem" )
63+ }
64+ }
65+
66+ func TestDefaultFileSystem_Executable (t * testing.T ) {
67+ fs := NewFileSystem ()
68+
69+ path , err := fs .Executable ()
70+
71+ if err != nil {
72+ t .Fatalf ("expected no error, got %v" , err )
73+ }
74+ if path == "" {
75+ t .Error ("expected non-empty executable path" )
76+ }
77+ }
78+
79+ func TestDefaultFileSystem_WriteFile (t * testing.T ) {
80+ fs := NewFileSystem ()
81+ filePath := filepath .Join (t .TempDir (), "test.txt" )
82+ data := []byte ("hello" )
83+
84+ err := fs .WriteFile (filePath , data , DefaultFileMod )
85+
86+ if err != nil {
87+ t .Fatalf ("expected no error, got %v" , err )
88+ }
89+
90+ got , err := os .ReadFile (filePath )
91+ if err != nil {
92+ t .Fatalf ("failed to read back file: %v" , err )
93+ }
94+ if string (got ) != string (data ) {
95+ t .Errorf ("expected %q, got %q" , data , got )
96+ }
97+ }
98+
99+ func TestDefaultFileSystem_ReadFile (t * testing.T ) {
100+ fs := NewFileSystem ()
101+ filePath := filepath .Join (t .TempDir (), "test.txt" )
102+ data := []byte ("hello" )
103+
104+ if err := os .WriteFile (filePath , data , DefaultFileMod ); err != nil {
105+ t .Fatal (err )
106+ }
107+
108+ got , err := fs .ReadFile (filePath )
109+
110+ if err != nil {
111+ t .Fatalf ("expected no error, got %v" , err )
112+ }
113+ if string (got ) != string (data ) {
114+ t .Errorf ("expected %q, got %q" , data , got )
115+ }
116+ }
117+
118+ func TestDefaultFileSystem_ReadFile_NotFound (t * testing.T ) {
119+ fs := NewFileSystem ()
120+
121+ _ , err := fs .ReadFile (filepath .Join (t .TempDir (), "nonexistent.txt" ))
122+
123+ if err == nil {
124+ t .Error ("expected error reading nonexistent file, got nil" )
125+ }
126+ }
0 commit comments