2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertRejects, assertThrows } from "@std/assert";
|
|
|
|
import * as path from "@std/path";
|
2019-03-12 05:52:43 +00:00
|
|
|
import { ensureFile, ensureFileSync } from "./ensure_file.ts";
|
2024-09-11 10:17:27 +00:00
|
|
|
import { IS_DENO_2 } from "../internal/_is_deno_2.ts";
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2020-09-08 09:43:43 +00:00
|
|
|
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
|
|
|
|
const testdataDir = path.resolve(moduleDir, "testdata");
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureFile() creates file if it does not exist", async function () {
|
2019-03-12 05:52:43 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_1");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
try {
|
|
|
|
await ensureFile(testFile);
|
|
|
|
|
|
|
|
// test file should exists.
|
|
|
|
await Deno.stat(testFile);
|
|
|
|
} finally {
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
}
|
2019-03-12 05:52:43 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureFileSync() creates file if it does not exist", function () {
|
2019-03-12 05:52:43 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_2");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
try {
|
|
|
|
ensureFileSync(testFile);
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
// test file should exists.
|
2019-11-13 18:42:34 +00:00
|
|
|
Deno.statSync(testFile);
|
2023-03-08 12:27:10 +00:00
|
|
|
} finally {
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
}
|
2019-03-12 05:52:43 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureFile() ensures existing file exists", async function () {
|
2019-03-12 05:52:43 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_3");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
try {
|
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
|
|
|
await Deno.writeFile(testFile, new Uint8Array());
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
await ensureFile(testFile);
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
// test file should exists.
|
|
|
|
await Deno.stat(testFile);
|
|
|
|
} finally {
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
}
|
2019-03-12 05:52:43 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureFileSync() ensures existing file exists", function () {
|
2019-03-12 05:52:43 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_4");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
try {
|
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
|
|
|
Deno.writeFileSync(testFile, new Uint8Array());
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
ensureFileSync(testFile);
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
// test file should exists.
|
2019-11-13 18:42:34 +00:00
|
|
|
Deno.statSync(testFile);
|
2023-03-08 12:27:10 +00:00
|
|
|
} finally {
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
}
|
2019-03-12 05:52:43 +00:00
|
|
|
});
|
2019-04-07 01:01:23 +00:00
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureFile() rejects if input is dir", async function () {
|
2019-04-07 01:01:23 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_5");
|
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
try {
|
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await ensureFile(testDir);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`Failed to ensure file exists: expected 'file', got 'dir'`,
|
2023-03-08 12:27:10 +00:00
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
}
|
2019-04-07 01:01:23 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureFileSync() throws if input is dir", function () {
|
2019-04-07 01:01:23 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_6");
|
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
try {
|
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
ensureFileSync(testDir);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`Failed to ensure file exists: expected 'file', got 'dir'`,
|
2023-03-08 12:27:10 +00:00
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
}
|
|
|
|
});
|
2019-04-07 01:01:23 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
Deno.test({
|
2023-11-23 06:59:59 +00:00
|
|
|
name: "ensureFile() rejects permission fs write error",
|
2023-03-08 12:27:10 +00:00
|
|
|
permissions: { read: true },
|
|
|
|
async fn() {
|
|
|
|
const testDir = path.join(testdataDir, "ensure_file_7");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
|
|
|
// ensureFile fails because this test doesn't have write permissions,
|
|
|
|
// but don't swallow that error.
|
|
|
|
await assertRejects(
|
|
|
|
async () => await ensureFile(testFile),
|
2024-09-11 10:17:27 +00:00
|
|
|
IS_DENO_2
|
|
|
|
// TODO(iuioiua): Just use `Deno.errors.NotCapable` once Deno 2 is released.
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
? (Deno as any).errors.NotCapable
|
|
|
|
: Deno.errors.PermissionDenied,
|
2023-03-08 12:27:10 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
2019-04-07 01:01:23 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
Deno.test({
|
2023-11-23 06:59:59 +00:00
|
|
|
name: "ensureFileSync() throws permission fs write error",
|
2023-03-08 12:27:10 +00:00
|
|
|
permissions: { read: true },
|
|
|
|
fn() {
|
|
|
|
const testDir = path.join(testdataDir, "ensure_file_8");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
|
|
|
// ensureFileSync fails because this test doesn't have write permissions,
|
|
|
|
// but don't swallow that error.
|
|
|
|
assertThrows(
|
|
|
|
() => ensureFileSync(testFile),
|
2024-09-11 10:17:27 +00:00
|
|
|
IS_DENO_2
|
|
|
|
// TODO(iuioiua): Just use `Deno.errors.NotCapable` once Deno 2 is released.
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
? (Deno as any).errors.NotCapable
|
|
|
|
: Deno.errors.PermissionDenied,
|
2023-03-08 12:27:10 +00:00
|
|
|
);
|
|
|
|
},
|
2019-04-07 01:01:23 +00:00
|
|
|
});
|
2024-01-08 13:36:41 +00:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name:
|
|
|
|
"ensureFile() can write file without write permissions on parent directory",
|
|
|
|
permissions: {
|
|
|
|
read: true,
|
|
|
|
write: [
|
|
|
|
path.join(testdataDir, "ensure_file_9"),
|
|
|
|
path.join(testdataDir, "ensure_file_9", "test.txt"),
|
|
|
|
],
|
2024-04-15 23:31:19 +00:00
|
|
|
run: [Deno.execPath()],
|
2024-01-08 13:36:41 +00:00
|
|
|
},
|
|
|
|
async fn() {
|
|
|
|
const testDir = path.join(testdataDir, "ensure_file_9");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
|
|
|
await Deno.permissions.revoke({ name: "write", path: testDir });
|
|
|
|
|
|
|
|
// should still work as the parent directory already exists
|
|
|
|
await ensureFile(testFile);
|
|
|
|
|
|
|
|
// test file should exist
|
|
|
|
await Deno.stat(testFile);
|
|
|
|
} finally {
|
|
|
|
// it's dirty, but we can't remove the test output in the same process after dropping the write permission
|
|
|
|
await new Deno.Command(Deno.execPath(), {
|
|
|
|
args: [
|
|
|
|
"eval",
|
2024-01-12 04:38:41 +00:00
|
|
|
"--no-lock",
|
2024-01-08 13:36:41 +00:00
|
|
|
`Deno.removeSync("${testDir}", { recursive: true });`,
|
|
|
|
],
|
|
|
|
}).output();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name:
|
|
|
|
"ensureFileSync() can write file without write permissions on parent directory",
|
|
|
|
permissions: {
|
|
|
|
read: true,
|
|
|
|
write: [
|
|
|
|
path.join(testdataDir, "ensure_file_10"),
|
|
|
|
path.join(testdataDir, "ensure_file_10", "test.txt"),
|
|
|
|
],
|
2024-04-15 23:31:19 +00:00
|
|
|
run: [Deno.execPath()],
|
2024-01-08 13:36:41 +00:00
|
|
|
},
|
|
|
|
fn() {
|
|
|
|
const testDir = path.join(testdataDir, "ensure_file_10");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
|
|
|
try {
|
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
|
|
|
Deno.permissions.revokeSync({ name: "write", path: testDir });
|
|
|
|
|
|
|
|
// should still work as the parent directory already exists
|
|
|
|
ensureFileSync(testFile);
|
|
|
|
|
|
|
|
// test file should exist
|
|
|
|
Deno.statSync(testFile);
|
|
|
|
} finally {
|
|
|
|
// it's dirty, but we can't remove the test output in the same process after dropping the write permission
|
|
|
|
new Deno.Command(Deno.execPath(), {
|
|
|
|
args: [
|
|
|
|
"eval",
|
2024-01-12 04:38:41 +00:00
|
|
|
"--no-lock",
|
2024-01-08 13:36:41 +00:00
|
|
|
`Deno.removeSync("${testDir}", { recursive: true });`,
|
|
|
|
],
|
|
|
|
}).outputSync();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|