summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/bin/try-runner.js
blob: 93b88fac23dd05ce418b0d50f0fce93f9f52894e (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* eslint-disable no-console */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

/*
 * A small test runner/reporter for node-based tests,
 * which are run via taskcluster node(debugger).
 *
 * Forked from
 * https://searchfox.org/mozilla-central/rev/c3453c7a0427eb27d467e1582f821f402aed9850/devtools/client/debugger/bin/try-runner.js
 */

const { execFileSync } = require("child_process");
const { readFileSync, writeFileSync } = require("fs");
const path = require("path");
const { pathToFileURL } = require("url");
const chalk = require("chalk");

function logErrors(tool, errors) {
  for (const error of errors) {
    console.log(`TEST-UNEXPECTED-FAIL | ${tool} | ${error}`);
  }
  return errors;
}

function execOut(...args) {
  let exitCode = 0;
  let out;
  let err;

  try {
    out = execFileSync(...args, {
      silent: false,
    });
  } catch (e) {
    // For debugging on (eg) try server...
    //
    // if (e) {
    //   logErrors("execOut", ["execFileSync returned exception: ", e]);
    // }

    out = e && e.stdout;
    err = e && e.stderr;
    exitCode = e && e.status;
  }
  return { exitCode, out: out && out.toString(), err: err && err.toString() };
}

function logStart(name) {
  console.log(`TEST-START | ${name}`);
}

function logSkip(name) {
  console.log(`TEST-SKIP | ${name}`);
}

const npmCommand = process.platform === "win32" ? "npm.cmd" : "npm";

const tests = {
  bundles() {
    logStart("bundles");

    const items = {
      "Activity Stream bundle": {
        path: path.join("data", "content", "activity-stream.bundle.js"),
      },
      "activity-stream.html": {
        path: path.join("prerendered", "activity-stream.html"),
      },
      "activity-stream-debug.html": {
        path: path.join("prerendered", "activity-stream-debug.html"),
      },
      "activity-stream-noscripts.html": {
        path: path.join("prerendered", "activity-stream-noscripts.html"),
      },
      "activity-stream-linux.css": {
        path: path.join("css", "activity-stream-linux.css"),
      },
      "activity-stream-mac.css": {
        path: path.join("css", "activity-stream-mac.css"),
      },
      "activity-stream-windows.css": {
        path: path.join("css", "activity-stream-windows.css"),
      },
      // These should get split out to their own try-runner eventually (bug 1866170).
      "about:welcome bundle": {
        path: path.join(
          "../",
          "aboutwelcome",
          "content",
          "aboutwelcome.bundle.js"
        ),
      },
      "aboutwelcome.css": {
        path: path.join("../", "aboutwelcome", "content", "aboutwelcome.css"),
        extraCheck: content => {
          if (content.match(/^\s*@import/m)) {
            return "aboutwelcome.css contains an @import statement. We should not import styles through the stylesheet, because it is loaded in multiple environments, including the browser chrome for feature callouts. To add other stylesheets to about:welcome or spotlight, add them to aboutwelcome.html or spotlight.html instead.";
          }
          return null;
        },
      },
      // These should get split out to their own try-runner eventually (bug 1866170).
      "about:asrouter bundle": {
        path: path.join(
          "../",
          "asrouter",
          "content",
          "asrouter-admin.bundle.js"
        ),
      },
      "ASRouterAdmin.css": {
        path: path.join(
          "../",
          "asrouter",
          "content",
          "components",
          "ASRouterAdmin",
          "ASRouterAdmin.css"
        ),
      },
    };
    const errors = [];

    for (const name of Object.keys(items)) {
      const item = items[name];
      item.before = readFileSync(item.path, item.encoding || "utf8");
    }

    let newtabBundleExitCode = execOut(npmCommand, ["run", "bundle"]).exitCode;

    // Until we split out the try runner for about:welcome out into its own
    // script, we manually run its bundle script.
    let cwd = process.cwd();
    process.chdir("../aboutwelcome");
    let welcomeBundleExitCode = execOut(npmCommand, ["run", "bundle"]).exitCode;
    process.chdir(cwd);

    // Same thing for about:asrouter
    process.chdir("../asrouter");
    let asrouterBundleExitCode = execOut(npmCommand, [
      "run",
      "bundle",
    ]).exitCode;
    process.chdir(cwd);

    for (const name of Object.keys(items)) {
      const item = items[name];
      const after = readFileSync(item.path, item.encoding || "utf8");

      if (item.before !== after) {
        errors.push(`${name} out of date`);
      }

      if (item.extraCheck) {
        const extraError = item.extraCheck(after);
        if (extraError) {
          errors.push(extraError);
        }
      }
    }

    if (newtabBundleExitCode !== 0) {
      errors.push("newtab npm:bundle did not run successfully");
    }

    if (welcomeBundleExitCode !== 0) {
      errors.push("about:welcome npm:bundle did not run successfully");
    }

    if (asrouterBundleExitCode !== 0) {
      errors.push("about:asrouter npm:bundle did not run successfully");
    }

    logErrors("bundles", errors);
    return errors.length === 0;
  },

  karma() {
    logStart(`karma ${process.cwd()}`);

    const errors = [];
    const { exitCode, out } = execOut(npmCommand, [
      "run",
      "testmc:unit",
      // , "--", "--log-level", "--verbose",
      // to debug the karma integration, uncomment the above line
    ]);

    // karma spits everything to stdout, not stderr, so if nothing came back on
    // stdout, give up now.
    if (!out) {
      return false;
    }

    // Detect mocha failures
    let jsonContent;
    try {
      // Note that this will be overwritten at each run, but that shouldn't
      // matter.
      jsonContent = readFileSync(path.join("logs", "karma-run-results.json"));
    } catch (ex) {
      console.error("exception reading karma-run-results.json: ", ex);
      return false;
    }
    const results = JSON.parse(jsonContent);
    // eslint-disable-next-line guard-for-in
    for (let testArray in results.result) {
      let failedTests = Array.from(results.result[testArray]).filter(
        test => !test.success && !test.skipped
      );

      errors.push(
        ...failedTests.map(
          test => `${test.suite.join(":")} ${test.description}: ${test.log[0]}`
        )
      );
    }

    // Detect istanbul failures (coverage thresholds set in karma config)
    const coverage = out.match(/ERROR.+coverage-istanbul.+/g);
    if (coverage) {
      errors.push(...coverage.map(line => line.match(/Coverage.+/)[0]));
    }

    logErrors(`karma ${process.cwd()}`, errors);

    console.log("-----karma stdout below this line---");
    console.log(out);
    console.log("-----karma stdout above this line---");

    // Pass if there's no detected errors and nothing unexpected.
    return errors.length === 0 && !exitCode;
  },

  welcomekarma() {
    let cwd = process.cwd();
    process.chdir("../aboutwelcome");
    const result = this.karma();
    process.chdir(cwd);
    return result;
  },

  asrouterkarma() {
    let cwd = process.cwd();
    process.chdir("../asrouter");
    const result = this.karma();
    process.chdir(cwd);
    return result;
  },

  zipCodeCoverage() {
    logStart("zipCodeCoverage");

    const newtabCoveragePath = "logs/coverage/lcov.info";
    const welcomeCoveragePath = "../aboutwelcome/logs/coverage/lcov.info";
    const asrouterCoveragePath = "../asrouter/logs/coverage/lcov.info";

    let newtabCoverage = readFileSync(newtabCoveragePath, "utf8");
    const welcomeCoverage = readFileSync(welcomeCoveragePath, "utf8");
    const asrouterCoverage = readFileSync(asrouterCoveragePath, "utf8");

    newtabCoverage = `${newtabCoverage}${welcomeCoverage}${asrouterCoverage}`;
    writeFileSync(newtabCoveragePath, newtabCoverage, "utf8");

    const { exitCode, out } = execOut("zip", [
      "-j",
      "logs/coverage/code-coverage-grcov",
      "logs/coverage/lcov.info",
    ]);

    console.log("zipCodeCoverage log output: ", out);

    if (!exitCode) {
      return true;
    }

    return false;
  },
};

async function main() {
  const { default: meow } = await import("meow");
  const fileUrl = pathToFileURL(__filename);
  const cli = meow(
    `
  Usage
    $ node bin/try-runner.js <tests> [options]

  Options
    -t NAME, --test NAME   Run only the specified test. If not specified,
                           all tests will be run. Argument can be passed 
                           multiple times to run multiple tests.
    --help                 Show this help message.

  Examples
    $ node bin/try-runner.js bundles karma
    $ node bin/try-runner.js -t karma -t zip
`,
    {
      description: false,
      // `pkg` is a tiny optimization. It prevents meow from looking for a package
      // that doesn't technically exist. meow searches for a package and changes
      // the process name to the package name. It resolves to the newtab
      // package.json, which would give a confusing name and be wasteful.
      pkg: {
        name: "try-runner",
        version: "1.0.0",
      },
      // `importMeta` is required by meow 10+. It was added to support ESM, but
      // meow now requires it, and no longer supports CJS style imports. But it
      // only uses import.meta.url, which can be polyfilled like this:
      importMeta: { url: fileUrl },
      flags: {
        test: {
          type: "string",
          isMultiple: true,
          alias: "t",
        },
      },
    }
  );
  const aliases = {
    bundle: "bundles",
    build: "bundles",
    coverage: "karma",
    cov: "karma",
    zip: "zipCodeCoverage",
    welcomecoverage: "welcomekarma",
    welcomecov: "welcomekarma",
    asroutercoverage: "asrouterkarma",
    asroutercov: "asrouterkarma",
  };

  const inputs = [...cli.input, ...cli.flags.test].map(input =>
    (aliases[input] || input).toLowerCase()
  );

  function shouldRunTest(name) {
    if (inputs.length) {
      return inputs.includes(name.toLowerCase());
    }
    return true;
  }

  const results = [];
  for (const name of Object.keys(tests)) {
    if (shouldRunTest(name)) {
      results.push([name, tests[name]()]);
    } else {
      logSkip(name);
    }
  }

  for (const [name, result] of results) {
    // colorize output based on result
    console.log(result ? chalk.green(`✓ ${name}`) : chalk.red(`✗ ${name}`));
  }

  const success = results.every(([, result]) => result);
  process.exitCode = success ? 0 : 1;
  console.log("CODE", process.exitCode);
}

main();