|
1
|
|
|
package main |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"os" |
|
5
|
|
|
"path/filepath" |
|
6
|
|
|
"testing" |
|
7
|
|
|
) |
|
8
|
|
|
|
|
9
|
|
|
func TestJoiner(t *testing.T) { |
|
10
|
|
|
// Disable progress bar for tests |
|
11
|
|
|
displayProgress = false |
|
12
|
|
|
|
|
13
|
|
|
// Create temporary directory for test files |
|
14
|
|
|
tempDir := t.TempDir() |
|
15
|
|
|
|
|
16
|
|
|
// Create test files |
|
17
|
|
|
file1Path := filepath.Join(tempDir, "file1") |
|
18
|
|
|
file2Path := filepath.Join(tempDir, "file2") |
|
19
|
|
|
file3Path := filepath.Join(tempDir, "file3") |
|
20
|
|
|
joinedPath := filepath.Join(tempDir, "joined") |
|
21
|
|
|
|
|
22
|
|
|
file1Content := "content of file 1" |
|
23
|
|
|
file2Content := "content of file 2" |
|
24
|
|
|
file3Content := "content of file 3" |
|
25
|
|
|
|
|
26
|
|
|
err := os.WriteFile(file1Path, []byte(file1Content), 0644) |
|
27
|
|
|
if err != nil { |
|
28
|
|
|
t.Fatalf("Failed to create test file1: %v", err) |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
err = os.WriteFile(file2Path, []byte(file2Content), 0644) |
|
32
|
|
|
if err != nil { |
|
33
|
|
|
t.Fatalf("Failed to create test file2: %v", err) |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
err = os.WriteFile(file3Path, []byte(file3Content), 0644) |
|
37
|
|
|
if err != nil { |
|
38
|
|
|
t.Fatalf("Failed to create test file3: %v", err) |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// Test joining files |
|
42
|
|
|
files := []string{file1Path, file2Path, file3Path} |
|
43
|
|
|
err = JoinFile(files, joinedPath) |
|
44
|
|
|
if err != nil { |
|
45
|
|
|
t.Fatalf("JoinFile() failed: %v", err) |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// Verify joined content |
|
49
|
|
|
joinedContent, err := os.ReadFile(joinedPath) |
|
50
|
|
|
if err != nil { |
|
51
|
|
|
t.Fatalf("Failed to read joined file: %v", err) |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
expectedContent := file1Content + file2Content + file3Content |
|
55
|
|
|
if string(joinedContent) != expectedContent { |
|
56
|
|
|
t.Errorf("Expected joined content '%s', got '%s'", expectedContent, string(joinedContent)) |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
func TestJoinerSorting(t *testing.T) { |
|
61
|
|
|
// Disable progress bar for tests |
|
62
|
|
|
displayProgress = false |
|
63
|
|
|
|
|
64
|
|
|
// Create temporary directory for test files |
|
65
|
|
|
tempDir := t.TempDir() |
|
66
|
|
|
|
|
67
|
|
|
// Create test files with names that need to be sorted |
|
68
|
|
|
fileAPath := filepath.Join(tempDir, "fileC") // Intentionally out of order |
|
69
|
|
|
fileBPath := filepath.Join(tempDir, "fileA") |
|
70
|
|
|
fileCPath := filepath.Join(tempDir, "fileB") |
|
71
|
|
|
joinedPath := filepath.Join(tempDir, "joined") |
|
72
|
|
|
|
|
73
|
|
|
// Write content that makes the sorting order obvious |
|
74
|
|
|
err := os.WriteFile(fileAPath, []byte("C"), 0644) |
|
75
|
|
|
if err != nil { |
|
76
|
|
|
t.Fatalf("Failed to create test file: %v", err) |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
err = os.WriteFile(fileBPath, []byte("A"), 0644) |
|
80
|
|
|
if err != nil { |
|
81
|
|
|
t.Fatalf("Failed to create test file: %v", err) |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
err = os.WriteFile(fileCPath, []byte("B"), 0644) |
|
85
|
|
|
if err != nil { |
|
86
|
|
|
t.Fatalf("Failed to create test file: %v", err) |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// Test joining files |
|
90
|
|
|
files := []string{fileAPath, fileBPath, fileCPath} |
|
91
|
|
|
err = JoinFile(files, joinedPath) |
|
92
|
|
|
if err != nil { |
|
93
|
|
|
t.Fatalf("JoinFile() failed: %v", err) |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Verify joined content based on lexicographical sorting |
|
97
|
|
|
joinedContent, err := os.ReadFile(joinedPath) |
|
98
|
|
|
if err != nil { |
|
99
|
|
|
t.Fatalf("Failed to read joined file: %v", err) |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// Expect files to be joined in alphabetical order (A, B, C) |
|
103
|
|
|
expectedContent := "ABC" |
|
104
|
|
|
if string(joinedContent) != expectedContent { |
|
105
|
|
|
t.Errorf("Expected joined content '%s', got '%s' (files not sorted correctly)", |
|
106
|
|
|
expectedContent, string(joinedContent)) |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
func TestJoinerError(t *testing.T) { |
|
111
|
|
|
// Disable progress bar for tests |
|
112
|
|
|
displayProgress = false |
|
113
|
|
|
|
|
114
|
|
|
// Create temporary directory for test files |
|
115
|
|
|
tempDir := t.TempDir() |
|
116
|
|
|
|
|
117
|
|
|
// Create a test file |
|
118
|
|
|
filePath := filepath.Join(tempDir, "file1") |
|
119
|
|
|
err := os.WriteFile(filePath, []byte("test content"), 0644) |
|
120
|
|
|
if err != nil { |
|
121
|
|
|
t.Fatalf("Failed to create test file: %v", err) |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// Include a non-existent file in the list |
|
125
|
|
|
nonExistentFile := filepath.Join(tempDir, "nonexistent") |
|
126
|
|
|
joinedPath := filepath.Join(tempDir, "joined") |
|
127
|
|
|
|
|
128
|
|
|
// Test joining files |
|
129
|
|
|
files := []string{filePath, nonExistentFile} |
|
130
|
|
|
err = JoinFile(files, joinedPath) |
|
131
|
|
|
|
|
132
|
|
|
// Expect an error because one file doesn't exist |
|
133
|
|
|
if err == nil { |
|
134
|
|
|
t.Errorf("Expected error when joining non-existent file, got nil") |
|
135
|
|
|
// Clean up if test fails |
|
136
|
|
|
os.Remove(joinedPath) |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
func TestJoinerWithProgressBar(t *testing.T) { |
|
141
|
|
|
// Enable progress bar for this test |
|
142
|
|
|
originalDisplayProgress := displayProgress |
|
143
|
|
|
displayProgress = true |
|
144
|
|
|
defer func() { |
|
145
|
|
|
displayProgress = originalDisplayProgress |
|
146
|
|
|
}() |
|
147
|
|
|
|
|
148
|
|
|
// Create temporary directory for test files |
|
149
|
|
|
tempDir := t.TempDir() |
|
150
|
|
|
|
|
151
|
|
|
// Create test files |
|
152
|
|
|
file1Path := filepath.Join(tempDir, "file1") |
|
153
|
|
|
file2Path := filepath.Join(tempDir, "file2") |
|
154
|
|
|
joinedPath := filepath.Join(tempDir, "joined") |
|
155
|
|
|
|
|
156
|
|
|
err := os.WriteFile(file1Path, []byte("content1"), 0644) |
|
157
|
|
|
if err != nil { |
|
158
|
|
|
t.Fatalf("Failed to create test file: %v", err) |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
err = os.WriteFile(file2Path, []byte("content2"), 0644) |
|
162
|
|
|
if err != nil { |
|
163
|
|
|
t.Fatalf("Failed to create test file: %v", err) |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// Test joining files with progress bar enabled |
|
167
|
|
|
files := []string{file1Path, file2Path} |
|
168
|
|
|
err = JoinFile(files, joinedPath) |
|
169
|
|
|
if err != nil { |
|
170
|
|
|
t.Fatalf("JoinFile() with progress bar failed: %v", err) |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
// Verify joined content |
|
174
|
|
|
joinedContent, err := os.ReadFile(joinedPath) |
|
175
|
|
|
if err != nil { |
|
176
|
|
|
t.Fatalf("Failed to read joined file: %v", err) |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
expectedContent := "content1content2" |
|
180
|
|
|
if string(joinedContent) != expectedContent { |
|
181
|
|
|
t.Errorf("Expected joined content '%s', got '%s'", expectedContent, string(joinedContent)) |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|