@chainsafe/eth2.0-spec-test-util
Version:
Spec test suite generator from yaml test files
149 lines (116 loc) • 4.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.describeDirectorySpecTest = describeDirectorySpecTest;
exports.InputType = void 0;
var _chai = require("chai");
var _deepmerge = _interopRequireDefault(require("deepmerge"));
var _fs = require("fs");
var _mocha = require("mocha");
var _path = require("path");
var _v8ProfilerNext = _interopRequireDefault(require("v8-profiler-next"));
var _nodejs = require("@chainsafe/eth2.0-utils/lib/nodejs");
var _ssz = require("@chainsafe/ssz");
var _util = require("./util");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/* eslint-disable @typescript-eslint/no-explicit-any */
let InputType;
exports.InputType = InputType;
(function (InputType) {
InputType["SSZ"] = "ssz";
InputType["YAML"] = "yaml";
})(InputType || (exports.InputType = InputType = {}));
const defaultOptions = {
inputTypes: {},
inputProcessing: {},
sszTypes: {},
getExpected: testCase => testCase,
shouldError: () => false,
shouldSkip: () => false,
expectFunc: (testCase, expected, actual) => (0, _chai.expect)(actual).to.be.deep.equal(expected),
timeout: 4000
};
function describeDirectorySpecTest(name, testCaseDirectoryPath, testFunction, options) {
// @ts-ignore
options = (0, _deepmerge.default)(defaultOptions, options);
if (!(0, _util.isDirectory)(testCaseDirectoryPath)) {
throw new Error("".concat(testCaseDirectoryPath, " is not directory"));
}
(0, _mocha.describe)(name, function () {
if (options.timeout) {
this.timeout(options.timeout);
}
const testCases = (0, _fs.readdirSync)(testCaseDirectoryPath).map(name => (0, _path.join)(testCaseDirectoryPath, name)).filter(_util.isDirectory);
testCases.forEach((testCaseDirectory, index) => {
generateTestCase(testCaseDirectory, index, testFunction, options);
});
});
}
function generateTestCase(testCaseDirectoryPath, index, testFunction, options) {
const name = (0, _path.basename)(testCaseDirectoryPath);
(0, _mocha.it)(name, function () {
const testCase = loadInputFiles(testCaseDirectoryPath, options);
if (options.shouldSkip && options.shouldSkip(testCase, name, index)) {
return this.skip();
}
if (options.shouldError && options.shouldError(testCase)) {
try {
testFunction(testCase, name);
} catch (e) {
return;
}
} else {
const profileId = "".concat(name, "-").concat(Date.now(), ".profile");
const profilingDirectory = process.env.GEN_PROFILE_DIR;
if (profilingDirectory) {
_v8ProfilerNext.default.startProfiling(profileId);
}
const result = testFunction(testCase, name);
if (profilingDirectory) {
const profile = _v8ProfilerNext.default.stopProfiling(profileId);
generateProfileReport(profile, profilingDirectory, profileId);
}
const expected = options.getExpected(testCase);
options.expectFunc(testCase, expected, result);
}
});
}
function loadInputFiles(directory, options) {
const testCase = {};
(0, _fs.readdirSync)(directory).map(name => (0, _path.join)(directory, name)).filter(file => {
if ((0, _util.isDirectory)(file)) {
return false;
}
const extension = options.inputTypes[(0, _path.parse)(file).name] || InputType.SSZ;
return file.endsWith(extension);
}).forEach(file => {
const inputName = (0, _path.basename)(file).replace(".ssz", "").replace(".yaml", "");
testCase[inputName] = deserializeTestCase(file, inputName, options);
if (file.endsWith(InputType.SSZ)) {
testCase["".concat(inputName, "_raw")] = (0, _fs.readFileSync)(file);
}
if (options.inputProcessing[inputName]) {
testCase[inputName] = options.inputProcessing[inputName](testCase[inputName]);
}
});
return testCase;
}
function deserializeTestCase(file, inputName, options) {
if (file.endsWith(InputType.SSZ)) {
return (0, _ssz.deserialize)(options.sszTypes[inputName], (0, _fs.readFileSync)(file));
} else {
return (0, _nodejs.loadYamlFile)(file);
}
}
function generateProfileReport(profile, directory, profileId) {
profile.export((error, result) => {
if (error) {
return;
}
(0, _fs.writeFile)("".concat(directory, "/").concat(profileId), result, () => {
profile.delete();
});
});
}
//# sourceMappingURL=single.js.map