Skip to content

Commit e59bf78

Browse files
fix(sandbox): Make WSL backend tests deterministic. (#948)
Co-authored-by: euxaristia <euxaristia@users.noreply.github.com> Co-authored-by: cairn-code <cairn-code@users.noreply.github.com>
1 parent 6645f97 commit e59bf78

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎internal/sandbox/adapters.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
type BackendOptions struct {
88
GOOS string
99
LookupExecutable func(string) (string, error)
10+
DetectWSL func() WSLInfo
1011
}
1112

1213
type Backend struct {
@@ -53,6 +54,7 @@ func SelectBackend(options BackendOptions) Backend {
5354
return NewSandboxManager(SandboxManagerOptions{
5455
GOOS: options.GOOS,
5556
LookupExecutable: options.LookupExecutable,
57+
DetectWSL: options.DetectWSL,
5658
}).Backend()
5759
}
5860

‎internal/sandbox/adapters_test.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func TestSelectBackendChoosesPlatformAdapterWithFallback(t *testing.T) {
4545
}
4646
return "", errors.New("missing")
4747
},
48+
DetectWSL: func() WSLInfo { return WSLInfo{} },
4849
})
4950
if backend.Name != BackendUnavailable || backend.Available {
5051
t.Fatalf("linux backend = %#v, want native sandbox unavailable without Linux helper", backend)
@@ -54,6 +55,20 @@ func TestSelectBackendChoosesPlatformAdapterWithFallback(t *testing.T) {
5455
}
5556
})
5657

58+
t.Run("linux helper missing under WSL reports WSL fallback", func(t *testing.T) {
59+
backend := SelectBackend(BackendOptions{
60+
GOOS: "linux",
61+
LookupExecutable: func(string) (string, error) { return "", errors.New("missing") },
62+
DetectWSL: func() WSLInfo { return WSLInfo{IsWSL: true, IsWSL2: true} },
63+
})
64+
if backend.Name != BackendWSL || backend.Available || !backend.Fallback {
65+
t.Fatalf("linux backend = %#v, want explicit WSL fallback", backend)
66+
}
67+
if !strings.Contains(backend.Message, "WSL2") {
68+
t.Fatalf("linux fallback message = %q, want WSL2 diagnostic", backend.Message)
69+
}
70+
})
71+
5772
t.Run("darwin sandbox exec available", func(t *testing.T) {
5873
backend := SelectBackend(BackendOptions{
5974
GOOS: "darwin",

‎internal/sandbox/manager.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const (
5151
type SandboxManagerOptions struct {
5252
GOOS string
5353
LookupExecutable func(string) (string, error)
54+
DetectWSL func() WSLInfo
5455
Backend Backend
5556
}
5657

@@ -96,7 +97,7 @@ func NewSandboxManager(options SandboxManagerOptions) SandboxManager {
9697
goos = runtime.GOOS
9798
}
9899
if backend.Name == "" {
99-
backend = selectPlatformBackend(goos, options.LookupExecutable)
100+
backend = selectPlatformBackend(goos, options.LookupExecutable, options.DetectWSL)
100101
}
101102
if backend.Platform == "" {
102103
backend.Platform = goos
@@ -168,10 +169,13 @@ func lookupExecutable(name string) (string, error) {
168169
return "", errors.New("executable file not found")
169170
}
170171

171-
func selectPlatformBackend(goos string, lookup func(string) (string, error)) Backend {
172+
func selectPlatformBackend(goos string, lookup func(string) (string, error), detect func() WSLInfo) Backend {
172173
if lookup == nil {
173174
lookup = lookupExecutable
174175
}
176+
if detect == nil {
177+
detect = detectWSL
178+
}
175179
switch goos {
176180
case "linux":
177181
if helper, err := lookup(LinuxSandboxHelperName); err == nil && helper != "" {
@@ -180,7 +184,7 @@ func selectPlatformBackend(goos string, lookup func(string) (string, error)) Bac
180184
}
181185
return nativeBackend(goos, BackendLinuxBwrap, helper, "Linux sandbox helper available")
182186
}
183-
if info := detectWSL(); info.IsWSL {
187+
if info := detect(); info.IsWSL {
184188
return wslBackend(goos, info)
185189
}
186190
return unavailableBackend(goos, "Linux sandbox helper is not available")

0 commit comments

Comments
 (0)