summaryrefslogtreecommitdiff
path: root/std/testing/runner_test.ts
blob: 40159db3d4166843f1bbb541345ade25828c84ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import { isWindows } from "../path/mod.ts";
import { test } from "./mod.ts";
import { findTestModules } from "./runner.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 findTestModulesArray(["."], [], TEST_DATA_PATH);
  assertEquals(urls.sort(), [
    `${TEST_DATA_URL}/bar_test.js`,
    `${TEST_DATA_URL}/foo_test.ts`,
    `${TEST_DATA_URL}/subdir/bar_test.js`,
    `${TEST_DATA_URL}/subdir/foo_test.ts`,
    `${TEST_DATA_URL}/subdir/test.js`,
    `${TEST_DATA_URL}/subdir/test.ts`,
    `${TEST_DATA_URL}/test.js`,
    `${TEST_DATA_URL}/test.ts`
  ]);
});

test(async function findTestModulesDir2(): Promise<void> {
  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`,
    `${TEST_DATA_URL}/subdir/test.js`,
    `${TEST_DATA_URL}/subdir/test.ts`
  ]);
});

test(async function findTestModulesGlob(): Promise<void> {
  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`,
    `${TEST_DATA_URL}/subdir/bar_test.js`,
    `${TEST_DATA_URL}/subdir/foo_test.ts`
  ]);
});

test(async function findTestModulesExcludeDir(): Promise<void> {
  const urls = await findTestModulesArray(["."], ["subdir"], TEST_DATA_PATH);
  assertEquals(urls.sort(), [
    `${TEST_DATA_URL}/bar_test.js`,
    `${TEST_DATA_URL}/foo_test.ts`,
    `${TEST_DATA_URL}/test.js`,
    `${TEST_DATA_URL}/test.ts`
  ]);
});

test(async function findTestModulesExcludeGlob(): Promise<void> {
  const urls = await findTestModulesArray(["."], ["**/foo*"], TEST_DATA_PATH);
  assertEquals(urls.sort(), [
    `${TEST_DATA_URL}/bar_test.js`,
    `${TEST_DATA_URL}/subdir/bar_test.js`,
    `${TEST_DATA_URL}/subdir/test.js`,
    `${TEST_DATA_URL}/subdir/test.ts`,
    `${TEST_DATA_URL}/test.js`,
    `${TEST_DATA_URL}/test.ts`
  ]);
});

test(async function findTestModulesRemote(): Promise<void> {
  const urls = [
    "https://example.com/colors_test.ts",
    "http://example.com/printf_test.ts"
  ];
  const matches = await findTestModulesArray(urls, []);
  assertEquals(matches, urls);
});