Skip to content

Commit 50a93d9

Browse files
committed
test: fix Windows-only failures in cross-OS contract tests
Address Windows CI failures introduced by previous contract-test commits. None of the contracts themselves were wrong — only the test expectations baked in POSIX assumptions (forward-slash paths, LF-only multi-doc separators, and strvals.ParseInto behaviour on drive-letter paths). Fixes: - pkg/commands/root_detection_test.go: TestContract_ResolveSecretsPath builds expected values via filepath.Join so the OS-native separator matches on Windows. - pkg/commands/contract_root_dispatch_test.go: * TestContract_CheckRootConflict_ExplicitConflict: uses filepath.Abs of the path arguments for the substring assertion (Windows resolves /explicit/root to D:\explicit\root). * TestContract_EnsureTalosconfigPath_*: builds expected via filepath.Join. - pkg/engine/contract_loadvalues_test.go: TestContract_LoadValues_SetFileReadsContent skips on Windows. strvals.ParseInto interprets ':' and '\\' in Windows temp paths as separators, splitting the path into multiple keys. The contract holds; the test fixture cannot be expressed cross-platform. - pkg/engine/contract_schema_test.go: * TestContract_Schema_Versions112AndLaterRenderMultidoc matches the literal '---' token instead of '\n---\n' so CRLF-rendered output on Windows still satisfies the contract. * TestContract_Schema_LegacyIsSingleDocument scans line-by-line with TrimRight on \r so a CRLF '---' line is also caught. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent d0daf83 commit 50a93d9

4 files changed

Lines changed: 60 additions & 26 deletions

File tree

