-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathadd_test.go
More file actions
340 lines (287 loc) · 9.95 KB
/
add_test.go
File metadata and controls
340 lines (287 loc) · 9.95 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package cmd
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/substantialcattle5/sietch/testutil"
)
func TestParseFileArguments(t *testing.T) {
tests := []struct {
name string
args []string
expected []FilePair
expectError bool
}{
{
name: "single file pair",
args: []string{"source.txt", "dest/"},
expected: []FilePair{
{Source: "source.txt", Destination: "dest/"},
},
expectError: false,
},
{
name: "multiple paired arguments",
args: []string{"file1.txt", "dest1/", "file2.txt", "dest2/"},
expected: []FilePair{
{Source: "file1.txt", Destination: "dest1/"},
{Source: "file2.txt", Destination: "dest2/"},
},
expectError: false,
},
{
name: "even number of args - paired pattern",
args: []string{"file1.txt", "file2.txt", "file3.txt", "dest/"},
expected: []FilePair{
{Source: "file1.txt", Destination: "file2.txt"},
{Source: "file3.txt", Destination: "dest/"},
},
expectError: false,
},
{
name: "insufficient arguments",
args: []string{"source.txt"},
expected: nil,
expectError: true,
},
{
name: "complex file paths",
args: []string{"/path/to/file1.txt", "/another/path/dest1/", "~/file2.txt", "./dest2/"},
expected: []FilePair{
{Source: "/path/to/file1.txt", Destination: "/another/path/dest1/"},
{Source: "~/file2.txt", Destination: "./dest2/"},
},
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parseFileArguments(tt.args)
if tt.expectError {
if err == nil {
t.Errorf("Expected error but got none")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if len(result) != len(tt.expected) {
t.Errorf("Expected %d pairs, got %d", len(tt.expected), len(result))
return
}
for i, expected := range tt.expected {
if result[i].Source != expected.Source {
t.Errorf("Pair %d: expected source %s, got %s", i, expected.Source, result[i].Source)
}
if result[i].Destination != expected.Destination {
t.Errorf("Pair %d: expected destination %s, got %s", i, expected.Destination, result[i].Destination)
}
}
})
}
}
func TestAddCommandUsageText(t *testing.T) {
// Check that usage text reflects multiple file support
usageText := addCmd.Use
if !strings.Contains(usageText, "[source_path2] [destination_path2]...") {
t.Errorf("Usage text should indicate multiple file support, got: %s", usageText)
}
}
func TestAddCommandLongDescription(t *testing.T) {
// Check that long description contains multiple file support information
longText := addCmd.Long
expectedPhrases := []string{
"multiple files",
"Paired arguments",
"Single destination",
"source1 dest1 source2 dest2",
"source1 source2 ... dest",
}
for _, phrase := range expectedPhrases {
if !strings.Contains(longText, phrase) {
t.Errorf("Long description should contain '%s'", phrase)
}
}
}
func TestAddCommandShortDescription(t *testing.T) {
// Check that short description reflects multiple file support
shortText := addCmd.Short
if !strings.Contains(shortText, "one or more files") {
t.Errorf("Short description should indicate multiple file support, got: %s", shortText)
}
}
func TestAddCommandWithMockVault(t *testing.T) {
testutil.SkipIfShort(t, "integration test")
// Create a mock vault for testing
mockConfig := testutil.NewMockConfig(t, "add-test")
mockConfig.SetupTestVault(t)
// Create test files
testFile1 := testutil.CreateTestFile(t, mockConfig.VaultPath, "test1.txt", "test content 1")
testFile2 := testutil.CreateTestFile(t, mockConfig.VaultPath, "test2.txt", "test content 2")
// Change to vault directory
originalDir, _ := os.Getwd()
os.Chdir(mockConfig.VaultPath)
defer os.Chdir(originalDir)
// Test multiple file addition (this would require more setup for full integration)
// For now, we test that the argument parsing works correctly
args := []string{testFile1, "docs/", testFile2, "data/"}
filePairs, err := parseFileArguments(args)
if err != nil {
t.Fatalf("Failed to parse arguments: %v", err)
}
expected := []FilePair{
{Source: testFile1, Destination: "docs/"},
{Source: testFile2, Destination: "data/"},
}
if len(filePairs) != len(expected) {
t.Fatalf("Expected %d pairs, got %d", len(expected), len(filePairs))
}
for i, expected := range expected {
if filePairs[i].Source != expected.Source {
t.Errorf("Pair %d: expected source %s, got %s", i, expected.Source, filePairs[i].Source)
}
if filePairs[i].Destination != expected.Destination {
t.Errorf("Pair %d: expected destination %s, got %s", i, expected.Destination, filePairs[i].Destination)
}
}
}
func TestAddCommandErrorHandling(t *testing.T) {
// Test error handling for various scenarios
tests := []struct {
name string
args []string
setupFunc func(t *testing.T, dir string) // Function to set up test scenario
expectError bool
}{
{
name: "nonexistent source file",
args: []string{"/nonexistent/file.txt", "dest/"},
setupFunc: func(t *testing.T, dir string) {
// No setup needed - file should not exist
},
expectError: true,
},
{
name: "directory as source",
args: []string{"."}, // Current directory
setupFunc: func(t *testing.T, dir string) {
// Current directory exists but is a directory
},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := testutil.TempDir(t, "error-test")
defer os.RemoveAll(tempDir)
// Run setup if provided
if tt.setupFunc != nil {
tt.setupFunc(t, tempDir)
}
// Change to temp directory
originalDir, _ := os.Getwd()
os.Chdir(tempDir)
defer os.Chdir(originalDir)
// Test that argument parsing works (even if file operations fail later)
_, err := parseFileArguments(tt.args)
if err != nil && !tt.expectError {
t.Errorf("Unexpected error in argument parsing: %v", err)
}
})
}
}
func TestAddCommandBatchProcessingOutput(t *testing.T) {
// Test that batch processing shows appropriate progress messages
// This is a unit test that focuses on the output formatting logic
tempDir := testutil.TempDir(t, "output-test")
defer os.RemoveAll(tempDir)
// Create test files
testFiles := make([]string, 3)
for i := 0; i < 3; i++ {
testFiles[i] = testutil.CreateTestFile(t, tempDir, "test"+string(rune('1'+i))+".txt", "content "+string(rune('1'+i)))
}
// Test paired arguments
pairedArgs := []string{testFiles[0], "dest1/", testFiles[1], "dest2/"}
filePairs, err := parseFileArguments(pairedArgs)
if err != nil {
t.Fatalf("Failed to parse paired arguments: %v", err)
}
if len(filePairs) != 2 {
t.Errorf("Expected 2 pairs, got %d", len(filePairs))
}
// Test single destination arguments
singleDestArgs := []string{testFiles[0], testFiles[1], "common-dest/"}
filePairs2, err := parseFileArguments(singleDestArgs)
if err != nil {
t.Fatalf("Failed to parse single destination arguments: %v", err)
}
if len(filePairs2) != 2 {
t.Errorf("Expected 2 pairs, got %d", len(filePairs2))
}
// Verify all files go to same destination
for _, pair := range filePairs2 {
if pair.Destination != "common-dest/" {
t.Errorf("Expected destination 'common-dest/', got '%s'", pair.Destination)
}
}
}
func TestAddCommandFilenameBugFix(t *testing.T) {
// Test for the bug fix: files with same basename from different directories
// should be stored with correct destination filenames, not source basenames
testutil.SkipIfShort(t, "integration test")
// Create a mock vault for testing
mockConfig := testutil.NewMockConfig(t, "filename-bug-test")
mockConfig.SetupTestVault(t)
// Create test files with same basename in different directories
sourceDir1 := testutil.TempDir(t, "source1")
sourceDir2 := testutil.TempDir(t, "source2")
testFile1 := testutil.CreateTestFile(t, sourceDir1, "test.txt", "content from dir 1")
testFile2 := testutil.CreateTestFile(t, sourceDir2, "test.txt", "content from dir 2")
// Change to vault directory
originalDir, _ := os.Getwd()
os.Chdir(mockConfig.VaultPath)
defer os.Chdir(originalDir)
// Test adding files with same basename to different destinations
// This tests the fix for the bug where filepath.Base(pair.Source) was used
// instead of destFileName for manifest storage
args := []string{testFile1, "docs/file1.txt", testFile2, "data/file2.txt"}
// Parse arguments to verify they are correct
filePairs, err := parseFileArguments(args)
if err != nil {
t.Fatalf("Failed to parse arguments: %v", err)
}
expected := []FilePair{
{Source: testFile1, Destination: "docs/file1.txt"},
{Source: testFile2, Destination: "data/file2.txt"},
}
if len(filePairs) != len(expected) {
t.Fatalf("Expected %d pairs, got %d", len(expected), len(filePairs))
}
// Verify the destinations are different (this is what the bug fix ensures)
if filePairs[0].Destination == filePairs[1].Destination {
t.Errorf("Bug not fixed: both files have same destination %s", filePairs[0].Destination)
}
// Verify that the destination filenames are different
destFileName1 := filepath.Base(filePairs[0].Destination)
destFileName2 := filepath.Base(filePairs[1].Destination)
if destFileName1 == destFileName2 {
t.Errorf("Bug not fixed: both files have same destination filename %s", destFileName1)
}
// Verify that source basenames are the same (this was causing the bug)
sourceBaseName1 := filepath.Base(filePairs[0].Source)
sourceBaseName2 := filepath.Base(filePairs[1].Source)
if sourceBaseName1 != sourceBaseName2 {
t.Errorf("Test setup error: source basenames should be same, got %s vs %s", sourceBaseName1, sourceBaseName2)
}
if sourceBaseName1 != "test.txt" {
t.Errorf("Test setup error: expected source basename 'test.txt', got %s", sourceBaseName1)
}
// The key test: destination filenames should be different
if destFileName1 != "file1.txt" || destFileName2 != "file2.txt" {
t.Errorf("Bug not fixed: expected destination filenames 'file1.txt' and 'file2.txt', got '%s' and '%s'",
destFileName1, destFileName2)
}
}