import * as Cucumber from 'cucumber' ; import * as path from 'path' ; import * as fs from 'fs-extra' ; import * as chai from 'chai' ; import { TestUtil } from '../test/TestUtil' ; chai . should ( ) ; chai . use ( require ( 'chai-as-promised' ) ) ; let jsonResult ; let cucumberType = '' ; param { string } featureSource return { Promise } async function runCucumberTest ( ) { const featurePath = path . resolve ( __dirname , '..' , '..' , 'cucumber' , 'features' ) ; let onlyRunTheseFiles = [ ] ; let featureFiles = fs . readdirSync ( featurePath ) ; featureFiles = featureFiles . filter ( ( file ) => { return file . includes ( '.feature' ) && ( onlyRunTheseFiles . length === 0 || onlyRunTheseFiles . includes ( file ) ) ; } ) ; const features = [ ] ; const otherFeatures = [ ] ; let tags = '' ; if ( process . env . OTHER_FABRIC ) { tags = '@otherFabric' ; cucumberType = 'other' ; } else if ( process . env . TWO_ORG_FABRIC ) { tags = '@twoOrgFabric' ; cucumberType = 'twoOrg' ; } else if ( process . env . OPSTOOLS_FABRIC ) { tags = '@opsToolsFabric' ; cucumberType = 'opsTools' ; } else { tags = 'not @otherFabric and not @opsToolsFabric and not @twoOrgFabric' ; cucumberType = 'oneOrg' ; } for ( const file of featureFiles ) { const featureSource = fs . readFileSync ( path . join ( featurePath , file ) , 'utf8' ) ; const feature = Cucumber . FeatureParser . parse ( { scenarioFilter : new Cucumber . ScenarioFilter ( { tagExpression : tags } ) , source : featureSource , uri : path . join ( featurePath , file ) } ) ; if ( file === 'fabric-environments.feature' ) { features [ 0 ] = feature ; } else if ( file === 'create.feature' ) { features [ 1 ] = feature ; } else if ( file === 'package.feature' ) { features [ 2 ] = feature ; } else if ( file === 'deploy.feature' ) { features [ 3 ] = feature ; } else if ( file === 'upgrade.feature' ) { features [ 4 ] = feature ; } else if ( file === 'submit.feature' ) { features [ 5 ] = feature ; } else if ( file === 'evaluate.feature' ) { features [ 6 ] = feature ; } else if ( file === 'wallet.feature' ) { features [ 7 ] = feature ; } else { otherFeatures . push ( feature ) ; } } features . push ( ... otherFeatures ) ; Cucumber . clearSupportCodeFns ( ) ; Cucumber . defineSupportCode ( ( context ) => { const stepdefs = require ( './steps/index' ) ; stepdefs . call ( context ) ; } ) ; const supportCodeLibrary = Cucumber . SupportCodeLibraryBuilder . build ( { cwd : '/' , fns : Cucumber . getSupportCodeFns ( ) } ) ; const jsonOptions = { colorsEnabled : true , cwd : '/' , log : ( data ) : any => { if ( data ) { jsonResult = data ; } } , supportCodeLibrary : supportCodeLibrary } ; const jsonFormatter = Cucumber . FormatterBuilder . build ( 'json' , jsonOptions ) ; const prettyOptions = { colorsEnabled : true , cwd : '/' , log : ( data ) : any => { console . log ( data ) ; } , supportCodeLibrary : supportCodeLibrary } ; const prettyFormatter = Cucumber . FormatterBuilder . build ( 'pretty' , prettyOptions ) ; const runtime = new Cucumber . Runtime ( { features : features . filter ( ( feature ) => ! ! feature ) , listeners : [ jsonFormatter , prettyFormatter ] , options : { strict : true } , supportCodeLibrary : supportCodeLibrary , } ) ; return runtime . start ( ) ; } export async function run ( testsRoot , clb ) { console . log ( testsRoot ) ; try { const result = await runCucumberTest ( ) ; await TestUtil . restoreAll ( ) ; await fs . remove ( path . join ( __dirname , '..' , '..' , '..' , 'cucumber' , 'tmp' , 'contracts' ) ) ; fs . ensureFileSync ( path . join ( __dirname , '..' , '..' , 'cucumber' , 'report' , `cucumber-report-${ cucumberType }.json` ) ) ; fs . writeFileSync ( path . join ( __dirname , '..' , '..' , 'cucumber' , 'report' , `cucumber-report-${ cucumberType }.json` ) , jsonResult ) ; if ( result ) { clb ( null , 0 ) ; } else { clb ( null , 1 ) ; } } catch ( error ) { console . log ( 'Error:' , error ) ; clb ( error ) ; } }