Skip to content

Commit

Permalink
Add tests for expandGlob()
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn committed Sep 26, 2019
1 parent ffc2a39 commit 31204c9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
85 changes: 80 additions & 5 deletions 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";

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -253,4 +259,73 @@ test({
}
});

async function expandGlobArray(
globString: string,
options: ExpandGlobOptions
): Promise<string[]> {
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<void> {
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<void> {
const options = { ...EG_OPTIONS, globstar: true };
assertEquals(await expandGlobArray(join("**", "abc"), options), [
"abc",
join("subdir", "abc")
]);
});

test(async function expandGlobIncludeDirs(): Promise<void> {
const options = { ...EG_OPTIONS, includeDirs: false };
assertEquals(await expandGlobArray("subdir", options), []);
});

runIfMain(import.meta);
Empty file added fs/testdata/glob/abc
Empty file.
Empty file added fs/testdata/glob/abcdef
Empty file.
Empty file added fs/testdata/glob/abcdefghi
Empty file.
Empty file added fs/testdata/glob/subdir/abc
Empty file.

0 comments on commit 31204c9

Please sign in to comment.