-
Notifications
You must be signed in to change notification settings - Fork 0
/
filei_test.go
191 lines (146 loc) · 3.69 KB
/
filei_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
package filei
import (
"os"
"testing"
)
func TestUploadFile(t *testing.T) {
file, _ := os.Create("testdata/new-text.txt")
err := UploadFile(file, "testdata/upload.txt")
if err != nil {
t.Errorf("UploadFile() error = %v", err)
}
var fakeFile *os.File
err = UploadFile(fakeFile, "testdata/upload.txt")
if err == nil {
t.Errorf("UploadFile() error data must have a value")
}
}
func TestGetFile(t *testing.T) {
_, err := GetFile("testdata/text.txt")
if err != nil {
t.Errorf("GetFile() error = %v", err)
}
_, err = GetFile("wrong path")
if err == nil {
t.Errorf("GetFile() error data must have a value")
}
}
func TestDeleteFile(t *testing.T) {
err := DeleteFile("testdata/delete-text.txt")
if err != nil {
t.Errorf("DeleteFile() error = %v", err)
}
err = DeleteFile("wrong path")
if err == nil {
t.Errorf("DeleteFile() error data must have a value")
}
os.Create("testdata/delete-text.txt")
}
func TestCreateFile(t *testing.T) {
data := []byte("milwad")
err := CreateFile("testdata/new-text.txt", data)
if err != nil {
t.Errorf("CreateFile() error = %v", err)
}
}
func TestExistsFile(t *testing.T) {
isExists := Exists("testdata/text.txt") // Must be true
if isExists != true {
t.Error("Exists() error data must be true")
}
isNotExists := Exists("wrong path") // Must be false
if isNotExists != false {
t.Error("Exists() error data must be false")
}
}
func TestCleanDirectory(t *testing.T) {
directory := "testdata/dir"
defer os.Create(directory + "/data.txt")
defer os.Create(directory + "/item.txt")
dirData, _ := os.ReadDir(directory)
if len(dirData) != 2 {
t.Fatal("CleanDirectory() error data must have 2 files")
}
CleanDirectory(directory)
dirData, _ = os.ReadDir(directory)
if len(dirData) == 2 {
t.Fatal("CleanDirectory() error data must have 0 files")
}
}
func TestDeleteDirectory(t *testing.T) {
directory := "testdata/dir2"
defer os.Mkdir(directory, 0755)
err := DeleteDirectory(directory)
if err != nil {
t.Fatalf("DeleteDirectory() error = %v", err)
}
}
func TestMoveFile(t *testing.T) {
sourcePath := "testdata/move-text.txt"
destPath := "testdata/move2-text.txt"
func() {
err := MoveFile(sourcePath, destPath)
if err != nil {
t.Fatalf("MoveFile() error = %v", err)
}
}()
defer MoveFile(destPath, sourcePath)
// Wrong path
err := MoveFile("wrong path", "wrong path")
if err == nil {
t.Fatalf("MoveFile() error data must have a value")
}
}
func TestFiles(t *testing.T) {
directory := "testdata"
data, err := Files(directory)
if err != nil {
t.Fatalf("Files() error = %v", err)
}
if len(data) != 8 {
t.Fatal("Files() error data must have 8 files")
}
}
func TestSize(t *testing.T) {
filePath := "testdata/text.txt"
size, err := Size(filePath)
if err != nil {
t.Fatalf("Size() error = %v", err)
}
if size != 13 {
t.Fatal("Size() error data must have 13 files")
}
}
func TestChmod(t *testing.T) {
filePath := "testdata/text.txt"
err := Chmod(filePath, 0700)
if err != nil {
t.Errorf("Chmod() error = %v", err)
}
err = Chmod("wrong path", 0700)
if err == nil {
t.Errorf("Chmod() error data must have a value")
}
}
func TestPrepend(t *testing.T) {
filePath := "testdata/pre-app.txt"
err := Prepend(filePath, "The first line\n")
if err != nil {
t.Errorf("Prepend() error = %v", err)
}
err = Prepend("wrong path", "The first line\n")
if err == nil {
t.Errorf("Prepend() error data must have a value")
}
}
func TestAppend(t *testing.T) {
filePath := "testdata/pre-app.txt"
err := Append(filePath, "The last line\n")
if err != nil {
t.Errorf("Append() error = %v", err)
}
err = Append("wrong path", "The last line\n")
if err == nil {
t.Errorf("Append() error data must have a value")
}
}