2019-11-02 00:06:25 +01:00
|
|
|
const _ = require('underscore/underscore.js');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2016-07-30 18:14:42 +02:00
|
|
|
|
|
|
|
exports.find_files_to_run = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
let oneFileFilter = [];
|
|
|
|
let testsDifference = [];
|
2016-08-26 04:49:43 +02:00
|
|
|
if (process.argv[2]) {
|
2016-07-30 18:14:42 +02:00
|
|
|
oneFileFilter = process.argv
|
2018-05-07 01:50:14 +02:00
|
|
|
.slice(2)
|
|
|
|
.filter(function (filename) {return (/[.]js$/).test(filename);})
|
|
|
|
.map(function (filename) {return filename.replace(/\.js$/i, '');});
|
2016-07-30 18:14:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// tests_dir is where we find our specific unit tests (as opposed
|
|
|
|
// to framework code)
|
2019-11-02 00:06:25 +01:00
|
|
|
const tests_dir = __dirname.replace(/zjsunit/, 'node_tests');
|
2016-07-30 18:14:42 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let tests = fs.readdirSync(tests_dir)
|
2018-05-07 01:50:14 +02:00
|
|
|
.filter(function (filename) {return !(/^\./i).test(filename);})
|
|
|
|
.filter(function (filename) {return (/\.js$/i).test(filename);})
|
|
|
|
.map(function (filename) {return filename.replace(/\.js$/i, '');});
|
2016-07-30 18:14:42 +02:00
|
|
|
|
|
|
|
if (oneFileFilter.length > 0) {
|
|
|
|
tests = tests.filter(function (filename) {
|
js: Convert a.indexOf(…) !== -1 to a.includes(…).
Babel polyfills this for us for Internet Explorer.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import K from "ast-types/gen/kinds";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
recast.visit(ast, {
visitBinaryExpression(path) {
const { operator, left, right } = path.node;
if (
n.CallExpression.check(left) &&
n.MemberExpression.check(left.callee) &&
!left.callee.computed &&
n.Identifier.check(left.callee.property) &&
left.callee.property.name === "indexOf" &&
left.arguments.length === 1 &&
checkExpression(left.arguments[0]) &&
((["===", "!==", "==", "!=", ">", "<="].includes(operator) &&
n.UnaryExpression.check(right) &&
right.operator == "-" &&
n.Literal.check(right.argument) &&
right.argument.value === 1) ||
([">=", "<"].includes(operator) &&
n.Literal.check(right) &&
right.value === 0))
) {
const test = b.callExpression(
b.memberExpression(left.callee.object, b.identifier("includes")),
[left.arguments[0]]
);
path.replace(
["!==", "!=", ">", ">="].includes(operator)
? test
: b.unaryExpression("!", test)
);
changed = true;
}
this.traverse(path);
},
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 04:55:06 +01:00
|
|
|
return oneFileFilter.includes(filename);
|
2016-07-30 18:14:42 +02:00
|
|
|
});
|
|
|
|
testsDifference = _.difference(oneFileFilter, tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
testsDifference.forEach(function (filename) {
|
2018-06-06 18:19:09 +02:00
|
|
|
throw filename + ".js does not exist";
|
2016-07-30 18:14:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
tests.sort();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const files = tests.map(function (fn) {
|
|
|
|
const obj = {};
|
2016-07-30 18:14:42 +02:00
|
|
|
obj.name = fn;
|
|
|
|
obj.full_name = path.join(tests_dir, fn);
|
|
|
|
return obj;
|
|
|
|
});
|
|
|
|
|
|
|
|
return files;
|
|
|
|
};
|