File size: 3,010 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import { spawn } from 'node:child_process';
import chalk from 'chalk';
// This is technically an import side-effect for the callee, but any TeamCity
// suite using it will need the behavior.
// It attempts to fix an issue where log messages are truncated on process.exit
// due to poor behavior in NodeJS (which is that io streams don't flush on exit)
// See https://github.com/nodejs/node/issues/6379
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,
// This is used by the jest-teamcity reporter to let us nest test results in the log output.
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 }']` );
// Avoid logging empty blocks when there's no output:
// Essentially, any messages with a flowId will get nested inside the
// test suite. This happens with the jest-teamcity reporter, making it
// easy to nest Jest's test results. We put this inside a block without
// a flowId so that messages without a flowId can still be contained
// within the block. (Like stdout.)
console.log(
`##teamcity[testSuiteStarted name=' Tests for ${ testId }' flowId='${ testId }']`
);
if ( stdout.trim() ) {
// TeamCity will take the service messages with a flowId (like from
// the jest test reporter) and put them under the test suite block.
// Non-service-message output will not be nested, but would be shown
// below. If everything in stdout was a service message, it will
// appear empty here. So we add a message to indicate that.
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 );
} );
}
|