Repository navigation
Expand file tree
/
Copy pathwriter_test.go
More file actions
81 lines (68 loc) · 1.63 KB
/
Copy pathwriter_test.go
File metadata and controls
81 lines (68 loc) · 1.63 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
// File writer_test.go tests the writer.
package writer
import (
"os"
"path"
"testing"
"github.com/verless/verless/test"
)
const (
testPath = "../example"
testOutPath = "../test-out-path"
)
func TestWriter_removeOutDirIfExists(t *testing.T) {
tests := map[string]struct {
// beforeTest is a callback which creates the folders/files
// to test a specific testcase.
beforeTest func()
// cleanupTest is a callback which the folders/files created
// by beforeTest and the test itself.
cleanupTest func()
expectedError string
}{
"normal": {
beforeTest: func() {},
cleanupTest: func() {},
},
"already exists": {
beforeTest: func() {
test.Ok(t, os.Mkdir(testOutPath, os.ModePerm))
file, err := os.Create(path.Join(testOutPath, "anyFile.txt"))
test.Ok(t, err)
_ = file.Close()
},
cleanupTest: func() {
err := os.RemoveAll(testOutPath)
test.Ok(t, err)
},
},
"already exists but without file": {
beforeTest: func() {
test.Ok(t, os.Mkdir(testOutPath, os.ModePerm))
},
cleanupTest: func() {
err := os.RemoveAll(testOutPath)
test.Ok(t, err)
},
},
}
for caseName, testCase := range tests {
t.Logf("Testing '%s'", caseName)
w := setupNewWriter(t)
testCase.beforeTest()
err := w.removeOutDirIfExists()
if testCase.expectedError == "" {
test.Ok(t, err)
} else {
test.Assert(t, err != nil && testCase.expectedError == err.Error(), "should error")
}
testCase.cleanupTest()
}
}
func setupNewWriter(t testing.TB) *writer {
w, err := New(testPath, testOutPath)
if err != nil {
t.Errorf("New should not throw an error: %v", err)
}
return w
}