Skip to content

Commit cc37024

Browse files
committed
tests: add tests on fsutils.ShortestRelPath
1 parent ca2769c commit cc37024

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

pkg/fsutils/fsutils.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ func Getwd() (string, error) {
3434
return
3535
}
3636

37-
evaledWd, err := EvalSymlinks(cachedWd)
37+
evaluatedWd, err := EvalSymlinks(cachedWd)
3838
if err != nil {
3939
cachedWd, cachedWdError = "", fmt.Errorf("can't eval symlinks on wd %s: %w", cachedWd, err)
4040
return
4141
}
4242

43-
cachedWd = evaledWd
43+
cachedWd = evaluatedWd
4444
})
4545

4646
return cachedWd, cachedWdError
@@ -83,8 +83,8 @@ func ShortestRelPath(path, wd string) (string, error) {
8383
path = evaledPath
8484

8585
// make path absolute and then relative to be able to fix this case:
86-
// we are in /test dir, we want to normalize ../test, and have file file.go in this dir;
87-
// it must have normalized path file.go, not ../test/file.go,
86+
// we are in `/test` dir, we want to normalize `../test`, and have file `file.go` in this dir;
87+
// it must have normalized path `file.go`, not `../test/file.go`,
8888
var absPath string
8989
if filepath.IsAbs(path) {
9090
absPath = path

pkg/fsutils/fsutils_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package fsutils
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestShortestRelPath(t *testing.T) {
12+
testCases := []struct {
13+
desc string
14+
path string
15+
wd string
16+
expected string
17+
}{
18+
{
19+
desc: "based on parent path",
20+
path: "fsutils_test.go",
21+
wd: filepath.Join("..", "fsutils"),
22+
expected: "fsutils_test.go",
23+
},
24+
{
25+
desc: "based on current working directory",
26+
path: "fsutils_test.go",
27+
wd: "",
28+
expected: "fsutils_test.go",
29+
},
30+
}
31+
32+
for _, test := range testCases {
33+
t.Run(test.desc, func(t *testing.T) {
34+
t.Parallel()
35+
36+
rel, err := ShortestRelPath("fsutils_test.go", filepath.Join("..", "fsutils"))
37+
require.NoError(t, err)
38+
39+
assert.Equal(t, test.expected, rel)
40+
})
41+
}
42+
}

0 commit comments

Comments
 (0)