|
1 | 1 | // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -import { describe, expect, it } from "vitest"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
5 | 5 |
|
6 | | -import { buildOpenshellExecArgs, computeExitCode } from "./exec"; |
| 6 | +import { |
| 7 | + buildOpenshellExecArgs, |
| 8 | + buildWorkdirProbeArgs, |
| 9 | + computeExitCode, |
| 10 | + evaluateWorkdirProbe, |
| 11 | + validateWorkdirOrFail, |
| 12 | + workdirMissingMessage, |
| 13 | +} from "./exec"; |
7 | 14 |
|
8 | 15 | describe("buildOpenshellExecArgs", () => { |
9 | 16 | it("targets the sandbox by name and forwards the user command after --", () => { |
@@ -115,3 +122,109 @@ describe("computeExitCode", () => { |
115 | 122 | }); |
116 | 123 | }); |
117 | 124 | }); |
| 125 | + |
| 126 | +describe("buildWorkdirProbeArgs", () => { |
| 127 | + it("targets the sandbox by name and probes the directory with test -d", () => { |
| 128 | + expect(buildWorkdirProbeArgs("alpha", "/sandbox/workspace")).toEqual([ |
| 129 | + "sandbox", |
| 130 | + "exec", |
| 131 | + "--name", |
| 132 | + "alpha", |
| 133 | + "--", |
| 134 | + "test", |
| 135 | + "-d", |
| 136 | + "/sandbox/workspace", |
| 137 | + ]); |
| 138 | + }); |
| 139 | + |
| 140 | + it("does not split a path argument that contains whitespace", () => { |
| 141 | + const argv = buildWorkdirProbeArgs("alpha", "/sandbox/with spaces/dir"); |
| 142 | + expect(argv[argv.length - 1]).toBe("/sandbox/with spaces/dir"); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +describe("workdirMissingMessage", () => { |
| 147 | + it("renders a user-facing CLI error with the offending path", () => { |
| 148 | + expect(workdirMissingMessage("/sandbox/workspace")).toBe( |
| 149 | + "error: --workdir: /sandbox/workspace does not exist inside the sandbox", |
| 150 | + ); |
| 151 | + }); |
| 152 | +}); |
| 153 | + |
| 154 | +describe("evaluateWorkdirProbe", () => { |
| 155 | + it("returns 'ok' when the probe exits 0", () => { |
| 156 | + expect(evaluateWorkdirProbe({ status: 0 })).toBe("ok"); |
| 157 | + }); |
| 158 | + |
| 159 | + it("returns 'missing' only for the canonical test -d failure (exit 1)", () => { |
| 160 | + expect(evaluateWorkdirProbe({ status: 1 })).toBe("missing"); |
| 161 | + }); |
| 162 | + |
| 163 | + it("returns 'unclear' for any other exit code so the main exec surfaces it", () => { |
| 164 | + expect(evaluateWorkdirProbe({ status: 2 })).toBe("unclear"); |
| 165 | + expect(evaluateWorkdirProbe({ status: 127 })).toBe("unclear"); |
| 166 | + expect(evaluateWorkdirProbe({ status: null })).toBe("unclear"); |
| 167 | + }); |
| 168 | + |
| 169 | + it("returns 'unclear' when spawn reports a transport error", () => { |
| 170 | + expect(evaluateWorkdirProbe({ status: null, error: new Error("ENOENT") })).toBe("unclear"); |
| 171 | + }); |
| 172 | +}); |
| 173 | + |
| 174 | +describe("validateWorkdirOrFail", () => { |
| 175 | + afterEach(() => { |
| 176 | + vi.restoreAllMocks(); |
| 177 | + }); |
| 178 | + |
| 179 | + it("passes through when the directory exists", () => { |
| 180 | + const run = vi.fn(() => ({ status: 0 })); |
| 181 | + const exitSpy = vi.spyOn(process, "exit").mockImplementation(((_code?: number) => { |
| 182 | + throw new Error("process.exit should not be called for ok outcome"); |
| 183 | + }) as never); |
| 184 | + const errSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 185 | + |
| 186 | + validateWorkdirOrFail("openshell", "alpha", "/sandbox/workspace", run); |
| 187 | + |
| 188 | + expect(run).toHaveBeenCalledWith("openshell", [ |
| 189 | + "sandbox", |
| 190 | + "exec", |
| 191 | + "--name", |
| 192 | + "alpha", |
| 193 | + "--", |
| 194 | + "test", |
| 195 | + "-d", |
| 196 | + "/sandbox/workspace", |
| 197 | + ]); |
| 198 | + expect(exitSpy).not.toHaveBeenCalled(); |
| 199 | + expect(errSpy).not.toHaveBeenCalled(); |
| 200 | + }); |
| 201 | + |
| 202 | + it("prints a friendly error and exits 1 when the directory is missing", () => { |
| 203 | + const run = vi.fn(() => ({ status: 1 })); |
| 204 | + const exitSpy = vi.spyOn(process, "exit").mockImplementation(((_code?: number) => { |
| 205 | + throw new Error("exit"); |
| 206 | + }) as never); |
| 207 | + const errSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 208 | + |
| 209 | + expect(() => validateWorkdirOrFail("openshell", "alpha", "/sandbox/workspace", run)).toThrow( |
| 210 | + "exit", |
| 211 | + ); |
| 212 | + expect(errSpy).toHaveBeenCalledWith( |
| 213 | + "error: --workdir: /sandbox/workspace does not exist inside the sandbox", |
| 214 | + ); |
| 215 | + expect(exitSpy).toHaveBeenCalledWith(1); |
| 216 | + }); |
| 217 | + |
| 218 | + it("does not abort when the probe outcome is unclear (lets main exec surface it)", () => { |
| 219 | + const run = vi.fn(() => ({ status: 127 })); |
| 220 | + const exitSpy = vi.spyOn(process, "exit").mockImplementation(((_code?: number) => { |
| 221 | + throw new Error("process.exit should not be called for unclear outcome"); |
| 222 | + }) as never); |
| 223 | + const errSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 224 | + |
| 225 | + validateWorkdirOrFail("openshell", "alpha", "/sandbox/workspace", run); |
| 226 | + |
| 227 | + expect(exitSpy).not.toHaveBeenCalled(); |
| 228 | + expect(errSpy).not.toHaveBeenCalled(); |
| 229 | + }); |
| 230 | +}); |
0 commit comments