-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_system_test.go
109 lines (90 loc) · 2.5 KB
/
file_system_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"errors"
"os"
"testing"
)
func TestSetupConfigFile_CreatesFileIfNotExist(t *testing.T) {
fs := &MockFileSystem{
homeDir: "/mock/home",
statError: os.ErrNotExist,
createErr: nil,
}
err := createConfigFileIfMissing(fs)
if err != nil {
t.Fatalf("Failed to create config file: %v", err)
}
}
func TestSetupConfigFile_DoesNotCreateFileIfExists(t *testing.T) {
fs := &MockFileSystem{
homeDir: "/mock/home",
statError: nil,
createErr: nil,
}
err := createConfigFileIfMissing(fs)
if err != nil {
t.Fatalf("Failed to create config file: %v", err)
}
}
func TestSetupConfigFile_HandleUserHomeDirError(t *testing.T) {
fs := &MockFileSystem{
homeDir: "",
statError: nil,
createErr: nil,
}
err := createConfigFileIfMissing(fs)
if err != nil {
t.Fatalf("Failed to create config file: %v", err)
}
}
func TestReadConfigFileString_Success(t *testing.T) {
mockFS := &MockFileSystem{
configFileContent: "com.apple.dock autohide 1\ncom.apple.finder ShowPathbar true\n",
}
content, err := readConfigFileString(mockFS)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
expectedContent := "com.apple.dock autohide 1\ncom.apple.finder ShowPathbar true\n"
if content != expectedContent {
t.Errorf("Expected content %q, got %q", expectedContent, content)
}
}
func TestReadConfigFileString_Empty(t *testing.T) {
mockFS := &MockFileSystem{
configFileContent: "",
}
content, err := readConfigFileString(mockFS)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
expectedContent := ""
if content != expectedContent {
t.Errorf("Expected content %q, got %q", expectedContent, content)
}
}
func TestReadConfigFileString_Error(t *testing.T) {
mockFS := &MockFileSystem{
statError: errors.New("read error"),
}
_, err := readConfigFileString(mockFS)
if err == nil {
t.Fatal("Expected error, got nil")
}
if !errors.Is(err, mockFS.statError) {
t.Errorf("Expected error %v, got %v", mockFS.statError, err)
}
}
func TestReadConfigFileString_MalformedContent(t *testing.T) {
mockFS := &MockFileSystem{
configFileContent: "com.apple.dock autohide\nmalformed line without key\ncom.apple.finder ShowPathbar true\n",
}
content, err := readConfigFileString(mockFS)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
expectedContent := "com.apple.dock autohide\nmalformed line without key\ncom.apple.finder ShowPathbar true\n"
if content != expectedContent {
t.Errorf("Expected content %q, got %q", expectedContent, content)
}
}