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 { assert, assertEquals, assertStringIncludes } from "../assert/mod.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,
|
|
|
|
};
|
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("expandGlobWildcard", async function () {
|
2019-10-16 18:39:33 +00:00
|
|
|
const options = EG_OPTIONS;
|
|
|
|
assertEquals(await expandGlobArray("*", options), [
|
2021-10-17 18:04:24 +00:00
|
|
|
"a[b]c",
|
2019-10-16 18:39:33 +00:00
|
|
|
"abc",
|
|
|
|
"abcdef",
|
|
|
|
"abcdefghi",
|
2020-03-28 17:03:49 +00:00
|
|
|
"subdir",
|
2019-10-16 18:39:33 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("expandGlobTrailingSeparator", async function () {
|
2019-10-16 18:39:33 +00:00
|
|
|
const options = EG_OPTIONS;
|
2023-03-06 07:35:44 +00:00
|
|
|
assertEquals(await expandGlobArray("*/", options), [
|
|
|
|
"a[b]c",
|
|
|
|
"subdir",
|
|
|
|
]);
|
2019-10-16 18:39:33 +00:00
|
|
|
});
|
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("expandGlobParent", async function () {
|
2019-10-16 18:39:33 +00:00
|
|
|
const options = EG_OPTIONS;
|
|
|
|
assertEquals(await expandGlobArray("subdir/../*", options), [
|
2021-10-17 18:04:24 +00:00
|
|
|
"a[b]c",
|
2019-10-16 18:39:33 +00:00
|
|
|
"abc",
|
|
|
|
"abcdef",
|
|
|
|
"abcdefghi",
|
2020-03-28 17:03:49 +00:00
|
|
|
"subdir",
|
2019-10-16 18:39:33 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("expandGlobExt", async function () {
|
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"]);
|
|
|
|
});
|
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("expandGlobGlobstar", async function () {
|
2023-01-16 15:26:27 +00:00
|
|
|
const options = { ...EG_OPTIONS };
|
2019-10-16 18:39:33 +00:00
|
|
|
assertEquals(
|
2023-01-16 15:26:27 +00:00
|
|
|
await expandGlobArray("**/abc", options),
|
2020-07-14 19:24:17 +00:00
|
|
|
["abc", join("subdir", "abc")],
|
2019-10-16 18:39:33 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("expandGlobGlobstarParent", async function () {
|
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
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-10-18 14:40:00 +00:00
|
|
|
Deno.test("expandGlobGlobstarFalseWithGlob", async function () {
|
|
|
|
const options = { ...EG_OPTIONS, globstar: false };
|
|
|
|
assertEquals(await expandGlobArray("**", options), [
|
|
|
|
".",
|
|
|
|
"a[b]c",
|
|
|
|
"abc",
|
|
|
|
"abcdef",
|
|
|
|
"abcdefghi",
|
|
|
|
"subdir",
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("expandGlobIncludeDirs", async function () {
|
2019-10-16 18:39:33 +00:00
|
|
|
const options = { ...EG_OPTIONS, includeDirs: false };
|
|
|
|
assertEquals(await expandGlobArray("subdir", options), []);
|
|
|
|
});
|
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("expandGlobPermError", async function () {
|
2019-12-12 00:42:21 +00:00
|
|
|
const exampleUrl = new URL("testdata/expand_wildcard.js", import.meta.url);
|
2022-11-15 06:00:59 +00:00
|
|
|
const command = new Deno.Command(Deno.execPath(), {
|
2022-10-18 14:40:00 +00:00
|
|
|
args: ["run", "--quiet", "--unstable", exampleUrl.toString()],
|
2019-12-12 00:42:21 +00:00
|
|
|
});
|
2022-11-15 06:00:59 +00:00
|
|
|
const { code, success, stdout, stderr } = await command.output();
|
2021-02-16 12:22:07 +00:00
|
|
|
const decoder = new TextDecoder();
|
2022-07-19 00:35:19 +00:00
|
|
|
assert(!success);
|
|
|
|
assertEquals(code, 1);
|
2022-05-25 09:08:27 +00:00
|
|
|
assertEquals(decoder.decode(stdout), "");
|
|
|
|
assertStringIncludes(decoder.decode(stderr), "Uncaught PermissionDenied");
|
2019-12-12 00:42:21 +00:00
|
|
|
});
|
2021-10-17 18:04:24 +00:00
|
|
|
|
|
|
|
Deno.test("expandGlobRootIsNotGlob", async function () {
|
|
|
|
const options = { ...EG_OPTIONS, root: join(EG_OPTIONS.root!, "a[b]c") };
|
|
|
|
assertEquals(await expandGlobArray("*", options), ["foo"]);
|
|
|
|
});
|
2023-03-06 07:35:44 +00:00
|
|
|
|
|
|
|
Deno.test("expandGlobFollowSymlink", async function () {
|
|
|
|
const options = {
|
|
|
|
...EG_OPTIONS,
|
|
|
|
root: join(EG_OPTIONS.root!, "link"),
|
|
|
|
followSymlinks: true,
|
|
|
|
};
|
|
|
|
assertEquals(await expandGlobArray("*", options), ["abc"]);
|
|
|
|
});
|