From 3fdfd6073a45fe712101739231f6688d3c5cda28 Mon Sep 17 00:00:00 2001 From: Matthew McEachen Date: Mon, 16 Dec 2024 11:57:31 -0800 Subject: [PATCH] improve tests: replace rootDir with systemDrive(). Add expected error regex pattern. --- src/test-utils/platform.ts | 3 ++- src/volume_metadata.test.ts | 15 +++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/test-utils/platform.ts b/src/test-utils/platform.ts index 78ac8b7..8739620 100644 --- a/src/test-utils/platform.ts +++ b/src/test-utils/platform.ts @@ -4,6 +4,7 @@ import { mkdirSync } from "node:fs"; import { homedir } from "node:os"; import { join } from "node:path"; import { env, platform } from "node:process"; +import { normalizePath } from "../path.js"; import { isMacOS, isWindows } from "../platform.js"; import { toNotBlank } from "../string.js"; @@ -18,7 +19,7 @@ export function describePlatform(...supported: NodeJS.Platform[]) { export function systemDrive() { if (isWindows) { - return toNotBlank(env["SystemDrive"] ?? "") ?? "C:\\"; + return normalizePath(toNotBlank(env["SystemDrive"] ?? "") ?? "C:\\") as string; } else { return "/"; } diff --git a/src/volume_metadata.test.ts b/src/volume_metadata.test.ts index f850487..57130b5 100644 --- a/src/volume_metadata.test.ts +++ b/src/volume_metadata.test.ts @@ -2,6 +2,7 @@ // src/volume_metadata.test.ts import { jest } from "@jest/globals"; +import { join } from "node:path"; import { compact, times } from "./array.js"; import { TimeoutError } from "./async.js"; import { @@ -15,11 +16,13 @@ import { IncludeSystemVolumesDefault } from "./options.js"; import { isLinux, isMacOS, isWindows } from "./platform.js"; import { pickRandom, randomLetter, randomLetters, shuffle } from "./random.js"; import { assertMetadata } from "./test-utils/assert.js"; +import { systemDrive } from "./test-utils/platform.js"; import { MiB } from "./units.js"; +const rootPath = systemDrive(); + describe("Volume Metadata", () => { it("should get root filesystem metadata", async () => { - const rootPath = isWindows ? "C:\\" : "/"; const metadata = await getVolumeMetadata(rootPath); console.dir(metadata); @@ -168,8 +171,6 @@ describe("getAllVolumeMetadata()", () => { if (!isWindows) { describe("Timeout Handling", () => { - const rootPath = isWindows ? "C:\\" : "/"; - it("should handle getVolumeMountPoints() timeout", async () => { await expect(getVolumeMountPoints({ timeoutMs: 1 })).rejects.toThrow( /timeout/i, @@ -187,10 +188,8 @@ if (!isWindows) { describe("Error Handling", () => { it("should handle invalid paths appropriately", async () => { const invalidPaths = [ - isWindows ? "A:\\" : "/nonexistent", - isWindows - ? "C:\\Really_Invalid_Path_123456789" - : "/really/invalid/path/123456789", + isWindows ? "A:\\" : "/nonexistent-root-directory", + join(rootPath, "nonexistent", "path", "123456789"), "", null, undefined, @@ -198,7 +197,7 @@ describe("Error Handling", () => { for (const path of invalidPaths) { await expect(getVolumeMetadata(path as string)).rejects.toThrow( - /ENOENT|invalid|not accessible/i, + /ENOENT|invalid|not accessible|opendir/i, ); } });