forked from huydx/hget
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjoiner_test.go
183 lines (149 loc) · 4.89 KB
/
joiner_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
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
package main
import (
"os"
"path/filepath"
"testing"
)
func TestJoiner(t *testing.T) {
// Disable progress bar for tests
displayProgress = false
// Create temporary directory for test files
tempDir := t.TempDir()
// Create test files
file1Path := filepath.Join(tempDir, "file1")
file2Path := filepath.Join(tempDir, "file2")
file3Path := filepath.Join(tempDir, "file3")
joinedPath := filepath.Join(tempDir, "joined")
file1Content := "content of file 1"
file2Content := "content of file 2"
file3Content := "content of file 3"
err := os.WriteFile(file1Path, []byte(file1Content), 0644)
if err != nil {
t.Fatalf("Failed to create test file1: %v", err)
}
err = os.WriteFile(file2Path, []byte(file2Content), 0644)
if err != nil {
t.Fatalf("Failed to create test file2: %v", err)
}
err = os.WriteFile(file3Path, []byte(file3Content), 0644)
if err != nil {
t.Fatalf("Failed to create test file3: %v", err)
}
// Test joining files
files := []string{file1Path, file2Path, file3Path}
err = JoinFile(files, joinedPath)
if err != nil {
t.Fatalf("JoinFile() failed: %v", err)
}
// Verify joined content
joinedContent, err := os.ReadFile(joinedPath)
if err != nil {
t.Fatalf("Failed to read joined file: %v", err)
}
expectedContent := file1Content + file2Content + file3Content
if string(joinedContent) != expectedContent {
t.Errorf("Expected joined content '%s', got '%s'", expectedContent, string(joinedContent))
}
}
func TestJoinerSorting(t *testing.T) {
// Disable progress bar for tests
displayProgress = false
// Create temporary directory for test files
tempDir := t.TempDir()
// Create test files with names that need to be sorted
fileAPath := filepath.Join(tempDir, "fileC") // Intentionally out of order
fileBPath := filepath.Join(tempDir, "fileA")
fileCPath := filepath.Join(tempDir, "fileB")
joinedPath := filepath.Join(tempDir, "joined")
// Write content that makes the sorting order obvious
err := os.WriteFile(fileAPath, []byte("C"), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
err = os.WriteFile(fileBPath, []byte("A"), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
err = os.WriteFile(fileCPath, []byte("B"), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Test joining files
files := []string{fileAPath, fileBPath, fileCPath}
err = JoinFile(files, joinedPath)
if err != nil {
t.Fatalf("JoinFile() failed: %v", err)
}
// Verify joined content based on lexicographical sorting
joinedContent, err := os.ReadFile(joinedPath)
if err != nil {
t.Fatalf("Failed to read joined file: %v", err)
}
// Expect files to be joined in alphabetical order (A, B, C)
expectedContent := "ABC"
if string(joinedContent) != expectedContent {
t.Errorf("Expected joined content '%s', got '%s' (files not sorted correctly)",
expectedContent, string(joinedContent))
}
}
func TestJoinerError(t *testing.T) {
// Disable progress bar for tests
displayProgress = false
// Create temporary directory for test files
tempDir := t.TempDir()
// Create a test file
filePath := filepath.Join(tempDir, "file1")
err := os.WriteFile(filePath, []byte("test content"), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Include a non-existent file in the list
nonExistentFile := filepath.Join(tempDir, "nonexistent")
joinedPath := filepath.Join(tempDir, "joined")
// Test joining files
files := []string{filePath, nonExistentFile}
err = JoinFile(files, joinedPath)
// Expect an error because one file doesn't exist
if err == nil {
t.Errorf("Expected error when joining non-existent file, got nil")
// Clean up if test fails
os.Remove(joinedPath)
}
}
func TestJoinerWithProgressBar(t *testing.T) {
// Enable progress bar for this test
originalDisplayProgress := displayProgress
displayProgress = true
defer func() {
displayProgress = originalDisplayProgress
}()
// Create temporary directory for test files
tempDir := t.TempDir()
// Create test files
file1Path := filepath.Join(tempDir, "file1")
file2Path := filepath.Join(tempDir, "file2")
joinedPath := filepath.Join(tempDir, "joined")
err := os.WriteFile(file1Path, []byte("content1"), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
err = os.WriteFile(file2Path, []byte("content2"), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Test joining files with progress bar enabled
files := []string{file1Path, file2Path}
err = JoinFile(files, joinedPath)
if err != nil {
t.Fatalf("JoinFile() with progress bar failed: %v", err)
}
// Verify joined content
joinedContent, err := os.ReadFile(joinedPath)
if err != nil {
t.Fatalf("Failed to read joined file: %v", err)
}
expectedContent := "content1content2"
if string(joinedContent) != expectedContent {
t.Errorf("Expected joined content '%s', got '%s'", expectedContent, string(joinedContent))
}
}