0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-30 09:08:00 -04:00

fix: better paths handling in test runner (denoland/deno_std#574)

Original: 5ef42ece7d
This commit is contained in:
Bartek Iwańczuk 2019-08-30 19:43:32 +02:00 committed by Ryan Dahl
parent fa790e8b52
commit 9382f38c7c
2 changed files with 90 additions and 16 deletions

View file

@ -1,4 +1,4 @@
#!/usr/bin/env deno -A
#!/usr/bin/env -S deno -A
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { parse } from "../flags/mod.ts";
import { glob, isGlob, walk } from "../fs/mod.ts";
@ -35,12 +35,68 @@ ARGS:
function filePathToRegExp(str: string): RegExp {
if (isGlob(str)) {
return glob(str);
return glob(str, { flags: "g" });
}
return RegExp(str);
return RegExp(str, "g");
}
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.
*
@ -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 foundTestUrls = await getMatchingUrls(includeFiles, excludeFiles, root);
const foundTestFiles: string[] = [];
for await (const { filename } of filesIterator) {
foundTestFiles.push(filename);
}
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({

26
testing/runner_test.ts Normal file
View file

@ -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);
});