1
- package system
1
+ package system_test
2
2
3
3
import (
4
4
"os"
8
8
"time"
9
9
10
10
"github.com/jrswab/lsq/config"
11
+ "github.com/jrswab/lsq/system"
11
12
i "github.com/jrswab/lsq/tests/integration"
12
13
)
13
14
@@ -93,7 +94,7 @@ func TestBasicJournalCreation(t *testing.T) {
93
94
}
94
95
}
95
96
96
- cfg , err := LoadConfig (tc .helper .ConfigPath )
97
+ cfg , err := system . LoadConfig (tc .helper .ConfigPath )
97
98
if err != nil {
98
99
t .Fatalf ("Failed to load config file: %v" , err )
99
100
}
@@ -114,7 +115,7 @@ func TestBasicJournalCreation(t *testing.T) {
114
115
}
115
116
116
117
// Get journal path and create the journal entry if needed
117
- expectedPath , err := GetJournal (cfg , helper .JournalsDir , date )
118
+ expectedPath , err := system . GetJournal (cfg , helper .JournalsDir , date )
118
119
if err != nil {
119
120
t .Fatalf ("Failed to get journal file: %v" , err )
120
121
}
@@ -135,3 +136,60 @@ func TestBasicJournalCreation(t *testing.T) {
135
136
})
136
137
}
137
138
}
139
+
140
+ func TestAppendToFile (t * testing.T ) {
141
+ tmpDir , err := os .MkdirTemp ("" , "test" )
142
+ if err != nil {
143
+ t .Fatal (err )
144
+ }
145
+ defer os .RemoveAll (tmpDir )
146
+
147
+ t .Run ("new file creation" , func (t * testing.T ) {
148
+ testFile := filepath .Join (tmpDir , "new.md" )
149
+ if err := system .AppendToFile (testFile , "new content" ); err != nil {
150
+ t .Errorf ("Failed to create new file: %v" , err )
151
+ }
152
+
153
+ content , err := os .ReadFile (testFile )
154
+ if err != nil {
155
+ t .Fatal (err )
156
+ }
157
+ expected := "- new content"
158
+ if string (content ) != expected {
159
+ t .Errorf ("Expected %q, got %q" , expected , string (content ))
160
+ }
161
+ })
162
+
163
+ t .Run ("append to existing" , func (t * testing.T ) {
164
+ testFile := filepath .Join (tmpDir , "existing.md" )
165
+ if err := system .AppendToFile (testFile , "first" ); err != nil {
166
+ t .Fatal (err )
167
+ }
168
+ if err := system .AppendToFile (testFile , "second" ); err != nil {
169
+ t .Errorf ("Failed to append: %v" , err )
170
+ }
171
+
172
+ content , err := os .ReadFile (testFile )
173
+ if err != nil {
174
+ t .Fatal (err )
175
+ }
176
+ expected := "- first\n - second"
177
+ if string (content ) != expected {
178
+ t .Errorf ("Expected %q, got %q" , expected , string (content ))
179
+ }
180
+ })
181
+
182
+ t .Run ("permission denied" , func (t * testing.T ) {
183
+ readOnlyDir := filepath .Join (tmpDir , "readonly" )
184
+ if err := os .Mkdir (readOnlyDir , 0500 ); err != nil {
185
+ t .Fatal (err )
186
+ }
187
+
188
+ testFile := filepath .Join (readOnlyDir , "test.md" )
189
+ err := system .AppendToFile (testFile , "content" )
190
+ if err == nil {
191
+ t .Error ("Expected error for read-only directory" )
192
+ }
193
+ })
194
+ }
195
+
0 commit comments