Skip to content

Commit c6d2fec

Browse files
committed
feat: add debug flag to command acceptance test
1 parent 985cffb commit c6d2fec

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

lib/test/command-acceptance.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export interface SpawnOptions {
2929
*/
3030
export default class CommandAcceptanceTest {
3131

32+
/**
33+
* Debug mode
34+
*/
35+
debug: string | boolean;
36+
3237
/**
3338
* The command to invoke, i.e. 'build' would test the invocation of '$ denali
3439
* build'
@@ -85,8 +90,12 @@ export default class CommandAcceptanceTest {
8590
* happening in a CommandAcceptanceTest
8691
* @param options.populateWithDummy Should the test directory be populated
8792
* with a copy of the dummy app?
93+
* @param options.debug If true, DEBUG=* will bet set for the spawned command,
94+
* and the output of the command will be streamed to the console. You can
95+
* also supply a string which will be used as the value of DEBUG to reduce
96+
* the noise.
8897
*/
89-
constructor(command: string, options: { dir?: string, environment?: string, name?: string, populateWithDummy?: boolean } = {}) {
98+
constructor(command: string, options: { dir?: string, environment?: string, name?: string, populateWithDummy?: boolean, debug?: boolean | string } = {}) {
9099
this.command = command;
91100
this.dir = options.dir || (<any>tmp.dirSync({
92101
dir: 'tmp',
@@ -96,6 +105,7 @@ export default class CommandAcceptanceTest {
96105
unsafeCleanup: !process.env.DENALI_LEAVE_TMP,
97106
prefix: `command-acceptance-test-${ options.name || command }-`
98107
})).name;
108+
this.debug = options.debug;
99109
this.environment = options.environment || 'development';
100110
this.projectRoot = process.cwd();
101111
// We don't use node_modules/.bin/denali because if denali-cli is linked in
@@ -183,10 +193,12 @@ export default class CommandAcceptanceTest {
183193
async spawn(options: SpawnOptions & { checkOutput: false | ((stdout: string, stderr: string, dir: string) => boolean) }) {
184194
return <any>new Promise((resolve, reject) => {
185195

196+
let env: { [key: string]: string } = { NODE_ENV: this.environment };
197+
if (this.debug) {
198+
env.DEBUG = this.debug === true ? '*' : this.debug;
199+
}
186200
this.spawnedCommand = spawn(this.denaliPath, this.command.split(' '), {
187-
env: Object.assign({}, process.env, {
188-
NODE_ENV: this.environment
189-
}, options.env || {}),
201+
env: Object.assign({}, process.env, env, options.env || {}),
190202
cwd: this.dir,
191203
stdio: 'pipe'
192204
});
@@ -203,11 +215,17 @@ export default class CommandAcceptanceTest {
203215
let output = d.toString();
204216
stdoutBuffer += output;
205217
combinedBuffer += output;
218+
if (this.debug) {
219+
process.stdout.write(output);
220+
}
206221
});
207222
this.spawnedCommand.stderr.on('data', (d) => {
208223
let output = d.toString();
209224
stderrBuffer += output;
210225
combinedBuffer += output;
226+
if (this.debug) {
227+
process.stderr.write(output);
228+
}
211229
if (options.failOnStderr && stderrBuffer.match(/[A-z0-9]/)) {
212230
process.removeListener('exit', cleanup);
213231
this.cleanup();

0 commit comments

Comments
 (0)