forked from evergreen-ci/pail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_test.go
More file actions
144 lines (129 loc) · 4.06 KB
/
Copy pathutil_test.go
File metadata and controls
144 lines (129 loc) · 4.06 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
package pail
import (
"archive/tar"
"bytes"
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWalkTree(t *testing.T) {
_, file, _, _ := runtime.Caller(0)
ctx := context.Background()
t.Run("CanceledContext", func(t *testing.T) {
tctx, cancel := context.WithCancel(ctx)
cancel()
out, err := walkLocalTree(tctx, filepath.Dir(file))
assert.Error(t, err)
assert.Nil(t, out)
})
t.Run("MissingPath", func(t *testing.T) {
out, err := walkLocalTree(ctx, "")
assert.NoError(t, err)
assert.Nil(t, out)
})
t.Run("WorkingExample", func(t *testing.T) {
out, err := walkLocalTree(ctx, filepath.Dir(file))
assert.NoError(t, err)
assert.NotNil(t, out)
})
t.Run("SymLink", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("git symlinks do not work on windows")
}
// This test requires that the benchmarks directory exists and there is
// a symlink to it in the testdata directory.
benchmarksDir := "benchmarks"
benchmarks, err := walkLocalTree(ctx, benchmarksDir)
require.NoError(t, err)
out, err := walkLocalTree(ctx, "testdata")
require.NoError(t, err)
fnMap := map[string]bool{}
for _, fn := range out {
fnMap[fn] = true
}
assert.True(t, fnMap["a_file.txt"])
assert.True(t, fnMap["z_file.txt"])
for _, fn := range benchmarks {
require.True(t, fnMap[filepath.Join(benchmarksDir, fn)])
}
})
}
func TestTarFile(t *testing.T) {
for testName, testCase := range map[string]func(t *testing.T, dir string){
"CreatesTarWithSingleFile": func(t *testing.T, dir string) {
fileName := "foo.txt"
fileContent := "bar"
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, fileName), []byte(fileContent), 0777))
b := &bytes.Buffer{}
tw := tar.NewWriter(b)
require.NoError(t, tarFile(tw, dir, fileName))
tr := tar.NewReader(b)
header, err := tr.Next()
require.NoError(t, err)
assert.Equal(t, fileName, header.Name)
assert.EqualValues(t, tar.TypeReg, header.Typeflag)
checkContent := &bytes.Buffer{}
_, err = io.Copy(checkContent, tr)
require.NoError(t, err)
assert.Equal(t, fileContent, checkContent.String())
_, err = tr.Next()
assert.Equal(t, io.EOF, err)
},
"CreatesTarWithDirectory": func(t *testing.T, dir string) {
subDirName := "foo"
absPath := filepath.Join(dir, subDirName)
require.NoError(t, os.Mkdir(absPath, 0777))
b := &bytes.Buffer{}
tw := tar.NewWriter(b)
require.NoError(t, tarFile(tw, dir, subDirName))
tr := tar.NewReader(b)
header, err := tr.Next()
require.NoError(t, err)
assert.Equal(t, subDirName+"/", header.Name)
assert.EqualValues(t, tar.TypeDir, header.Typeflag)
},
"CreatesTarWithFilesInSubdirectory": func(t *testing.T, dir string) {
relFilePath := filepath.Join("foo", "bar.txt")
absPath := filepath.Join(dir, relFilePath)
require.NoError(t, os.MkdirAll(filepath.Dir(absPath), 0777))
fileContent := []byte("bat")
require.NoError(t, ioutil.WriteFile(absPath, fileContent, 0777))
b := &bytes.Buffer{}
tw := tar.NewWriter(b)
require.NoError(t, tarFile(tw, dir, relFilePath))
tr := tar.NewReader(b)
header, err := tr.Next()
require.NoError(t, err)
assert.Equal(t, filepath.ToSlash(relFilePath), header.Name)
assert.EqualValues(t, tar.TypeReg, header.Typeflag)
checkContent, err := ioutil.ReadAll(tr)
require.NoError(t, err)
assert.Equal(t, fileContent, checkContent)
},
"FailsForFileNotWithinBaseDirectory": func(t *testing.T, dir string) {
tmpFile, err := ioutil.TempFile("", "outside_tar_dir")
require.NoError(t, err)
require.NoError(t, tmpFile.Close())
b := &bytes.Buffer{}
tw := tar.NewWriter(b)
assert.Error(t, tarFile(tw, dir, tmpFile.Name()))
},
"FailsForNonexistentFile": func(t *testing.T, dir string) {
b := &bytes.Buffer{}
tw := tar.NewWriter(b)
assert.Error(t, tarFile(tw, dir, "nonexistent_file"))
},
} {
t.Run(testName, func(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "tar_file_test")
require.NoError(t, err)
testCase(t, tmpDir)
})
}
}