summaryrefslogtreecommitdiff
path: root/std/testing/runner.ts
blob: 3bea649f713c7cf727db72236e7689d719350b9a (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env -S deno -A
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { parse } from "../flags/mod.ts";
import { ExpandGlobOptions, expandGlob } from "../fs/mod.ts";
import { isWindows, join } from "../path/mod.ts";
import { RunTestsOptions, runTests } from "./mod.ts";
const { DenoError, ErrorKind, args, cwd, exit } = Deno;

const DIR_GLOBS = [join("**", "?(*_)test.{js,ts}")];

function showHelp(): void {
  console.log(`Deno test runner

USAGE:
  deno -A https://deno.land/std/testing/runner.ts [OPTIONS] [MODULES...]

OPTIONS:
  -q, --quiet                 Don't show output from test cases
  -f, --failfast              Stop running tests on first error
  -e, --exclude <MODULES...>  List of comma-separated modules to exclude
  --allow-none                Exit with status 0 even when no test modules are
                              found

ARGS:
  [MODULES...]  List of test modules to run.
                A directory <dir> will expand to:
                  ${DIR_GLOBS.map((s: string): string => `${join("<dir>", s)}`)
                    .join(`
                  `)}
                Defaults to "." when none are provided.

Note that modules can refer to file paths or URLs. File paths support glob
expansion.

Examples:
      deno test src/**/*_test.ts
      deno test tests`);
}

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;
    },
    [[], []]
  );
}

function filePathToUrl(path: string): string {
  return `file://${isWindows ? "/" : ""}${path.replace(/\\/g, "/")}`;
}

/**
 * Given a list of globs or URLs to include and exclude and a root directory
 * 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(
  includeModules: string[],
  excludeModules: string[],
  root: string = cwd()
): AsyncIterableIterator<string> {
  const [includePaths, includeUrls] = partition(includeModules, isRemoteUrl);
  const [excludePaths, excludeUrls] = partition(excludeModules, isRemoteUrl);

  const expandGlobOpts: ExpandGlobOptions = {
    root,
    exclude: excludePaths,
    includeDirs: true,
    extended: true,
    globstar: true
  };

  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 shouldIncludeUrl = (url: string): boolean =>
    !excludeUrlPatterns.some((p: RegExp): boolean => !!url.match(p));

  yield* includeUrls.filter(shouldIncludeUrl);
}

export interface RunTestModulesOptions extends RunTestsOptions {
  include?: string[];
  exclude?: string[];
  allowNone?: boolean;
}

/**
 * Renders test file that will be run.
 *
 * It's done to optimize compilation of test files, because
 * dynamically importing them one by one takes very long time.
 * @TODO(bartlomieju): try to optimize compilation by reusing same compiler host
 * multiple times
 * @param testModules
 */
function renderTestFile(testModules: string[]): string {
  let testFile = "";

  for (const testModule of testModules) {
    // NOTE: this is intentional that template string is not used
    // because of TS compiler quirkness of trying to import it
    // rather than treating it like a variable
    testFile += 'import "' + testModule + '"\n';
  }

  return testFile;
}

/**
 * Import the specified test modules and run their tests as a suite.
 *
 * Test modules are specified as an array of strings and can include local files
 * or URLs.
 *
 * File matching and excluding support glob syntax - arguments recognized as
 * globs will be expanded using `glob()` from the `fs` module.
 *
 * Example:
 *
 *       runTestModules({ include: ["**\/*_test.ts", "**\/test.ts"] });
 *
 * Any matched directory `<dir>` will expand to:
 *   <dir>/**\/?(*_)test.{js,ts}
 *
 * So the above example is captured naturally by:
 *
 *       runTestModules({ include: ["."] });
 *
 * Which is the default used for:
 *
 *       runTestModules();
 */
// TODO: Change return type to `Promise<void>` once, `runTests` is updated
// to return boolean instead of exiting.
export async function runTestModules({
  include = ["."],
  exclude = [],
  allowNone = false,
  parallel = false,
  exitOnFail = false,
  only = /[^\s]/,
  skip = /^\s*$/,
  disableLog = false
}: RunTestModulesOptions = {}): Promise<void> {
  let moduleCount = 0;
  const testModules = [];
  for await (const testModule of findTestModules(include, exclude)) {
    testModules.push(testModule);
    moduleCount++;
  }

  if (moduleCount == 0) {
    const noneFoundMessage = "No matching test modules found.";
    if (!allowNone) {
      throw new DenoError(ErrorKind.NotFound, noneFoundMessage);
    } else if (!disableLog) {
      console.log(noneFoundMessage);
    }
    return;
  }

  // Create temporary test file which contains
  // all matched modules as import statements.
  const testFile = renderTestFile(testModules);
  // Select where temporary test file will be stored.
  // If `DENO_DIR` is set it means that user intentionally wants to store
  // modules there - so it's a sane default to store there.
  // Fallback is current directory which again seems like a sane default,
  // user is probably working on project in this directory or even
  // cd'ed into current directory to quickly run test from this directory.
  const root = Deno.env("DENO_DIR") || Deno.cwd();
  const testFilePath = join(root, ".deno.test.ts");
  const data = new TextEncoder().encode(testFile);
  await Deno.writeFile(testFilePath, data);

  // Import temporary test file and delete it immediately after importing so it's not cluttering disk.
  //
  // You may think that this will cause recompilation on each run, but this actually
  // tricks Deno to not recompile files if there's no need.
  // Eg.
  //   1. On first run of $DENO_DIR/.deno.test.ts Deno will compile and cache temporary test file and all of its imports
  //   2. Temporary test file is removed by test runner
  //   3. On next test run file is created again. If no new modules were added then temporary file contents are identical.
  //      Deno will not compile temporary test file again, but load it directly into V8.
  //   4. Deno starts loading imports one by one.
  //   5. If imported file is outdated, Deno will recompile this single file.
  let err;
  try {
    await import(`file://${testFilePath}`);
  } catch (e) {
    err = e;
  } finally {
    await Deno.remove(testFilePath);
  }

  if (err) {
    throw err;
  }

  if (!disableLog) {
    console.log(`Found ${moduleCount} matching test modules.`);
  }

  await runTests({
    parallel,
    exitOnFail,
    only,
    skip,
    disableLog
  });
}

async function main(): Promise<void> {
  const parsedArgs = parse(args.slice(1), {
    boolean: ["allow-none", "failfast", "help", "quiet"],
    string: ["exclude"],
    alias: {
      exclude: ["e"],
      failfast: ["f"],
      help: ["h"],
      quiet: ["q"]
    },
    default: {
      "allow-none": false,
      failfast: false,
      help: false,
      quiet: false
    }
  });
  if (parsedArgs.help) {
    return showHelp();
  }

  const include =
    parsedArgs._.length > 0
      ? (parsedArgs._ as string[]).flatMap((fileGlob: string): string[] =>
          fileGlob.split(",")
        )
      : ["."];
  const exclude =
    parsedArgs.exclude != null ? (parsedArgs.exclude as string).split(",") : [];
  const allowNone = parsedArgs["allow-none"];
  const exitOnFail = parsedArgs.failfast;
  const disableLog = parsedArgs.quiet;

  try {
    await runTestModules({
      include,
      exclude,
      allowNone,
      exitOnFail,
      disableLog
    });
  } catch (error) {
    if (!disableLog) {
      console.error(error.message);
    }
    exit(1);
  }
}

if (import.meta.main) {
  main();
}