Skip to content

Commit 1372b6a

Browse files
committed
test: add unit test
1 parent 9927626 commit 1372b6a

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[![Coverage Status](https://img.shields.io/coveralls/kikobeats/tinyrun.svg?style=flat-square)](https://coveralls.io/github/kikobeats/tinyrun)
1010
[![NPM Status](https://img.shields.io/npm/dm/tinyrun.svg?style=flat-square)](https://www.npmjs.org/package/tinyrun)
1111

12-
**tinyrun** runs multiple commands in parallel with minimal footprint (~2KB).
12+
**tinyrun** executes multiple commands in parallel with minimal footprint (~2KB).
1313

1414
It can run one-off commands:
1515

bin/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ const exit = ({ exitCode, signalCode, duration }, task) => {
6868
)
6969
}
7070

71-
require('tinyrun')(tasks, {
71+
require('tinyrun')({
72+
tasks,
7273
stdout,
7374
stderr: toStream(process.stderr),
7475
start,
75-
exit
76+
exit,
77+
childOpts: { shell: true }
7678
})

src/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ const forwardSignals = subprocess =>
77
process.on(signal, () => subprocess.kill(signal))
88
})
99

10-
module.exports = (tasks, streams) =>
10+
module.exports = ({ tasks, childOpts, start, exit, ...pipes }) =>
1111
Promise.all(
1212
tasks.map(task => {
13-
const subprocess = require('tinyspawn')(task.cmd, { shell: true })
14-
streams.start(subprocess, task)
13+
const subprocess = require('tinyspawn')(task.cmd, childOpts)
14+
start(subprocess, task)
1515
subprocess.catch(() => {})
1616

1717
const duration = timeSpan()
1818

1919
;['stdout', 'stderr'].forEach(pipe =>
20-
subprocess[pipe].on('data', data => streams[pipe](data, task))
20+
subprocess[pipe].on('data', data => pipes[pipe](data, task))
2121
)
2222

23-
subprocess.on('exit', exitCode => {
24-
streams.exit(
23+
subprocess.once('exit', async exitCode => {
24+
exit(
2525
{ exitCode, signalCode: subprocess.signalCode, duration: duration() },
2626
task
2727
)

test/index.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,44 @@
11
'use strict'
22

3-
// const test = require('ava')
3+
const test = require('ava')
44

5-
// const tinyrun = require('tinyrun')
5+
const noop = () => {}
66

7-
// test('run multiple tasks', async () => {
8-
// const tasks = [
9-
// { name: 'task1', command: 'echo foo' },
10-
// { name: 'task2', command: 'echo bar' }
11-
// ]
7+
const tinyrun = require('tinyrun')
128

13-
// // const output = await tinyrun(tasks)
14-
// })
9+
test('run multiple tasks', async t => {
10+
const durations = []
11+
12+
const promises = await tinyrun({
13+
tasks: [
14+
{ name: 'task1', cmd: 'sleep 1 && echo foo' },
15+
{ name: 'task2', cmd: 'echo bar' }
16+
],
17+
stdout: noop,
18+
stderr: noop,
19+
start: noop,
20+
exit: ({ duration }, task) => durations.push({ name: task.name, duration }),
21+
childOpts: { shell: true }
22+
})
23+
24+
t.true(Array.isArray(promises))
25+
t.is(promises.length, 2)
26+
t.is(promises[0].name, 'task1')
27+
t.is(promises[1].name, 'task2')
28+
29+
t.true(promises[0].subprocess instanceof Promise)
30+
t.true(promises[1].subprocess instanceof Promise)
31+
32+
const resolved = await Promise.resolve(promises).then(async tasks => {
33+
return Promise.all(
34+
tasks.map(async task => {
35+
task.subprocess = await task.subprocess
36+
return task
37+
})
38+
)
39+
})
40+
41+
t.is(resolved[0].subprocess.stdout, 'foo')
42+
t.is(resolved[1].subprocess.stdout, 'bar')
43+
t.true(durations[1].duration >= 1000)
44+
})

0 commit comments

Comments
 (0)