Skip to content

Commit a8d65fb

Browse files
committed
feat: add tests for search and default dir
1 parent e907415 commit a8d65fb

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

internal/fsext/fs.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import (
88
"github.com/go-task/task/v3/internal/sysinfo"
99
)
1010

11+
// DefaultDir will return the default directory given an entrypoint or
12+
// directory. If the directory is set, it will ensure it is an absolute path and
13+
// return it. If the entrypoint is set, but the directory is not, it will leave
14+
// the directory blank. If both are empty, it will default the directory to the
15+
// current working directory.
1116
func DefaultDir(entrypoint, dir string) string {
1217
// If the directory is set, ensure it is an absolute path
1318
if dir != "" {
@@ -51,6 +56,11 @@ func Search(entrypoint, dir string, possibleFilenames []string) (string, string,
5156
}
5257
if dir == "" {
5358
dir = filepath.Dir(entrypoint)
59+
} else {
60+
dir, err = filepath.Abs(dir)
61+
if err != nil {
62+
return "", "", err
63+
}
5464
}
5565
return entrypoint, dir, nil
5666
}

internal/fsext/fs_test.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package fsext
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestDefaultDir(t *testing.T) {
12+
wd, err := os.Getwd()
13+
require.NoError(t, err)
14+
tests := []struct {
15+
name string
16+
entrypoint string
17+
dir string
18+
expected string
19+
}{
20+
{
21+
name: "default to current working directory",
22+
entrypoint: "",
23+
dir: "",
24+
expected: wd,
25+
},
26+
{
27+
name: "resolves relative dir path",
28+
entrypoint: "",
29+
dir: "./dir",
30+
expected: filepath.Join(wd, "dir"),
31+
},
32+
{
33+
name: "return entrypoint if set",
34+
entrypoint: filepath.Join(wd, "entrypoint"),
35+
dir: "",
36+
expected: "",
37+
},
38+
{
39+
name: "if entrypoint and dir are set",
40+
entrypoint: filepath.Join(wd, "entrypoint"),
41+
dir: filepath.Join(wd, "dir"),
42+
expected: filepath.Join(wd, "dir"),
43+
},
44+
{
45+
name: "if entrypoint and dir are set and dir is relative",
46+
entrypoint: filepath.Join(wd, "entrypoint"),
47+
dir: "./dir",
48+
expected: filepath.Join(wd, "dir"),
49+
},
50+
}
51+
for _, tt := range tests {
52+
t.Run(tt.name, func(t *testing.T) {
53+
require.Equal(t, tt.expected, DefaultDir(tt.entrypoint, tt.dir))
54+
})
55+
}
56+
}
57+
58+
func TestSearch(t *testing.T) {
59+
wd, err := os.Getwd()
60+
require.NoError(t, err)
61+
tests := []struct {
62+
name string
63+
entrypoint string
64+
dir string
65+
possibleFilenames []string
66+
expectedEntrypoint string
67+
expectedDir string
68+
}{
69+
{
70+
name: "find foo.txt using relative entrypoint",
71+
entrypoint: "./testdata/foo.txt",
72+
possibleFilenames: []string{"foo.txt"},
73+
expectedEntrypoint: filepath.Join(wd, "testdata", "foo.txt"),
74+
expectedDir: filepath.Join(wd, "testdata"),
75+
},
76+
{
77+
name: "find foo.txt using absolute entrypoint",
78+
entrypoint: filepath.Join(wd, "testdata", "foo.txt"),
79+
possibleFilenames: []string{"foo.txt"},
80+
expectedEntrypoint: filepath.Join(wd, "testdata", "foo.txt"),
81+
expectedDir: filepath.Join(wd, "testdata"),
82+
},
83+
{
84+
name: "find foo.txt using relative dir",
85+
dir: "./testdata",
86+
possibleFilenames: []string{"foo.txt"},
87+
expectedEntrypoint: filepath.Join(wd, "testdata", "foo.txt"),
88+
expectedDir: filepath.Join(wd, "testdata"),
89+
},
90+
{
91+
name: "find foo.txt using absolute dir",
92+
dir: filepath.Join(wd, "testdata"),
93+
possibleFilenames: []string{"foo.txt"},
94+
expectedEntrypoint: filepath.Join(wd, "testdata", "foo.txt"),
95+
expectedDir: filepath.Join(wd, "testdata"),
96+
},
97+
{
98+
name: "find foo.txt using relative dir and relative entrypoint",
99+
entrypoint: "./testdata/foo.txt",
100+
dir: "./testdata/some/other/dir",
101+
possibleFilenames: []string{"foo.txt"},
102+
expectedEntrypoint: filepath.Join(wd, "testdata", "foo.txt"),
103+
expectedDir: filepath.Join(wd, "testdata", "some", "other", "dir"),
104+
},
105+
{
106+
name: "find fs.go using no entrypoint or dir",
107+
entrypoint: "",
108+
dir: "",
109+
possibleFilenames: []string{"fs.go"},
110+
expectedEntrypoint: filepath.Join(wd, "fs.go"),
111+
expectedDir: wd,
112+
},
113+
{
114+
name: "find ../../Taskfile.yml using no entrypoint or dir by walking",
115+
entrypoint: "",
116+
dir: "",
117+
possibleFilenames: []string{"Taskfile.yml"},
118+
expectedEntrypoint: filepath.Join(wd, "..", "..", "Taskfile.yml"),
119+
expectedDir: filepath.Join(wd, "..", ".."),
120+
},
121+
{
122+
name: "find foo.txt first if listed first in possible filenames",
123+
entrypoint: "./testdata",
124+
possibleFilenames: []string{"foo.txt", "bar.txt"},
125+
expectedEntrypoint: filepath.Join(wd, "testdata", "foo.txt"),
126+
expectedDir: filepath.Join(wd, "testdata"),
127+
},
128+
{
129+
name: "find bar.txt first if listed first in possible filenames",
130+
entrypoint: "./testdata",
131+
possibleFilenames: []string{"bar.txt", "foo.txt"},
132+
expectedEntrypoint: filepath.Join(wd, "testdata", "bar.txt"),
133+
expectedDir: filepath.Join(wd, "testdata"),
134+
},
135+
}
136+
for _, tt := range tests {
137+
t.Run(tt.name, func(t *testing.T) {
138+
entrypoint, dir, err := Search(tt.entrypoint, tt.dir, tt.possibleFilenames)
139+
require.NoError(t, err)
140+
require.Equal(t, tt.expectedEntrypoint, entrypoint)
141+
require.Equal(t, tt.expectedDir, dir)
142+
})
143+
}
144+
}

internal/fsext/testdata/bar.txt

Whitespace-only changes.

internal/fsext/testdata/foo.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)