2019-12-11 19:42:21 -05:00
|
|
|
const { cwd, execPath, run } = Deno;
|
2020-04-01 15:23:39 -04:00
|
|
|
import { decode } from "../encoding/utf8.ts";
|
2020-06-05 23:43:00 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
assertStringContains,
|
|
|
|
} from "../testing/asserts.ts";
|
2019-10-16 14:39:33 -04:00
|
|
|
import {
|
|
|
|
join,
|
|
|
|
joinGlobs,
|
|
|
|
normalize,
|
2020-03-28 13:03:49 -04:00
|
|
|
relative,
|
2020-04-30 06:47:53 -04:00
|
|
|
fromFileUrl,
|
2019-10-16 14:39:33 -04:00
|
|
|
} from "../path/mod.ts";
|
|
|
|
import {
|
|
|
|
ExpandGlobOptions,
|
|
|
|
expandGlob,
|
2020-03-28 13:03:49 -04:00
|
|
|
expandGlobSync,
|
2019-10-16 14:39:33 -04:00
|
|
|
} from "./expand_glob.ts";
|
|
|
|
|
|
|
|
async function expandGlobArray(
|
|
|
|
globString: string,
|
|
|
|
options: ExpandGlobOptions
|
|
|
|
): Promise<string[]> {
|
|
|
|
const paths: string[] = [];
|
2020-04-29 16:00:31 -04:00
|
|
|
for await (const { path } of expandGlob(globString, options)) {
|
|
|
|
paths.push(path);
|
2019-10-16 14:39:33 -04:00
|
|
|
}
|
|
|
|
paths.sort();
|
|
|
|
const pathsSync = [...expandGlobSync(globString, options)].map(
|
2020-04-29 16:00:31 -04:00
|
|
|
({ path }): string => path
|
2019-10-16 14:39:33 -04:00
|
|
|
);
|
|
|
|
pathsSync.sort();
|
|
|
|
assertEquals(paths, pathsSync);
|
|
|
|
const root = normalize(options.root || cwd());
|
|
|
|
for (const path of paths) {
|
|
|
|
assert(path.startsWith(root));
|
|
|
|
}
|
|
|
|
const relativePaths = paths.map(
|
|
|
|
(path: string): string => relative(root, path) || "."
|
|
|
|
);
|
|
|
|
relativePaths.sort();
|
|
|
|
return relativePaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EG_OPTIONS: ExpandGlobOptions = {
|
2020-04-30 06:47:53 -04:00
|
|
|
root: fromFileUrl(new URL(join("testdata", "glob"), import.meta.url)),
|
2019-10-16 14:39:33 -04:00
|
|
|
includeDirs: true,
|
|
|
|
extended: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
globstar: false,
|
2019-10-16 14:39:33 -04:00
|
|
|
};
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("expandGlobWildcard", async function (): Promise<void> {
|
2019-10-16 14:39:33 -04:00
|
|
|
const options = EG_OPTIONS;
|
|
|
|
assertEquals(await expandGlobArray("*", options), [
|
|
|
|
"abc",
|
|
|
|
"abcdef",
|
|
|
|
"abcdefghi",
|
2020-03-28 13:03:49 -04:00
|
|
|
"subdir",
|
2019-10-16 14:39:33 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("expandGlobTrailingSeparator", async function (): Promise<void> {
|
2019-10-16 14:39:33 -04:00
|
|
|
const options = EG_OPTIONS;
|
|
|
|
assertEquals(await expandGlobArray("*/", options), ["subdir"]);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("expandGlobParent", async function (): Promise<void> {
|
2019-10-16 14:39:33 -04:00
|
|
|
const options = EG_OPTIONS;
|
|
|
|
assertEquals(await expandGlobArray("subdir/../*", options), [
|
|
|
|
"abc",
|
|
|
|
"abcdef",
|
|
|
|
"abcdefghi",
|
2020-03-28 13:03:49 -04:00
|
|
|
"subdir",
|
2019-10-16 14:39:33 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("expandGlobExt", async function (): Promise<void> {
|
2019-10-16 14:39:33 -04:00
|
|
|
const options = { ...EG_OPTIONS, extended: true };
|
|
|
|
assertEquals(await expandGlobArray("abc?(def|ghi)", options), [
|
|
|
|
"abc",
|
2020-03-28 13:03:49 -04:00
|
|
|
"abcdef",
|
2019-10-16 14:39:33 -04:00
|
|
|
]);
|
|
|
|
assertEquals(await expandGlobArray("abc*(def|ghi)", options), [
|
|
|
|
"abc",
|
|
|
|
"abcdef",
|
2020-03-28 13:03:49 -04:00
|
|
|
"abcdefghi",
|
2019-10-16 14:39:33 -04:00
|
|
|
]);
|
|
|
|
assertEquals(await expandGlobArray("abc+(def|ghi)", options), [
|
|
|
|
"abcdef",
|
2020-03-28 13:03:49 -04:00
|
|
|
"abcdefghi",
|
2019-10-16 14:39:33 -04:00
|
|
|
]);
|
|
|
|
assertEquals(await expandGlobArray("abc@(def|ghi)", options), ["abcdef"]);
|
|
|
|
assertEquals(await expandGlobArray("abc{def,ghi}", options), ["abcdef"]);
|
|
|
|
assertEquals(await expandGlobArray("abc!(def|ghi)", options), ["abc"]);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("expandGlobGlobstar", async function (): Promise<void> {
|
2019-10-16 14:39:33 -04:00
|
|
|
const options = { ...EG_OPTIONS, globstar: true };
|
|
|
|
assertEquals(
|
|
|
|
await expandGlobArray(joinGlobs(["**", "abc"], options), options),
|
|
|
|
["abc", join("subdir", "abc")]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("expandGlobGlobstarParent", async function (): Promise<void> {
|
2019-10-16 14:39:33 -04:00
|
|
|
const options = { ...EG_OPTIONS, globstar: true };
|
|
|
|
assertEquals(
|
|
|
|
await expandGlobArray(joinGlobs(["subdir", "**", ".."], options), options),
|
|
|
|
["."]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("expandGlobIncludeDirs", async function (): Promise<void> {
|
2019-10-16 14:39:33 -04:00
|
|
|
const options = { ...EG_OPTIONS, includeDirs: false };
|
|
|
|
assertEquals(await expandGlobArray("subdir", options), []);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("expandGlobPermError", async function (): Promise<void> {
|
2019-12-11 19:42:21 -05:00
|
|
|
const exampleUrl = new URL("testdata/expand_wildcard.js", import.meta.url);
|
|
|
|
const p = run({
|
2020-05-04 07:03:30 -04:00
|
|
|
cmd: [execPath(), "run", "--unstable", exampleUrl.toString()],
|
2019-12-11 19:42:21 -05:00
|
|
|
stdin: "null",
|
|
|
|
stdout: "piped",
|
2020-03-28 13:03:49 -04:00
|
|
|
stderr: "piped",
|
2019-12-11 19:42:21 -05:00
|
|
|
});
|
|
|
|
assertEquals(await p.status(), { code: 1, success: false });
|
|
|
|
assertEquals(decode(await p.output()), "");
|
2020-06-05 23:43:00 -04:00
|
|
|
assertStringContains(
|
2019-12-11 19:42:21 -05:00
|
|
|
decode(await p.stderrOutput()),
|
|
|
|
"Uncaught PermissionDenied"
|
|
|
|
);
|
2020-03-18 19:25:55 -04:00
|
|
|
p.close();
|
2019-12-11 19:42:21 -05:00
|
|
|
});
|