2021-11-19 21:02:02 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies.
|
|
|
|
*/
|
|
|
|
const fs = require( 'fs' );
|
|
|
|
const sprintf = require( 'sprintf-js' ).sprintf;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies.
|
|
|
|
*/
|
2021-11-26 20:55:22 +00:00
|
|
|
const {
|
|
|
|
resolveLocalE2ePath,
|
|
|
|
resolvePackage,
|
|
|
|
resolvePackagePath,
|
|
|
|
} = require( '../utils' );
|
2021-11-19 21:02:02 +00:00
|
|
|
|
2021-11-26 20:55:22 +00:00
|
|
|
const args = process.argv.slice( 2 );
|
|
|
|
const [ command, packageName ] = args;
|
2021-11-19 21:02:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Install the test scripts and sample default.json configuration
|
|
|
|
*/
|
|
|
|
if ( command == 'install' ) {
|
2021-12-08 17:47:30 +00:00
|
|
|
// `package` is a reserved word
|
|
|
|
const pkg = resolvePackage( packageName ).name;
|
|
|
|
if ( ! pkg.length ) {
|
2021-11-26 20:55:22 +00:00
|
|
|
//@todo add error message
|
|
|
|
return;
|
2021-11-19 21:02:02 +00:00
|
|
|
}
|
2021-12-08 17:47:30 +00:00
|
|
|
const packageSlug = pkg.replace( '@', '' ).replace( /\//g, '.' );
|
|
|
|
const { testSpecs, defaultJson, initializeSh } = require( `${pkg}/installFiles` );
|
2021-11-19 21:02:02 +00:00
|
|
|
|
|
|
|
// Write sample default.json
|
2021-11-26 20:55:22 +00:00
|
|
|
if ( defaultJson ) {
|
|
|
|
const defaultJsonName = `config/default-${packageSlug}.json`;
|
|
|
|
const defaultJsonSample = resolveLocalE2ePath( defaultJsonName );
|
2021-12-08 17:47:30 +00:00
|
|
|
const packageJsonSample = resolvePackagePath( defaultJson, pkg );
|
2021-11-26 20:55:22 +00:00
|
|
|
fs.copyFileSync( packageJsonSample, defaultJsonSample );
|
2021-12-08 17:47:30 +00:00
|
|
|
console.log( `Created sample test configuration to 'tests/e2e/${defaultJsonName}'.` );
|
2021-11-26 20:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write sample initialize.sh
|
|
|
|
if ( initializeSh ) {
|
|
|
|
const defaultInitName = `docker/${packageSlug}.sh`;
|
|
|
|
const defaultInitPath = resolveLocalE2ePath( defaultInitName );
|
2021-12-08 17:47:30 +00:00
|
|
|
const packageInitPath = resolvePackagePath( initializeSh, pkg );
|
2021-11-26 20:55:22 +00:00
|
|
|
fs.copyFileSync( packageInitPath, defaultInitPath );
|
2021-12-08 17:47:30 +00:00
|
|
|
console.log( `Created sample test container initialization script to 'tests/e2e/${defaultInitName}'.` );
|
2021-11-26 20:55:22 +00:00
|
|
|
}
|
2021-11-19 21:02:02 +00:00
|
|
|
|
|
|
|
// Write test files
|
2021-11-26 20:55:22 +00:00
|
|
|
if ( ! testSpecs ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-08 17:47:30 +00:00
|
|
|
const testsSpecFile = resolvePackagePath( testSpecs, pkg );
|
2021-11-26 20:55:22 +00:00
|
|
|
const specs = fs.readFileSync( testsSpecFile );
|
|
|
|
const tests = JSON.parse( specs );
|
|
|
|
const { active, deprecated } = tests;
|
|
|
|
|
|
|
|
// Allow multiple spec file extensions and formats.
|
|
|
|
let testExtension = 'test.js';
|
|
|
|
let testFormat = '';
|
|
|
|
for ( let a = 2; a < args.length; a++ ) {
|
|
|
|
const nextArg = a + 1;
|
|
|
|
if ( nextArg >= args.length ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch ( args[ a ] ) {
|
|
|
|
case '--format':
|
|
|
|
testFormat = args[ nextArg ];
|
|
|
|
break;
|
|
|
|
case '--ext':
|
|
|
|
testExtension = args[ nextArg ];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( active && active.length ) {
|
2021-12-08 17:47:30 +00:00
|
|
|
const blankLine = '';
|
2021-11-19 21:02:02 +00:00
|
|
|
const eol = "\n";
|
2021-11-26 20:55:22 +00:00
|
|
|
const autoGenerate = sprintf( '/* This file was auto-generated by the command `npx wc-e2e install %s. */', packageName );
|
|
|
|
let importLineFormat;
|
|
|
|
if ( testFormat.toLowerCase() == 'cjs' ) {
|
2021-12-08 17:47:30 +00:00
|
|
|
importLineFormat = sprintf( "const {%%s} = require( '%s' );", pkg );
|
2021-11-26 20:55:22 +00:00
|
|
|
} else {
|
2021-12-08 17:47:30 +00:00
|
|
|
importLineFormat = sprintf( "import {%%s} from '%s';", pkg );
|
2021-11-26 20:55:22 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 17:47:30 +00:00
|
|
|
// 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 );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2021-11-19 21:02:02 +00:00
|
|
|
// Loop through folders and files to write test scripts.
|
2021-11-26 20:55:22 +00:00
|
|
|
for ( let f = 0; f < active.length; f++ ) {
|
|
|
|
const testFolder = active[ f ];
|
|
|
|
const { testFiles } = testFolder;
|
|
|
|
|
|
|
|
if ( ! testFiles || ! testFiles.length ) {
|
2021-11-19 21:02:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const specFolder = testFolder.name.length ? `specs/${testFolder.name}` : 'specs';
|
2021-12-08 17:47:30 +00:00
|
|
|
specFolderPath = resolveLocalE2ePath( specFolder );
|
2021-11-19 21:02:02 +00:00
|
|
|
|
|
|
|
// Create the test folder if it doesn't exist.
|
2021-11-26 20:55:22 +00:00
|
|
|
if ( ! fs.existsSync( specFolderPath ) ) {
|
2021-11-19 21:02:02 +00:00
|
|
|
fs.mkdirSync( specFolderPath );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the test files.
|
2021-11-26 20:55:22 +00:00
|
|
|
for ( let t = 0; t < testFiles.length; t++ ) {
|
|
|
|
const testFile = testFiles[ t ];
|
2021-11-19 21:02:02 +00:00
|
|
|
if ( ! testFile.functions.length ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const testFileName = `${specFolder}/${testFile.name}.${testExtension}`;
|
|
|
|
const testFilePath = resolveLocalE2ePath( testFileName );
|
|
|
|
|
|
|
|
// @todo Add check to see if file exists
|
|
|
|
// fs.existsSync
|
|
|
|
// @todo Confirm overwrite
|
|
|
|
// readline
|
|
|
|
|
|
|
|
console.log( 'Writing tests/e2e/' + testFileName );
|
|
|
|
let buffer = [ autoGenerate ];
|
|
|
|
let testSeparator, testTerminator, importPrefix;
|
|
|
|
|
|
|
|
// Add the import line.
|
|
|
|
if ( testFile.functions.length > 3 ) {
|
|
|
|
testSeparator = ',' + eol;
|
|
|
|
testTerminator = eol;
|
|
|
|
importPrefix = eol;
|
|
|
|
} else {
|
|
|
|
testSeparator = ', ';
|
2021-12-08 17:47:30 +00:00
|
|
|
testTerminator = ' ';
|
2021-11-19 21:02:02 +00:00
|
|
|
importPrefix = ' ';
|
|
|
|
}
|
|
|
|
const testImport = testFile.functions.join( testSeparator ) + testTerminator;
|
2021-12-08 17:47:30 +00:00
|
|
|
buffer.push( sprintf( importLineFormat, importPrefix + testImport ), blankLine );
|
2021-11-19 21:02:02 +00:00
|
|
|
|
|
|
|
// Add test function calls and write the file
|
|
|
|
let functionCalls = testFile.functions.map( functionName => functionName + '();' );
|
2021-12-08 17:47:30 +00:00
|
|
|
buffer.push( ...functionCalls, blankLine );
|
2021-11-19 21:02:02 +00:00
|
|
|
fs.writeFileSync( testFilePath, buffer.join( eol ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 20:55:22 +00:00
|
|
|
|
|
|
|
// @todo: deprecated files.
|
2021-11-19 21:02:02 +00:00
|
|
|
}
|