WIP, add autogenerated tests against upstream
parent
4cb239c6a0
commit
52f39570a7
@ -0,0 +1,45 @@
|
||||
"use strict";
|
||||
|
||||
const measureTime = require("./astformer/util/measure-time");
|
||||
const transpile = require("./transpile");
|
||||
|
||||
module.exports = function evaluate(nixCode) {
|
||||
let transpiled = transpile(nixCode);
|
||||
|
||||
const api = {
|
||||
builtins: {},
|
||||
$$jsNix$memoize: function (func) {
|
||||
let isCalled = false;
|
||||
let storedResult;
|
||||
|
||||
return function (arg) {
|
||||
if (isCalled === false) {
|
||||
storedResult = func(arg);
|
||||
isCalled = true;
|
||||
}
|
||||
|
||||
return storedResult;
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: Switch to Node `vm` API instead, and check whether there's a polyfill for it for non-Node environments, build a custom one if not
|
||||
const context = { module: {}, exports: {} };
|
||||
context.module.exports = exports;
|
||||
|
||||
new Function("module", transpiled)(context.module);
|
||||
|
||||
// Warm-up for hot VM performance testing
|
||||
// for (let i = 0; i < 10000; i++) {
|
||||
// context.module.exports(api);
|
||||
// }
|
||||
|
||||
let result = measureTime(() => context.module.exports(api));
|
||||
|
||||
if (process.env.DEBUG_NIX) {
|
||||
console.log("-- EVALUATION RESULT:");
|
||||
console.log(result);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
@ -0,0 +1,40 @@
|
||||
"use strict";
|
||||
|
||||
const tape = require("tape-catch");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const evaluate = require("../src/evaluate");
|
||||
|
||||
const NIX_SOURCE_REPO = process.env.NIX_SOURCE_REPO;
|
||||
|
||||
if (NIX_SOURCE_REPO == null) {
|
||||
throw new Error(`To run the upstream Nix language tests, you must specify a NIX_SOURCE_REPO environment variable, that points at the root of a local checkout of the Git repository for Nix`);
|
||||
}
|
||||
|
||||
const testsPath = path.join(NIX_SOURCE_REPO, "tests/lang");
|
||||
|
||||
let tests = fs.readdirSync(testsPath)
|
||||
.filter((entry) => entry.endsWith(".exp"))
|
||||
.map((entry) => entry.replace(/\.exp$/, ""));
|
||||
|
||||
for (let test of tests) {
|
||||
try {
|
||||
let expression = fs.readFileSync(path.join(testsPath, `${test}.nix`), "utf8");
|
||||
let expectedResult = fs.readFileSync(path.join(testsPath, `${test}.exp`), "utf8").replace(/\n$/, "");
|
||||
|
||||
tape(`Nix upstream language tests - ${test}`, (test) => {
|
||||
test.plan(1);
|
||||
|
||||
let result = evaluate(expression).value.toString();
|
||||
|
||||
test.equals(expectedResult, result);
|
||||
});
|
||||
} catch (error) {
|
||||
// FIXME: This would currently cause ENOENTs during evaluation (eg. reading a file from Nix itself) to be ignored
|
||||
if (error.code === "ENOENT") {
|
||||
// skip
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue