Give installFiles its own index

This commit is contained in:
Ron Rennick 2021-12-08 13:47:30 -04:00
parent 6989bd61f9
commit 59f1f3a6b5
4 changed files with 46 additions and 33 deletions

View File

@ -4,16 +4,4 @@
const allSpecs = require( './specs' );
const fs = require( 'fs' );
/**
* Test set up configuration for the test scaffolding tool.
*/
const packageInstallFiles = {
testSpecs: 'data/install-specs.json',
defaultJson: 'data/default-test-config.json',
initializeSh: 'data/initialize.sh.default',
};
module.exports = {
allSpecs,
packageInstallFiles,
};
module.exports = allSpecs;

View File

@ -0,0 +1,5 @@
module.exports = {
testSpecs: 'data/scaffold-tests.json',
defaultJson: 'data/default-test-config.json',
initializeSh: 'data/initialize.sh.default',
};

View File

@ -22,32 +22,31 @@ const [ command, packageName ] = args;
* Install the test scripts and sample default.json configuration
*/
if ( command == 'install' ) {
//**
const package = resolvePackage( packageName ).name;
if ( ! package.length ) {
// `package` is a reserved word
const pkg = resolvePackage( packageName ).name;
if ( ! pkg.length ) {
//@todo add error message
return;
}
const packageSlug = package.replace( '@', '' ).replace( /\//g, '.' );
const { packageInstallFiles } = require( package );
const { testSpecs, defaultJson, initializeSh } = packageInstallFiles;
const packageSlug = pkg.replace( '@', '' ).replace( /\//g, '.' );
const { testSpecs, defaultJson, initializeSh } = require( `${pkg}/installFiles` );
// Write sample default.json
if ( defaultJson ) {
const defaultJsonName = `config/default-${packageSlug}.json`;
const defaultJsonSample = resolveLocalE2ePath( defaultJsonName );
const packageJsonSample = resolvePackagePath( defaultJson, package );
const packageJsonSample = resolvePackagePath( defaultJson, pkg );
fs.copyFileSync( packageJsonSample, defaultJsonSample );
console.log( `\nCreated sample test configuration to 'tests/e2e/${defaultJsonName}'.\n` );
console.log( `Created sample test configuration to 'tests/e2e/${defaultJsonName}'.` );
}
// Write sample initialize.sh
if ( initializeSh ) {
const defaultInitName = `docker/${packageSlug}.sh`;
const defaultInitPath = resolveLocalE2ePath( defaultInitName );
const packageInitPath = resolvePackagePath( initializeSh, package );
const packageInitPath = resolvePackagePath( initializeSh, pkg );
fs.copyFileSync( packageInitPath, defaultInitPath );
console.log( `\nCreated sample test container initialization script to 'tests/e2e/${defaultInitName}'.\n` );
console.log( `Created sample test container initialization script to 'tests/e2e/${defaultInitName}'.` );
}
// Write test files
@ -55,7 +54,7 @@ if ( command == 'install' ) {
return;
}
const testsSpecFile = resolvePackagePath( testSpecs, package );
const testsSpecFile = resolvePackagePath( testSpecs, pkg );
const specs = fs.readFileSync( testsSpecFile );
const tests = JSON.parse( specs );
const { active, deprecated } = tests;
@ -79,15 +78,26 @@ if ( command == 'install' ) {
}
if ( active && active.length ) {
const blankLine = '';
const eol = "\n";
const autoGenerate = sprintf( '/* This file was auto-generated by the command `npx wc-e2e install %s. */', packageName );
let importLineFormat;
if ( testFormat.toLowerCase() == 'cjs' ) {
importLineFormat = sprintf( "const {%%s} = require( '%s' );", package );
importLineFormat = sprintf( "const {%%s} = require( '%s' );", pkg );
} else {
importLineFormat = sprintf( "import {%%s} from '%s';", package );
importLineFormat = sprintf( "import {%%s} from '%s';", pkg );
}
// Create the entire folder structure if not present
let specFolderPath;
const rootFolders = [ '../../tests', '../e2e', 'specs' ];
rootFolders.forEach( ( folder ) => {
specFolderPath = resolveLocalE2ePath( folder );
if ( ! fs.existsSync( specFolderPath ) ) {
fs.mkdirSync( specFolderPath );
}
} );
// Loop through folders and files to write test scripts.
for ( let f = 0; f < active.length; f++ ) {
const testFolder = active[ f ];
@ -98,7 +108,7 @@ if ( command == 'install' ) {
}
const specFolder = testFolder.name.length ? `specs/${testFolder.name}` : 'specs';
const specFolderPath = resolveLocalE2ePath( specFolder );
specFolderPath = resolveLocalE2ePath( specFolder );
// Create the test folder if it doesn't exist.
if ( ! fs.existsSync( specFolderPath ) ) {
@ -131,19 +141,17 @@ if ( command == 'install' ) {
importPrefix = eol;
} else {
testSeparator = ', ';
testTerminator = '';
testTerminator = ' ';
importPrefix = ' ';
}
const testImport = testFile.functions.join( testSeparator ) + testTerminator;
// '' adds a blank line
buffer.push( sprintf( importLineFormat, importPrefix + testImport ), '' );
buffer.push( sprintf( importLineFormat, importPrefix + testImport ), blankLine );
// Add test function calls and write the file
let functionCalls = testFile.functions.map( functionName => functionName + '();' );
buffer.push( ...functionCalls );
buffer.push( ...functionCalls, blankLine );
fs.writeFileSync( testFilePath, buffer.join( eol ) );
}
console.log( eol );
}
}

View File

@ -32,7 +32,19 @@ const resolvePackage = ( packageName, allowRecurse = true ) => {
const resolvedPackage = {};
try {
resolvedPackage.path = require.resolve( packageName );
const resolvedPath = path.dirname( require.resolve( packageName ) );
const buildPaths = [ 'dist', 'build', 'build-modules' ];
// Remove build paths from the resolved path.
let resolvedParts = resolvedPath.split( path.sep );
for ( let rp = resolvedParts.length - 1; rp >= 0; rp-- ) {
if ( buildPaths.includes( resolvedParts[ rp ] ) ) {
resolvedParts = resolvedParts.slice( 0, -1 );
} else {
break;
}
}
resolvedPackage.path = resolvedParts.join( path.sep );
resolvedPackage.name = packageName;
} catch ( e ) {
// Package name installed is not the package name.