Skip to content

Commit a6ce54e

Browse files
test(file-safety): kill the three options-guard mutants with platform-pinned specs
The changed-code sweep left three survivors on 1910 that a spec change can close: safeWriteText.ts:402 reads options?.onWarning, and :572 and :692 read options?.execFileRunner. The mutated form differs from the clean one only when the options object is absent, so the documented platform override cannot trip these chains: passing { platform: "win32" } is what makes options non-null, and non-null options is the case the chain already handles. Two tests therefore call safeWriteText with no options at all and pin process.platform for the duration of the call, restoring its own descriptor afterwards, so the win32 DACL steps run the same way on a Linux runner as on Windows instead of being gated on the host. The third test keeps the override form and pins the icacls save-then-restore ordering that the OS-gated test at line 752 could not assert off a Windows host; it kills none of the three mutants, and that gap is reported in the test comment rather than papered over with a weaker assertion. Arm evidence, each mutant written over the shipped source, the suite run against it, then the source restored and hash-compared: 402 sha256-16 8e91f5e89c64731c 1 failed, 125 passed - fails the default-sink warning test 572 sha256-16 7adb6c41be7b21f2 3 failed, 123 passed - also fails the shipped no-options test and the save/restore ordering test 692 sha256-16 f4b9b9c332cce6ee 3 failed, 123 passed - the same two tests Suite 123 -> 126 tests, exit code 0. Shipped source unchanged: safeWriteText.ts 37,164 bytes fb308ea2d1a62b44. No Stryker directive added, no suppressions touched, no changeset.
1 parent 6010a57 commit a6ce54e

1 file changed

Lines changed: 88 additions & 7 deletions

File tree

