Skip to content

Commit

Permalink
test: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Aug 1, 2024
1 parent 9927626 commit 1372b6a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[![Coverage Status](https://img.shields.io/coveralls/kikobeats/tinyrun.svg?style=flat-square)](https://coveralls.io/github/kikobeats/tinyrun)
[![NPM Status](https://img.shields.io/npm/dm/tinyrun.svg?style=flat-square)](https://www.npmjs.org/package/tinyrun)

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

It can run one-off commands:

Expand Down
6 changes: 4 additions & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ const exit = ({ exitCode, signalCode, duration }, task) => {
)
}

require('tinyrun')(tasks, {
require('tinyrun')({
tasks,
stdout,
stderr: toStream(process.stderr),
start,
exit
exit,
childOpts: { shell: true }
})
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ const forwardSignals = subprocess =>
process.on(signal, () => subprocess.kill(signal))
})

module.exports = (tasks, streams) =>
module.exports = ({ tasks, childOpts, start, exit, ...pipes }) =>
Promise.all(
tasks.map(task => {
const subprocess = require('tinyspawn')(task.cmd, { shell: true })
streams.start(subprocess, task)
const subprocess = require('tinyspawn')(task.cmd, childOpts)
start(subprocess, task)
subprocess.catch(() => {})

const duration = timeSpan()

;['stdout', 'stderr'].forEach(pipe =>
subprocess[pipe].on('data', data => streams[pipe](data, task))
subprocess[pipe].on('data', data => pipes[pipe](data, task))
)

subprocess.on('exit', exitCode => {
streams.exit(
subprocess.once('exit', async exitCode => {
exit(
{ exitCode, signalCode: subprocess.signalCode, duration: duration() },
task
)
Expand Down
48 changes: 39 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
'use strict'

// const test = require('ava')
const test = require('ava')

// const tinyrun = require('tinyrun')
const noop = () => {}

// test('run multiple tasks', async () => {
// const tasks = [
// { name: 'task1', command: 'echo foo' },
// { name: 'task2', command: 'echo bar' }
// ]
const tinyrun = require('tinyrun')

// // const output = await tinyrun(tasks)
// })
test('run multiple tasks', async t => {
const durations = []

const promises = await tinyrun({
tasks: [
{ name: 'task1', cmd: 'sleep 1 && echo foo' },
{ name: 'task2', cmd: 'echo bar' }
],
stdout: noop,
stderr: noop,
start: noop,
exit: ({ duration }, task) => durations.push({ name: task.name, duration }),
childOpts: { shell: true }
})

t.true(Array.isArray(promises))
t.is(promises.length, 2)
t.is(promises[0].name, 'task1')
t.is(promises[1].name, 'task2')

t.true(promises[0].subprocess instanceof Promise)
t.true(promises[1].subprocess instanceof Promise)

const resolved = await Promise.resolve(promises).then(async tasks => {
return Promise.all(
tasks.map(async task => {
task.subprocess = await task.subprocess
return task
})
)
})

t.is(resolved[0].subprocess.stdout, 'foo')
t.is(resolved[1].subprocess.stdout, 'bar')
t.true(durations[1].duration >= 1000)
})

0 comments on commit 1372b6a

Please sign in to comment.