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