2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-01-27 21:13:17 -05:00
|
|
|
import { assertEquals, assertStrContains } 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";
|
|
|
|
|
|
|
|
const testdataDir = path.resolve("fs", "testdata");
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function existsFile(): Promise<void> {
|
2019-03-11 13:14:26 -04:00
|
|
|
assertEquals(
|
|
|
|
await exists(path.join(testdataDir, "not_exist_file.ts")),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
assertEquals(await existsSync(path.join(testdataDir, "0.ts")), true);
|
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function existsFileSync(): 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-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function existsDirectory(): Promise<void> {
|
2019-03-11 13:14:26 -04:00
|
|
|
assertEquals(
|
|
|
|
await exists(path.join(testdataDir, "not_exist_directory")),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
assertEquals(existsSync(testdataDir), true);
|
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function existsDirectorySync(): void {
|
2019-03-11 13:14:26 -04:00
|
|
|
assertEquals(
|
|
|
|
existsSync(path.join(testdataDir, "not_exist_directory")),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
assertEquals(existsSync(testdataDir), true);
|
|
|
|
});
|
2019-03-18 12:34:54 -04:00
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function existsLinkSync(): 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
|
2019-03-18 12:34:54 -04:00
|
|
|
assertEquals(existsSync(path.join(testdataDir, "0-link.ts")), true);
|
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function existsLink(): 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
|
2019-03-18 12:34:54 -04:00
|
|
|
assertEquals(await exists(path.join(testdataDir, "0-link.ts")), true);
|
|
|
|
});
|
2019-12-13 09:47:09 -05:00
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function existsPermission(): Promise<void> {
|
2019-12-13 09:47:09 -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",
|
|
|
|
file: "0.ts"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
async: false,
|
|
|
|
output: "run again with the --allow-read flag",
|
|
|
|
file: "0.ts"
|
|
|
|
},
|
|
|
|
// 2
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
async: true,
|
|
|
|
output: "exist",
|
|
|
|
file: "0.ts"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
async: false,
|
|
|
|
output: "exist",
|
|
|
|
file: "0.ts"
|
|
|
|
},
|
|
|
|
// 3
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
async: true,
|
|
|
|
output: "run again with the --allow-read flag",
|
|
|
|
file: "no_exist_file_for_test.ts"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
async: false,
|
|
|
|
output: "run again with the --allow-read flag",
|
|
|
|
file: "no_exist_file_for_test.ts"
|
|
|
|
},
|
|
|
|
// 4
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
async: true,
|
|
|
|
output: "not exist",
|
|
|
|
file: "no_exist_file_for_test.ts"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
async: false,
|
|
|
|
output: "not exist",
|
|
|
|
file: "no_exist_file_for_test.ts"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const s of scenes) {
|
|
|
|
console.log(
|
|
|
|
`test ${s.async ? "exists" : "existsSync"}("testdata/${s.file}") ${
|
|
|
|
s.read ? "with" : "without"
|
|
|
|
} --allow-read`
|
|
|
|
);
|
|
|
|
|
|
|
|
const args = [Deno.execPath(), "run"];
|
|
|
|
|
|
|
|
if (s.read) {
|
|
|
|
args.push("--allow-read");
|
|
|
|
}
|
|
|
|
|
|
|
|
args.push(path.join(testdataDir, s.async ? "exists.ts" : "exists_sync.ts"));
|
|
|
|
args.push(s.file);
|
|
|
|
|
|
|
|
const { stdout } = Deno.run({
|
|
|
|
stdout: "piped",
|
|
|
|
cwd: testdataDir,
|
|
|
|
args: args
|
|
|
|
});
|
|
|
|
|
2020-02-19 15:36:18 -05:00
|
|
|
const output = await Deno.readAll(stdout!);
|
2019-12-13 09:47:09 -05:00
|
|
|
|
2020-01-27 21:13:17 -05:00
|
|
|
assertStrContains(new TextDecoder().decode(output), s.output);
|
2019-12-13 09:47:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// done
|
|
|
|
});
|