summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/bin/try-runner.js
blob: 8b240465b6624e933c40cbc459d4c232d3908322 (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
/* 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/source/devtools/client/debugger/bin/try-runner.js
 */

const { execFileSync } = require("child_process");
const { readFileSync } = require("fs");
const path = require("path");

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 checkBundles() {
  logStart("checkBundles");

  const ASbundle = path.join("data", "content", "activity-stream.bundle.js");
  const AWbundle = path.join(
    "aboutwelcome",
    "content",
    "aboutwelcome.bundle.js"
  );
  let errors = [];

  let ASbefore = readFileSync(ASbundle, "utf8");
  let AWbefore = readFileSync(AWbundle, "utf8");

  execOut("npm", ["run", "bundle"]);

  let ASafter = readFileSync(ASbundle, "utf8");
  let AWafter = readFileSync(AWbundle, "utf8");

  if (ASbefore !== ASafter) {
    errors.push("Activity Stream bundle out of date");
  }

  if (AWbefore !== AWafter) {
    errors.push("About:welcome bundle out of date");
  }

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

function karma() {
  logStart("karma");

  const errors = [];
  const { exitCode, out } = execOut("npm", [
    "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", 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;
}

function sasslint() {
  logStart("sasslint");
  const { exitCode, out } = execOut("npm", [
    "run",
    "--silent",
    "lint:sasslint",
    "--",
    "--format",
    "json",
  ]);

  // Successful exit and no output means sasslint passed.
  if (!exitCode && !out.length) {
    return true;
  }

  let fileObjects = JSON.parse(out);
  let filesWithIssues = fileObjects.filter(
    file => file.warningCount || file.errorCount
  );

  let errs = [];
  let errorString;
  filesWithIssues.forEach(file => {
    file.messages.forEach(messageObj => {
      errorString = `${file.filePath}(${messageObj.line}, ${messageObj.column}): ${messageObj.message} (${messageObj.ruleId})`;
      errs.push(errorString);
    });
  });

  const errors = logErrors("sasslint", errs);

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

function zipCodeCoverage() {
  logStart("zipCodeCoverage");
  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;
}

const tests = {};
const success = [checkBundles, karma, zipCodeCoverage, sasslint].every(
  t => (tests[t.name] = t())
);
console.log(tests);

process.exitCode = success ? 0 : 1;
console.log("CODE", process.exitCode);