Path: blob/main/tests/unit/ral/safe-remove-dir.test.ts
6451 views
/*1* safe-remove-dir.test.ts2*3* Copyright (C) 2025 Posit Software, PBC4*5*/67import { unitTest } from "../../test.ts";8import { assert, assertThrows } from "testing/asserts";910import { createTempContext } from "../../../src/core/temp.ts";11import { ensureDirSync, safeRemoveDirSync } from "../../../src/deno_ral/fs.ts";12import { join } from "../../../src/deno_ral/path.ts";13import { isWindows } from "../../../src/deno_ral/platform.ts";1415unitTest("safeRemoveDirSync", async () => {1617const temp = createTempContext();1819const d1 = temp.createDir();20const path = join(d1, "do-not-touch");21const boundary = join(d1, "project-root");22ensureDirSync(path);23ensureDirSync(boundary);2425assertThrows(() => {26safeRemoveDirSync(path, boundary);27});2829assert(Deno.statSync(path).isDirectory);3031temp.cleanup();32});3334unitTest("safeRemoveDirSync with symlinks", async () => {35const temp = createTempContext();3637const realDir = temp.createDir();38const projectRoot = join(realDir, "project-root");39const subDir = join(projectRoot, "test_files");40ensureDirSync(projectRoot);41ensureDirSync(subDir);4243const symlinkPath = join(realDir, "project-symlink");44Deno.symlinkSync(projectRoot, symlinkPath);4546// Test: path via symlink, boundary via real path - should succeed47const pathViaSymlink = join(symlinkPath, "test_files");48safeRemoveDirSync(pathViaSymlink, projectRoot);4950// Recreate for next test51ensureDirSync(subDir);5253// Test: path via real path, boundary via symlink - should succeed54safeRemoveDirSync(subDir, symlinkPath);5556// Cleanup symlink57Deno.removeSync(symlinkPath);5859temp.cleanup();60}, { ignore: isWindows });616263