Skip to content

Commit

Permalink
support multi-line outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
mceachen committed Mar 5, 2019
1 parent dca94b9 commit 429da01
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ See [Semver](http://semver.org/).
- 🐞 Backwards-compatible bug fixes
- 📦 Minor packaging changes

## v5.2.1

- 🐞 `.pass` and `.fail` regex now support multiple line outputs per task.

## v5.2.0

- 🐞 [BatchProcessOptions](https://batch-cluster.js.org/classes/batchclusteroptions.html)`.pass`
Expand Down
22 changes: 22 additions & 0 deletions src/BatchCluster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,28 @@ describe("BatchCluster", function() {
)
})

it("accepts single and multi-line responses", async () => {
setFailrate(0)
const expected: string[] = []
const results = await Promise.all(
times(15, idx => {
// Make a distribution of single, double, and triple line outputs:
const worlds = times(idx % 3, ea => "world " + ea)
expected.push(
[idx + " HELLO", ...worlds]
.join("\n")
.toUpperCase()
)
const cmd = ["upcase " + idx + " hello", ...worlds].join(
"<br>"
)
return bc.enqueueTask(new Task(cmd, parser))
})
)
expect(results).to.eql(expected)
return
})

it("rejects a command that results in FAIL", async function() {
const task = new Task("invalid command", parser)
let error: Error | undefined
Expand Down
6 changes: 3 additions & 3 deletions src/BatchClusterOptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BatchProcessOptions } from "./BatchProcessOptions"
import { ChildProcessFactory } from "./BatchCluster"
import { BatchProcessOptions } from "./BatchProcessOptions"
import { InternalBatchProcessOptions } from "./InternalBatchProcessOptions"
import { toS, blank } from "./String"
import { blank, toS } from "./String"

/**
* These parameter values have somewhat sensible defaults, but can be
Expand Down Expand Up @@ -113,7 +113,7 @@ export type AllOpts = BatchClusterOptions &
function toRe(s: string | RegExp) {
return s instanceof RegExp
? s
: new RegExp("^(?:(.*)(?:\\r?\\n))?" + s + "(?:\\r?\\n)?$")
: new RegExp("^(?:([\\s\\S]*?)(?:\\r?\\n))?" + s + "(?:\\r?\\n)?$")
}

export function verifyOptions(
Expand Down
11 changes: 7 additions & 4 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const rng =
Math.random

async function onLine(line: string): Promise<void> {
// write(`# ${_p.pid} onLine(${line.trim()})`)
// write(`# ${_p.pid} onLine(${line.trim()}) (newline = ${_p.env.newline})`)
const r = rng()
if (r < failrate) {
if (_p.env.unluckyfail === "1") {
Expand All @@ -58,6 +58,9 @@ async function onLine(line: string): Promise<void> {
const tokens = line.split(/\s+/)
const firstToken = tokens.shift()

// support multi-line outputs:
const postToken = tokens.join(" ").split("<br>").join(newline)

try {
switch (firstToken) {
case "flaky":
Expand All @@ -81,12 +84,12 @@ async function onLine(line: string): Promise<void> {
break

case "upcase":
write(tokens.join(" ").toUpperCase())
write(postToken.toUpperCase())
write("PASS")
break

case "downcase":
write(tokens.join(" ").toLowerCase())
write(postToken.toLowerCase())
write("PASS")
break

Expand Down Expand Up @@ -115,7 +118,7 @@ async function onLine(line: string): Promise<void> {
// debouncing:
write("PASS")
await delay(1)
console.error("Error: " + tokens.join(" "))
console.error("Error: " + postToken)
break

default:
Expand Down

0 comments on commit 429da01

Please sign in to comment.