11import cp = require( 'child_process' ) ;
22import fs = require( 'fs' ) ;
3- import ncp = require( 'child_process' ) ;
43import os = require( 'os' ) ;
54import path = require( 'path' ) ;
65import cmdm = require( './taskcommand' ) ;
76import shelljs = require( 'shelljs' ) ;
8- import deasync = require( 'deasync' )
97const Downloader = require ( "nodejs-file-downloader" ) ;
108
119const COMMAND_TAG = '[command]' ;
@@ -20,7 +18,6 @@ export class MockTestRunner {
2018
2119 this . _taskJsonPath = taskJsonPath || '' ;
2220 this . _testPath = testPath ;
23- this . nodePath = this . getNodePathSync ( ) ;
2421 }
2522
2623 private _testPath = '' ;
@@ -34,14 +31,20 @@ export class MockTestRunner {
3431 public errorIssues : string [ ] = [ ] ;
3532 public warningIssues : string [ ] = [ ] ;
3633
37- public async LoadAsync ( testPath : string , taskJsonPath ?: string ) : Promise < MockTestRunner > {
34+ public async LoadAsync ( testPath ? : string , taskJsonPath ?: string ) : Promise < MockTestRunner > {
3835 return new Promise ( async ( resolve , reject ) => {
3936 if ( this . nodePath != '' ) {
4037 resolve ( this ) ;
4138 return ;
4239 }
43- this . _taskJsonPath = taskJsonPath || '' ;
44- this . _testPath = testPath ;
40+ if ( testPath ) {
41+ this . _testPath = testPath ;
42+ }
43+
44+ if ( taskJsonPath ) {
45+ this . _taskJsonPath = taskJsonPath ;
46+ }
47+
4548 this . nodePath = await this . getNodePath ( ) ;
4649 resolve ( this )
4750 } )
@@ -82,7 +85,11 @@ export class MockTestRunner {
8285 let nodePath = this . nodePath ;
8386 if ( nodeVersion ) {
8487 nodePath = await this . getNodePath ( nodeVersion ) ;
88+ } else if ( ! nodePath ) {
89+ nodePath = await this . getNodePath ( ) ;
90+ this . nodePath = nodePath ;
8591 }
92+
8693 let spawn = cp . spawnSync ( nodePath , [ this . _testPath ] ) ;
8794
8895 // Clean environment
@@ -155,15 +162,6 @@ export class MockTestRunner {
155162 }
156163 }
157164
158- /**
159- * @deprecated This method uses library which is not prefered to use on production
160- */
161- public run ( nodeVersion ?: number ) : void {
162- let completeExecution = false ;
163- this . runAsync ( nodeVersion ) . then ( t => completeExecution = true )
164- deasync . loopWhile ( function ( ) { return ! completeExecution } )
165- }
166-
167165 // Returns a path to node.exe with the correct version for this task (based on if its node10 or node)
168166 private async getNodePath ( nodeVersion ?: number ) : Promise < string > {
169167 const version : number = nodeVersion || this . getNodeVersion ( ) ;
@@ -195,13 +193,6 @@ export class MockTestRunner {
195193 }
196194 }
197195
198- private getNodePathSync ( nodeVersion ?: number ) : string {
199- let nodePath = ''
200- this . getNodePath ( nodeVersion ) . then ( t => nodePath = t ) ;
201- deasync . loopWhile ( function ( ) { return nodePath == '' ; } ) ;
202- return nodePath
203- }
204-
205196 // Determines the correct version of node to use based on the contents of the task's task.json. Defaults to Node 16.
206197 private getNodeVersion ( ) : number {
207198 const taskJsonPath : string = this . getTaskJsonPath ( ) ;
@@ -321,7 +312,7 @@ export class MockTestRunner {
321312 const originalCwd : string = process . cwd ( ) ;
322313 process . chdir ( downloadDestination ) ;
323314 try {
324- ncp . execSync ( `tar -xzf "${ path . join ( downloadDestination , tarGzName ) } "` ) ;
315+ cp . execSync ( `tar -xzf "${ path . join ( downloadDestination , tarGzName ) } "` ) ;
325316 }
326317 catch {
327318 throw new Error ( 'Failed to unzip node tar.gz from ' + url ) ;
0 commit comments