#!/usr/bin/env node /** * External dependencies. */ const fs = require( 'fs' ); const sprintf = require( 'sprintf-js' ).sprintf; /** * Internal dependencies. */ const { resolveLocalE2ePath } = require( '../utils' ); const [ command, package ] = process.argv.slice( 2 ); /** * 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' ); } 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" ] } ] } ] }; // Write sample default.json const packageSlug = package.replace( '@', '' ).replace( /\//g, '.' ); 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` ); // Write test files if ( testSpecs.active && testSpecs.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 ); // 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 ) { continue; } const specFolder = testFolder.name.length ? `specs/${testFolder.name}` : 'specs'; const specFolderPath = resolveLocalE2ePath( specFolder ); // Create the test folder if it doesn't exist. if ( ! fs.existsSync( specFolderPath) ) { fs.mkdirSync( specFolderPath ); } // Create the test files. for ( let t = 0; t < testFolder.testFiles.length; t++ ) { const testFile = testFolder.testFiles[ t ]; 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 = ', '; testTerminator = ''; importPrefix = ' '; } const testImport = testFile.functions.join( testSeparator ) + testTerminator; // '' adds a blank line buffer.push( sprintf( importLineFormat, importPrefix + testImport ), '' ); // Add test function calls and write the file let functionCalls = testFile.functions.map( functionName => functionName + '();' ); buffer.push( ...functionCalls ); fs.writeFileSync( testFilePath, buffer.join( eol ) ); } console.log( eol ); } } }