-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathfile_test.go
More file actions
327 lines (269 loc) · 9.15 KB
/
file_test.go
File metadata and controls
327 lines (269 loc) · 9.15 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/gruntwork-io/fetch/source"
_ "github.com/gruntwork-io/fetch/source/github"
"github.com/stretchr/testify/require"
)
// Although other tests besides those in this file require this env var, this init() func will cover all tests.
func init() {
if os.Getenv("GITHUB_OAUTH_TOKEN") == "" {
fmt.Println("ERROR: These tests require that env var GITHUB_OAUTH_TOKEN be set to a GitHub Personal Access Token.")
fmt.Println("See the tests cases to see which GitHub repos the oAuth token needs access to.")
os.Exit(1)
}
}
func TestDownloadGitTagZipFile(t *testing.T) {
t.Parallel()
config := source.Config{
ApiVersion: "v3",
Logger: GetProjectLogger(),
}
src, err := source.NewSource(source.TypeGitHub, config)
require.NoError(t, err)
cases := []struct {
repoUrl string
gitTag string
githubToken string
}{
{"https://github.com/gruntwork-io/fetch-test-public", "v0.0.1", ""},
{"https://github.com/gruntwork-io/fetch-test-private", "v0.0.2", os.Getenv("GITHUB_OAUTH_TOKEN")},
}
for _, tc := range cases {
repo, err := src.ParseUrl(tc.repoUrl, tc.githubToken)
require.NoError(t, err)
zipFilePath, err := src.DownloadSourceZip(repo, tc.gitTag)
defer os.RemoveAll(zipFilePath)
if err != nil {
t.Fatalf("Failed to download file: %s", err)
}
if _, err := os.Stat(zipFilePath); os.IsNotExist(err) {
t.Fatalf("Downloaded file doesn't exist at the expected path of %s", zipFilePath)
}
}
}
func TestDownloadGitBranchZipFile(t *testing.T) {
t.Parallel()
config := source.Config{
ApiVersion: "v3",
Logger: GetProjectLogger(),
}
src, err := source.NewSource(source.TypeGitHub, config)
require.NoError(t, err)
cases := []struct {
repoUrl string
branchName string
githubToken string
}{
{"https://github.com/gruntwork-io/fetch-test-public", "sample-branch", ""},
{"https://github.com/gruntwork-io/fetch-test-private", "sample-branch", os.Getenv("GITHUB_OAUTH_TOKEN")},
}
for _, tc := range cases {
repo, err := src.ParseUrl(tc.repoUrl, tc.githubToken)
require.NoError(t, err)
zipFilePath, err := src.DownloadSourceZip(repo, tc.branchName)
defer os.RemoveAll(zipFilePath)
if err != nil {
t.Fatalf("Failed to download file: %s", err)
}
if _, err := os.Stat(zipFilePath); os.IsNotExist(err) {
t.Fatalf("Downloaded file doesn't exist at the expected path of %s", zipFilePath)
}
}
}
func TestDownloadBadGitBranchZipFile(t *testing.T) {
t.Parallel()
config := source.Config{
ApiVersion: "v3",
Logger: GetProjectLogger(),
}
src, err := source.NewSource(source.TypeGitHub, config)
require.NoError(t, err)
cases := []struct {
repoUrl string
branchName string
githubToken string
}{
{"https://github.com/gruntwork-io/fetch-test-public", "branch-that-doesnt-exist", ""},
}
for _, tc := range cases {
repo, err := src.ParseUrl(tc.repoUrl, tc.githubToken)
require.NoError(t, err)
zipFilePath, err := src.DownloadSourceZip(repo, tc.branchName)
defer os.RemoveAll(zipFilePath)
if err == nil {
t.Fatalf("Expected that attempt to download repo %s for branch \"%s\" would fail, but received no error.", tc.repoUrl, tc.branchName)
}
}
}
func TestDownloadGitCommitFile(t *testing.T) {
t.Parallel()
config := source.Config{
ApiVersion: "v3",
Logger: GetProjectLogger(),
}
src, err := source.NewSource(source.TypeGitHub, config)
require.NoError(t, err)
cases := []struct {
repoUrl string
commitSha string
githubToken string
}{
{"https://github.com/gruntwork-io/fetch-test-public", "d2de34edb4c6564e0674b3f390b3b1fb0468183a", ""},
{"https://github.com/gruntwork-io/fetch-test-public", "57752e7f1df0acbd3c1e61545d5c4d0e87699d84", ""},
{"https://github.com/gruntwork-io/fetch-test-public", "f32a08313e30f116a1f5617b8b68c11f1c1dbb61", ""},
{"https://github.com/gruntwork-io/fetch-test-private", "676cfb92b54d33538c756c7a9479bfc3f6b44de2", os.Getenv("GITHUB_OAUTH_TOKEN")},
}
for _, tc := range cases {
repo, err := src.ParseUrl(tc.repoUrl, tc.githubToken)
require.NoError(t, err)
zipFilePath, err := src.DownloadSourceZip(repo, tc.commitSha)
defer os.RemoveAll(zipFilePath)
if err != nil {
t.Fatalf("Failed to download file: %s", err)
}
if _, err := os.Stat(zipFilePath); os.IsNotExist(err) {
t.Fatalf("Downloaded file doesn't exist at the expected path of %s", zipFilePath)
}
}
}
func TestDownloadBadGitCommitFile(t *testing.T) {
t.Parallel()
config := source.Config{
ApiVersion: "v3",
Logger: GetProjectLogger(),
}
src, err := source.NewSource(source.TypeGitHub, config)
require.NoError(t, err)
cases := []struct {
repoUrl string
commitSha string
githubToken string
}{
{"https://github.com/gruntwork-io/fetch-test-public", "hello-world", ""},
{"https://github.com/gruntwork-io/fetch-test-public", "i-am-a-non-existent-commit", ""},
// remove a single letter from the beginning of an otherwise legit commit sha
// interestingly, through testing I found that GitHub will attempt to find the right commit sha if you
// truncate the end of it.
{"https://github.com/gruntwork-io/fetch-test-public", "7752e7f1df0acbd3c1e61545d5c4d0e87699d84", ""},
}
for _, tc := range cases {
repo, err := src.ParseUrl(tc.repoUrl, tc.githubToken)
require.NoError(t, err)
zipFilePath, err := src.DownloadSourceZip(repo, tc.commitSha)
defer os.RemoveAll(zipFilePath)
if err == nil {
t.Fatalf("Expected that attempt to download repo %s at commit sha \"%s\" would fail, but received no error.", tc.repoUrl, tc.commitSha)
}
}
}
func TestDownloadZipFileWithBadRepoValues(t *testing.T) {
t.Parallel()
config := source.Config{
ApiVersion: "v3",
Logger: GetProjectLogger(),
}
src, err := source.NewSource(source.TypeGitHub, config)
require.NoError(t, err)
cases := []struct {
repoUrl string
gitTag string
githubToken string
}{
{"https://github.com/gruntwork-io/non-existent-repo", "x.y.z", ""},
}
for _, tc := range cases {
repo, err := src.ParseUrl(tc.repoUrl, tc.githubToken)
require.NoError(t, err)
zipFilePath, err := src.DownloadSourceZip(repo, tc.gitTag)
defer os.RemoveAll(zipFilePath)
if err == nil {
t.Fatalf("Expected error for bad repo values: %s:%s", tc.repoUrl, tc.gitTag)
}
}
}
func TestExtractFiles(t *testing.T) {
t.Parallel()
cases := []struct {
localFilePath string
filePathToExtract string
expectedNumFiles int
nonemptyFiles []string
}{
{"test-fixtures/fetch-test-public-0.0.1.zip", "/", 1, nil},
{"test-fixtures/fetch-test-public-0.0.2.zip", "/", 2, nil},
{"test-fixtures/fetch-test-public-0.0.3.zip", "/", 4, []string{"/README.md"}},
{"test-fixtures/fetch-test-public-0.0.3.zip", "/folder", 2, nil},
{"test-fixtures/fetch-test-public-0.0.4.zip", "/aaa", 2, []string{"/hello.txt", "/subaaa/subhello.txt"}},
}
for _, tc := range cases {
// Create a temp directory
tempDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("Failed to create temp directory: %s", err)
}
defer os.RemoveAll(tempDir)
fileCount, err := extractFiles(tc.localFilePath, tc.filePathToExtract, tempDir)
if err != nil {
t.Fatalf("Failed to extract files: %s", err)
}
if fileCount != tc.expectedNumFiles {
t.Fatalf("Expected to extract %d files, extracted %d instead", tc.expectedNumFiles, fileCount)
}
// Count the number of files in the directory
var numFiles int
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
numFiles++
}
return nil
})
if numFiles != tc.expectedNumFiles {
t.Fatalf("While extracting %s, expected to find %d file(s), but found %d. Local path = %s", tc.localFilePath, tc.expectedNumFiles, numFiles, tempDir)
}
// Ensure that files declared to be non-empty are in fact non-empty
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
relativeFilename := strings.TrimPrefix(path, tempDir)
if !info.IsDir() && slices.Contains(tc.nonemptyFiles, relativeFilename) {
if info.Size() == 0 {
t.Fatalf("Expected %s in %s to have non-zero file size, but found file size = %d.\n", relativeFilename, tc.localFilePath, info.Size())
}
}
return nil
})
}
}
func TestExtractFilesExtractFile(t *testing.T) {
// Create a temp directory
tempDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("Failed to create temp directory: %s", err)
}
defer os.RemoveAll(tempDir)
zipFilePath := "test-fixtures/fetch-test-public-0.0.4.zip"
filePathToExtract := "zzz.txt"
localFileName := "/localzzz.txt"
expectedFileCount := 1
localPathName := filepath.Join(tempDir, localFileName)
fileCount, err := extractFiles(zipFilePath, filePathToExtract, localPathName)
if err != nil {
t.Fatalf("Failed to extract files: %s", err)
}
if fileCount != expectedFileCount {
t.Fatalf("Expected to extract %d files, extracted %d instead", expectedFileCount, fileCount)
}
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
relativeFilename := strings.TrimPrefix(path, tempDir)
if !info.IsDir() {
if relativeFilename != localFileName {
t.Fatalf("Expected local file %s to be created, but not found.\n", localFileName)
}
}
return nil
})
}