Skip to content

Commit

Permalink
TDD
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgo committed Nov 20, 2019
1 parent 4f8327c commit 5a0f63a
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 234 deletions.
26 changes: 12 additions & 14 deletions test/input-test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { strict as assert } from "assert"
import { startNodeProcess } from "./helpers/start-node-process"

describe("STDIN", function() {
it("allows entering text into the running process", async function() {
// start a process that reads from STDIN
const observable = startNodeProcess(
"process.stdin.on('data', data => { process.stdout.write(data.toString()) });\
test("ObservableProcess.stdin", async function() {
// start a process that reads from STDIN
const observable = startNodeProcess(
"process.stdin.on('data', data => { process.stdout.write(data.toString()) });\
process.stdin.on('end', () => { process.stdout.write('\\nEND') })"
)
)

// write some stuff into the STDIN stream of this process
observable.stdin.write("hello")
// write some stuff into the STDIN stream of this process
observable.stdin.write("hello")

// close the STDIN stream
observable.stdin.end()
// close the STDIN stream
observable.stdin.end()

// verify
await observable.waitForEnd()
assert.equal(observable.output.fullText(), "hello\nEND")
})
// verify
await observable.waitForEnd()
assert.equal(observable.output.fullText(), "hello\nEND")
})
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--require ts-node/register
--bail
--slow 0
--ui qunit
184 changes: 41 additions & 143 deletions test/output-test.ts
Original file line number Diff line number Diff line change
@@ -1,155 +1,53 @@
import { strict as assert } from "assert"
import { startNodeProcess } from "./helpers/start-node-process"

