add --ext and --format arguments

This commit is contained in:
Ron Rennick 2021-11-26 16:55:22 -04:00
parent a21af3d3c6
commit 25cd08933c
2 changed files with 83 additions and 72 deletions

View File

@ -5,18 +5,15 @@ const allSpecs = require( './specs' );
const fs = require( 'fs' );
/**
* Read test set up configuration for the test scaffolding tool.
* Test set up configuration for the test scaffolding tool.
*/
const getObjectFromJsonFile = ( filename ) => {
const specs = fs.readFileSync( filename );
return JSON.parse( specs );
const packageInstallFiles = {
testSpecs: 'data/install-specs.json',
defaultJson: 'data/default-test-config.json',
initializeSh: 'data/initialize.sh.default',
};
const getTestInstallSpecs = () => getObjectFromJsonFile( './data/install-specs.json' );
const getSampleDefaultJson = () => getObjectFromJsonFile( './data/default-test-config.json' );
module.exports = {
allSpecs,
getTestInstallSpecs,
getSampleDefaultJson,
packageInstallFiles,
};

View File

@ -9,79 +9,91 @@ const sprintf = require( 'sprintf-js' ).sprintf;
/**
* Internal dependencies.
*/
const { resolveLocalE2ePath } = require( '../utils' );
const {
resolveLocalE2ePath,
resolvePackage,
resolvePackagePath,
} = require( '../utils' );
const [ command, package ] = process.argv.slice( 2 );
const args = process.argv.slice( 2 );
const [ command, packageName ] = args;
/**
* Install the test scripts and sample default.json configuration
*/
if ( command == 'install' ) {
/**
const { getTestInstallSpecs, getSampleDefaultJson } = require( package );
if ( getTestInstallSpecs instanceof Function ) {
console.log( 'is a function' );
//**
const package = resolvePackage( packageName ).name;
if ( ! package.length ) {
//@todo add error message
return;
}
if ( getSampleDefaultJson instanceof Function ) {
const sampleDefaultJson = getSampleDefaultJson();
console.log( sampleDefaultJson );
}
/**/
// @todo Add logic for dynamic file extension
const testExtension = 'test.js';
const sampleDefaultJson = {
"url": "http://localhost:8084/",
"users": {
"admin": {
"username": "admin",
"password": "password"
},
"customer": {
"username": "customer",
"password": "password"
}
},
};
const testSpecs = {
"active": [
{
"name": "example",
"description": "Shopper tests",
"testFiles": [
{
"name": "cart-begin",
"functions": [ "runCartPageTest" ]
}, {
"name": "cart-end",
"functions": [
"testCartEnd",
"testCartEndPart2",
"testCartEndPart3",
"testCartEndPart4"
]
}
]
}
]
};
const packageSlug = package.replace( '@', '' ).replace( /\//g, '.' );
const { packageInstallFiles } = require( package );
const { testSpecs, defaultJson, initializeSh } = packageInstallFiles;
// Write sample default.json
const packageSlug = package.replace( '@', '' ).replace( /\//g, '.' );
if ( defaultJson ) {
const defaultJsonName = `config/default-${packageSlug}.json`;
const defaultJsonSample = resolveLocalE2ePath( defaultJsonName );
fs.writeFileSync( defaultJsonSample, JSON.stringify( sampleDefaultJson , null, 2 ) );
console.log( `\nWrote sample test configuration to 'tests/e2e/${defaultJsonName}'.\n` );
const packageJsonSample = resolvePackagePath( defaultJson, package );
fs.copyFileSync( packageJsonSample, defaultJsonSample );
console.log( `\nCreated sample test configuration to 'tests/e2e/${defaultJsonName}'.\n` );
}
// Write sample initialize.sh
if ( initializeSh ) {
const defaultInitName = `docker/${packageSlug}.sh`;
const defaultInitPath = resolveLocalE2ePath( defaultInitName );
const packageInitPath = resolvePackagePath( initializeSh, package );
fs.copyFileSync( packageInitPath, defaultInitPath );
console.log( `\nCreated sample test container initialization script to 'tests/e2e/${defaultInitName}'.\n` );
}
// Write test files
if ( testSpecs.active && testSpecs.active.length ) {
if ( ! testSpecs ) {
return;
}
const testsSpecFile = resolvePackagePath( testSpecs, package );
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 ) {
const eol = "\n";
const autoGenerate = sprintf( '/* This file was auto-generated by the command `npx wc-e2e install %s. */', package );
const importLineFormat = sprintf( "import {%%s} from '%s';", package );
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 );
} else {
importLineFormat = sprintf( "import {%%s} from '%s';", package );
}
// Loop through folders and files to write test scripts.
for ( let f = 0; f < testSpecs.active.length; f++ ) {
const testFolder = testSpecs.active[ f ];
if ( ! testFolder.testFiles || ! testFolder.testFiles.length ) {
for ( let f = 0; f < active.length; f++ ) {
const testFolder = active[ f ];
const { testFiles } = testFolder;
if ( ! testFiles || ! testFiles.length ) {
continue;
}
@ -94,8 +106,8 @@ if ( command == 'install' ) {
}
// Create the test files.
for ( let t = 0; t < testFolder.testFiles.length; t++ ) {
const testFile = testFolder.testFiles[ t ];
for ( let t = 0; t < testFiles.length; t++ ) {
const testFile = testFiles[ t ];
if ( ! testFile.functions.length ) {
continue;
}
@ -134,4 +146,6 @@ if ( command == 'install' ) {
console.log( eol );
}
}
// @todo: deprecated files.
}