Skip to content

Commit 44a5f20

Browse files
committed
test: default file system
1 parent 1c800e5 commit 44a5f20

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

internal/utils/filesystem.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,27 @@ func CreateFolderIfMissing(dir string) error {
2626

2727
return nil
2828
}
29+
30+
type FileSystem interface {
31+
Executable() (string, error)
32+
ReadFile(name string) ([]byte, error)
33+
WriteFile(name string, data []byte, perm os.FileMode) error
34+
}
35+
36+
type defaultFileSystem struct{}
37+
38+
func (*defaultFileSystem) Executable() (string, error) {
39+
return os.Executable()
40+
}
41+
42+
func (*defaultFileSystem) ReadFile(name string) ([]byte, error) {
43+
return os.ReadFile(name)
44+
}
45+
46+
func (*defaultFileSystem) WriteFile(name string, data []byte, perm os.FileMode) error {
47+
return os.WriteFile(name, data, perm)
48+
}
49+
50+
func NewFileSystem() FileSystem {
51+
return &defaultFileSystem{}
52+
}

internal/utils/filesystem_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
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+
2236
func 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

Comments
 (0)