describe(".output", function() {
it("returns the accumulated output of the STDOUT and STDERR streams", async function() {
const observable = startNodeProcess(
'process.stdout.write("hello");\
process.stderr.write("world")'
)
await observable.waitForEnd()
assert.equal(observable.output.fullText(), "helloworld")
})

it("allows awaiting given text in the combined command output", async function() {
const observable = startNodeProcess(
'process.stdout.write("hello")\n\
process.stderr.write("world")'
)
const text = await observable.output.waitForText("helloworld")
assert.equal(text, "helloworld")
})

it("aborts the text search after the optional timeout has been reached", async function() {
const observable = startNodeProcess("setTimeout(function() {}, 3)")
const promise = observable.output.waitForText("hello", 1)
await assert.rejects(
promise,
new Error(
'Text "hello" not found within 1 ms. The captured text so far is:\n'
)
)
})

it("allows awaiting a regex in the combined command output", async function() {
const observable = startNodeProcess(
'process.stdout.write("hello")\n\
process.stderr.write("world")'
)
const text = await observable.output.waitForRegex(/h.+d/)
assert.equal(text, "helloworld")
})

it("aborts the regex search after the optional timeout has been reached", async function() {
const observable = startNodeProcess("setTimeout(function() {}, 3)")
const promise = observable.output.waitForRegex(/h.+d/, 1)
await assert.rejects(
promise,
new Error(
"Regex /h.+d/ not found within 1 ms. The captured text so far is:\n"
)
)
})
suite("ObservableProcess.output")

test("reading", async function() {
const observable = startNodeProcess(
'process.stdout.write("hello");\
process.stderr.write("world")'
)
await observable.waitForEnd()
assert.equal(observable.output.fullText(), "helloworld")
})

describe(".stdout", function() {
it("returns the accumulated output of the STDOUT stream", async function() {
const observable = startNodeProcess(
'process.stdout.write("hello");\
process.stderr.write("world")'
)
await observable.waitForEnd()
assert.equal(observable.stdout.fullText(), "hello")
})

it("allows awaiting given text in the STDOUT stream", async function() {
const observable = startNodeProcess(
'process.stderr.write("hello")\n\
process.stdout.write("world")'
)
const text = await observable.stdout.waitForText("world")
assert.equal(text, "world")
})

it("aborts the text search after the optional timeout has been reached", async function() {
const observable = startNodeProcess("setTimeout(function() {}, 3)")
const promise = observable.stdout.waitForText("hello", 1)
await assert.rejects(
promise,
new Error(
'Text "hello" not found within 1 ms. The captured text so far is:\n'
)
)
})

it("allows awaiting a given regex in the STDOUT stream", async function() {
const observable = startNodeProcess(
'process.stderr.write("hello")\n\
process.stdout.write("world")'
)
const text = await observable.stdout.waitForRegex(/w.+d/)
assert.equal(text, "world")
})

it("aborts the regex search after the optional timeout has been reached", async function() {
const observable = startNodeProcess("setTimeout(function() {}, 3)")
const promise = observable.stdout.waitForRegex(/w.+d/, 1)
await assert.rejects(
promise,
new Error(
"Regex /w.+d/ not found within 1 ms. The captured text so far is:\n"
)
)
})
test("waiting for text", async function() {
const observable = startNodeProcess(
'process.stdout.write("hello");\
process.stderr.write("world")'
)
const text = await observable.output.waitForText("helloworld")
assert.equal(text, "helloworld")
})

describe(".stderr", function() {
it("returns the accumulated output of the STDERR stream", async function() {
const observable = startNodeProcess(
'process.stdout.write("hello");\
process.stderr.write("world")'
)
await observable.waitForEnd()
assert.equal(observable.stderr.fullText(), "world")
})

it("allows awaiting given text in the STDERR stream", async function() {
const observable = startNodeProcess(
'process.stdout.write("hello")\n\
process.stderr.write("world")'
)
const text = await observable.stderr.waitForText("world")
assert.equal(text, "world")
})

it("aborts the text search after the optional timeout has been reached", async function() {
const observable = startNodeProcess("setTimeout(function() {}, 10)")
const promise = observable.stderr.waitForText("hello", 1)
await assert.rejects(
promise,
new Error(
'Text "hello" not found within 1 ms. The captured text so far is:\n'
)
test("waiting for text times out", async function() {
const observable = startNodeProcess("setTimeout(function() {}, 3)")
const promise = observable.output.waitForText("hello", 1)
await assert.rejects(
promise,
new Error(
'Text "hello" not found within 1 ms. The captured text so far is:\n'
)
})
)
})

it("allows awaiting a given regex in the STDERR stream", async function() {
const observable = startNodeProcess(
'process.stdout.write("hello")\n\
process.stderr.write("world")'
)
const text = await observable.stderr.waitForRegex(/w.+d/)
assert.equal(text, "world")
})
test("waiting for regex", async function() {
const observable = startNodeProcess(
'process.stdout.write("hello")\n\
process.stderr.write("world")'
)
const text = await observable.output.waitForRegex(/h.+d/)
assert.equal(text, "helloworld")
})

it("aborts the regex search after the optional timeout has been reached", async function() {
const observable = startNodeProcess("setTimeout(function() {}, 10)")
const promise = observable.stderr.waitForRegex(/w.+d/, 1)
await assert.rejects(
promise,
new Error(
"Regex /w.+d/ not found within 1 ms. The captured text so far is:\n"
)
test("waiting for regex times out", async function() {
const observable = startNodeProcess("setTimeout(function() {}, 3)")
const promise = observable.output.waitForRegex(/h.+d/, 1)
await assert.rejects(
promise,
new Error(
"Regex /h.+d/ not found within 1 ms. The captured text so far is:\n"
)
})
)
})
16 changes: 7 additions & 9 deletions test/pid-test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { startNodeProcess } from "./helpers/start-node-process"
import { strict as assert } from "assert"
import { startNodeProcess } from "./helpers/start-node-process"

describe(".pid()", function() {
it("returns the process id", async function() {
const oProcess = startNodeProcess("setTimeout(function() {}, 1)")
const pid = oProcess.pid()
assert.equal(typeof pid, "number")
assert.ok(pid > 0)
await oProcess.waitForEnd()
})
test("ObservableProcess.pid()", async function() {
const oProcess = startNodeProcess("setTimeout(function() {}, 1)")
const pid = oProcess.pid()
assert.equal(typeof pid, "number")
assert.ok(pid > 0)
await oProcess.waitForEnd()
})
84 changes: 41 additions & 43 deletions test/start-test.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,51 @@
import { strict as assert } from "assert"
import { createObservableProcess } from "../src/observable-process"

describe(".spawn()", function() {
it("starts a process via an argv array", async function() {
const observable = createObservableProcess([
"node",
"-e",
"console.log('hello')"
])
await observable.waitForEnd()
assert.equal(observable.exitCode, 0)
})
suite("ObservableProcess.spawn()")

it("starts a process via a string", async function() {
const observable = createObservableProcess("node -e console.log('hello')")
await observable.waitForEnd()
assert.equal(observable.exitCode, 0)
})
test("starting a process via an argv array", async function() {
const observable = createObservableProcess([
"node",
"-e",
"console.log('hello')"
])
await observable.waitForEnd()
assert.equal(observable.exitCode, 0)
})

it("starts processes in the path", async function() {
const observable = createObservableProcess("node -h")
await observable.waitForEnd()
assert.equal(observable.exitCode, 0)
})
test("starting a process via a string", async function() {
const observable = createObservableProcess("node -e console.log('hello')")
await observable.waitForEnd()
assert.equal(observable.exitCode, 0)
})

it("throws if it receives no command to run", function() {
assert.throws(function() {
// @ts-ignore
createObservableProcess()
}, new Error("createObservableProcess: no command to execute given"))
})
test("starting processes in the path", async function() {
const observable = createObservableProcess("node -h")
await observable.waitForEnd()
assert.equal(observable.exitCode, 0)
})

test("no command to run", function() {
assert.throws(function() {
// @ts-ignore
createObservableProcess()
}, new Error("createObservableProcess: no command to execute given"))
})

it("throws if it receives neither a string nor argv array", function() {
assert.throws(function() {
// @ts-ignore
createObservableProcess(1)
}, new Error(
"observable.spawn: you must provide the command to run as a string or string[]"
))
})
test("wrong argument type", function() {
assert.throws(function() {
// @ts-ignore
createObservableProcess(1)
}, new Error(
"observable.spawn: you must provide the command to run as a string or string[]"
))
})

describe("environment variables", function() {
it("allows to provide custom environment variables for running processes", async function() {
const observable = createObservableProcess(
["node", "-e", "console.log('foo:', process.env.foo)"],
{ env: { foo: "bar", PATH: process.env.PATH } }
)
await observable.waitForEnd()
assert.equal(observable.output.fullText(), "foo: bar\n")
})
test("providing environment variables", async function() {
const observable = createObservableProcess(
["node", "-e", "console.log('foo:', process.env.foo)"],
{ env: { foo: "bar", PATH: process.env.PATH } }
)
await observable.waitForEnd()
assert.equal(observable.output.fullText(), "foo: bar\n")
})
Loading

0 comments on commit 5a0f63a

Please sign in to comment.