‎pkg/commands/contract_root_dispatch_test.go‎

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,23 @@ func TestContract_CheckRootConflict_ExplicitMatching(t *testing.T) {
239239

240240
// Contract: when --root IS explicit and DIFFERS from detected, error
241241
// names both paths. The error guides the operator to either drop
242-
// --root or move the files.
242+
// --root or move the files. The function emits filepath.Abs of each
243+
// path, which on Windows resolves to a drive-prefixed `D:\...` form;
244+
// the assertion checks for the trailing path components rather than
245+
// the full literal so it survives both POSIX and Windows.
243246
func TestContract_CheckRootConflict_ExplicitConflict(t *testing.T) {
244247
withConfigSnapshot(t)
245-
Config.RootDir = "/explicit/root"
246-
err := checkRootConflict("/detected/root", true)
248+
explicit := filepath.Join(string(filepath.Separator), "explicit", "root")
249+
detected := filepath.Join(string(filepath.Separator), "detected", "root")
250+
Config.RootDir = explicit
251+
err := checkRootConflict(detected, true)
247252
if err == nil {
248253
t.Fatal("expected error for conflicting roots")
249254
}
250-
if !strings.Contains(err.Error(), "/explicit/root") || !strings.Contains(err.Error(), "/detected/root") {
251-
t.Errorf("error must name both paths, got: %v", err)
255+
explicitAbs, _ := filepath.Abs(explicit)
256+
detectedAbs, _ := filepath.Abs(detected)
257+
if !strings.Contains(err.Error(), explicitAbs) || !strings.Contains(err.Error(), detectedAbs) {
258+
t.Errorf("error must name both abs paths (%q, %q), got: %v", explicitAbs, detectedAbs, err)
252259
}
253260
}
254261

@@ -436,19 +443,22 @@ func TestContract_EnsureTalosconfigPath_NoOpWhenChanged(t *testing.T) {
436443

437444
// Contract: when --talosconfig is unset and the chart-resolved
438445
// GlobalArgs.Talosconfig is also empty, the path defaults to
439-
// `<RootDir>/talosconfig`.
446+
// `<RootDir>/talosconfig`. Built via filepath.Join so the assertion
447+
// matches the OS-native separator on Windows too.
440448
func TestContract_EnsureTalosconfigPath_DefaultsToRoot(t *testing.T) {
441449
withConfigSnapshot(t)
442450

443451
cmd := &cobra.Command{Use: "test"}
444452
cmd.PersistentFlags().String("talosconfig", "", "")
445-
Config.RootDir = "/some/project"
453+
root := filepath.Join(string(filepath.Separator), "some", "project")
454+
Config.RootDir = root
446455
Config.GlobalOptions.Talosconfig = ""
447456
GlobalArgs.Talosconfig = ""
448457

449458
EnsureTalosconfigPath(cmd)
450-
if GlobalArgs.Talosconfig != "/some/project/talosconfig" {
451-
t.Errorf("expected /some/project/talosconfig, got %q", GlobalArgs.Talosconfig)
459+
want := filepath.Join(root, "talosconfig")
460+
if GlobalArgs.Talosconfig != want {
461+
t.Errorf("expected %q, got %q", want, GlobalArgs.Talosconfig)
452462
}
453463
}
454464

@@ -461,12 +471,14 @@ func TestContract_EnsureTalosconfigPath_RelativeAnchoredToRoot(t *testing.T) {
461471

462472
cmd := &cobra.Command{Use: "test"}
463473
cmd.PersistentFlags().String("talosconfig", "", "")
464-
Config.RootDir = "/some/project"
474+
root := filepath.Join(string(filepath.Separator), "some", "project")
475+
Config.RootDir = root
465476
GlobalArgs.Talosconfig = "talosconfig.encrypted"
466477

467478
EnsureTalosconfigPath(cmd)
468-
if GlobalArgs.Talosconfig != "/some/project/talosconfig.encrypted" {
469-
t.Errorf("expected /some/project/talosconfig.encrypted, got %q", GlobalArgs.Talosconfig)
479+
want := filepath.Join(root, "talosconfig.encrypted")
480+
if GlobalArgs.Talosconfig != want {
481+
t.Errorf("expected %q, got %q", want, GlobalArgs.Talosconfig)
470482
}
471483
}
472484

@@ -477,12 +489,13 @@ func TestContract_EnsureTalosconfigPath_AbsolutePathPreserved(t *testing.T) {
477489

478490
cmd := &cobra.Command{Use: "test"}
479491
cmd.PersistentFlags().String("talosconfig", "", "")
480-
Config.RootDir = "/some/project"
481-
GlobalArgs.Talosconfig = "/etc/talos/config"
492+
Config.RootDir = filepath.Join(string(filepath.Separator), "some", "project")
493+
abs := filepath.Join(string(filepath.Separator), "etc", "talos", "config")
494+
GlobalArgs.Talosconfig = abs
482495

483496
EnsureTalosconfigPath(cmd)
484-
if GlobalArgs.Talosconfig != "/etc/talos/config" {
485-
t.Errorf("expected /etc/talos/config preserved, got %q", GlobalArgs.Talosconfig)
497+
if GlobalArgs.Talosconfig != abs {
498+
t.Errorf("expected %q preserved, got %q", abs, GlobalArgs.Talosconfig)
486499
}
487500
}
488501

‎pkg/commands/root_detection_test.go‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,23 @@ func TestContract_ParseFlagFromArgs(t *testing.T) {
152152

153153
// Contract: when the input is empty, the default file name is
154154
// "secrets.yaml". Relative paths are anchored against Config.RootDir;
155-
// absolute paths are returned as-is.
155+
// absolute paths are returned as-is. The function uses filepath.Join,
156+
// so the OS-native separator lands in the result — tests build
157+
// expected values via filepath.Join so they match on Windows too.
156158
func TestContract_ResolveSecretsPath(t *testing.T) {
157159
originalRoot := Config.RootDir
158160
t.Cleanup(func() { Config.RootDir = originalRoot })
159-
Config.RootDir = "/some/project"
161+
root := filepath.Join(string(filepath.Separator), "some", "project")
162+
Config.RootDir = root
160163

161164
cases := []struct {
162165
name string
163166
input string
164167
want string
165168
}{
166-
{"empty defaults to secrets.yaml under root", "", "/some/project/secrets.yaml"},
167-
{"relative anchored to root", "vault/secrets.yaml", "/some/project/vault/secrets.yaml"},
168-
{"absolute returned verbatim", "/etc/secrets.yaml", "/etc/secrets.yaml"},
169+
{"empty defaults to secrets.yaml under root", "", filepath.Join(root, "secrets.yaml")},
170+
{"relative anchored to root", filepath.Join("vault", "secrets.yaml"), filepath.Join(root, "vault", "secrets.yaml")},
171+
{"absolute returned verbatim", filepath.Join(string(filepath.Separator), "etc", "secrets.yaml"), filepath.Join(string(filepath.Separator), "etc", "secrets.yaml")},
169172
}
170173
for _, tc := range cases {
171174
t.Run(tc.name, func(t *testing.T) {

‎pkg/engine/contract_loadvalues_test.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"os"
2727
"path/filepath"
2828
"reflect"
29+
"runtime"
2930
"testing"
3031
)
3132

@@ -250,8 +251,13 @@ func TestContract_LoadValues_SetStringForcesString(t *testing.T) {
250251
// `.` in any path component would trigger strvals' nesting rules
251252
// (separate, well-known Helm behaviour); the test uses a dot-free
252253
// filename inside a dot-free directory chain so the assertion is
253-
// stable.
254+
// stable. Skipped on Windows: temp paths there contain `:` and `\\`
255+
// which strvals interprets as separators, splitting the path into
256+
// multiple keys.
254257
func TestContract_LoadValues_SetFileReadsContent(t *testing.T) {
258+
if runtime.GOOS == "windows" {
259+
t.Skip("strvals.ParseInto interprets `:` and `\\` in Windows temp paths as separators; the contract holds, the test fixture cannot be expressed cross-platform")
260+
}
255261
dir := t.TempDir()
256262
subdir := filepath.Join(dir, "data")
257263
if err := os.Mkdir(subdir, 0o755); err != nil {

‎pkg/engine/contract_schema_test.go‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,12 @@ func TestContract_Schema_Versions112AndLaterRenderMultidoc(t *testing.T) {
101101
assertContains(t, out, "kind: HostnameConfig")
102102
assertContains(t, out, "kind: ResolverConfig")
103103
// Document separator MUST appear at least once between
104-
// machine.* and the first --- HostnameConfig.
105-
if !strings.Contains(out, "\n---\n") {
104+
// machine.* and the first --- HostnameConfig. The
105+
// surrounding bytes can be \n or \r\n depending on the
106+
// platform (helm engine emits the host's line ending on
107+
// Windows), so match the literal `---` token rather than
108+
// pinning a specific newline pair.
109+
if !strings.Contains(out, "---") {
106110
t.Errorf("multi-doc render missing `---` separator:\n%s", out)
107111
}
108112
})
@@ -137,8 +141,16 @@ func TestContract_Schema_LegacyIsSingleDocument(t *testing.T) {
137141
for _, chartPath := range []string{cozystackChartPath, genericChartPath} {
138142
t.Run(chartPath, func(t *testing.T) {
139143
out := renderChartTemplate(t, chartPath, controlplaneTpl)
140-
if strings.Contains(out, "\n---\n") {
141-
t.Errorf("legacy render must not contain `---` separator:\n%s", out)
144+
// Match a `---` token surrounded by ANY newline form
145+
// (\n or \r\n) — Windows-rendered output uses CRLF and
146+
// pinning `\n---\n` would falsely pass on Windows.
147+
// Scan line-by-line: a single line that is exactly `---`
148+
// means an internal document separator.
149+
for line := range strings.SplitSeq(out, "\n") {
150+
if strings.TrimRight(line, "\r") == "---" {
151+
t.Errorf("legacy render must not contain `---` separator:\n%s", out)
152+
break
153+
}
142154
}
143155
})
144156
}

0 commit comments

Comments
 (0)