import { existsSync , mkdirSync , rmdirSync } from 'fs' ; import packageJson from 'package-json' ; import { logger } from 'flex-dev-utils' ; import { homeDir , TestParams , TestScenario , TestSuite , testSuites } from '.' ; import { api } from '../utils' ; param step param params const runTest = async ( step , params ) : Promise < void > => { const stepStr = '0' . repeat ( Math . max ( 0 , 3 - String ( step ) . length ) ) + String ( step ) ; const testFile = `step${ stepStr }` ; const testSuite = require ( `${ __dirname }/../tests/${ testFile }` ) . default as TestSuite ; logger . info ( `Step ${ stepStr } - ${ testSuite . description } ..[${ new Date ( ) . toISOString ( ) }]..` ) ; if ( testSuite . before ) { await testSuite . before ( params ) ; } await testSuite ( params ) ; if ( testSuite . after ) { await testSuite . after ( params ) ; } } ; param params const printParameters = ( params ) => { logger . info ( `Running Plugins E2E Test with parameters:` ) ; Object . keys ( params ) . filter ( ( k ) => ! params [ k ] . __hidden ) . forEach ( ( k ) => { const p = { ... params [ k ] } ; delete p . __hidden ; logger . info ( `- ${ k }: ${ JSON . stringify ( p , null , 2 ) }` ) ; } ) ; } ; /**
 * Converts the --step arg to steps
 */ const getArgs = ( flag ) : string [ ] => { const _get = ( ... argv ) : string [ ] => { const index = argv . indexOf ( flag ) ; if ( index === - 1 ) { return [ ] ; } const arg = argv [ index + 1 ] ; return [ arg , ... _get ( ... argv . splice ( index + 1 ) ) ] ; } ; return _get ( ... process . argv ) ; } ; param testParams the  TestParams to use
  const beforeAll = async ( testParams ) => { if ( testParams . scenario . packageVersion === 'latest' ) { const pkg = await packageJson ( 'flex-plugin-scripts' , { version : 'latest' } ) ; testParams . scenario . packageVersion = pkg . version as string ; } } ; /**
 * Runs before the test
 */ const beforeEach = async ( ) => { if ( existsSync ( homeDir ) ) { rmdirSync ( homeDir , { recursive : true } ) ; } mkdirSync ( homeDir ) ; await api . cleanup ( ) ; } ; param testParams the  TestParams param testScenarios the  TestScenario const runAll = async ( testParams , testScenarios ) : Promise < void > => { for ( const testScenario of testScenarios ) { const params = { ... testParams } ; params . scenario = { ... params . scenario , ... testScenario } ; printParameters ( params ) ; await beforeEach ( ) ; for ( let i = 0 ; i < testSuites . length ; i ++ ) { await runTest ( i + 1 , params ) ; } } } ; param testParams the  TestParams const runSelected = async ( testParams ) : Promise < void > => { const steps = getArgs ( '--step' ) ; const scenarios = getArgs ( '--scenario' ) ; const params = { ... testParams } ; for ( const testScenario of scenarios ) { params . scenario = { ... params . scenario , ... JSON . parse ( testScenario ) } ; } printParameters ( params ) ; for ( let i = 0 ; i < steps . length ; i ++ ) { await runTest ( parseInt ( steps [ i ] , 10 ) , { ... params } ) ; } } ; param testParams the  TestParams to use
 *  param testScenarios the  TestScenario to test against
  const runner = async ( testParams , testScenarios ) : Promise < void > => { const _testParams = { ... testParams } ; const _testScenario = [ ... testScenarios ] ; await beforeAll ( _testParams ) ; if ( ! process . argv . includes ( '--step' ) ) { await runAll ( _testParams , _testScenario ) ; return ; } await runSelected ( _testParams ) ; } ; export default runner ;