|
|
import { spawn } from 'node:child_process'; |
|
|
import chalk from 'chalk'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for ( const stream of [ process.stdout, process.stderr ] ) { |
|
|
stream?._handle?.setBlocking?.( true ); |
|
|
} |
|
|
|
|
|
export default async function runTask( { name = 'yarn', args, env = {}, testId } ) { |
|
|
return new Promise( ( resolve, reject ) => { |
|
|
const startTime = Date.now(); |
|
|
console.log( `Spawning task: ${ name } ${ args }` ); |
|
|
const task = spawn( name, args.split( ' ' ), { |
|
|
shell: true, |
|
|
env: { |
|
|
...process.env, |
|
|
...env, |
|
|
|
|
|
TEAMCITY_FLOWID: testId, |
|
|
}, |
|
|
} ); |
|
|
|
|
|
let stdout = ''; |
|
|
let stderr = ''; |
|
|
task.stdout.on( 'data', ( data ) => { |
|
|
stdout += data; |
|
|
} ); |
|
|
task.stderr.on( 'data', ( data ) => { |
|
|
stderr += data; |
|
|
} ); |
|
|
|
|
|
task.on( 'close', ( exitCode ) => { |
|
|
const duration = Date.now() - startTime; |
|
|
|
|
|
const color = exitCode === 0 ? chalk.green : chalk.red; |
|
|
|
|
|
console.log( |
|
|
chalk.bold( |
|
|
color( |
|
|
`Task ${ testId } ${ exitCode === 0 ? 'succeeded' : 'failed' } after ${ duration }ms.` |
|
|
) |
|
|
) |
|
|
); |
|
|
|
|
|
console.log( `##teamcity[blockOpened name=' Output for ${ testId }']` ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log( |
|
|
`##teamcity[testSuiteStarted name=' Tests for ${ testId }' flowId='${ testId }']` |
|
|
); |
|
|
if ( stdout.trim() ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log( |
|
|
chalk.italic( 'If no output is shown, look for it under the test section for this task.' ) |
|
|
); |
|
|
console.log( '....STDOUT....' ); |
|
|
console.log( stdout ); |
|
|
} |
|
|
if ( stderr.trim() ) { |
|
|
console.log( '....STDERR....' ); |
|
|
console.log( stderr ); |
|
|
} |
|
|
console.log( |
|
|
`##teamcity[testSuiteFinished name=' Tests for ${ testId }' flowId='${ testId }']` |
|
|
); |
|
|
|
|
|
console.log( `##teamcity[blockClosed name=' Output for ${ testId }']` ); |
|
|
|
|
|
if ( exitCode === 0 ) { |
|
|
resolve(); |
|
|
} else { |
|
|
reject( exitCode ); |
|
|
} |
|
|
} ); |
|
|
task.on( 'error', reject ); |
|
|
} ); |
|
|
} |
|
|
|