GeezerVison is showing github.com — fix(sandbox): Make WSL backend tests deterministic. (#948) · Twigpine/zero@e59bf78
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: internal/sandbox/adapters.go +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | type BackendOptions struct { | 8 | 8 | GOOS string | 9 | 9 | LookupExecutable func(string) (string, error) | | 10 | + DetectWSL func() WSLInfo | 10 | 11 | } | 11 | 12 |
| 12 | 13 | type Backend struct { | @@ -53,6 +54,7 @@ func SelectBackend(options BackendOptions) Backend { | 53 | 54 | return NewSandboxManager(SandboxManagerOptions{ | 54 | 55 | GOOS: options.GOOS, | 55 | 56 | LookupExecutable: options.LookupExecutable, | | 57 | + DetectWSL: options.DetectWSL, | 56 | 58 | }).Backend() | 57 | 59 | } | 58 | 60 |
| |
Collapse file Copy file name to clipboardExpand all lines: internal/sandbox/adapters_test.go +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,6 +45,7 @@ func TestSelectBackendChoosesPlatformAdapterWithFallback(t *testing.T) { | 45 | 45 | } | 46 | 46 | return "", errors.New("missing") | 47 | 47 | }, | | 48 | + DetectWSL: func() WSLInfo { return WSLInfo{} }, | 48 | 49 | }) | 49 | 50 | if backend.Name != BackendUnavailable || backend.Available { | 50 | 51 | t.Fatalf("linux backend = %#v, want native sandbox unavailable without Linux helper", backend) | @@ -54,6 +55,20 @@ func TestSelectBackendChoosesPlatformAdapterWithFallback(t *testing.T) { | 54 | 55 | } | 55 | 56 | }) | 56 | 57 |
| | 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 | + | 57 | 72 | t.Run("darwin sandbox exec available", func(t *testing.T) { | 58 | 73 | backend := SelectBackend(BackendOptions{ | 59 | 74 | GOOS: "darwin", | |
Collapse file Copy file name to clipboardExpand all lines: internal/sandbox/manager.go +7-3Lines changed: 7 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -51,6 +51,7 @@ const ( | 51 | 51 | type SandboxManagerOptions struct { | 52 | 52 | GOOS string | 53 | 53 | LookupExecutable func(string) (string, error) | | 54 | + DetectWSL func() WSLInfo | 54 | 55 | Backend Backend | 55 | 56 | } | 56 | 57 |
| @@ -96,7 +97,7 @@ func NewSandboxManager(options SandboxManagerOptions) SandboxManager { | 96 | 97 | goos = runtime.GOOS | 97 | 98 | } | 98 | 99 | if backend.Name == "" { | 99 | | - backend = selectPlatformBackend(goos, options.LookupExecutable) | | 100 | + backend = selectPlatformBackend(goos, options.LookupExecutable, options.DetectWSL) | 100 | 101 | } | 101 | 102 | if backend.Platform == "" { | 102 | 103 | backend.Platform = goos | @@ -168,10 +169,13 @@ func lookupExecutable(name string) (string, error) { | 168 | 169 | return "", errors.New("executable file not found") | 169 | 170 | } | 170 | 171 |
| 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 { | 172 | 173 | if lookup == nil { | 173 | 174 | lookup = lookupExecutable | 174 | 175 | } | | 176 | + if detect == nil { | | 177 | + detect = detectWSL | | 178 | + } | 175 | 179 | switch goos { | 176 | 180 | case "linux": | 177 | 181 | if helper, err := lookup(LinuxSandboxHelperName); err == nil && helper != "" { | @@ -180,7 +184,7 @@ func selectPlatformBackend(goos string, lookup func(string) (string, error)) Bac | 180 | 184 | } | 181 | 185 | return nativeBackend(goos, BackendLinuxBwrap, helper, "Linux sandbox helper available") | 182 | 186 | } | 183 | | - if info := detectWSL(); info.IsWSL { | | 187 | + if info := detect(); info.IsWSL { | 184 | 188 | return wslBackend(goos, info) | 185 | 189 | } | 186 | 190 | return unavailableBackend(goos, "Linux sandbox helper is not available") | |
|