-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
260 lines (203 loc) · 7.59 KB
/
utils_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
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
package utils_test
import (
"io/fs"
"io/ioutil"
"os"
"path"
"strings"
"testing"
"github.com/GiulianoDecesares/go-utils"
)
func TestFileExists(context *testing.T) {
context.Log("Testing file exists")
fileToTest := "test.txt"
tempDir := context.TempDir()
fullPath := path.Join(tempDir, fileToTest)
context.Logf("Creating file %s in %s", fileToTest, tempDir)
if result := ioutil.WriteFile(fullPath, []byte("Testing file writting"), 0755); result == nil {
context.Log("File created")
if utils.FileExists(fullPath) {
context.Log("File check success")
} else {
context.Error("File check failed. Expected file exists, got file doesn't exists")
}
context.Log("Deleting file and testing again")
if result := os.Remove(fullPath); result == nil {
context.Log("File deleted")
if utils.FileExists(fullPath) {
context.Error("File check failed. Expected file doesn't exists, got file exists")
} else {
context.Log("File check success")
}
} else {
context.Errorf("Error while deleting file %s", result.Error())
}
} else {
context.Errorf("Error while writting file: %s", result.Error())
}
}
func TestDirExists(context *testing.T) {
context.Log("Testing dir exists")
dirToTest := "Test"
tempDir := context.TempDir()
fullPath := path.Join(tempDir, dirToTest)
context.Logf("Creating dir %s in %s", dirToTest, tempDir)
if result := os.Mkdir(fullPath, os.ModeDir); result == nil {
context.Log("Directory created")
if utils.DirExists(fullPath) {
context.Log("Directory check success")
} else {
context.Error("Directory check failed. Expected directory to exists, got directory doesn't exists")
}
context.Log("Deleting directory and testing again")
if result := os.Remove(fullPath); result == nil {
context.Log("Directory removed")
if utils.DirExists(fullPath) {
context.Error("Directory check failed. Expected directory to not exist, got directory exists")
} else {
context.Log("Directory check success")
}
} else {
context.Errorf("Error while deleting directory %s", result.Error())
}
} else {
context.Errorf("Error while creating directory: %s", result.Error())
}
}
func TestIsDir(context *testing.T) {
tempDir := context.TempDir()
fileName := "test.txt"
context.Log("Testing IsDir method against directory")
if isDir, result := utils.IsDir(tempDir); result == nil {
if isDir {
context.Logf("Directory %s IsDir returned true", tempDir)
} else {
context.Errorf("Directory %s IsDir returned false", tempDir)
}
} else {
context.Errorf("Error while checking if dir %s is a directory: %s", tempDir, result.Error())
}
context.Log("Testing IsDir method against file")
context.Log("Creating file")
fullFilePath := path.Join(tempDir, fileName)
if result := ioutil.WriteFile(fullFilePath, []byte("Testing file writting"), 0755); result != nil {
context.Fatalf("Error while creating file: %s", result.Error())
}
context.Log("File created")
if isDir, result := utils.IsDir(fullFilePath); result == nil {
if isDir {
context.Errorf("File %s IsDir returned true", fullFilePath)
} else {
context.Logf("File %s IsDir returned false", fullFilePath)
}
} else {
context.Errorf("Error while checking if file %s is a directory: %s", fullFilePath, result.Error())
}
}
func TestIsFile(context *testing.T) {
tempDir := context.TempDir()
fileName := "test.txt"
context.Log("Testing IsFile method against directory")
if isFile, result := utils.IsFile(tempDir); result == nil {
if isFile {
context.Errorf("Directory %s IsFile returned true", tempDir)
} else {
context.Logf("Directory %s IsFile returned false", tempDir)
}
} else {
context.Errorf("Error while checking if dir %s is a file: %s", tempDir, result.Error())
}
context.Log("Testing IsFile method against file")
context.Log("Creating file")
fullFilePath := path.Join(tempDir, fileName)
if result := ioutil.WriteFile(fullFilePath, []byte("Testing file writting"), 0755); result != nil {
context.Fatalf("Error while creating file: %s", result.Error())
}
context.Log("File created")
if isFile, result := utils.IsFile(fullFilePath); result == nil {
if isFile {
context.Logf("File %s IsFile returned true", fullFilePath)
} else {
context.Errorf("File %s IsFile returned false", fullFilePath)
}
} else {
context.Errorf("Error while checking if file %s is a file: %s", fullFilePath, result.Error())
}
}
func TestSearchFilesByExtension(context *testing.T) {
tempDir := context.TempDir()
files := map[string]string{
"test.txt": path.Join(tempDir, "test.txt"),
"test2.txt": path.Join(tempDir, "test2.txt"),
"test.jpg": path.Join(tempDir, "test.jpg"),
"test.bin": path.Join(tempDir, "test.bin"),
"test2.bin": path.Join(tempDir, "test2.bin"),
"test3.bin": path.Join(tempDir, "test3.bin"),
"test.go": path.Join(tempDir, "test.go"),
}
context.Log("Creating all files")
for fileName, filePath := range files {
context.Logf("Creating %s file in %s", fileName, filePath)
if result := ioutil.WriteFile(filePath, []byte{}, 0755); result != nil {
context.Fatalf("Error while creating %s file: %s", fileName, result.Error())
}
}
context.Log("Searching files by extension and checking")
type CheckFiles func(dir string, extension string, expectedFilesAmount int, context *testing.T)
var checkFilesMethod CheckFiles = func(dir string, extension string, expectedFilesAmount int, context *testing.T) {
context.Logf("Checking for %s files", strings.ToUpper(extension))
files := utils.SearchFilesByExtension(dir, extension)
if len(files) == expectedFilesAmount {
context.Logf("Check for %s files success", strings.ToUpper(extension))
} else {
context.Errorf("Expected %d %s files, found %d", expectedFilesAmount, strings.ToUpper(extension), len(files))
}
}
checkFilesMethod(tempDir, "txt", 2, context)
checkFilesMethod(tempDir, "jpg", 1, context)
checkFilesMethod(tempDir, "bin", 3, context)
checkFilesMethod(tempDir, "go", 1, context)
}
func TestCopy(context *testing.T) {
context.Log("Creating file")
var result error
tempDir := context.TempDir()
baseFileFullPath := path.Join(tempDir, "test.txt")
testDir := path.Join(tempDir, "TestDirectory")
testFileFullPath := path.Join(testDir, "test.txt")
if result = ioutil.WriteFile(baseFileFullPath, []byte("Testing file content"), 0755); result != nil {
context.Fatalf("Error while creating testing file %s", result.Error())
}
if result = os.Mkdir(testDir, os.ModeDir); result != nil {
context.Fatalf("Error while creating dir %s", result.Error())
}
context.Log("Copying file to testing dir")
if result = utils.Copy(baseFileFullPath, testDir); result != nil {
context.Fatalf("Error while copying file to test dir %s", result.Error())
}
context.Log("File copyied. Checking")
var baseFile *os.File
if baseFile, result = os.Open(baseFileFullPath); result != nil {
context.Fatalf("Error while trying to open base file %s", result.Error())
}
defer baseFile.Close()
var testFile *os.File
if testFile, result = os.Open(testFileFullPath); result != nil {
context.Fatalf("Error while trying to open test file %s", result.Error())
}
defer testFile.Close()
context.Log("Checking both files")
var baseFileInfo fs.FileInfo
if baseFileInfo, result = baseFile.Stat(); result != nil {
context.Fatalf("Error while getting base file info: %s", result.Error())
}
var testFileInfo fs.FileInfo
if testFileInfo, result = testFile.Stat(); result != nil {
context.Fatalf("Error while getting test file info: %s", result.Error())
}
if baseFileInfo.Size() == testFileInfo.Size() {
context.Log("Copyied file check success")
} else {
context.Error("Copyied file is different from base file")
}
}