From 31204c9d8f04a6ce9cdf7d6c8961fe74db539d7d Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Thu, 26 Sep 2019 02:46:46 +0100 Subject: [PATCH] Add tests for expandGlob() --- fs/glob_test.ts | 85 ++++++++++++++++++++++++++++++++++--- fs/testdata/glob/abc | 0 fs/testdata/glob/abcdef | 0 fs/testdata/glob/abcdefghi | 0 fs/testdata/glob/subdir/abc | 0 5 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 fs/testdata/glob/abc create mode 100644 fs/testdata/glob/abcdef create mode 100644 fs/testdata/glob/abcdefghi create mode 100644 fs/testdata/glob/subdir/abc diff --git a/fs/glob_test.ts b/fs/glob_test.ts index 9151b9e9e6e1..ab2857e65b92 100644 --- a/fs/glob_test.ts +++ b/fs/glob_test.ts @@ -1,9 +1,16 @@ -const { mkdir } = Deno; -type FileInfo = Deno.FileInfo; +const { cwd, mkdir } = Deno; import { test, runIfMain } from "../testing/mod.ts"; import { assert, assertEquals } from "../testing/asserts.ts"; -import { glob, isGlob } from "./glob.ts"; -import { join } from "./path.ts"; +import { SEP, isWindows } from "./path/constants.ts"; +import { + ExpandGlobOptions, + expandGlob, + glob, + isGlob, + expandGlobSync +} from "./glob.ts"; +import { join, relative } from "./path.ts"; +import { WalkInfo } from "./walk.ts"; import { testWalk } from "./walk_test.ts"; import { touch, walkArray } from "./walk_test.ts"; @@ -131,7 +138,6 @@ testWalk( const arr = await walkArray(".", { match: [glob("x.*", { flags: "g", globstar: true })] }); - console.log(arr); assertEquals(arr.length, 2); assertEquals(arr[0], "x.js"); assertEquals(arr[1], "x.ts"); @@ -253,4 +259,73 @@ test({ } }); +async function expandGlobArray( + globString: string, + options: ExpandGlobOptions +): Promise { + const infos: WalkInfo[] = []; + for await (const info of expandGlob(globString, options)) { + infos.push(info); + } + infos.sort(); + const infosSync = [...expandGlobSync(globString, options)]; + infosSync.sort(); + assertEquals(infos, infosSync); + const root = options.root || cwd(); + const paths = infos.map(({ filename }): string => filename); + 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 = { + root: new URL(join("testdata", "glob"), import.meta.url).pathname + .slice(isWindows ? 1 : 0) + .replace(/\//g, SEP), + includeDirs: true, + extended: false, + globstar: false, + strict: false, + filepath: false, + flags: "" +}; + +test(async function expandGlobExt(): Promise { + const options = { ...EG_OPTIONS, extended: true }; + assertEquals(await expandGlobArray("abc?(def|ghi)", options), [ + "abc", + "abcdef" + ]); + assertEquals(await expandGlobArray("abc*(def|ghi)", options), [ + "abc", + "abcdef", + "abcdefghi" + ]); + assertEquals(await expandGlobArray("abc+(def|ghi)", options), [ + "abcdef", + "abcdefghi" + ]); + assertEquals(await expandGlobArray("abc@(def|ghi)", options), ["abcdef"]); + assertEquals(await expandGlobArray("abc{def,ghi}", options), ["abcdef"]); + assertEquals(await expandGlobArray("abc!(def|ghi)", options), ["abc"]); +}); + +test(async function expandGlobGlobstar(): Promise { + const options = { ...EG_OPTIONS, globstar: true }; + assertEquals(await expandGlobArray(join("**", "abc"), options), [ + "abc", + join("subdir", "abc") + ]); +}); + +test(async function expandGlobIncludeDirs(): Promise { + const options = { ...EG_OPTIONS, includeDirs: false }; + assertEquals(await expandGlobArray("subdir", options), []); +}); + runIfMain(import.meta); diff --git a/fs/testdata/glob/abc b/fs/testdata/glob/abc new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/fs/testdata/glob/abcdef b/fs/testdata/glob/abcdef new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/fs/testdata/glob/abcdefghi b/fs/testdata/glob/abcdefghi new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/fs/testdata/glob/subdir/abc b/fs/testdata/glob/subdir/abc new file mode 100644 index 000000000000..e69de29bb2d1