-
Notifications
You must be signed in to change notification settings - Fork 16
/
files_test.go
202 lines (164 loc) · 6.32 KB
/
files_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
package pgo_test
import (
"math"
"os"
"testing"
"github.com/arthurkushman/pgo"
"github.com/stretchr/testify/assert"
)
const (
strToWrite = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
fileName = "example.txt"
defaultDomain = "http://google.com"
file1 = "file1.txt"
file2 = "file2.txt"
dir1 = "dir1"
dir2 = "dir2"
symlink = "symlink"
)
func TestFileGetContents(t *testing.T) {
n, err := pgo.FilePutContents(fileName, strToWrite)
assert.NoError(t, err)
// base case - readAll
strBase, err := pgo.FileGetContents(fileName)
assert.NoError(t, err)
assert.Equalf(t, len(strBase), n, "want %d bytes of data, got %d", n, len(strBase))
// reading full file with limit
str, err := pgo.FileGetContents(fileName, nil, 0, n)
assert.NoError(t, err)
assert.Equalf(t, len(str), n, "want %d bytes of data, got %d", n, len(str))
// reading offset/limit
off := n / 3
lim := n/2 - off - 1
ss, err := pgo.FileGetContents(fileName, nil, off, lim)
assert.NoError(t, err)
assert.Equalf(t, len(ss), lim, "want %d bytes of data, got %d", n, len(str))
sOff, err := pgo.FileGetContents(fileName, nil, off)
assert.NoError(t, err)
assert.Equalf(t, len(sOff), n-off, "want %d bytes of data, got %d", n-off, len(sOff))
}
func TestFileGetContentsHttp(t *testing.T) {
str, err := pgo.FileGetContents(defaultDomain)
assert.NoError(t, err)
assert.NotEmpty(t, str)
}
func TestFileGetContentsPanics(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic with offset")
}
}()
// panic with non existent file
_, err := pgo.FileGetContents("non-existent.txt", nil, math.MaxInt64)
assert.Error(t, err)
// panic with non existent, but with limit
_, err = pgo.FileGetContents("non-existent.txt", nil, math.MaxInt64, math.MaxInt64)
assert.Error(t, err)
// panic with existent but out of range offset
_, err = pgo.FileGetContents(fileName, nil, math.MaxInt64)
assert.NoError(t, err)
// panic with existent but out of range offset and limit
_, err = pgo.FileGetContents(fileName, nil, math.MaxInt64, math.MaxInt64)
assert.NoError(t, err)
}
func TestFileGetContentsInvalidTypes(t *testing.T) {
content, err := pgo.FileGetContents(fileName, nil, "", "")
assert.Empty(t, content)
assert.EqualError(t, err, "Error on passing params with wrong types to FileGetContents: offset string and limit string")
content2, err2 := pgo.FileGetContents(fileName, nil, "")
assert.Empty(t, content2)
assert.EqualError(t, err2, "Error on passing params to FileGetContents: offset string")
}
func TestFileGetContentsHttpGetRequest(t *testing.T) {
content, err := pgo.FileGetContents(defaultDomain, pgo.NewContext())
assert.NoError(t, err)
assert.NotEmpty(t, content)
}
func TestFileGetContentsHttpInvalidRequest(t *testing.T) {
ctx := pgo.NewContext()
ctx.RequestMethod = "INVALID()"
_, err := pgo.FileGetContents(defaultDomain, ctx)
assert.Errorf(t, err, "Request has not been failed with error")
ctx.RequestMethod = "OPTIONS"
_, er := pgo.FileGetContents("https://abrakadabra.comz.ru", ctx)
assert.Errorf(t, er, "Request has not been failed with error")
}
func TestFilePutContents(t *testing.T) {
// test write to file with append without options
n1, err := pgo.FilePutContents(fileName, strToWrite)
assert.NoError(t, err)
assert.Equalf(t, n1, len(strToWrite), "want %d bytes of data, got %d", n1, len(strToWrite))
// test write to file with append option
n2, err := pgo.FilePutContents(fileName, strToWrite, pgo.FileAppend)
assert.NoError(t, err)
assert.Equalf(t, n2, len(strToWrite), "want %d bytes of data, got %d", n2, len(strToWrite))
}
func TestFilePutContentsErrors(t *testing.T) {
n1, err := pgo.FilePutContents(fileName, strToWrite, "")
assert.EqualError(t, err, "type of 3d parameter must be an int, got string")
assert.Equal(t, n1, -1)
n2, err := pgo.FilePutContents("fakefile.out", "", 0x1212) // setting fake flags to invoke error from os.OpenFile
assert.Error(t, err)
assert.Equal(t, n2, -1)
}
// todo: Fix to working version of test for MoveUploadedFile
//func TestFilePutContents2(t *testing.T) {
// fileHeader := make([]*multipart.FileHeader, 10)
//
// req := http.Request{
// Method: "POST",
// MultipartForm: &multipart.Form{
// Value: map[string][]string{
// "foo": {"bar"},
// },
// File: map[string][]*multipart.FileHeader{
// "foo": fileHeader,
// },
// },
// }
//
// c := &pgo.Context{
// Req: &req,
// }
//
// c.MoveUploadedFile("foo", "./baz.txt")
//}
func TestFileExists(t *testing.T) {
_, err := pgo.FilePutContents(file1, "foo bar baz")
assert.NoError(t, err)
defer os.Remove(file1)
f1 := pgo.FileExists(file1)
assert.Truef(t, f1, "File exists and returning %v", f1)
f2 := pgo.FileExists(file2)
assert.Falsef(t, f2, "File doesn't exist and returning %v", f2)
}
func TestIsDir(t *testing.T) {
err := os.Mkdir(dir1, 0644)
assert.NoError(t, err)
defer os.Remove(dir1)
isDir := pgo.IsDir(dir1)
assert.Truef(t, isDir, "Directory dir1 is an existent dir but IsDir returned %v", isDir)
isDir2 := pgo.IsDir(dir2)
assert.Falsef(t, isDir2, "Directory "+dir2+" is non-existent dir but IsDir returned %v", isDir)
}
func TestIsFile(t *testing.T) {
_, err := pgo.FilePutContents(file1, "foo bar baz")
assert.NoError(t, err)
defer os.Remove(file1)
isFile := pgo.IsFile(file1)
assert.Truef(t, isFile, "File "+file1+" is a regular file, but IsFile returned %v", isFile)
isFile2 := pgo.IsFile(file2)
assert.Falsef(t, isFile2, "File "+file2+" is not a regular file, but IsFile returned %v", isFile2)
}
func TestIsLnik(t *testing.T) {
_, err := pgo.FilePutContents(file1, "foo bar baz")
assert.NoError(t, err)
err = os.Symlink(file1, symlink)
assert.NoError(t, err)
defer os.Remove(file1)
defer os.Remove(symlink)
isSymlink := pgo.IsLink(symlink)
assert.Truef(t, isSymlink, symlink+" is a symlinkr, but IsLink returned %v", isSymlink)
isSymlink2 := pgo.IsLink(file1)
assert.Falsef(t, isSymlink2, file1+" is a regular file, but IsLink returned %v", isSymlink2)
}