2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-10-26 11:03:30 -04:00
|
|
|
import { assertEquals, assertStringIncludes } from "../testing/asserts.ts";
|
2019-10-16 14:39:33 -04:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-03-11 13:14:26 -04:00
|
|
|
import { exists, existsSync } from "./exists.ts";
|
|
|
|
|
2020-09-08 05:43:43 -04:00
|
|
|
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
|
|
|
|
const testdataDir = path.resolve(moduleDir, "testdata");
|
2019-03-11 13:14:26 -04:00
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
Deno.test("[fs] existsFile", async function (): Promise<void> {
|
2019-03-11 13:14:26 -04:00
|
|
|
assertEquals(
|
|
|
|
await exists(path.join(testdataDir, "not_exist_file.ts")),
|
2020-07-14 15:24:17 -04:00
|
|
|
false,
|
2019-03-11 13:14:26 -04:00
|
|
|
);
|
|
|
|
assertEquals(await existsSync(path.join(testdataDir, "0.ts")), true);
|
|
|
|
});
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
Deno.test("[fs] existsFileSync", function (): void {
|
2019-03-11 13:14:26 -04:00
|
|
|
assertEquals(existsSync(path.join(testdataDir, "not_exist_file.ts")), false);
|
|
|
|
assertEquals(existsSync(path.join(testdataDir, "0.ts")), true);
|
|
|
|
});
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
Deno.test("[fs] existsDirectory", async function (): Promise<void> {
|
2019-03-11 13:14:26 -04:00
|
|
|
assertEquals(
|
|
|
|
await exists(path.join(testdataDir, "not_exist_directory")),
|
2020-07-14 15:24:17 -04:00
|
|
|
false,
|
2019-03-11 13:14:26 -04:00
|
|
|
);
|
|
|
|
assertEquals(existsSync(testdataDir), true);
|
|
|
|
});
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
Deno.test("[fs] existsDirectorySync", function (): void {
|
2019-03-11 13:14:26 -04:00
|
|
|
assertEquals(
|
|
|
|
existsSync(path.join(testdataDir, "not_exist_directory")),
|
2020-07-14 15:24:17 -04:00
|
|
|
false,
|
2019-03-11 13:14:26 -04:00
|
|
|
);
|
|
|
|
assertEquals(existsSync(testdataDir), true);
|
|
|
|
});
|
2019-03-18 12:34:54 -04:00
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
Deno.test("[fs] existsLinkSync", function (): void {
|
2019-06-19 00:22:01 -04:00
|
|
|
// TODO(axetroy): generate link file use Deno api instead of set a link file
|
|
|
|
// in repository
|
2020-03-28 13:03:49 -04:00
|
|
|
assertEquals(existsSync(path.join(testdataDir, "0-link")), true);
|
2019-03-18 12:34:54 -04:00
|
|
|
});
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
Deno.test("[fs] existsLink", async function (): Promise<void> {
|
2019-06-19 00:22:01 -04:00
|
|
|
// TODO(axetroy): generate link file use Deno api instead of set a link file
|
|
|
|
// in repository
|
2020-03-28 13:03:49 -04:00
|
|
|
assertEquals(await exists(path.join(testdataDir, "0-link")), true);
|
2019-03-18 12:34:54 -04:00
|
|
|
});
|
2019-12-13 09:47:09 -05:00
|
|
|
|
2020-02-27 15:12:04 -05:00
|
|
|
interface Scenes {
|
|
|
|
read: boolean; // --allow-read
|
|
|
|
async: boolean;
|
|
|
|
output: string;
|
|
|
|
file: string; // target file to run
|
|
|
|
}
|
|
|
|
|
|
|
|
const scenes: Scenes[] = [
|
|
|
|
// 1
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
async: true,
|
|
|
|
output: "run again with the --allow-read flag",
|
2020-03-28 13:03:49 -04:00
|
|
|
file: "0.ts",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
async: false,
|
|
|
|
output: "run again with the --allow-read flag",
|
2020-03-28 13:03:49 -04:00
|
|
|
file: "0.ts",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
// 2
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
async: true,
|
|
|
|
output: "exist",
|
2020-03-28 13:03:49 -04:00
|
|
|
file: "0.ts",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
async: false,
|
|
|
|
output: "exist",
|
2020-03-28 13:03:49 -04:00
|
|
|
file: "0.ts",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
// 3
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
async: true,
|
|
|
|
output: "run again with the --allow-read flag",
|
2020-03-28 13:03:49 -04:00
|
|
|
file: "no_exist_file_for_test.ts",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
async: false,
|
|
|
|
output: "run again with the --allow-read flag",
|
2020-03-28 13:03:49 -04:00
|
|
|
file: "no_exist_file_for_test.ts",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
// 4
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
async: true,
|
|
|
|
output: "not exist",
|
2020-03-28 13:03:49 -04:00
|
|
|
file: "no_exist_file_for_test.ts",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
async: false,
|
|
|
|
output: "not exist",
|
2020-03-28 13:03:49 -04:00
|
|
|
file: "no_exist_file_for_test.ts",
|
|
|
|
},
|
2020-02-27 15:12:04 -05:00
|
|
|
];
|
2019-12-13 09:47:09 -05:00
|
|
|
|
2020-02-27 15:12:04 -05:00
|
|
|
for (const s of scenes) {
|
|
|
|
let title = `test ${s.async ? "exists" : "existsSync"}("testdata/${s.file}")`;
|
|
|
|
title += ` ${s.read ? "with" : "without"} --allow-read`;
|
2020-03-28 13:03:49 -04:00
|
|
|
Deno.test(`[fs] existsPermission ${title}`, async function (): Promise<void> {
|
2020-11-20 12:01:58 -05:00
|
|
|
const args = [Deno.execPath(), "run", "--quiet"];
|
2019-12-13 09:47:09 -05:00
|
|
|
|
|
|
|
if (s.read) {
|
|
|
|
args.push("--allow-read");
|
|
|
|
}
|
|
|
|
|
|
|
|
args.push(path.join(testdataDir, s.async ? "exists.ts" : "exists_sync.ts"));
|
|
|
|
args.push(s.file);
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
const p = Deno.run({
|
2019-12-13 09:47:09 -05:00
|
|
|
stdout: "piped",
|
|
|
|
cwd: testdataDir,
|
2020-03-28 13:03:49 -04:00
|
|
|
cmd: args,
|
2019-12-13 09:47:09 -05:00
|
|
|
});
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
const output = await p.output();
|
|
|
|
p.close();
|
2020-10-26 11:03:30 -04:00
|
|
|
assertStringIncludes(new TextDecoder().decode(output), s.output);
|
2020-02-27 15:12:04 -05:00
|
|
|
});
|
2019-12-13 09:47:09 -05:00
|
|
|
// done
|
2020-02-27 15:12:04 -05:00
|
|
|
}
|