-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Move test helpers into dev/ directory
- Delete test output directory after every test - Add test output directories to gitignore - Add npm-debug.log to gitignore template
- Loading branch information
Showing
7 changed files
with
139 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ target | |
index.node | ||
**/node_modules | ||
**/.DS_Store | ||
npm-debug.log* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { ChildProcess } from 'child_process'; | ||
import { PassThrough, Readable, Writable } from 'stream'; | ||
import { StringDecoder } from 'string_decoder'; | ||
|
||
function readChunks(input: Readable): Readable { | ||
let output = new PassThrough({ objectMode: true }); | ||
let decoder = new StringDecoder('utf8'); | ||
input.on('data', data => { | ||
output.write(decoder.write(data)); | ||
}); | ||
input.on('close', () => { | ||
output.write(decoder.end()); | ||
output.destroy(); | ||
}); | ||
return output; | ||
} | ||
|
||
function splitLines(s: string): string[] { | ||
return s.split(/([^\n]*\r?\n)/).filter(x => x); | ||
} | ||
|
||
function isCompleteLine(s: string): boolean { | ||
return s.endsWith('\n'); | ||
} | ||
|
||
class LinesBuffer { | ||
|
||
// INVARIANT: (this.buffer.length > 0) && | ||
// !isCompleteLine(this.buffer[this.buffer.length - 1]) | ||
// In other words, the last line in the buffer is always incomplete. | ||
private buffer: string[]; | ||
|
||
constructor() { | ||
this.buffer = [""]; | ||
} | ||
|
||
add(lines: string[]) { | ||
if (isCompleteLine(lines[lines.length - 1])) { | ||
lines.push(""); | ||
} | ||
this.buffer[this.buffer.length - 1] += lines.shift(); | ||
this.buffer = this.buffer.concat(lines); | ||
} | ||
|
||
find(p: (s: string) => boolean): string[] | null { | ||
let index = this.buffer.findIndex(p); | ||
if (index === -1) { | ||
return null; | ||
} | ||
let extracted = this.buffer.splice(0, index + 1); | ||
if (this.buffer.length === 0) { | ||
this.buffer.push(""); | ||
} | ||
return extracted; | ||
} | ||
} | ||
|
||
async function* run(script: Record<string, string>, stdin: Writable, stdout: Readable) { | ||
let lines = new LinesBuffer(); | ||
|
||
let keys = Object.keys(script); | ||
let i = 0; | ||
for await (let chunk of readChunks(stdout)) { | ||
lines.add(splitLines(chunk)); | ||
let found = lines.find(line => line.startsWith(keys[i])); | ||
if (found) { | ||
stdin.write(script[keys[i]] + "\n"); | ||
yield found; | ||
i++; | ||
if (i >= keys.length) { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function exit(child: ChildProcess): Promise<number | null> { | ||
let resolve: (code: number | null) => void; | ||
let result: Promise<number | null> = new Promise(res => { resolve = res; }); | ||
child.on('exit', code => { | ||
resolve(code); | ||
}); | ||
return result; | ||
} | ||
|
||
export default async function expect(child: ChildProcess, script: Record<string, string>): Promise<number | null> { | ||
for await (let _ of run(script, child.stdin!, child.stdout!)) { } | ||
|
||
return await exit(child); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
}, | ||
"include": [ | ||
"src/**/*", | ||
"dev/**/*", | ||
"test/**/*" | ||
], | ||
"exclude": [ | ||
|