add --ext and --format arguments
This commit is contained in:
parent
a21af3d3c6
commit
25cd08933c
|
@ -5,18 +5,15 @@ const allSpecs = require( './specs' );
|
||||||
const fs = require( 'fs' );
|
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 packageInstallFiles = {
|
||||||
const specs = fs.readFileSync( filename );
|
testSpecs: 'data/install-specs.json',
|
||||||
return JSON.parse( specs );
|
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 = {
|
module.exports = {
|
||||||
allSpecs,
|
allSpecs,
|
||||||
getTestInstallSpecs,
|
packageInstallFiles,
|
||||||
getSampleDefaultJson,
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -9,79 +9,91 @@ const sprintf = require( 'sprintf-js' ).sprintf;
|
||||||
/**
|
/**
|
||||||
* Internal dependencies.
|
* 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
|
* Install the test scripts and sample default.json configuration
|
||||||
*/
|
*/
|
||||||
if ( command == 'install' ) {
|
if ( command == 'install' ) {
|
||||||
/**
|
//**
|
||||||
const { getTestInstallSpecs, getSampleDefaultJson } = require( package );
|
const package = resolvePackage( packageName ).name;
|
||||||
|
if ( ! package.length ) {
|
||||||
if ( getTestInstallSpecs instanceof Function ) {
|
//@todo add error message
|
||||||
console.log( 'is a function' );
|
return;
|
||||||
}
|
}
|
||||||
if ( getSampleDefaultJson instanceof Function ) {
|
const packageSlug = package.replace( '@', '' ).replace( /\//g, '.' );
|
||||||
const sampleDefaultJson = getSampleDefaultJson();
|
const { packageInstallFiles } = require( package );
|
||||||
console.log( sampleDefaultJson );
|
const { testSpecs, defaultJson, initializeSh } = packageInstallFiles;
|
||||||
}
|
|
||||||
/**/
|
|
||||||
// @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"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
|
|
||||||
// Write sample default.json
|
// Write sample default.json
|
||||||
const packageSlug = package.replace( '@', '' ).replace( /\//g, '.' );
|
if ( defaultJson ) {
|
||||||
const defaultJsonName = `config/default-${packageSlug}.json`;
|
const defaultJsonName = `config/default-${packageSlug}.json`;
|
||||||
const defaultJsonSample = resolveLocalE2ePath( defaultJsonName );
|
const defaultJsonSample = resolveLocalE2ePath( defaultJsonName );
|
||||||
fs.writeFileSync( defaultJsonSample, JSON.stringify( sampleDefaultJson , null, 2 ) );
|
const packageJsonSample = resolvePackagePath( defaultJson, package );
|
||||||
console.log( `\nWrote sample test configuration to 'tests/e2e/${defaultJsonName}'.\n` );
|
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
|
// 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 eol = "\n";
|
||||||
const autoGenerate = sprintf( '/* This file was auto-generated by the command `npx wc-e2e install %s. */', package );
|
const autoGenerate = sprintf( '/* This file was auto-generated by the command `npx wc-e2e install %s. */', packageName );
|
||||||
const importLineFormat = sprintf( "import {%%s} from '%s';", package );
|
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.
|
// Loop through folders and files to write test scripts.
|
||||||
for ( let f = 0; f < testSpecs.active.length; f++ ) {
|
for ( let f = 0; f < active.length; f++ ) {
|
||||||
const testFolder = testSpecs.active[ f ];
|
const testFolder = active[ f ];
|
||||||
if ( ! testFolder.testFiles || ! testFolder.testFiles.length ) {
|
const { testFiles } = testFolder;
|
||||||
|
|
||||||
|
if ( ! testFiles || ! testFiles.length ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,13 +101,13 @@ if ( command == 'install' ) {
|
||||||
const specFolderPath = resolveLocalE2ePath( specFolder );
|
const specFolderPath = resolveLocalE2ePath( specFolder );
|
||||||
|
|
||||||
// Create the test folder if it doesn't exist.
|
// Create the test folder if it doesn't exist.
|
||||||
if ( ! fs.existsSync( specFolderPath) ) {
|
if ( ! fs.existsSync( specFolderPath ) ) {
|
||||||
fs.mkdirSync( specFolderPath );
|
fs.mkdirSync( specFolderPath );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the test files.
|
// Create the test files.
|
||||||
for ( let t = 0; t < testFolder.testFiles.length; t++ ) {
|
for ( let t = 0; t < testFiles.length; t++ ) {
|
||||||
const testFile = testFolder.testFiles[ t ];
|
const testFile = testFiles[ t ];
|
||||||
if ( ! testFile.functions.length ) {
|
if ( ! testFile.functions.length ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -134,4 +146,6 @@ if ( command == 'install' ) {
|
||||||
console.log( eol );
|
console.log( eol );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @todo: deprecated files.
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue