-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
192 lines (169 loc) · 4.14 KB
/
file_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
// SPDX-FileCopyrightText: 2022 Weston Schmidt <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package githubfs
import (
"fmt"
"io/fs"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewFile(t *testing.T) {
parent := &dir{
org: "org",
repo: "repo",
}
now := time.Now()
content := []byte("hello, world")
tests := []struct {
description string
name string
opts []fileOpt
expectSize int
expectMode fs.FileMode
expectTime time.Time
expectContent []byte
expectUrl string
}{
{
description: "simple test",
name: "foo",
expectMode: fs.FileMode(0644),
}, {
description: "with content",
name: "foo",
opts: []fileOpt{withContent(content)},
expectSize: len(content),
expectContent: content,
expectMode: fs.FileMode(0644),
}, {
description: "with mod time",
name: "foo",
opts: []fileOpt{withModTime(now)},
expectTime: now,
expectMode: fs.FileMode(0644),
}, {
description: "with file type",
name: "foo",
opts: []fileOpt{withMode(0755)},
expectMode: fs.FileMode(0755),
}, {
description: "with file type",
name: "foo",
opts: []fileOpt{withSize(10)},
expectMode: fs.FileMode(0644),
expectSize: 10,
}, {
description: "with content and size",
name: "foo",
opts: []fileOpt{withContent(content), withSize(1000)},
expectSize: len(content),
expectContent: content,
expectMode: fs.FileMode(0644),
}, {
description: "with content and size, reversed order",
name: "foo",
opts: []fileOpt{withSize(1000), withContent(content)},
expectSize: len(content),
expectContent: content,
expectMode: fs.FileMode(0644),
}, {
description: "with url",
name: "foo",
opts: []fileOpt{withUrl("foobar")},
expectUrl: "foobar",
expectMode: fs.FileMode(0644),
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
f := newFile(parent, tc.name, tc.opts...)
assert.NotNil(f)
assert.Equal("org", f.owner)
assert.Equal("repo", f.repo)
assert.Equal(int64(tc.expectSize), f.info.size)
assert.Equal(tc.expectMode, f.info.mode)
assert.Equal(tc.expectTime, f.info.modTime)
assert.Equal(tc.expectUrl, f.url)
})
}
}
func TestToDirEntry(t *testing.T) {
parent := &dir{
org: "org",
repo: "repo",
}
assert := assert.New(t)
f := newFile(parent, "name")
assert.NotNil(f)
de := f.toDirEntry()
assert.Equal("name", de.Name())
}
func TestNewFileHandle(t *testing.T) {
tests := []struct {
description string
name string
opts []fileOpt
payload string
statusCode int
expectErr bool
}{
{
description: "simple test",
name: "file_1",
payload: "file_1 payload",
}, {
description: "simple 404 test",
name: "file_1",
payload: "file_1 payload",
statusCode: 404,
expectErr: true,
}, {
description: "simple empty test",
name: "file_1",
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if tc.statusCode != 0 {
w.WriteHeader(tc.statusCode)
}
fmt.Fprintf(w, tc.payload)
}))
gfs := FS{
httpClient: &http.Client{},
}
parent := dir{
gfs: &gfs,
org: "org",
repo: "repo",
}
f := newFile(&parent, tc.name, withUrl(server.URL), withSize(10))
require.NotNil(f)
got, err := f.newFileHandle()
if !tc.expectErr {
assert.NoError(err)
assert.NotNil(got)
assert.Equal(int64(len(tc.payload)), got.info.Size())
if len(tc.payload) > 0 {
b := make([]byte, 50)
n, err := got.Read(b)
assert.NoError(err)
assert.Equal(len(tc.payload), n)
assert.Equal(string(b[:n]), tc.payload)
}
} else {
assert.Error(err)
assert.Nil(got)
}
server.Close()
})
}
}