"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 ;
}
}
}