diff options
Diffstat (limited to 'testing')
-rwxr-xr-x | testing/runner.ts | 89 | ||||
-rw-r--r-- | testing/runner_test.ts | 29 |
2 files changed, 63 insertions, 55 deletions
diff --git a/testing/runner.ts b/testing/runner.ts index b0357d8e4..a78ed2b3a 100755 --- a/testing/runner.ts +++ b/testing/runner.ts @@ -1,14 +1,9 @@ #!/usr/bin/env -S deno -A // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { parse } from "../flags/mod.ts"; -import { - WalkInfo, - expandGlobSync, - glob, - ExpandGlobOptions -} from "../fs/mod.ts"; +import { ExpandGlobOptions, expandGlob } from "../fs/mod.ts"; import { isWindows } from "../fs/path/constants.ts"; -import { isAbsolute, join } from "../fs/path/mod.ts"; +import { join } from "../fs/path/mod.ts"; import { RunTestsOptions, runTests } from "./mod.ts"; const { DenoError, ErrorKind, args, cwd, exit } = Deno; @@ -64,60 +59,56 @@ function filePathToUrl(path: string): string { return `file://${isWindows ? "/" : ""}${path.replace(/\\/g, "/")}`; } -function expandDirectory(dir: string, options: ExpandGlobOptions): WalkInfo[] { - return DIR_GLOBS.flatMap((s: string): WalkInfo[] => [ - ...expandGlobSync(s, { ...options, root: dir }) - ]); -} - /** * Given a list of globs or URLs to include and exclude and a root directory - * from which to expand relative globs, return a list of URLs + * from which to expand relative globs, yield a list of URLs * (file: or remote) that should be imported for the test runner. */ -export async function findTestModules( +export async function* findTestModules( includeModules: string[], excludeModules: string[], root: string = cwd() -): Promise<string[]> { +): AsyncIterableIterator<string> { const [includePaths, includeUrls] = partition(includeModules, isRemoteUrl); const [excludePaths, excludeUrls] = partition(excludeModules, isRemoteUrl); - const expandGlobOpts = { + const expandGlobOpts: ExpandGlobOptions = { root, + exclude: excludePaths, + includeDirs: true, extended: true, - globstar: true, - filepath: true + globstar: true }; - // TODO: We use the `g` flag here to support path prefixes when specifying - // excludes. Replace with a solution that does this more correctly. - const excludePathPatterns = excludePaths.map( - (s: string): RegExp => - glob(isAbsolute(s) ? s : join(root, s), { ...expandGlobOpts, flags: "g" }) - ); + async function* expandDirectory(d: string): AsyncIterableIterator<string> { + for (const dirGlob of DIR_GLOBS) { + for await (const walkInfo of expandGlob(dirGlob, { + ...expandGlobOpts, + root: d, + includeDirs: false + })) { + yield filePathToUrl(walkInfo.filename); + } + } + } + + for (const globString of includePaths) { + for await (const walkInfo of expandGlob(globString, expandGlobOpts)) { + if (walkInfo.info.isDirectory()) { + yield* expandDirectory(walkInfo.filename); + } else { + yield filePathToUrl(walkInfo.filename); + } + } + } + const excludeUrlPatterns = excludeUrls.map( (url: string): RegExp => RegExp(url) ); - const notExcludedPath = ({ filename }: WalkInfo): boolean => - !excludePathPatterns.some((p: RegExp): boolean => !!filename.match(p)); - const notExcludedUrl = (url: string): boolean => + const shouldIncludeUrl = (url: string): boolean => !excludeUrlPatterns.some((p: RegExp): boolean => !!url.match(p)); - const matchedPaths = includePaths - .flatMap((s: string): WalkInfo[] => [...expandGlobSync(s, expandGlobOpts)]) - .filter(notExcludedPath) - .flatMap(({ filename, info }): string[] => - info.isDirectory() - ? expandDirectory(filename, { ...expandGlobOpts, includeDirs: false }) - .filter(notExcludedPath) - .map(({ filename }): string => filename) - : [filename] - ); - - const matchedUrls = includeUrls.filter(notExcludedUrl); - - return [...matchedPaths.map(filePathToUrl), ...matchedUrls]; + yield* includeUrls.filter(shouldIncludeUrl); } export interface RunTestModulesOptions extends RunTestsOptions { @@ -162,9 +153,13 @@ export async function runTestModules({ skip = /^\s*$/, disableLog = false }: RunTestModulesOptions = {}): Promise<void> { - const testModuleUrls = await findTestModules(include, exclude); + let moduleCount = 0; + for await (const testModule of findTestModules(include, exclude)) { + await import(testModule); + moduleCount++; + } - if (testModuleUrls.length == 0) { + if (moduleCount == 0) { const noneFoundMessage = "No matching test modules found."; if (!allowNone) { throw new DenoError(ErrorKind.NotFound, noneFoundMessage); @@ -175,11 +170,7 @@ export async function runTestModules({ } if (!disableLog) { - console.log(`Found ${testModuleUrls.length} matching test modules.`); - } - - for (const url of testModuleUrls) { - await import(url); + console.log(`Found ${moduleCount} matching test modules.`); } await runTests({ diff --git a/testing/runner_test.ts b/testing/runner_test.ts index 9b6214918..e2617b155 100644 --- a/testing/runner_test.ts +++ b/testing/runner_test.ts @@ -3,17 +3,30 @@ import { test } from "./mod.ts"; import { findTestModules } from "./runner.ts"; import { isWindows } from "../fs/path/constants.ts"; import { assertEquals } from "../testing/asserts.ts"; +const { cwd } = Deno; function urlToFilePath(url: URL): string { // Since `new URL('file:///C:/a').pathname` is `/C:/a`, remove leading slash. return url.pathname.slice(url.protocol == "file:" && isWindows ? 1 : 0); } +async function findTestModulesArray( + include: string[], + exclude: string[], + root: string = cwd() +): Promise<string[]> { + const result = []; + for await (const testModule of findTestModules(include, exclude, root)) { + result.push(testModule); + } + return result; +} + const TEST_DATA_URL = new URL("testdata", import.meta.url); const TEST_DATA_PATH = urlToFilePath(TEST_DATA_URL); test(async function findTestModulesDir1(): Promise<void> { - const urls = await findTestModules(["."], [], TEST_DATA_PATH); + const urls = await findTestModulesArray(["."], [], TEST_DATA_PATH); assertEquals(urls.sort(), [ `${TEST_DATA_URL}/bar_test.js`, `${TEST_DATA_URL}/foo_test.ts`, @@ -27,7 +40,7 @@ test(async function findTestModulesDir1(): Promise<void> { }); test(async function findTestModulesDir2(): Promise<void> { - const urls = await findTestModules(["subdir"], [], TEST_DATA_PATH); + const urls = await findTestModulesArray(["subdir"], [], TEST_DATA_PATH); assertEquals(urls.sort(), [ `${TEST_DATA_URL}/subdir/bar_test.js`, `${TEST_DATA_URL}/subdir/foo_test.ts`, @@ -37,7 +50,11 @@ test(async function findTestModulesDir2(): Promise<void> { }); test(async function findTestModulesGlob(): Promise<void> { - const urls = await findTestModules(["**/*_test.{js,ts}"], [], TEST_DATA_PATH); + const urls = await findTestModulesArray( + ["**/*_test.{js,ts}"], + [], + TEST_DATA_PATH + ); assertEquals(urls.sort(), [ `${TEST_DATA_URL}/bar_test.js`, `${TEST_DATA_URL}/foo_test.ts`, @@ -47,7 +64,7 @@ test(async function findTestModulesGlob(): Promise<void> { }); test(async function findTestModulesExcludeDir(): Promise<void> { - const urls = await findTestModules(["."], ["subdir"], TEST_DATA_PATH); + const urls = await findTestModulesArray(["."], ["subdir"], TEST_DATA_PATH); assertEquals(urls.sort(), [ `${TEST_DATA_URL}/bar_test.js`, `${TEST_DATA_URL}/foo_test.ts`, @@ -57,7 +74,7 @@ test(async function findTestModulesExcludeDir(): Promise<void> { }); test(async function findTestModulesExcludeGlob(): Promise<void> { - const urls = await findTestModules(["."], ["**/foo*"], TEST_DATA_PATH); + const urls = await findTestModulesArray(["."], ["**/foo*"], TEST_DATA_PATH); assertEquals(urls.sort(), [ `${TEST_DATA_URL}/bar_test.js`, `${TEST_DATA_URL}/subdir/bar_test.js`, @@ -73,6 +90,6 @@ test(async function findTestModulesRemote(): Promise<void> { "https://example.com/colors_test.ts", "http://example.com/printf_test.ts" ]; - const matches = await findTestModules(urls, []); + const matches = await findTestModulesArray(urls, []); assertEquals(matches, urls); }); |