-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
85 lines (76 loc) · 2.11 KB
/
main_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
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"testing"
)
const testEnv = "PF_TMP_TEST"
func Test_pathfinder(t *testing.T) {
tmpDir := t.TempDir()
err := os.Setenv(testEnv, tmpDir)
if err != nil {
t.Fatalf("Error setting test environment key : %v\n", err)
}
defer t.Cleanup(func() {
err = os.Unsetenv(testEnv)
if err != nil {
HandleError(err)
}
})
err = os.Chdir(tmpDir)
if err != nil {
t.Fatalf("Error changing directories : %v\n", err)
}
var dirs [5]string
for i := 1; i <= 4; i++ {
dirs[i] = filepath.Join(tmpDir, fmt.Sprintf("dir%d", i))
err = os.MkdirAll(dirs[i], 0777)
if err != nil {
t.Fatalf("Error creating sub directories : %v\n", err)
}
}
dirs[0] = filepath.Join(tmpDir, "uniquedirthatdoesntexistsanywhereelse")
// This is done to make sure test2 passes without any external conflicts
err = os.MkdirAll(dirs[0], 0777)
if err != nil {
t.Fatalf("Error creating sub directories : %v\n", err)
}
type args struct {
w bytes.Buffer
c *Cache
ignoreDir bool
path string
}
testCache := InitCache()
tests := []struct {
name string
args args
wantBuf string // Contents of bytes.Buffer
wantRet int // Return code
}{
{
name: "Folder not found", args: args{c: testCache, ignoreDir: false, path: "notfounddir"}, wantBuf: "notfounddir", wantRet: EXIT_FOLDERNOTFOUND,
},
{
name: "Folder exists but ignored (-i)", args: args{c: testCache, ignoreDir: true, path: filepath.Base(dirs[0])}, wantBuf: filepath.Base(dirs[0]), wantRet: EXIT_FOLDERNOTFOUND,
},
{
name: "Folder found without ignore", args: args{c: testCache, ignoreDir: false, path: filepath.Base(dirs[3])}, wantBuf: dirs[3], wantRet: EXIT_SUCCESS,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.c.cleanCache()
gotRet := pathfinder(&tt.args.w, tt.args.c, tt.args.ignoreDir, tt.args.path)
gotBuf := tt.args.w.String()
if gotBuf != tt.wantBuf {
t.Errorf("pathfinder = %v, wantBuf = %v", gotBuf, tt.wantBuf)
}
if gotRet != tt.wantRet {
t.Errorf("pathfinder = %v, wantRet = %v", gotRet, tt.wantRet)
}
})
}
}