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