Skip to content

Commit 606e896

Browse files
committed
Add filesystem package with OSFileSystem implementation and MockFileSystem for testing
1 parent 56adbfe commit 606e896

File tree

4 files changed

+66
-58
lines changed

4 files changed

+66
-58
lines changed
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package filesystem
22

33
import (
44
"os"
@@ -15,25 +15,31 @@ type FileSystem interface {
1515
WriteFile(name string, content string) error
1616
}
1717

18-
type fileSystem struct{}
18+
// OSFileSystem is a concrete implementation of the FileSystem interface
19+
type OSFileSystem struct{}
1920

20-
func (f *fileSystem) UserHomeDir() (string, error) {
21+
// NewOSFileSystem creates a new instance of OSFileSystem
22+
func NewOSFileSystem() *OSFileSystem {
23+
return &OSFileSystem{}
24+
}
25+
26+
func (f *OSFileSystem) UserHomeDir() (string, error) {
2127
return os.UserHomeDir()
2228
}
2329

24-
func (f *fileSystem) Stat(name string) (os.FileInfo, error) {
30+
func (f *OSFileSystem) Stat(name string) (os.FileInfo, error) {
2531
return os.Stat(name)
2632
}
2733

28-
func (f *fileSystem) Create(name string) (*os.File, error) {
34+
func (f *OSFileSystem) Create(name string) (*os.File, error) {
2935
return os.Create(name)
3036
}
3137

32-
func (f *fileSystem) WriteFile(name string, content string) error {
38+
func (f *OSFileSystem) WriteFile(name string, content string) error {
3339
return os.WriteFile(name, []byte(content), 0644)
3440
}
3541

36-
func (f *fileSystem) ReadFile(name string) (string, error) {
42+
func (f *OSFileSystem) ReadFile(name string) (string, error) {
3743
content, err := os.ReadFile(name)
3844
if err != nil {
3945
return "", err
@@ -42,7 +48,7 @@ func (f *fileSystem) ReadFile(name string) (string, error) {
4248
}
4349

4450
// createConfigFileIfMissing checks for the existence of the config file and creates it if it doesn't exist
45-
func createConfigFileIfMissing(fs FileSystem) error {
51+
func CreateConfigFileIfMissing(fs FileSystem) error {
4652
if _, err := fs.Stat(config.ConfigFilePath); os.IsNotExist(err) {
4753
file, err := fs.Create(config.ConfigFilePath)
4854
if err != nil {
@@ -54,6 +60,6 @@ func createConfigFileIfMissing(fs FileSystem) error {
5460
}
5561

5662
// readConfigFileString reads the config file and returns its content as a string
57-
func readConfigFileString(fs FileSystem) (string, error) {
63+
func ReadConfigFileString(fs FileSystem) (string, error) {
5864
return fs.ReadFile(config.ConfigFilePath)
5965
}
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
package main
1+
package filesystem
22

33
import (
44
"os"
55
)
66

77
// MockFileSystem is a mock implementation of the FileSystem interface
88
type MockFileSystem struct {
9-
homeDir string
10-
statError error
11-
createErr error
12-
configFileContent string
13-
writeFileErr error
14-
writeFileContent string
9+
HomeDir string
10+
StatError error
11+
CreateErr error
12+
ConfigFileContent string
13+
WriteFileErr error
14+
WriteFileContent string
1515
}
1616

1717
func (m *MockFileSystem) UserHomeDir() (string, error) {
18-
return m.homeDir, nil
18+
return m.HomeDir, nil
1919
}
2020

2121
func (m *MockFileSystem) Stat(name string) (os.FileInfo, error) {
22-
if m.statError != nil {
23-
return nil, m.statError
22+
if m.StatError != nil {
23+
return nil, m.StatError
2424
}
25-
if m.configFileContent != "" {
25+
if m.ConfigFileContent != "" {
2626
return nil, os.ErrNotExist
2727
}
2828
return nil, nil
2929
}
3030

3131
func (m *MockFileSystem) Create(name string) (*os.File, error) {
32-
return nil, m.createErr
32+
return nil, m.CreateErr
3333
}
3434

3535
func (m *MockFileSystem) ReadFile(name string) (string, error) {
36-
if m.statError != nil {
37-
return "", m.statError
36+
if m.StatError != nil {
37+
return "", m.StatError
3838
}
39-
return m.configFileContent, nil
39+
return m.ConfigFileContent, nil
4040
}
4141

4242
func (m *MockFileSystem) WriteFile(name string, content string) error {
43-
m.writeFileContent = content
44-
return m.writeFileErr
45-
}
43+
m.WriteFileContent = content
44+
return m.WriteFileErr
45+
}
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,58 @@
1-
package main
1+
package filesystem_test
22

33
import (
44
"errors"
55
"os"
66
"testing"
7+
8+
"github.com/fumiya-kume/mdefaults/internal/filesystem"
79
)
810

911
func TestSetupConfigFile_CreatesFileIfNotExist(t *testing.T) {
10-
fs := &MockFileSystem{
11-
homeDir: "/mock/home",
12-
statError: os.ErrNotExist,
13-
createErr: nil,
12+
fs := &filesystem.MockFileSystem{
13+
HomeDir: "/mock/home",
14+
StatError: os.ErrNotExist,
15+
CreateErr: nil,
1416
}
1517

16-
err := createConfigFileIfMissing(fs)
18+
err := filesystem.CreateConfigFileIfMissing(fs)
1719
if err != nil {
1820
t.Fatalf("Failed to create config file: %v", err)
1921
}
2022
}
2123

2224
func TestSetupConfigFile_DoesNotCreateFileIfExists(t *testing.T) {
23-
fs := &MockFileSystem{
24-
homeDir: "/mock/home",
25-
statError: nil,
26-
createErr: nil,
25+
fs := &filesystem.MockFileSystem{
26+
HomeDir: "/mock/home",
27+
StatError: nil,
28+
CreateErr: nil,
2729
}
2830

29-
err := createConfigFileIfMissing(fs)
31+
err := filesystem.CreateConfigFileIfMissing(fs)
3032
if err != nil {
3133
t.Fatalf("Failed to create config file: %v", err)
3234
}
3335
}
3436

3537
func TestSetupConfigFile_HandleUserHomeDirError(t *testing.T) {
36-
fs := &MockFileSystem{
37-
homeDir: "",
38-
statError: nil,
39-
createErr: nil,
38+
fs := &filesystem.MockFileSystem{
39+
HomeDir: "",
40+
StatError: nil,
41+
CreateErr: nil,
4042
}
4143

42-
err := createConfigFileIfMissing(fs)
44+
err := filesystem.CreateConfigFileIfMissing(fs)
4345
if err != nil {
4446
t.Fatalf("Failed to create config file: %v", err)
4547
}
4648
}
4749

4850
func TestReadConfigFileString_Success(t *testing.T) {
49-
mockFS := &MockFileSystem{
50-
configFileContent: "com.apple.dock autohide 1\ncom.apple.finder ShowPathbar true\n",
51+
mockFS := &filesystem.MockFileSystem{
52+
ConfigFileContent: "com.apple.dock autohide 1\ncom.apple.finder ShowPathbar true\n",
5153
}
5254

53-
content, err := readConfigFileString(mockFS)
55+
content, err := filesystem.ReadConfigFileString(mockFS)
5456
if err != nil {
5557
t.Fatalf("Expected no error, got %v", err)
5658
}
@@ -62,11 +64,11 @@ func TestReadConfigFileString_Success(t *testing.T) {
6264
}
6365

6466
func TestReadConfigFileString_Empty(t *testing.T) {
65-
mockFS := &MockFileSystem{
66-
configFileContent: "",
67+
mockFS := &filesystem.MockFileSystem{
68+
ConfigFileContent: "",
6769
}
6870

69-
content, err := readConfigFileString(mockFS)
71+
content, err := filesystem.ReadConfigFileString(mockFS)
7072
if err != nil {
7173
t.Fatalf("Expected no error, got %v", err)
7274
}
@@ -78,26 +80,26 @@ func TestReadConfigFileString_Empty(t *testing.T) {
7880
}
7981

8082
func TestReadConfigFileString_Error(t *testing.T) {
81-
mockFS := &MockFileSystem{
82-
statError: errors.New("read error"),
83+
mockFS := &filesystem.MockFileSystem{
84+
StatError: errors.New("read error"),
8385
}
8486

85-
_, err := readConfigFileString(mockFS)
87+
_, err := filesystem.ReadConfigFileString(mockFS)
8688
if err == nil {
8789
t.Fatal("Expected error, got nil")
8890
}
8991

90-
if !errors.Is(err, mockFS.statError) {
91-
t.Errorf("Expected error %v, got %v", mockFS.statError, err)
92+
if !errors.Is(err, mockFS.StatError) {
93+
t.Errorf("Expected error %v, got %v", mockFS.StatError, err)
9294
}
9395
}
9496

9597
func TestReadConfigFileString_MalformedContent(t *testing.T) {
96-
mockFS := &MockFileSystem{
97-
configFileContent: "com.apple.dock autohide\nmalformed line without key\ncom.apple.finder ShowPathbar true\n",
98+
mockFS := &filesystem.MockFileSystem{
99+
ConfigFileContent: "com.apple.dock autohide\nmalformed line without key\ncom.apple.finder ShowPathbar true\n",
98100
}
99101

100-
content, err := readConfigFileString(mockFS)
102+
content, err := filesystem.ReadConfigFileString(mockFS)
101103
if err != nil {
102104
t.Fatalf("Expected no error, got %v", err)
103105
}
@@ -106,4 +108,4 @@ func TestReadConfigFileString_MalformedContent(t *testing.T) {
106108
if content != expectedContent {
107109
t.Errorf("Expected content %q, got %q", expectedContent, content)
108110
}
109-
}
111+
}

0 commit comments

Comments
 (0)