Skip to content

Commit 0d7ed16

Browse files
committed
test: make NormalizeDir/WriteRootConfig/Regenerate tests Windows-portable
After NormalizeDir was canonicalized to forward slashes (and WriteRootConfig already runs all paths through toGitPath), the corresponding test expectations — built via filepath.Join, which uses OS-native separators — no longer matched on Windows. Run those expectations through the same slash-conversion as the production code. POSIX-rooted absolute paths like /Users/x/projects/work are not absolute on Windows (filepath.Abs resolves them against the cwd's drive), so the two NormalizeDir rows that use them are skipped on Windows. Equivalent semantics are still covered by the tilde and relative-path rows. Signed-off-by: Andre Nogueira <aanogueira@protonmail.com>
1 parent 58ee829 commit 0d7ed16

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

internal/config/dirs_test.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ package config
33
import (
44
"os"
55
"path/filepath"
6+
"runtime"
67
"strings"
78
"testing"
89
)
910

11+
// toFwd mirrors NormalizeDir's backslash-to-slash canonicalization so test
12+
// expectations built via filepath.Join (which uses OS-native separators)
13+
// stay comparable on Windows.
14+
func toFwd(p string) string { return strings.ReplaceAll(p, `\`, `/`) }
15+
1016
func TestNormalizeDir(t *testing.T) {
1117
t.Parallel()
1218

@@ -20,23 +26,35 @@ func TestNormalizeDir(t *testing.T) {
2026
t.Fatalf("Getwd failed: %v", err)
2127
}
2228

23-
tests := []struct {
29+
type tcase struct {
2430
name string
2531
in string
2632
want string
27-
}{
28-
{"absolute path gets trailing slash", "/Users/x/projects/work", "/Users/x/projects/work/"},
29-
{
30-
"absolute path keeps existing trailing slash",
31-
"/Users/x/projects/work/",
32-
"/Users/x/projects/work/",
33-
},
34-
{"tilde expands to home", "~/projects/work", filepath.Join(home, "projects", "work") + "/"},
35-
{"relative resolves against cwd", "./foo", filepath.Join(cwd, "foo") + "/"},
33+
}
34+
35+
tests := []tcase{
36+
{"tilde expands to home", "~/projects/work", toFwd(filepath.Join(home, "projects", "work")) + "/"},
37+
{"relative resolves against cwd", "./foo", toFwd(filepath.Join(cwd, "foo")) + "/"},
3638
{"single-star glob passes through unchanged", "~/work/*/repo", "~/work/*/repo"},
3739
{"double-star glob passes through unchanged", "~/work/**", "~/work/**"},
3840
}
3941

42+
// POSIX-rooted absolute paths (`/Users/x/...`) are not absolute on
43+
// Windows — `filepath.Abs` resolves them against the cwd's drive,
44+
// producing `D:/Users/x/...`. Skip those rows on Windows; the
45+
// equivalent semantics are exercised by the tilde + relative cases
46+
// above, which build absolute paths via filepath.Join.
47+
if runtime.GOOS != "windows" {
48+
tests = append(tests,
49+
tcase{"absolute path gets trailing slash", "/Users/x/projects/work", "/Users/x/projects/work/"},
50+
tcase{
51+
"absolute path keeps existing trailing slash",
52+
"/Users/x/projects/work/",
53+
"/Users/x/projects/work/",
54+
},
55+
)
56+
}
57+
4058
for _, tc := range tests {
4159
t.Run(tc.name, func(t *testing.T) {
4260
t.Parallel()

internal/git/git_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func TestWriteRootConfigDefaultAndAssignments(t *testing.T) {
331331
t.Errorf("missing [include] block:\n%s", content)
332332
}
333333

334-
if !strings.Contains(content, "path = "+defaultProfilePath) {
334+
if !strings.Contains(content, "path = "+toGitPath(defaultProfilePath)) {
335335
t.Errorf("missing default profile path:\n%s", content)
336336
}
337337

@@ -464,7 +464,7 @@ func TestRegenerate(t *testing.T) {
464464
}
465465

466466
rootStr := string(root)
467-
wantInclude := filepath.Join(profilesDir, "work.gitconfig")
467+
wantInclude := toGitPath(filepath.Join(profilesDir, "work.gitconfig"))
468468

469469
if !strings.Contains(rootStr, "path = "+wantInclude) {
470470
t.Errorf("root missing default include for %q:\n%s", wantInclude, rootStr)

0 commit comments

Comments
 (0)