‎src/services/file-safety/__tests__/safeWriteText.spec.ts‎

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,13 +1833,15 @@ describe("residue a publish must not lose", () => {
18331833
// bodies unchanged; the helpers they need are the sweep helpers, kept here so each assertion stays
18341834
// in the shape that killed its mutant. 44 of these killed a mutant the shipped 75 tests let live,
18351835
// and K313b, K353c, K413b and K637b are the four modified forms that killed theirs.
1836-
// Mutants with no test here fall into three named classes: equivalence at lines 475, 476, 490, 714
1837-
// and 758 (the strict comparison already forces the value the replacement produces, or the state the
1838-
// replacement reaches is unreachable), the win32 DACL optional-chain rows at 572 and 692 (killed on
1839-
// this Windows host by the shipped no-options test above, which never overrides `platform`, so they
1840-
// need a leg that runs the real win32 step), line 402, which nothing in this package kills on this
1841-
// host either, and the line 474 second-`&&` mutant, which does not reproduce here at all. Each is
1842-
// named with its mechanism in that comment rather than papered over with a weaker assertion.
1836+
// Mutants with no test here are the equivalence rows: lines 475, 476, 490, 714 and 758, where the
1837+
// strict comparison already forces the value the replacement produces or the state the replacement
1838+
// reaches is unreachable. The win32 optional-chain rows at 402, 572 and 692 are covered by the three
1839+
// tests at the end of this block: the guard being mutated dereferences the whole options object, so
1840+
// passing the documented `platform` override makes it non-null and cannot trip the chain - those two
1841+
// tests supply no options object and pin process.platform for the call, which reads the same on a
1842+
// Linux runner as on this host. The line 474 whole-condition mutant does not reproduce here at all;
1843+
// CI killed it in the run of these specs (322 killed printed against head 6010a57a0b53), so the
1844+
// mechanism is named rather than papered over with a weaker assertion.
18431845

18441846
describe("assertions ported from the changed-code mutation sweep", () => {
18451847
beforeEach(() => {
@@ -2312,4 +2314,83 @@ describe("assertions ported from the changed-code mutation sweep", () => {
23122314
spy.mock.calls.some((c: unknown[]) => String(c[0]).includes("onWarning callback failed: sinkblew")),
23132315
).toBe(true)
23142316
})
2317+
// The three remaining optional chains guard the whole options object, not the field named in the
2318+
// replacement: line 402 reads options?.onWarning, lines 572 and 692 read options?.execFileRunner. A
2319+
// mutation to `options.field` therefore only differs from the clean program when options is absent,
2320+
// which is why these two tests pass no options object at all, and why the documented platform
2321+
// override cannot kill them: supplying `{ platform: "win32" }` is what makes options non-null, and
2322+
// non-null options is exactly the case the chain already handles. To reach the win32 steps with no
2323+
// options object the tests pin process.platform for the duration of the call and restore its own
2324+
// descriptor after it, so the assertion runs identically on Linux and on Windows instead of being
2325+
// gated on the host. The third test keeps the override form and asserts only the reachability the
2326+
// OS-gated test at line 752 could not assert off a Windows host; it kills none of the three mutants,
2327+
// and that gap is the finding, not an oversight.
2328+
const withPlatformPinned = async <T>(value: string, run: () => Promise<T>): Promise<T> => {
2329+
const own = Object.getOwnPropertyDescriptor(process, "platform")
2330+
Object.defineProperty(process, "platform", { value, configurable: true, writable: true })
2331+
try {
2332+
return await run()
2333+
} finally {
2334+
if (own) {
2335+
Object.defineProperty(process, "platform", own)
2336+
}
2337+
}
2338+
}
2339+
const win32IcaclsOk = () =>
2340+
vi.mocked(execFile).mockImplementation(((
2341+
_cmd: string,
2342+
_args: unknown[],
2343+
_opts: unknown,
2344+
cb: (e: unknown, stdout?: string) => void,
2345+
) => {
2346+
cb(null, "")
2347+
return { kill: () => true } as never
2348+
}) as never)
2349+
it("K402 K572 K692 the default sink gets the warning when no options object exists", async () => {
2350+
const targetPath = "/tmp/test-dir/target.txt"
2351+
vi.mocked(fs.realpath).mockResolvedValue(targetPath)
2352+
// The DACL save succeeds and the restore fails, so the post-commit warning at line 703 is the
2353+
// only warning this call can produce, and with no options object its sink is the default
2354+
// console.warn fallback that line 402 selects through the mutated chain.
2355+
restoreFailExecFile2()
2356+
const spy = vi.spyOn(console, "warn").mockImplementation(() => {})
2357+
const failure = await withPlatformPinned("win32", () => safeWriteText(targetPath, "x")).catch((e: unknown) => e)
2358+
expect(failure).toBeInstanceOf(DaclRestoreError)
2359+
const printed = spy.mock.calls.map((c: unknown[]) => String(c[0]))
2360+
expect(printed.some((m: string) => m.includes("could not restore its saved access rights"))).toBe(true)
2361+
// Under the line 402 mutant the fallback lookup throws before any sink exists and the warning
2362+
// arrives only as a report about the callback, so the message text is the discriminating fact.
2363+
expect(printed.some((m: string) => m.includes("onWarning callback"))).toBe(false)
2364+
expect(execFile).toHaveBeenCalledTimes(2)
2365+
spy.mockRestore()
2366+
})
2367+
it("K572 K692 the DACL save and restore both run when no options object exists", async () => {
2368+
const targetPath = "/tmp/test-dir/target.txt"
2369+
vi.mocked(fs.realpath).mockResolvedValue(targetPath)
2370+
win32IcaclsOk()
2371+
const spy = vi.spyOn(console, "warn").mockImplementation(() => {})
2372+
const result = await withPlatformPinned("win32", () => safeWriteText(targetPath, "x"))
2373+
expect(result).toEqual({ leftoverPaths: [] })
2374+
expect(execFile).toHaveBeenCalledTimes(2)
2375+
expect(vi.mocked(execFile).mock.calls.map((c: unknown[]) => String((c[1] as unknown[])[1]))).toEqual([
2376+
"/save",
2377+
"/restore",
2378+
])
2379+
expect(vi.mocked(fs.unlink).mock.calls.some((c: unknown[]) => String(c[0]).includes("safeWriteText.acl"))).toBe(
2380+
true,
2381+
)
2382+
expect(spy.mock.calls.length).toBe(0)
2383+
spy.mockRestore()
2384+
})
2385+
it("K572 K692 the documented override reaches the win32 DACL steps on any host", async () => {
2386+
const targetPath = "/tmp/test-dir/target.txt"
2387+
vi.mocked(fs.realpath).mockResolvedValue(targetPath)
2388+
win32IcaclsOk()
2389+
await expect(safeWriteText(targetPath, "x", { platform: "win32" })).resolves.toEqual({ leftoverPaths: [] })
2390+
expect(execFile).toHaveBeenCalledTimes(2)
2391+
expect(vi.mocked(execFile).mock.calls.map((c: unknown[]) => String((c[1] as unknown[])[1]))).toEqual([
2392+
"/save",
2393+
"/restore",
2394+
])
2395+
})
23152396
})

0 commit comments

Comments
 (0)