Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29eba08

Browse files
committed
refactor: prefer promises over test.cb
This is a followup to PR #1211 to use promises everywhere and migrate away from `test.cb()`. The `test.cb()` function is removed in a future version of ava. I made an attempt to convert the skipped shell.cmd() async test cases, but async functionality isn't implemented so it's possible the test conversion is not totally correct.
1 parent ffdd60b commit 29eba08

File tree

3 files changed

+76
-74
lines changed

3 files changed

+76
-74
lines changed

test/cmd.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -217,50 +217,49 @@ test('cmd returns a ShellString', t => {
217217
// async
218218
//
219219

220+
function cmdAsync(...commandArgs) {
221+
return new Promise((resolve) => {
222+
shell.cmd(...commandArgs, (code, stdout, stderr) => {
223+
resolve({ code, stdout, stderr });
224+
});
225+
});
226+
}
227+
220228
// TODO(nfischer): enable after we implement async.
221-
test.cb.skip('no callback', t => {
229+
test.skip('no callback', t => {
222230
const c = shell.cmd(shell.config.execPath, '-e', 'console.log(1234)', { async: true });
223231
t.falsy(shell.error());
224232
t.truthy('stdout' in c, 'async exec returns child process object');
225-
t.end();
226233
});
227234

228235
// TODO(nfischer): enable after we implement async.
229-
test.cb.skip('callback as 2nd argument', t => {
230-
shell.cmd(shell.config.execPath, '-e', 'console.log(5678);', (code, stdout, stderr) => {
231-
t.is(code, 0);
232-
t.is(stdout, '5678\n');
233-
t.is(stderr, '');
234-
t.end();
235-
});
236+
test.skip('callback as 2nd argument', async t => {
237+
const result = await cmdAsync(shell.config.execPath, '-e', 'console.log(5678);');
238+
t.is(result.code, 0);
239+
t.is(result.stdout, '5678\n');
240+
t.is(result.stderr, '');
236241
});
237242

238243
// TODO(nfischer): enable after we implement async.
239-
test.cb.skip('callback as end argument', t => {
240-
shell.cmd(shell.config.execPath, '-e', 'console.log(5566);', { async: true }, (code, stdout, stderr) => {
241-
t.is(code, 0);
242-
t.is(stdout, '5566\n');
243-
t.is(stderr, '');
244-
t.end();
245-
});
244+
test.skip('callback as end argument', async t => {
245+
const result = await cmdAsync(shell.config.execPath, '-e', 'console.log(5566);', { async: true });
246+
t.is(result.code, 0);
247+
t.is(result.stdout, '5566\n');
248+
t.is(result.stderr, '');
246249
});
247250

248251
// TODO(nfischer): enable after we implement async.
249-
test.cb.skip('callback as 3rd argument (silent:true)', t => {
250-
shell.cmd(shell.config.execPath, '-e', 'console.log(5678);', { silent: true }, (code, stdout, stderr) => {
251-
t.is(code, 0);
252-
t.is(stdout, '5678\n');
253-
t.is(stderr, '');
254-
t.end();
255-
});
252+
test.skip('callback as 3rd argument (silent:true)', async t => {
253+
const result = await cmdAsync(shell.config.execPath, '-e', 'console.log(5678);', { silent: true });
254+
t.is(result.code, 0);
255+
t.is(result.stdout, '5678\n');
256+
t.is(result.stderr, '');
256257
});
257258

258259
// TODO(nfischer): enable after we implement async.
259-
test.cb.skip('command that fails', t => {
260-
shell.cmd('shx', 'cp', 'onlyOneCpArgument.txt', { silent: true }, (code, stdout, stderr) => {
261-
t.is(code, 1);
262-
t.is(stdout, '');
263-
t.is(stderr, 'cp: missing <source> and/or <dest>\n');
264-
t.end();
265-
});
260+
test.skip('command that fails', async t => {
261+
const result = await cmdAsync('shx', 'cp', 'onlyOneCpArgument.txt', { silent: true });
262+
t.is(result.code, 1);
263+
t.is(result.stdout, '');
264+
t.is(result.stderr, 'cp: missing <source> and/or <dest>\n');
266265
});

test/exec.js

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -216,56 +216,53 @@ test('options.fatal = false and unknown command', t => {
216216
// async
217217
//
218218

219-
test.cb('no callback', t => {
219+
function execAsync(...execArgs) {
220+
return new Promise((resolve) => {
221+
shell.exec(...execArgs, (code, stdout, stderr) => {
222+
resolve({ code, stdout, stderr });
223+
});
224+
});
225+
}
226+
227+
test('no callback', t => {
220228
const c = shell.exec(`${JSON.stringify(shell.config.execPath)} -e "console.log(1234)"`, { async: true });
221229
t.falsy(shell.error());
222230
t.truthy('stdout' in c, 'async exec returns child process object');
223-
t.end();
224231
});
225232

226-
test.cb('callback as 2nd argument', t => {
227-
shell.exec(`${JSON.stringify(shell.config.execPath)} -e "console.log(5678);"`, (code, stdout, stderr) => {
228-
t.is(code, 0);
229-
t.is(stdout, '5678\n');
230-
t.is(stderr, '');
231-
t.end();
232-
});
233+
test('callback as 2nd argument', async t => {
234+
const result = await execAsync(`${JSON.stringify(shell.config.execPath)} -e "console.log(5678);"`);
235+
t.is(result.code, 0);
236+
t.is(result.stdout, '5678\n');
237+
t.is(result.stderr, '');
233238
});
234239

235-
test.cb('callback as end argument', t => {
236-
shell.exec(`${JSON.stringify(shell.config.execPath)} -e "console.log(5566);"`, { async: true }, (code, stdout, stderr) => {
237-
t.is(code, 0);
238-
t.is(stdout, '5566\n');
239-
t.is(stderr, '');
240-
t.end();
241-
});
240+
test('callback as end argument', async t => {
241+
const result = await execAsync(`${JSON.stringify(shell.config.execPath)} -e "console.log(5566);"`, { async: true });
242+
t.is(result.code, 0);
243+
t.is(result.stdout, '5566\n');
244+
t.is(result.stderr, '');
242245
});
243246

244-
test.cb('callback as 3rd argument (silent:true)', t => {
245-
shell.exec(`${JSON.stringify(shell.config.execPath)} -e "console.log(5678);"`, { silent: true }, (code, stdout, stderr) => {
246-
t.is(code, 0);
247-
t.is(stdout, '5678\n');
248-
t.is(stderr, '');
249-
t.end();
250-
});
247+
test('callback as 3rd argument (silent:true)', async t => {
248+
const result = await execAsync(`${JSON.stringify(shell.config.execPath)} -e "console.log(5678);"`, { silent: true });
249+
t.is(result.code, 0);
250+
t.is(result.stdout, '5678\n');
251+
t.is(result.stderr, '');
251252
});
252253

253-
test.cb('command that fails', t => {
254-
shell.exec('shx cp onlyOneCpArgument.txt', { silent: true }, (code, stdout, stderr) => {
255-
t.is(code, 1);
256-
t.is(stdout, '');
257-
t.is(stderr, 'cp: missing <source> and/or <dest>\n');
258-
t.end();
259-
});
254+
test('command that fails', async t => {
255+
const result = await execAsync('shx cp onlyOneCpArgument.txt', { silent: true });
256+
t.is(result.code, 1);
257+
t.is(result.stdout, '');
258+
t.is(result.stderr, 'cp: missing <source> and/or <dest>\n');
260259
});
261260

262-
test.cb('encoding option works with async', t => {
263-
shell.exec(`${JSON.stringify(shell.config.execPath)} -e "console.log(5566);"`, { async: true, encoding: 'buffer' }, (code, stdout, stderr) => {
264-
t.is(code, 0);
265-
t.truthy(Buffer.isBuffer(stdout));
266-
t.truthy(Buffer.isBuffer(stderr));
267-
t.is(stdout.toString(), '5566\n');
268-
t.is(stderr.toString(), '');
269-
t.end();
270-
});
261+
test('encoding option works with async', async t => {
262+
const result = await execAsync(`${JSON.stringify(shell.config.execPath)} -e "console.log(5566);"`, { async: true, encoding: 'buffer' });
263+
t.is(result.code, 0);
264+
t.truthy(Buffer.isBuffer(result.stdout));
265+
t.truthy(Buffer.isBuffer(result.stderr));
266+
t.is(result.stdout.toString(), '5566\n');
267+
t.is(result.stderr.toString(), '');
271268
});

test/pipe.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ test('Synchronous exec', t => {
6565
t.is(result.toString(), 'alphaaaaaaabeta\nalphbeta\n');
6666
});
6767

68-
test.cb('Asynchronous exec', t => {
69-
shell.cat('test/resources/grep/file').exec('shx grep "alpha*beta"', (code, stdout) => {
70-
t.is(code, 0);
71-
t.is(stdout, 'alphaaaaaaabeta\nalphbeta\n');
72-
t.end();
68+
test('Asynchronous exec', async t => {
69+
const promise = new Promise(resolve => {
70+
shell.cat('test/resources/grep/file')
71+
.exec('shx grep "alpha*beta"', (code, stdout, stderr) => {
72+
resolve({ code, stdout, stderr });
73+
});
7374
});
75+
76+
const result = await promise;
77+
t.is(result.code, 0);
78+
t.is(result.stdout, 'alphaaaaaaabeta\nalphbeta\n');
79+
t.is(result.stderr, '');
7480
});

0 commit comments

Comments
 (0)