Skip to content

Commit 8a70431

Browse files
committed
test(uninstall): satisfy conditional guardrail
Signed-off-by: Aaron Erickson <aerickson@nvidia.com>
1 parent 3f5ee67 commit 8a70431

1 file changed

Lines changed: 39 additions & 26 deletions

File tree

src/lib/state/portable-uninstall-retirement.test.ts

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function fixture() {
3939

4040
type Fixture = ReturnType<typeof fixture>;
4141
type TargetRole = "config" | "receipt" | "registry";
42+
const noMutation = (): void => undefined;
4243

4344
function prepareFixture(test: Fixture) {
4445
return preparePortableRetirement(test.homeDir, [RECEIPT_BASENAME]);
@@ -217,12 +218,15 @@ describe("portable uninstall retirement state", () => {
217218
const outside = path.join(test.homeDir, "outside");
218219
fs.mkdirSync(outside, { mode: 0o700 });
219220
const unlink = fs.unlinkSync.bind(fs);
221+
const replacePortableDirectory = () => {
222+
fs.rmdirSync(portableDir);
223+
fs.symlinkSync(outside, portableDir, "dir");
224+
};
220225
vi.spyOn(fs, "unlinkSync").mockImplementation((target) => {
221226
unlink(target);
222-
if (String(target).includes(".containers.conf.portable-uninstall-")) {
223-
fs.rmdirSync(portableDir);
224-
fs.symlinkSync(outside, portableDir, "dir");
225-
}
227+
(String(target).includes(".containers.conf.portable-uninstall-")
228+
? replacePortableDirectory
229+
: noMutation)();
226230
});
227231

228232
expect(() => publishAndRetirePortableEvidence(prepareFixture(test))).toThrow();
@@ -238,13 +242,16 @@ describe("portable uninstall retirement state", () => {
238242
const outside = path.join(test.homeDir, "outside");
239243
fs.mkdirSync(path.join(outside, "portable"), { mode: 0o700, recursive: true });
240244
const unlink = fs.unlinkSync.bind(fs);
245+
const replaceConfigDirectory = () => {
246+
fs.rmdirSync(portableDir);
247+
fs.rmdirSync(configDir);
248+
fs.symlinkSync(outside, configDir, "dir");
249+
};
241250
vi.spyOn(fs, "unlinkSync").mockImplementation((target) => {
242251
unlink(target);
243-
if (String(target).includes(".containers.conf.portable-uninstall-")) {
244-
fs.rmdirSync(portableDir);
245-
fs.rmdirSync(configDir);
246-
fs.symlinkSync(outside, configDir, "dir");
247-
}
252+
(String(target).includes(".containers.conf.portable-uninstall-")
253+
? replaceConfigDirectory
254+
: noMutation)();
248255
});
249256

250257
expect(() => publishAndRetirePortableEvidence(prepareFixture(test))).toThrow();
@@ -268,14 +275,18 @@ describe("portable uninstall retirement state", () => {
268275
const lstat = fs.lstatSync.bind(fs);
269276
vi.spyOn(fs, "lstatSync").mockImplementation(((target, options) => {
270277
const stat = lstat(target, options as never);
271-
if (String(target) !== portableDir || typeof stat.uid !== "bigint") return stat;
272-
return new Proxy(stat, {
273-
get(current, property) {
274-
if (property === "uid") return current.uid + 1n;
275-
const value = Reflect.get(current, property, current) as unknown;
276-
return typeof value === "function" ? value.bind(current) : value;
277-
},
278-
});
278+
return String(target) === portableDir && typeof stat.uid === "bigint"
279+
? new Proxy(stat, {
280+
get(current, property) {
281+
const value = Reflect.get(current, property, current) as unknown;
282+
return property === "uid"
283+
? current.uid + 1n
284+
: typeof value === "function"
285+
? value.bind(current)
286+
: value;
287+
},
288+
})
289+
: stat;
279290
}) as typeof fs.lstatSync);
280291

281292
expect(() => publishAndRetirePortableEvidence(prepareFixture(test))).toThrow(/Unsafe/);
@@ -289,11 +300,12 @@ describe("portable uninstall retirement state", () => {
289300
const marker = path.join(portableDir, "concurrent.conf");
290301
const rmdir = fs.rmdirSync.bind(fs);
291302
let inserted = false;
303+
const insertMarker = () => {
304+
inserted = true;
305+
fs.writeFileSync(marker, "concurrent\n", { mode: 0o600 });
306+
};
292307
vi.spyOn(fs, "rmdirSync").mockImplementation((target) => {
293-
if (!inserted && String(target) === portableDir) {
294-
inserted = true;
295-
fs.writeFileSync(marker, "concurrent\n", { mode: 0o600 });
296-
}
308+
(!inserted && String(target) === portableDir ? insertMarker : noMutation)();
297309
return rmdir(target);
298310
});
299311

@@ -307,13 +319,14 @@ describe("portable uninstall retirement state", () => {
307319
const portableDir = path.dirname(test.config);
308320
const readdir = fs.readdirSync.bind(fs);
309321
let replaced = false;
322+
const replacePortableDirectory = () => {
323+
replaced = true;
324+
fs.rmdirSync(portableDir);
325+
fs.mkdirSync(portableDir, { mode: 0o700 });
326+
};
310327
vi.spyOn(fs, "readdirSync").mockImplementation(((target, options) => {
311328
const entries = readdir(target, options as never);
312-
if (!replaced && String(target) === portableDir) {
313-
replaced = true;
314-
fs.rmdirSync(portableDir);
315-
fs.mkdirSync(portableDir, { mode: 0o700 });
316-
}
329+
(!replaced && String(target) === portableDir ? replacePortableDirectory : noMutation)();
317330
return entries;
318331
}) as typeof fs.readdirSync);
319332

0 commit comments

Comments
 (0)