package execenv import ( "runtime" "slices" "testing" ) func TestKeyIdentityForWindows(t *testing.T) { tests := []struct { name string key string windows bool want string }{ {name: "Unix stays exact", key: "BeAdS_DiR", want: "BeAdS_DiR"}, {name: "Windows lowercases", key: "BeAdS_DiR", windows: true, want: "beads_dir"}, {name: "Windows keeps long s distinct", key: "BEADſ_DIR", windows: true, want: "beadſ_dir"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { goos := "linux" if tt.windows { goos = "windows" } if got := KeyIdentityForOS(tt.key, goos); got != tt.want { t.Errorf("KeyIdentityForOS(%q, %q) = %q, want %q", tt.key, goos, got, tt.want) } }) } } func TestEntryKey(t *testing.T) { for _, tc := range []struct{ entry, want string }{ {"NAME=value=with=equals", "NAME"}, {"NAME=", "NAME"}, {"GIT_CONFIG_COUNT", "GIT_CONFIG_COUNT"}, {"GIT_DIR", "GIT_DIR"}, {"", ""}, {`=C:=C:\work`, "=C:"}, {"=leading", ""}, } { t.Run(tc.entry, func(t *testing.T) { if got := EntryKey(tc.entry); got != tc.want { t.Errorf("EntryKey(%q) = %q, want %q", tc.entry, got, tc.want) } }) } } func TestKeyEqualUsesHostSemantics(t *testing.T) { if got, want := KeyEqual("BeAdS_DiR", "BEADS_DIR"), runtime.GOOS == "windows"; got != want { t.Errorf("KeyEqual mixed-case = %v, want %v on %s", got, want, runtime.GOOS) } if KeyEqual("BEADſ_DIR", "BEADS_DIR") { t.Error("KeyEqual merged Unicode long s with ASCII s") } } func TestEnvironmentOperationsForBothHosts(t *testing.T) { for _, tc := range []struct { name string windows bool }{ {name: "Unix"}, {name: "Windows", windows: true}, } { t.Run(tc.name, func(t *testing.T) { previousHost := hostOS hostOS = "linux" if tc.windows { hostOS = "windows" } t.Cleanup(func() { hostOS = previousHost }) // Exercise all four exported host wrappers, so hardwiring one to // POSIX semantics is falsifiable even when this test runs on Linux. env := []string{"MIXED=canonical-first", "mixed=lower-last", "KEEP=first", "KEEP=second", "MALFORMED", `=C:=C:\work`, "MIXEſ=near-collision"} original := slices.Clone(env) if got := KeyEqual("MIXED", "mixed"); got != tc.windows { t.Errorf("mixed-case equality = %v", got) } if KeyEqual("MIXEſ", "MIXES") { t.Error("equality merged the Unicode near-collision") } wantValue := "canonical-first" wantWithout := []string{"KEEP=first", "KEEP=second", "MALFORMED", `=C:=C:\work`, "MIXEſ=near-collision"} if tc.windows { wantValue = "lower-last" } else { wantWithout = append([]string{"mixed=lower-last"}, wantWithout...) } if value, ok := Lookup(env, "MIXED"); !ok || value != wantValue { t.Errorf("lookup = %q, %v, want %q, true", value, ok, wantValue) } if value, ok := Lookup(env, "=C:"); !ok || value != `C:\work` { t.Errorf("drive lookup = %q, %v", value, ok) } if got := ContainsKeyWithPrefix(env, "miX"); got != tc.windows { t.Errorf("mixed-case prefix = %v", got) } if got := KeyHasPrefixForOS("MIXED", "miX", hostOS); got != tc.windows { t.Errorf("explicit key prefix = %v", got) } if got := Without(env, "MIXED"); !slices.Equal(got, wantWithout) { t.Errorf("without = %q, want %q", got, wantWithout) } if !slices.Equal(env, original) { t.Errorf("input mutated: %q", env) } }) } } func TestWithoutUsesHostSemanticsAndPreservesOtherEntries(t *testing.T) { in := []string{ "FIRST=keep-first", "beads_dir=drop-on-windows", "BEADS_DIR=drop-canonical", "ALLOWED=keep-duplicate-first", "MALFORMED", "BEADſ_DIR=keep-unicode-near-collision", "ALLOWED=keep-duplicate-second", `=C:=C:\work`, "LAST=keep-last", } original := slices.Clone(in) got := Without(in, "BEADS_DIR") want := []string{ "FIRST=keep-first", "beads_dir=drop-on-windows", "ALLOWED=keep-duplicate-first", "MALFORMED", "BEADſ_DIR=keep-unicode-near-collision", "ALLOWED=keep-duplicate-second", `=C:=C:\work`, "LAST=keep-last", } if runtime.GOOS == "windows" { want = slices.Delete(want, 1, 2) } if !slices.Equal(got, want) { t.Fatalf("Without() = %q, want %q on %s", got, want, runtime.GOOS) } if !slices.Equal(in, original) { t.Fatalf("Without modified input: got %q, want %q", in, original) } } func TestLookupUsesLastEffectiveValue(t *testing.T) { env := []string{ "TARGET=first", "target=mixed-case", "TARGET=last", "MALFORMED", `=C:=C:\work`, } value, ok := Lookup(env, "TARGET") if !ok || value != "last" { t.Fatalf("Lookup(TARGET) = %q, %v; want last, true", value, ok) } if value, ok := Lookup(env, "target"); runtime.GOOS == "windows" { if !ok || value != "last" { t.Fatalf("Lookup(target) = %q, %v; want last, true on Windows", value, ok) } } else if !ok || value != "mixed-case" { t.Fatalf("Lookup(target) = %q, %v; want mixed-case, true on %s", value, ok, runtime.GOOS) } if value, ok := Lookup(env, "MISSING"); ok || value != "" { t.Fatalf("Lookup(MISSING) = %q, %v; want empty, false", value, ok) } if value, ok := Lookup(env, "=C:"); !ok || value != `C:\work` { t.Fatalf("Lookup(=C:) = %q, %v; want %q, true", value, ok, `C:\work`) } } func TestContainsKeyWithPrefixUsesKeyIdentity(t *testing.T) { env := []string{ "MALFORMED", `=C:=C:\work`, "dolt_remote_password=secret", "DOLT_REMOTEſ_PASSWORD=near-collision", } if got, want := ContainsKeyWithPrefix(env, "DOLT_REMOTE_"), runtime.GOOS == "windows"; got != want { t.Errorf("ContainsKeyWithPrefix mixed-case = %v, want %v on %s", got, want, runtime.GOOS) } if ContainsKeyWithPrefix([]string{"DOLT_REMOTEſ_PASSWORD=secret"}, "DOLT_REMOTES_") { t.Error("ContainsKeyWithPrefix merged Unicode long s with ASCII s") } if ContainsKeyWithPrefix([]string{"DOLT_REMOTE_PASSWORD", `=C:=C:\work`}, "DOLT_REMOTE_") { t.Error("ContainsKeyWithPrefix treated a malformed or drive entry as a matching key-value entry") } }