2023-01-03 10:47:44 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2023-07-13 07:04:30 +00:00
|
|
|
import { assertRejects, assertThrows } from "../assert/mod.ts";
|
2019-10-16 18:39:33 +00:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-03-12 05:52:43 +00:00
|
|
|
import { ensureFile, ensureFileSync } from "./ensure_file.ts";
|
|
|
|
|
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
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("ensureFileIfItNotExist", 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
|
|
|
});
|
|
|
|
|
2022-08-24 01:21:57 +00:00
|
|
|
Deno.test("ensureFileSyncIfItNotExist", 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
|
|
|
});
|
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("ensureFileIfItExist", 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
|
|
|
});
|
|
|
|
|
2022-08-24 01:21:57 +00:00
|
|
|
Deno.test("ensureFileSyncIfItExist", 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
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("ensureFileIfItExistAsDir", 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,
|
|
|
|
`Ensure path exists, expected 'file', got 'dir'`,
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
}
|
2019-04-07 01:01:23 +00:00
|
|
|
});
|
|
|
|
|
2022-08-24 01:21:57 +00:00
|
|
|
Deno.test("ensureFileSyncIfItExistAsDir", 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,
|
|
|
|
`Ensure path exists, expected 'file', got 'dir'`,
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
}
|
|
|
|
});
|
2019-04-07 01:01:23 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
Deno.test({
|
|
|
|
name: "ensureFileShouldNotSwallowErrors",
|
|
|
|
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),
|
|
|
|
Deno.errors.PermissionDenied,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
2019-04-07 01:01:23 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
Deno.test({
|
|
|
|
name: "ensureFileSyncShouldNotSwallowErrors",
|
|
|
|
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),
|
|
|
|
Deno.errors.PermissionDenied,
|
|
|
|
);
|
|
|
|
},
|
2019-04-07 01:01:23 +00:00
|
|
|
});
|