forked from rliebz/tusk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_test.go
114 lines (86 loc) · 1.92 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
package runner
import (
"os"
"path/filepath"
"testing"
"github.com/rliebz/ghost"
"github.com/rliebz/ghost/be"
)
func TestSearchForFile(t *testing.T) {
tmpdir := useTempDir(t)
emptyDir := mkDir(t, tmpdir, "empty")
topDir := mkDir(t, tmpdir, "top")
topConfig := mkConfigFile(t, topDir, "tusk.yml")
yamlDir := mkDir(t, tmpdir, "yaml")
yamlConfig := mkConfigFile(t, yamlDir, "tusk.yaml")
nestedDir := mkDir(t, topDir, "foo", "bar")
nestedConfig := mkConfigFile(t, nestedDir, "tusk.yml")
inheritedDir := mkDir(t, topDir, "baz", "empty")
tests := []struct {
wd string
wantPath string
}{
{
wd: emptyDir,
wantPath: "",
},
{
wd: topDir,
wantPath: topConfig,
},
{
wd: yamlDir,
wantPath: yamlConfig,
},
{
wd: nestedDir,
wantPath: nestedConfig,
},
{
wd: inheritedDir,
wantPath: topConfig,
},
}
for _, tt := range tests {
t.Run(tt.wd+"_"+tt.wantPath, func(t *testing.T) {
g := ghost.New(t)
err := os.Chdir(tt.wd)
g.NoError(err)
fullPath, err := searchForFile()
g.NoError(err)
g.Should(be.Equal(tt.wantPath, fullPath))
})
}
}
func useTempDir(t *testing.T) string {
t.Helper()
g := ghost.New(t)
// MacOS gets fancy with symlinks, so this gets us the real working path.
tmpdir, err := filepath.EvalSymlinks(t.TempDir())
g.NoError(err)
oldwd, err := os.Getwd()
g.NoError(err)
err = os.Chdir(tmpdir)
g.NoError(err)
t.Cleanup(func() {
err := os.Chdir(oldwd)
g.Should(be.Nil(err))
})
return tmpdir
}
func mkDir(t *testing.T, elem ...string) string {
t.Helper()
g := ghost.New(t)
fullPath := filepath.Join(elem...)
err := os.MkdirAll(fullPath, 0o700)
g.NoError(err)
return fullPath
}
func mkConfigFile(t *testing.T, dir, fileName string) string {
t.Helper()
g := ghost.New(t)
fullPath := filepath.Join(dir, fileName)
err := os.WriteFile(fullPath, []byte{}, 0o600)
g.NoError(err)
return fullPath
}