Skip to content

Commit 6a751d3

Browse files
authored
test: make formatter import tests portable (#7882)
1 parent 928e478 commit 6a751d3

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

caddyconfig/caddyfile/format_imports_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ func TestDiscoverImportedFiles(t *testing.T) {
6161
t.Fatal(err)
6262
}
6363
// sites/a.caddy is discovered; "mysnip" is a snippet (defined in a.caddy), not a file
64-
want := []string{filepath.Join(dir, "sites", "a.caddy")}
65-
abs := func(p string) string { a, _ := filepath.Abs(p); return a }
66-
if len(files) != 1 || abs(files[0]) != abs(want[0]) {
64+
wantPath, err := canonicalPath(filepath.Join(dir, "sites", "a.caddy"))
65+
if err != nil {
66+
t.Fatal(err)
67+
}
68+
want := []string{wantPath}
69+
if len(files) != 1 || files[0] != want[0] {
6770
t.Errorf("got %v, want %v", files, want)
6871
}
6972
}

cmd/commands_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ func writeTestFile(t *testing.T, path, content string) {
4646
}
4747
}
4848

49+
func canonicalTestPath(t *testing.T, path string) string {
50+
t.Helper()
51+
path, err := filepath.EvalSymlinks(path)
52+
if err != nil {
53+
t.Fatal(err)
54+
}
55+
return path
56+
}
57+
4958
func captureStdout(t *testing.T, fn func() (int, error)) (string, int, error) {
5059
t.Helper()
5160
r, w, err := os.Pipe()
@@ -112,6 +121,11 @@ func TestCmdFmtImportsOverwrite(t *testing.T) {
112121
if err != nil {
113122
t.Fatal(err)
114123
}
124+
// Windows loads FileInfo IDs lazily when SameFile is first called. Prime
125+
// them before replacement so these values continue to identify the old files.
126+
if !os.SameFile(rootInfoBefore, rootInfoBefore) || !os.SameFile(importedInfoBefore, importedInfoBefore) {
127+
t.Fatal("could not snapshot file identity")
128+
}
115129

116130
fl := newFmtFlags(rootPath, true, false, true)
117131
code, err := cmdFmt(fl)
@@ -175,7 +189,7 @@ func TestCmdFmtImportsPreviewReportsFormattingDifference(t *testing.T) {
175189
if code != caddy.ExitCodeFailedStartup {
176190
t.Errorf("expected ExitCodeFailedStartup, got %d", code)
177191
}
178-
if !strings.Contains(output, "# "+importedPath) {
192+
if !strings.Contains(output, "# "+canonicalTestPath(t, importedPath)) {
179193
t.Errorf("output missing imported file header; got:\n%s", output)
180194
}
181195
if diff && !strings.Contains(output, "+ \trespond 200") {
@@ -257,10 +271,10 @@ func TestCmdFmtImportsPrintsHeaders(t *testing.T) {
257271
if code != caddy.ExitCodeSuccess {
258272
t.Errorf("expected ExitCodeSuccess, got %d", code)
259273
}
260-
if !strings.Contains(output, "# "+rootPath) {
274+
if !strings.Contains(output, "# "+canonicalTestPath(t, rootPath)) {
261275
t.Errorf("output missing root header; got:\n%s", output)
262276
}
263-
if !strings.Contains(output, "# "+importedPath) {
277+
if !strings.Contains(output, "# "+canonicalTestPath(t, importedPath)) {
264278
t.Errorf("output missing imported file header; got:\n%s", output)
265279
}
266280
}

0 commit comments

Comments
 (0)