refactor(test): snapshot env in shell utils e2e

This commit is contained in:
Peter Steinberger
2026-02-21 18:43:58 +00:00
parent c3e1c82871
commit 7ba09e414f

View File

@@ -2,13 +2,13 @@ import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { captureEnv } from "../test-utils/env.js";
import { getShellConfig, resolveShellFromPath } from "./shell-utils.js";
const isWin = process.platform === "win32";
describe("getShellConfig", () => {
const originalShell = process.env.SHELL;
const originalPath = process.env.PATH;
let envSnapshot: ReturnType<typeof captureEnv>;
const tempDirs: string[] = [];
const createTempBin = (files: string[]) => {
@@ -23,22 +23,14 @@ describe("getShellConfig", () => {
};
beforeEach(() => {
envSnapshot = captureEnv(["SHELL", "PATH"]);
if (!isWin) {
process.env.SHELL = "/usr/bin/fish";
}
});
afterEach(() => {
if (originalShell == null) {
delete process.env.SHELL;
} else {
process.env.SHELL = originalShell;
}
if (originalPath == null) {
delete process.env.PATH;
} else {
process.env.PATH = originalPath;
}
envSnapshot.restore();
for (const dir of tempDirs.splice(0)) {
fs.rmSync(dir, { recursive: true, force: true });
}
@@ -81,7 +73,7 @@ describe("getShellConfig", () => {
});
describe("resolveShellFromPath", () => {
const originalPath = process.env.PATH;
let envSnapshot: ReturnType<typeof captureEnv>;
const tempDirs: string[] = [];
const createTempBin = (name: string, executable: boolean) => {
@@ -97,12 +89,12 @@ describe("resolveShellFromPath", () => {
return dir;
};
beforeEach(() => {
envSnapshot = captureEnv(["PATH"]);
});
afterEach(() => {
if (originalPath == null) {
delete process.env.PATH;
} else {
process.env.PATH = originalPath;
}
envSnapshot.restore();
for (const dir of tempDirs.splice(0)) {
fs.rmSync(dir, { recursive: true, force: true });
}