You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.5 KiB
JavaScript

"use strict";
const tape = require("tape-catch");
const fs = require("fs");
const path = require("path");
const util = require("util");
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$/, ""));
function formatResultNode(node) {
if (typeof node === "string") {
return `"${node.replace(/"/g, '\\"')}"`;
} else if (Array.isArray(node)) {
return `[ ${node.map(formatResultNode).join(" ")} ]`;
} else {
return node.toString();
}
}
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 = formatResultNode(evaluate(expression).value);
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;
}
}
}