remote-jobs/test/lib/index.js
James Nylen 5cce269df4
Refactor validation logic into a short script and a larger library file (#444)
* Refactor validation logic into a short script and a larger library file

* Add unit tests for utility functions
2018-06-25 00:00:12 -05:00

74 lines
2.1 KiB
JavaScript

const path = require( 'path' );
const fs = require( 'fs' );
const { spawnSync } = require( 'child_process' );
const { expect } = require( 'chai' );
const { parseFromDirectory } = require( '../../lib' );
const fixturesPath = path.join( __dirname, '..', 'fixtures' );
const validatePath = path.join( __dirname, '..', '..', 'bin', 'validate.js' );
/**
* Parse a content tree from a set of fixture files.
*/
exports.parseFixtures = dirName => {
return parseFromDirectory( path.join( fixturesPath, dirName ) );
};
/**
* Parse a content tree from a set of fixture files and verify that the
* resulting errors are as expected.
*/
exports.expectValidateFixturesResult = ( dirName, expected ) => {
const result = exports.parseFixtures( dirName );
if ( result.ok ) {
expect( result ).not.to.have.key( 'errors' );
} else {
expect( result.errors ).to.be.an( 'array' );
expect( result.errors.length ).to.be.greaterThan( 0 );
}
expect( expected.errorCount ).to.equal( ( result.errors || [] ).length );
expect( expected.output ).to.eql( ( result.errors || [] ).map( err => {
return err.filename + ': ' + err.message;
} ) );
return result;
};
/**
* Parse a content tree from a set of fixture files by running the validation
* script in a separate process, and return the resulting output.
*/
exports.runValidationScriptWithFixtures = ( dirName, env = {} ) => {
const result = spawnSync( process.execPath, [
validatePath,
path.join( fixturesPath, dirName ),
], { env } );
if ( result.error ) {
throw result.error;
}
expect( result.stderr.toString() ).to.equal( '' );
const output = result.stdout.toString().trim().split( '\n' );
const exitCode = result.status;
expect( output[ output.length - 1 ] ).to.equal(
exitCode + ' problem' + ( exitCode === 1 ? '' : 's' ) + ' detected'
);
if ( output.length >= 2 ) {
expect( output[ output.length - 2 ] ).to.equal( '' );
output.splice( -2 );
} else {
output.splice( -1 );
}
if ( process.env.DUMP_OUTPUT ) {
output.forEach( s => {
console.log( "'%s',", s.replace( /('|\\)/g, "\\$1" ) );
} );
}
return { output, exitCode };
};