Skip to content

Commit

Permalink
test(fs): do not write files in source tree during test (denoland#6236)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbronder authored Dec 23, 2024
1 parent ed52423 commit 9bf17e4
Show file tree
Hide file tree
Showing 9 changed files with 438 additions and 196 deletions.
51 changes: 33 additions & 18 deletions fs/empty_dir_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import {
import * as path from "@std/path";
import { emptyDir, emptyDirSync } from "./empty_dir.ts";

const testdataDir = path.join(import.meta.dirname!, "testdata");

Deno.test("emptyDir() creates a new dir if it does not exist", async function () {
const testDir = path.join(testdataDir, "empty_dir_test_1");
const tempDirPath = await Deno.makeTempDir({ prefix: "deno_std_empty_dir_" });
const testDir = path.join(tempDirPath, "empty_dir_test_1");
const testNestDir = path.join(testDir, "nest");
// empty a dir which not exist. then it will create new one
await emptyDir(testNestDir);
Expand All @@ -21,13 +20,16 @@ Deno.test("emptyDir() creates a new dir if it does not exist", async function ()
const stat = await Deno.stat(testNestDir);
assertEquals(stat.isDirectory, true);
} finally {
// remove the test dir
await Deno.remove(testDir, { recursive: true });
// Cleanup and remove test directories.
await Deno.remove(tempDirPath, { recursive: true });
}
});

Deno.test("emptyDirSync() creates a new dir if it does not exist", function () {
const testDir = path.join(testdataDir, "empty_dir_test_2");
const tempDirPath = Deno.makeTempDirSync({
prefix: "deno_std_empty_dir_sync_",
});
const testDir = path.join(tempDirPath, "empty_dir_test_2");
const testNestDir = path.join(testDir, "nest");
// empty a dir which does not exist, then it will a create new one.
emptyDirSync(testNestDir);
Expand All @@ -37,13 +39,14 @@ Deno.test("emptyDirSync() creates a new dir if it does not exist", function () {
const stat = Deno.statSync(testNestDir);
assertEquals(stat.isDirectory, true);
} finally {
// remove the test dir
Deno.removeSync(testDir, { recursive: true });
// Cleanup and remove test directories.
Deno.removeSync(tempDirPath, { recursive: true });
}
});

Deno.test("emptyDir() empties nested dirs and files", async function () {
const testDir = path.join(testdataDir, "empty_dir_test_3");
const tempDirPath = await Deno.makeTempDir({ prefix: "deno_std_empty_dir_" });
const testDir = path.join(tempDirPath, "empty_dir_test_3");
const testNestDir = path.join(testDir, "nest");
// create test dir
await emptyDir(testNestDir);
Expand Down Expand Up @@ -80,13 +83,16 @@ Deno.test("emptyDir() empties nested dirs and files", async function () {
},
);
} finally {
// remote test dir
await Deno.remove(testDir, { recursive: true });
// Cleanup and remove test directory.
await Deno.remove(tempDirPath, { recursive: true });
}
});

Deno.test("emptyDirSync() empties nested dirs and files", function () {
const testDir = path.join(testdataDir, "empty_dir_test_4");
const tempDirPath = Deno.makeTempDirSync({
prefix: "deno_std_empty_dir_sync_",
});
const testDir = path.join(tempDirPath, "empty_dir_test_4");
const testNestDir = path.join(testDir, "nest");
// create test dir
emptyDirSync(testNestDir);
Expand Down Expand Up @@ -119,11 +125,14 @@ Deno.test("emptyDirSync() empties nested dirs and files", function () {
Deno.statSync(testDirFile);
});
} finally {
// remote test dir
Deno.removeSync(testDir, { recursive: true });
// Cleanup and remove test directory.
Deno.removeSync(tempDirPath, { recursive: true });
}
});

// Testing the permissions of emptyDir and emptyDirSync functions in a script
// that is running inside a Deno child process.
const testdataDir = path.join(import.meta.dirname!, "testdata");
interface Scenes {
read: boolean; // --allow-read
write: boolean; // --allow-write
Expand Down Expand Up @@ -191,9 +200,11 @@ for (const s of scenes) {
Deno.test(`${title} permission`, async function (): Promise<
void
> {
const testfolder = path.join(testdataDir, "testfolder");

const tempDirPath = await Deno.makeTempDir({
prefix: "deno_std_empty_dir_permissions_",
});
try {
const testfolder = path.join(tempDirPath, "testfolder");
await Deno.mkdir(testfolder);

await Deno.writeTextFile(
Expand Down Expand Up @@ -224,6 +235,10 @@ for (const s of scenes) {
),
);

// Passing the testfolder path as an argument to empty_dir.ts and
// empty_dir_sync.ts scripts.
args.push(testfolder);

const command = new Deno.Command(Deno.execPath(), {
args,
stderr: "inherit",
Expand All @@ -233,13 +248,13 @@ for (const s of scenes) {
} catch (err) {
// deno-lint-ignore no-console
console.log(err);
await Deno.remove(testfolder, { recursive: true });
await Deno.remove(tempDirPath, { recursive: true });
throw err;
}
} finally {
// Make the test rerunnable
// Otherwise it would throw an error due to mkdir fail.
await Deno.remove(testfolder, { recursive: true });
await Deno.remove(tempDirPath, { recursive: true });
// done
}
});
Expand Down
40 changes: 29 additions & 11 deletions fs/ensure_dir_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
const testdataDir = path.resolve(moduleDir, "testdata", "ensure_dir");

Deno.test("ensureDir() creates dir if it does not exist", async function () {
const baseDir = path.join(testdataDir, "not_exist");
const tempDirPath = await Deno.makeTempDir({
prefix: "deno_std_ensure_dir_",
});
const baseDir = path.join(tempDirPath, "not_exist");
const testDir = path.join(baseDir, "test");

try {
Expand All @@ -18,12 +21,15 @@ Deno.test("ensureDir() creates dir if it does not exist", async function () {
// test dir should exists.
await Deno.stat(testDir);
} finally {
await Deno.remove(baseDir, { recursive: true });
await Deno.remove(tempDirPath, { recursive: true });
}
});

Deno.test("ensureDirSync() creates dir if it does not exist", function () {
const baseDir = path.join(testdataDir, "sync_not_exist");
const tempDirPath = Deno.makeTempDirSync({
prefix: "deno_std_ensure_dir_sync_",
});
const baseDir = path.join(tempDirPath, "sync_not_exist");
const testDir = path.join(baseDir, "test");

try {
Expand All @@ -37,7 +43,10 @@ Deno.test("ensureDirSync() creates dir if it does not exist", function () {
});

Deno.test("ensureDir() ensures existing dir exists", async function () {
const baseDir = path.join(testdataDir, "exist");
const tempDirPath = await Deno.makeTempDir({
prefix: "deno_std_ensure_dir_",
});
const baseDir = path.join(tempDirPath, "exist");
const testDir = path.join(baseDir, "test");

try {
Expand All @@ -49,12 +58,15 @@ Deno.test("ensureDir() ensures existing dir exists", async function () {
// test dir should still exists.
await Deno.stat(testDir);
} finally {
await Deno.remove(baseDir, { recursive: true });
await Deno.remove(tempDirPath, { recursive: true });
}
});

Deno.test("ensureDirSync() ensures existing dir exists", function () {
const baseDir = path.join(testdataDir, "sync_exist");
const tempDirPath = Deno.makeTempDirSync({
prefix: "deno_std_ensure_dir_sync_",
});
const baseDir = path.join(tempDirPath, "sync_exist");
const testDir = path.join(baseDir, "test");

try {
Expand All @@ -66,7 +78,7 @@ Deno.test("ensureDirSync() ensures existing dir exists", function () {
// test dir should still exists.
Deno.statSync(testDir);
} finally {
Deno.removeSync(baseDir, { recursive: true });
Deno.removeSync(tempDirPath, { recursive: true });
}
});

Expand Down Expand Up @@ -95,7 +107,10 @@ Deno.test("ensureDirSync() accepts links to dirs", function () {
});

Deno.test("ensureDir() rejects if input is a file", async function () {
const baseDir = path.join(testdataDir, "exist_file");
const tempDirPath = await Deno.makeTempDir({
prefix: "deno_std_ensure_dir_",
});
const baseDir = path.join(tempDirPath, "exist_file");
const testFile = path.join(baseDir, "test");

try {
Expand All @@ -109,12 +124,15 @@ Deno.test("ensureDir() rejects if input is a file", async function () {
`Failed to ensure directory exists: expected 'dir', got 'file'`,
);
} finally {
await Deno.remove(baseDir, { recursive: true });
await Deno.remove(tempDirPath, { recursive: true });
}
});

Deno.test("ensureDirSync() throws if input is a file", function () {
const baseDir = path.join(testdataDir, "exist_file_async");
const tempDirPath = Deno.makeTempDirSync({
prefix: "deno_std_ensure_dir_sync_",
});
const baseDir = path.join(tempDirPath, "exist_file_sync");
const testFile = path.join(baseDir, "test");

try {
Expand All @@ -128,7 +146,7 @@ Deno.test("ensureDirSync() throws if input is a file", function () {
`Failed to ensure directory exists: expected 'dir', got 'file'`,
);
} finally {
Deno.removeSync(baseDir, { recursive: true });
Deno.removeSync(tempDirPath, { recursive: true });
}
});

Expand Down
Loading

0 comments on commit 9bf17e4

Please sign in to comment.