'use strict' ; import * as vscode from 'vscode' ; import * as sinon from 'sinon' ; import * as path from 'path' ; import { UserInputUtilHelper } from './userInputUtilHelper' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { CommandUtil } from '../../extension/util/CommandUtil' ; import { SmartContractHelper , LanguageType } from './smartContractHelper' ; import { FabricGatewayRegistryEntry , FabricGatewayRegistry } from 'ibm-blockchain-platform-common' ; import { UserInputUtil } from '../../extension/commands/UserInputUtil' ; export class GeneratedTestsHelper { mySandBox : sinon . SinonSandbox ; userInputUtilHelper : UserInputUtilHelper ; smartContractHelper : SmartContractHelper ; constructor ( sandbox , userInputUtilHelper , smartContractHelper ) { this . mySandBox = sandbox ; this . userInputUtilHelper = userInputUtilHelper ; this . smartContractHelper = smartContractHelper ; } public async generateSmartContractTests ( name , version , language , gatewayName ) { let gatewayEntry ; try { gatewayEntry = await FabricGatewayRegistry . instance ( ) . get ( gatewayName ) ; } catch ( error ) { gatewayEntry = new FabricGatewayRegistryEntry ( ) ; gatewayEntry . name = gatewayName ; gatewayEntry . associatedWallet = 'Org1' ; } this . userInputUtilHelper . showGatewayQuickPickStub . resolves ( { label : gatewayName , data : gatewayEntry } ) ; this . userInputUtilHelper . showChannelStub . resolves ( 'mychannel' ) ; this . userInputUtilHelper . showClientInstantiatedSmartContractsStub . resolves ( { label : `${ name }@${ version }` , data : { name : name , channel : 'mychannel' , version : version } } ) ; this . userInputUtilHelper . showLanguagesQuickPickStub . resolves ( { label : language , type : LanguageType . CONTRACT } ) ; const contractDirectory = this . smartContractHelper . getContractDirectory ( name ) ; const workspaceFolder = this . getWorkspaceFolder ( name , contractDirectory ) ; this . userInputUtilHelper . getWorkspaceFoldersStub . returns ( [ workspaceFolder ] ) ; this . userInputUtilHelper . showYesNoQuickPick . withArgs ( 'Is this a private data contract?' ) . resolves ( UserInputUtil . NO ) ; this . userInputUtilHelper . showWorkspaceQuickPickBoxStub . withArgs ( 'Choose a workspace folder to create functional tests for' ) . resolves ( { label : `${ name }@${ version }` , data : workspaceFolder } ) ; const packageJSONPath = path . join ( contractDirectory , 'package.json' ) ; this . userInputUtilHelper . findFilesStub . resolves ( [ vscode . Uri . file ( packageJSONPath ) ] ) ; if ( language === 'Java' ) { this . userInputUtilHelper . showConfirmationWarningMessageStub . withArgs ( `This task might overwrite ${ path . join ( contractDirectory , 'build.gradle' ) }. Do you wish to continue?` ) . resolves ( true ) ; this . userInputUtilHelper . showWarningMessageStub . withArgs ( 'A build file was modified. Do you want to synchronize the Java classpath/configuration?' ) . resolves ( 'Now' ) ; } await vscode . commands . executeCommand ( ExtensionCommands . TEST_SMART_CONTRACT ) ; } public async runSmartContractTests ( name , testLanguage , contractAssetType ) { const contractDirectory = this . smartContractHelper . getContractDirectory ( name ) ; let fileExtension ; if ( testLanguage === 'JavaScript' ) { fileExtension = 'js' ; } else if ( testLanguage === 'TypeScript' ) { fileExtension = 'ts' ; } else if ( testLanguage === 'Java' ) { fileExtension = 'java' ; } else { return ; } let testResult ; let success = false ; let testCommand ; if ( testLanguage === 'Java' ) { const capsContractName = name [ 0 ] . toUpperCase ( ) + name . slice ( 1 ) ; const capsAssetType = contractAssetType [ 0 ] . toUpperCase ( ) + contractAssetType . slice ( 1 ) ; testCommand = `./gradlew test --tests org.example.Fv${ capsAssetType }Contract${ capsContractName }001Test*.submitCreate${ capsAssetType }Test` ; testResult = await CommandUtil . sendCommand ( testCommand , contractDirectory ) ; if ( testResult . includes ( 'BUILD SUCCESSFUL' ) ) { success = true ; } } else { testCommand = `node_modules/.bin/mocha ${ path . join ( contractDirectory , 'functionalTests' , contractAssetType ) }Contract-${ name }@0.0.1.test.${ fileExtension } --grep="create${ contractAssetType }"` ; if ( testLanguage === 'TypeScript' ) { testCommand += ` -r ts-node/register` ; } testResult = await CommandUtil . sendCommand ( testCommand , contractDirectory ) ; if ( testResult . includes ( '1 passing' ) ) { success = true ; } } return success ; } private getWorkspaceFolder ( name , contractDirectory ) { const workspaceFolder = { index : 0 , name : name , uri : vscode . Uri . file ( contractDirectory ) } ; return workspaceFolder ; } }