Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better paths handling in test runner #574

Merged
merged 9 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 62 additions & 14 deletions testing/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,68 @@ ARGS:

function filePathToRegExp(str: string): RegExp {
if (isGlob(str)) {
return glob(str);
return glob(str, { flags: "g" });
}

return RegExp(str);
}

function isRemoteUrl(url: string): boolean {
return /^https?:\/\//.test(url);
}

function partition(
arr: string[],
callback: (el: string) => boolean
): [string[], string[]] {
return arr.reduce(
(paritioned: [string[], string[]], el: string): [string[], string[]] => {
paritioned[callback(el) ? 1 : 0].push(el);
return paritioned;
},
[[], []]
);
}

/**
* Given list of globs or URLs to include and exclude and root directory return
* list of file URLs that should be imported for test runner.
*/
export async function getMatchingUrls(
matchPaths: string[],
excludePaths: string[],
root: string
): Promise<string[]> {
const [includeLocal, includeRemote] = partition(matchPaths, isRemoteUrl);
const [excludeLocal, excludeRemote] = partition(excludePaths, isRemoteUrl);

const localFileIterator = walk(root, {
match: includeLocal.map((f: string): RegExp => filePathToRegExp(f)),
skip: excludeLocal.map((f: string): RegExp => filePathToRegExp(f))
});

let matchingLocalUrls: string[] = [];
for await (const { filename } of localFileIterator) {
matchingLocalUrls.push(`file://${filename}`);
}

const excludeRemotePatterns = excludeRemote.map(
(url: string): RegExp => RegExp(url)
);
const matchingRemoteUrls = includeRemote.filter(
(candidateUrl: string): boolean => {
return !excludeRemotePatterns.some(
(pattern: RegExp): boolean => {
const r = pattern.test(candidateUrl);
pattern.lastIndex = 0;
return r;
}
);
}
);

return matchingLocalUrls.concat(matchingRemoteUrls);
}
/**
* This function runs matching test files in `root` directory.
*
Expand Down Expand Up @@ -95,25 +151,17 @@ export async function main(root: string = cwd()): Promise<void> {
excludeFiles = [];
}

const filesIterator = walk(root, {
match: includeFiles.map((f: string): RegExp => filePathToRegExp(f)),
skip: excludeFiles.map((f: string): RegExp => filePathToRegExp(f))
});

const foundTestFiles: string[] = [];
for await (const { filename } of filesIterator) {
foundTestFiles.push(filename);
}
const foundTestUrls = await getMatchingUrls(includeFiles, excludeFiles, root);

if (foundTestFiles.length === 0) {
if (foundTestUrls.length === 0) {
console.error("No matching test files found.");
return;
}

console.log(`Found ${foundTestFiles.length} matching test files.`);
console.log(`Found ${foundTestUrls.length} matching test files.`);

for (const filename of foundTestFiles) {
await import(`file://${filename}`);
for (const url of foundTestUrls) {
await import(url);
}

await runTests({
Expand Down
26 changes: 26 additions & 0 deletions testing/runner_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test } from "./mod.ts";
import { assertEquals } from "../testing/asserts.ts";
import { getMatchingUrls } from "./runner.ts";

const fileName = window.location.href;
const TEST_ROOT_PATH = fileName.slice(7, fileName.indexOf("testing")) + "fmt";

test(async function getMatchingUrlsRemote(): Promise<void> {
const matches = [
"https://deno.land/std/fmt/colors_test.ts",
"http://deno.land/std/fmt/printf_test.ts"
];

const urls = await getMatchingUrls(matches, [], TEST_ROOT_PATH);
assertEquals(urls, matches);
});

test(async function getMatchingUrlsLocal(): Promise<void> {
const urls = await getMatchingUrls(
["fmt/*_test.ts"],
["colors*"],
TEST_ROOT_PATH
);
assertEquals(urls.length, 1);
});