first-base 1.5.2 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.node-version +1 -1
- package/.npm-version +1 -1
- package/README.md +175 -142
- package/dist/find-root-dir.js +3 -4
- package/dist/index.d.ts +73 -66
- package/dist/index.js +5 -1
- package/dist/index.js.flow +42 -40
- package/package.json +38 -38
package/.node-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
v24.13.0
|
package/.npm-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
11.6.2
|
package/README.md
CHANGED
|
@@ -1,142 +1,175 @@
|
|
|
1
|
-
# `first-base`
|
|
2
|
-
|
|
3
|
-
Integration testing for CLI applications.
|
|
4
|
-
|
|
5
|
-
## Usage Example
|
|
6
|
-
|
|
7
|
-
```js
|
|
8
|
-
const { spawn } = require("first-base");
|
|
9
|
-
|
|
10
|
-
test("something", async () => {
|
|
11
|
-
const run = spawn("node", ["-i"]); // launch node REPL
|
|
12
|
-
await run.outputContains("> "); // wait until `> ` is logged
|
|
13
|
-
run.write("2 + 2\n"); // enter `2 + 2` and press enter
|
|
14
|
-
await run.outputContains("4"); // wait until `4` is logged
|
|
15
|
-
run.kill(); // Ctrl+C
|
|
16
|
-
await run.completion; // Wait until process exits
|
|
17
|
-
});
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
## API
|
|
21
|
-
|
|
22
|
-
### `spawn(command: string, args?: Array<string>, options?: Object) => RunContext`
|
|
23
|
-
|
|
24
|
-
`args` and `options` are the same as [child_process.spawn](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options).
|
|
25
|
-
|
|
26
|
-
Returns a `RunContext` object; see below.
|
|
27
|
-
|
|
28
|
-
### `RunContext#result`
|
|
29
|
-
|
|
30
|
-
An object with the following properties on it:
|
|
31
|
-
|
|
32
|
-
- `stdout` (`string`): All the data the process has written to STDOUT so far
|
|
33
|
-
- `stderr` (`string`): All the data the process has written to STDERR so far
|
|
34
|
-
- `code` (`number | null`): Exit status code, if the process has finished
|
|
35
|
-
- `error` (`boolean`): If the process errored out, this is `true`
|
|
36
|
-
|
|
37
|
-
This object gets updated over time as the process runs.
|
|
38
|
-
|
|
39
|
-
#### Usage
|
|
40
|
-
|
|
41
|
-
```js
|
|
42
|
-
const run = spawn("ls", { cwd: __dirname });
|
|
43
|
-
console.log(run.result); // { stdout: '', stderr: '', code: null, error: false }
|
|
44
|
-
await run.completion;
|
|
45
|
-
console.log(run.result); // { stdout: 'README.md\npackage.json\nindex.js\n', stderr: '', code: 0, error: false }
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
### `RunContext#completion`
|
|
49
|
-
|
|
50
|
-
A Promise that gets resolved when the process completes. You can `await` it in your tests.
|
|
51
|
-
|
|
52
|
-
#### Usage
|
|
53
|
-
|
|
54
|
-
```js
|
|
55
|
-
const run = spawn("ls", { cwd: __dirname });
|
|
56
|
-
await run.completion; // Waits until the `ls` process finishes
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
### `RunContext#debug() => RunContext`
|
|
60
|
-
|
|
61
|
-
Enables debug logging for the `RunContext` and returns it. Useful when your tests are failing and you want to understand what's going on.
|
|
62
|
-
|
|
63
|
-
Returns itself so you can add it to a variable declaration easily.
|
|
64
|
-
|
|
65
|
-
#### Usage
|
|
66
|
-
|
|
67
|
-
```js
|
|
68
|
-
const run = spawn("ls", { cwd: __dirname }).debug();
|
|
69
|
-
// The following messages are logged to the console over time:
|
|
70
|
-
//
|
|
71
|
-
// STDOUT: README.md\npackage.json\nindex.js
|
|
72
|
-
// Process exited
|
|
73
|
-
// { stdout: 'README.md\npackage.json\nindex.js', stderr: '', code: 0, error: false }
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
### `RunContext#outputContains(value: string | RegExp) => Promise<void>`
|
|
77
|
-
|
|
78
|
-
Returns a Promise that will resolve once the process's output (combined STDOUT/STDERR) contains either the specified string or matches the specified RegExp. Ignores ANSI control characters.
|
|
79
|
-
|
|
80
|
-
#### Usage
|
|
81
|
-
|
|
82
|
-
```js
|
|
83
|
-
const run = spawn("node", ["-i"]); // start Node.js REPL
|
|
84
|
-
await run.outputContains("> "); // Wait until prompt appears
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
### `RunContext#clearOutputContainsBuffer()`
|
|
88
|
-
|
|
89
|
-
Clears the buffer of "seen" output as far as the `outputContains` method is concerned. Useful if the output already contains the specified value, and you want to wait until it appears a second time.
|
|
90
|
-
|
|
91
|
-
#### Usage
|
|
92
|
-
|
|
93
|
-
```js
|
|
94
|
-
const run = spawn("node", ["-i"]); // start Node.js REPL
|
|
95
|
-
await run.outputContains("> "); // Wait until prompt appears
|
|
96
|
-
run.write("2 + 2\n"); // Write 2 + 2 then press enter
|
|
97
|
-
run.clearOutputContainsBuffer();
|
|
98
|
-
await run.outputContains("> "); // Wait until prompt appears a second time. If we hadn't cleared the buffer, this would resolve immediately.
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
### `RunContext#write(data: string | Buffer)`
|
|
102
|
-
|
|
103
|
-
Write some data into the process's STDIN stream.
|
|
104
|
-
|
|
105
|
-
#### Usage
|
|
106
|
-
|
|
107
|
-
```js
|
|
108
|
-
const run = spawn("node", ["-i"]); // start Node.js REPL
|
|
109
|
-
await run.outputContains("> "); // Wait until prompt appears
|
|
110
|
-
run.write("2 + 2\n"); // Write 2 + 2 then press enter
|
|
111
|
-
await run.outputContains("4");
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
### `RunContext#close(stream: 'stdin' | 'stdout' | 'stderr')`
|
|
115
|
-
|
|
116
|
-
Close one of the processes's associated stdio streams.
|
|
117
|
-
|
|
118
|
-
#### Usage
|
|
119
|
-
|
|
120
|
-
```js
|
|
121
|
-
const run = spawn("node", ["-i"]); // start Node.js REPL
|
|
122
|
-
await run.outputContains("> "); // Wait until prompt appears
|
|
123
|
-
run.close("stdin"); // Like pressing Ctrl+D; sends EOF
|
|
124
|
-
await run.completion;
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### `RunContext#kill(signal?: string)`
|
|
128
|
-
|
|
129
|
-
Kills the process. If no signal is specified, it defaults to `"SIGINT"`.
|
|
130
|
-
|
|
131
|
-
#### Usage
|
|
132
|
-
|
|
133
|
-
```js
|
|
134
|
-
const run = spawn("node", ["-i"]);
|
|
135
|
-
run.kill(); // Kill with SIGINT
|
|
136
|
-
// OR:
|
|
137
|
-
run.kill("SIGKILL"); // Kill with SIGKILL
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
1
|
+
# `first-base`
|
|
2
|
+
|
|
3
|
+
Integration testing for CLI applications.
|
|
4
|
+
|
|
5
|
+
## Usage Example
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
const { spawn } = require("first-base");
|
|
9
|
+
|
|
10
|
+
test("something", async () => {
|
|
11
|
+
const run = spawn("node", ["-i"]); // launch node REPL
|
|
12
|
+
await run.outputContains("> "); // wait until `> ` is logged
|
|
13
|
+
run.write("2 + 2\n"); // enter `2 + 2` and press enter
|
|
14
|
+
await run.outputContains("4"); // wait until `4` is logged
|
|
15
|
+
run.kill(); // Ctrl+C
|
|
16
|
+
await run.completion; // Wait until process exits
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## API
|
|
21
|
+
|
|
22
|
+
### `spawn(command: string, args?: Array<string>, options?: Object) => RunContext`
|
|
23
|
+
|
|
24
|
+
`args` and `options` are the same as [child_process.spawn](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options).
|
|
25
|
+
|
|
26
|
+
Returns a `RunContext` object; see below.
|
|
27
|
+
|
|
28
|
+
### `RunContext#result`
|
|
29
|
+
|
|
30
|
+
An object with the following properties on it:
|
|
31
|
+
|
|
32
|
+
- `stdout` (`string`): All the data the process has written to STDOUT so far
|
|
33
|
+
- `stderr` (`string`): All the data the process has written to STDERR so far
|
|
34
|
+
- `code` (`number | null`): Exit status code, if the process has finished
|
|
35
|
+
- `error` (`boolean`): If the process errored out, this is `true`
|
|
36
|
+
|
|
37
|
+
This object gets updated over time as the process runs.
|
|
38
|
+
|
|
39
|
+
#### Usage
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
const run = spawn("ls", { cwd: __dirname });
|
|
43
|
+
console.log(run.result); // { stdout: '', stderr: '', code: null, error: false }
|
|
44
|
+
await run.completion;
|
|
45
|
+
console.log(run.result); // { stdout: 'README.md\npackage.json\nindex.js\n', stderr: '', code: 0, error: false }
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### `RunContext#completion`
|
|
49
|
+
|
|
50
|
+
A Promise that gets resolved when the process completes. You can `await` it in your tests.
|
|
51
|
+
|
|
52
|
+
#### Usage
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
const run = spawn("ls", { cwd: __dirname });
|
|
56
|
+
await run.completion; // Waits until the `ls` process finishes
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### `RunContext#debug() => RunContext`
|
|
60
|
+
|
|
61
|
+
Enables debug logging for the `RunContext` and returns it. Useful when your tests are failing and you want to understand what's going on.
|
|
62
|
+
|
|
63
|
+
Returns itself so you can add it to a variable declaration easily.
|
|
64
|
+
|
|
65
|
+
#### Usage
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
const run = spawn("ls", { cwd: __dirname }).debug();
|
|
69
|
+
// The following messages are logged to the console over time:
|
|
70
|
+
//
|
|
71
|
+
// STDOUT: README.md\npackage.json\nindex.js
|
|
72
|
+
// Process exited
|
|
73
|
+
// { stdout: 'README.md\npackage.json\nindex.js', stderr: '', code: 0, error: false }
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### `RunContext#outputContains(value: string | RegExp) => Promise<void>`
|
|
77
|
+
|
|
78
|
+
Returns a Promise that will resolve once the process's output (combined STDOUT/STDERR) contains either the specified string or matches the specified RegExp. Ignores ANSI control characters.
|
|
79
|
+
|
|
80
|
+
#### Usage
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
const run = spawn("node", ["-i"]); // start Node.js REPL
|
|
84
|
+
await run.outputContains("> "); // Wait until prompt appears
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### `RunContext#clearOutputContainsBuffer()`
|
|
88
|
+
|
|
89
|
+
Clears the buffer of "seen" output as far as the `outputContains` method is concerned. Useful if the output already contains the specified value, and you want to wait until it appears a second time.
|
|
90
|
+
|
|
91
|
+
#### Usage
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
const run = spawn("node", ["-i"]); // start Node.js REPL
|
|
95
|
+
await run.outputContains("> "); // Wait until prompt appears
|
|
96
|
+
run.write("2 + 2\n"); // Write 2 + 2 then press enter
|
|
97
|
+
run.clearOutputContainsBuffer();
|
|
98
|
+
await run.outputContains("> "); // Wait until prompt appears a second time. If we hadn't cleared the buffer, this would resolve immediately.
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### `RunContext#write(data: string | Buffer)`
|
|
102
|
+
|
|
103
|
+
Write some data into the process's STDIN stream.
|
|
104
|
+
|
|
105
|
+
#### Usage
|
|
106
|
+
|
|
107
|
+
```js
|
|
108
|
+
const run = spawn("node", ["-i"]); // start Node.js REPL
|
|
109
|
+
await run.outputContains("> "); // Wait until prompt appears
|
|
110
|
+
run.write("2 + 2\n"); // Write 2 + 2 then press enter
|
|
111
|
+
await run.outputContains("4");
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### `RunContext#close(stream: 'stdin' | 'stdout' | 'stderr')`
|
|
115
|
+
|
|
116
|
+
Close one of the processes's associated stdio streams.
|
|
117
|
+
|
|
118
|
+
#### Usage
|
|
119
|
+
|
|
120
|
+
```js
|
|
121
|
+
const run = spawn("node", ["-i"]); // start Node.js REPL
|
|
122
|
+
await run.outputContains("> "); // Wait until prompt appears
|
|
123
|
+
run.close("stdin"); // Like pressing Ctrl+D; sends EOF
|
|
124
|
+
await run.completion;
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### `RunContext#kill(signal?: string)`
|
|
128
|
+
|
|
129
|
+
Kills the process. If no signal is specified, it defaults to `"SIGINT"`.
|
|
130
|
+
|
|
131
|
+
#### Usage
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
const run = spawn("node", ["-i"]);
|
|
135
|
+
run.kill(); // Kill with SIGINT
|
|
136
|
+
// OR:
|
|
137
|
+
run.kill("SIGKILL"); // Kill with SIGKILL
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### `RunContext#cleanResult()`
|
|
141
|
+
|
|
142
|
+
A function which returns a new object with the same shape as `RunContext#result` but with its stdout/stderr passed through the `sanitizers` Array (see below).
|
|
143
|
+
|
|
144
|
+
Unlike `result`, this object does NOT get updated over time as the process runs.
|
|
145
|
+
|
|
146
|
+
### `sanitizers: Array<(string) => string>`
|
|
147
|
+
|
|
148
|
+
An array of functions that will be run on stdout/stderr when calling
|
|
149
|
+
`RunContext#cleanResult()`.
|
|
150
|
+
|
|
151
|
+
By default, it contains 5 functions, which are run in order:
|
|
152
|
+
|
|
153
|
+
- `stripAnsi`: Removes ANSI control characters
|
|
154
|
+
- `replaceRootDir`: Replaces eg `/home/suchipi/Code/first-base/src/index.js` with `<rootDir>/src/index.js`
|
|
155
|
+
- This function searches upwards for the root dir using a heuristic, and caches results in the {@link Map} `replaceRootDir.cache`.
|
|
156
|
+
- The heuristic is:
|
|
157
|
+
- Look upwards for a folder containing `.git` or `.hg`
|
|
158
|
+
- if none is found, look upwards for a folder containing `package-lock.json`, `.gitignore` or `.hgignore`,
|
|
159
|
+
- if none is found, look upwards for a folder containing `package.json` or `README.md`
|
|
160
|
+
- if none is found, consider the present working directory to be the root dir.
|
|
161
|
+
- `replaceCwd`: Replaces the current working directory with `<cwd>`
|
|
162
|
+
- `collapseStackTrace`: For Node.JS-style stack traces, replaces the long chain of "at ..." lines with a single "at somewhere" line
|
|
163
|
+
- `omitThrowLineNumber`: For Node.JS error source display, removes the line number
|
|
164
|
+
|
|
165
|
+
You can remove them or replace them or add to them by mutating the `sanitizers` Array.
|
|
166
|
+
|
|
167
|
+
### `allInflightRunContexts: Set<RunContext>`
|
|
168
|
+
|
|
169
|
+
All runs that have been spawned which haven't reached completion (see `RunContext#completion`).
|
|
170
|
+
|
|
171
|
+
It may be beneficial to clean these up in a test timeout handler or etc.
|
|
172
|
+
|
|
173
|
+
## License
|
|
174
|
+
|
|
175
|
+
MIT
|
package/dist/find-root-dir.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
function _createForOfIteratorHelper(
|
|
4
|
-
function _unsupportedIterableToArray(
|
|
5
|
-
function _arrayLikeToArray(
|
|
3
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
|
4
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
5
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
6
6
|
var fs = require("fs");
|
|
7
7
|
var _require = require("nice-path"),
|
|
8
8
|
Path = _require.Path;
|
|
@@ -11,7 +11,6 @@ var weakRootDirIndicators = ["package-lock.json", ".gitignore", ".hgignore"];
|
|
|
11
11
|
var veryWeakRootDirIndicators = ["package.json", "README.md"];
|
|
12
12
|
function hasFile(dir, filename) {
|
|
13
13
|
var fullPath = dir.concat(filename).toString();
|
|
14
|
-
console.log("checking for ".concat(fullPath));
|
|
15
14
|
try {
|
|
16
15
|
return fs.existsSync(fullPath);
|
|
17
16
|
} catch (err) {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,66 +1,73 @@
|
|
|
1
|
-
export type Options = {
|
|
2
|
-
cwd?: string;
|
|
3
|
-
env?: { [varName: string]: any };
|
|
4
|
-
argv0?: string;
|
|
5
|
-
detached?: boolean;
|
|
6
|
-
uid?: number;
|
|
7
|
-
gid?: number;
|
|
8
|
-
shell?: boolean | string;
|
|
9
|
-
windowsVerbatimArguments?: boolean;
|
|
10
|
-
windowsHide?: boolean;
|
|
11
|
-
pty?: boolean;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export type RunContext = {
|
|
15
|
-
result: {
|
|
16
|
-
stdout: string;
|
|
17
|
-
stderr: string;
|
|
18
|
-
code: null | number;
|
|
19
|
-
error: boolean;
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* Same as {@link RunContext.result}, but with {@link sanitizers} run on
|
|
23
|
-
* stdout/stderr.
|
|
24
|
-
*/
|
|
25
|
-
cleanResult(): {
|
|
26
|
-
stdout: string;
|
|
27
|
-
stderr: string;
|
|
28
|
-
code: null | number;
|
|
29
|
-
error: boolean;
|
|
30
|
-
};
|
|
31
|
-
completion: Promise<void>;
|
|
32
|
-
debug(): RunContext;
|
|
33
|
-
outputContains(value: string | RegExp): Promise<void>;
|
|
34
|
-
clearOutputContainsBuffer(): void;
|
|
35
|
-
// TODO: Should be string | Buffer, but idk how to use Buffer since they might not be using node types
|
|
36
|
-
write(data: any): void;
|
|
37
|
-
close(stream: "stdin" | "stdout" | "stderr"): void;
|
|
38
|
-
kill(signal?: string): void;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export const spawn: ((cmd: string) => RunContext) &
|
|
42
|
-
((cmd: string, args: Array<string>) => RunContext) &
|
|
43
|
-
((cmd: string, options: Options) => RunContext) &
|
|
44
|
-
((cmd: string, args: Array<string>, options: Options) => RunContext);
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* An array of functions that will be run on stdout/stderr when calling
|
|
48
|
-
* {@link RunContext.cleanResult}.
|
|
49
|
-
*
|
|
50
|
-
* By default, it contains 5 functions, which are run in order:
|
|
51
|
-
*
|
|
52
|
-
* - `stripAnsi`: Removes ANSI control characters
|
|
53
|
-
* - `replaceRootDir`: Replaces eg `/home/suchipi/Code/first-base/src/index.js` with `<rootDir>/src/index.js`
|
|
54
|
-
* - This function searches upwards for the root dir using a heuristic, and caches results in the {@link Map} `replaceRootDir.cache`.
|
|
55
|
-
* - The heuristic is:
|
|
56
|
-
* - Look upwards for a folder containing `.git` or `.hg`
|
|
57
|
-
* - if none is found, look upwards for a folder containing `package-lock.json`, `.gitignore` or `.hgignore`,
|
|
58
|
-
* - if none is found, look upwards for a folder containing `package.json` or `README.md`
|
|
59
|
-
* - if none is found, consider the present working directory to be the root dir.
|
|
60
|
-
* - `replaceCwd`: Replaces the current working directory with `<cwd>`
|
|
61
|
-
* - `collapseStackTrace`: For Node.JS-style stack traces, replaces the long chain of "at ..." lines with a single "at somewhere" line
|
|
62
|
-
* - `omitThrowLineNumber`: For Node.JS error source display, removes the line number
|
|
63
|
-
*
|
|
64
|
-
* You can remove them or replace them or add to them by mutating the `sanitizers` Array.
|
|
65
|
-
*/
|
|
66
|
-
export const sanitizers: Array<(str: string) => string>;
|
|
1
|
+
export type Options = {
|
|
2
|
+
cwd?: string;
|
|
3
|
+
env?: { [varName: string]: any };
|
|
4
|
+
argv0?: string;
|
|
5
|
+
detached?: boolean;
|
|
6
|
+
uid?: number;
|
|
7
|
+
gid?: number;
|
|
8
|
+
shell?: boolean | string;
|
|
9
|
+
windowsVerbatimArguments?: boolean;
|
|
10
|
+
windowsHide?: boolean;
|
|
11
|
+
pty?: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type RunContext = {
|
|
15
|
+
result: {
|
|
16
|
+
stdout: string;
|
|
17
|
+
stderr: string;
|
|
18
|
+
code: null | number;
|
|
19
|
+
error: boolean;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Same as {@link RunContext.result}, but with {@link sanitizers} run on
|
|
23
|
+
* stdout/stderr.
|
|
24
|
+
*/
|
|
25
|
+
cleanResult(): {
|
|
26
|
+
stdout: string;
|
|
27
|
+
stderr: string;
|
|
28
|
+
code: null | number;
|
|
29
|
+
error: boolean;
|
|
30
|
+
};
|
|
31
|
+
completion: Promise<void>;
|
|
32
|
+
debug(): RunContext;
|
|
33
|
+
outputContains(value: string | RegExp): Promise<void>;
|
|
34
|
+
clearOutputContainsBuffer(): void;
|
|
35
|
+
// TODO: Should be string | Buffer, but idk how to use Buffer since they might not be using node types
|
|
36
|
+
write(data: any): void;
|
|
37
|
+
close(stream: "stdin" | "stdout" | "stderr"): void;
|
|
38
|
+
kill(signal?: string): void;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const spawn: ((cmd: string) => RunContext) &
|
|
42
|
+
((cmd: string, args: Array<string>) => RunContext) &
|
|
43
|
+
((cmd: string, options: Options) => RunContext) &
|
|
44
|
+
((cmd: string, args: Array<string>, options: Options) => RunContext);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* An array of functions that will be run on stdout/stderr when calling
|
|
48
|
+
* {@link RunContext.cleanResult}.
|
|
49
|
+
*
|
|
50
|
+
* By default, it contains 5 functions, which are run in order:
|
|
51
|
+
*
|
|
52
|
+
* - `stripAnsi`: Removes ANSI control characters
|
|
53
|
+
* - `replaceRootDir`: Replaces eg `/home/suchipi/Code/first-base/src/index.js` with `<rootDir>/src/index.js`
|
|
54
|
+
* - This function searches upwards for the root dir using a heuristic, and caches results in the {@link Map} `replaceRootDir.cache`.
|
|
55
|
+
* - The heuristic is:
|
|
56
|
+
* - Look upwards for a folder containing `.git` or `.hg`
|
|
57
|
+
* - if none is found, look upwards for a folder containing `package-lock.json`, `.gitignore` or `.hgignore`,
|
|
58
|
+
* - if none is found, look upwards for a folder containing `package.json` or `README.md`
|
|
59
|
+
* - if none is found, consider the present working directory to be the root dir.
|
|
60
|
+
* - `replaceCwd`: Replaces the current working directory with `<cwd>`
|
|
61
|
+
* - `collapseStackTrace`: For Node.JS-style stack traces, replaces the long chain of "at ..." lines with a single "at somewhere" line
|
|
62
|
+
* - `omitThrowLineNumber`: For Node.JS error source display, removes the line number
|
|
63
|
+
*
|
|
64
|
+
* You can remove them or replace them or add to them by mutating the `sanitizers` Array.
|
|
65
|
+
*/
|
|
66
|
+
export const sanitizers: Array<(str: string) => string>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* All runs that have been spawned which haven't reached completion.
|
|
70
|
+
*
|
|
71
|
+
* It may be beneficial to clean these up in a test timeout handler or etc.
|
|
72
|
+
*/
|
|
73
|
+
export const allInflightRunContexts: Set<RunContext>;
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ var normalSpawn = require("child_process").spawn;
|
|
|
5
5
|
var stripAnsi = require("strip-ansi");
|
|
6
6
|
var _require = require("./sanitizers"),
|
|
7
7
|
sanitizers = _require.sanitizers;
|
|
8
|
+
var allInflightRunContexts = new Set();
|
|
8
9
|
|
|
9
10
|
// Run a child process and return a "run context" object
|
|
10
11
|
// to interact with it. Function signature is the same as
|
|
@@ -147,6 +148,7 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
|
|
|
147
148
|
unreffable = child;
|
|
148
149
|
}
|
|
149
150
|
running = true;
|
|
151
|
+
allInflightRunContexts.add(runContext);
|
|
150
152
|
var checkForPendingOutputRequestsToResolve = function checkForPendingOutputRequestsToResolve() {
|
|
151
153
|
pendingOutputContainsRequests.forEach(function (request) {
|
|
152
154
|
if (typeof request.value === "string") {
|
|
@@ -191,6 +193,7 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
|
|
|
191
193
|
debugLog("Process ".concat(reason));
|
|
192
194
|
debugLog(runContext.result);
|
|
193
195
|
running = false;
|
|
196
|
+
allInflightRunContexts["delete"](runContext);
|
|
194
197
|
resolve();
|
|
195
198
|
pendingOutputContainsRequests.forEach(function (request) {
|
|
196
199
|
request.reject(new Error("Child process ".concat(reason, " before its output contained the requested content: ").concat(request.value)));
|
|
@@ -209,5 +212,6 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
|
|
|
209
212
|
};
|
|
210
213
|
module.exports = {
|
|
211
214
|
spawn: spawn,
|
|
212
|
-
sanitizers: sanitizers
|
|
215
|
+
sanitizers: sanitizers,
|
|
216
|
+
allInflightRunContexts: allInflightRunContexts
|
|
213
217
|
};
|
package/dist/index.js.flow
CHANGED
|
@@ -1,40 +1,42 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
export type Options = {
|
|
3
|
-
cwd?: ?string,
|
|
4
|
-
env?: ?{ [varName: string]: any },
|
|
5
|
-
argv0?: ?string,
|
|
6
|
-
detached?: ?boolean,
|
|
7
|
-
uid?: ?number,
|
|
8
|
-
gid?: ?number,
|
|
9
|
-
shell?: ?boolean | string,
|
|
10
|
-
windowsVerbatimArguments?: ?boolean,
|
|
11
|
-
windowsHide?: ?boolean,
|
|
12
|
-
pty?: ?boolean,
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export type RunContext = {
|
|
16
|
-
result: {
|
|
17
|
-
stdout: string,
|
|
18
|
-
stderr: string,
|
|
19
|
-
code: null | number,
|
|
20
|
-
error: boolean,
|
|
21
|
-
},
|
|
22
|
-
completion: Promise<void>,
|
|
23
|
-
debug(): RunContext,
|
|
24
|
-
outputContains(value: string | RegExp): Promise<void>,
|
|
25
|
-
clearOutputContainsBuffer(): void,
|
|
26
|
-
write(data: string | Buffer): void,
|
|
27
|
-
close(stream: "stdin" | "stdout" | "stderr"): void,
|
|
28
|
-
kill(signal?: string): void,
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
interface Exports {
|
|
32
|
-
spawn(cmd: string): RunContext;
|
|
33
|
-
spawn(cmd: string, args: Array<string>): RunContext;
|
|
34
|
-
spawn(cmd: string, options: Options): RunContext;
|
|
35
|
-
spawn(cmd: string, args: Array<string>, options: Options): RunContext;
|
|
36
|
-
|
|
37
|
-
sanitizers: Array<(str: string) => string>;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
1
|
+
// @flow
|
|
2
|
+
export type Options = {
|
|
3
|
+
cwd?: ?string,
|
|
4
|
+
env?: ?{ [varName: string]: any },
|
|
5
|
+
argv0?: ?string,
|
|
6
|
+
detached?: ?boolean,
|
|
7
|
+
uid?: ?number,
|
|
8
|
+
gid?: ?number,
|
|
9
|
+
shell?: ?boolean | string,
|
|
10
|
+
windowsVerbatimArguments?: ?boolean,
|
|
11
|
+
windowsHide?: ?boolean,
|
|
12
|
+
pty?: ?boolean,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type RunContext = {
|
|
16
|
+
result: {
|
|
17
|
+
stdout: string,
|
|
18
|
+
stderr: string,
|
|
19
|
+
code: null | number,
|
|
20
|
+
error: boolean,
|
|
21
|
+
},
|
|
22
|
+
completion: Promise<void>,
|
|
23
|
+
debug(): RunContext,
|
|
24
|
+
outputContains(value: string | RegExp): Promise<void>,
|
|
25
|
+
clearOutputContainsBuffer(): void,
|
|
26
|
+
write(data: string | Buffer): void,
|
|
27
|
+
close(stream: "stdin" | "stdout" | "stderr"): void,
|
|
28
|
+
kill(signal?: string): void,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
interface Exports {
|
|
32
|
+
spawn(cmd: string): RunContext;
|
|
33
|
+
spawn(cmd: string, args: Array<string>): RunContext;
|
|
34
|
+
spawn(cmd: string, options: Options): RunContext;
|
|
35
|
+
spawn(cmd: string, args: Array<string>, options: Options): RunContext;
|
|
36
|
+
|
|
37
|
+
sanitizers: Array<(str: string) => string>;
|
|
38
|
+
|
|
39
|
+
allInflightRunContexts: Set<RunContext>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
declare module.exports: Exports;
|
package/package.json
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "first-base",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Integration testing for CLI applications",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"repository": {
|
|
7
|
-
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/suchipi/first-base.git"
|
|
9
|
-
},
|
|
10
|
-
"author": "Lily Skye <me@suchipi.com>",
|
|
11
|
-
"license": "MIT",
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"strip-ansi": "^5.0.0",
|
|
14
|
-
"nice-path": "^2.
|
|
15
|
-
},
|
|
16
|
-
"optionalDependencies": {
|
|
17
|
-
"node-pty": "^1.0.0"
|
|
18
|
-
},
|
|
19
|
-
"devDependencies": {
|
|
20
|
-
"@babel/cli": "^7.
|
|
21
|
-
"@babel/core": "^7.
|
|
22
|
-
"@babel/preset-env": "^7.
|
|
23
|
-
"babel-core": "^7.0.0-bridge.0",
|
|
24
|
-
"babel-jest": "^
|
|
25
|
-
"eslint": "^8.56.0",
|
|
26
|
-
"eslint-config-unobtrusive": "^1.2.5",
|
|
27
|
-
"eslint-plugin-import": "^2.29.1",
|
|
28
|
-
"jest": "^
|
|
29
|
-
"prettier": "^3.
|
|
30
|
-
},
|
|
31
|
-
"scripts": {
|
|
32
|
-
"build": "mkdir -p dist; rm -rf dist/*; babel src --out-dir dist && cp src/index.js.flow dist/ && cp src/index.d.ts dist/",
|
|
33
|
-
"test": "jest"
|
|
34
|
-
},
|
|
35
|
-
"jest": {
|
|
36
|
-
"prettierPath": null
|
|
37
|
-
}
|
|
38
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "first-base",
|
|
3
|
+
"version": "1.6.1",
|
|
4
|
+
"description": "Integration testing for CLI applications",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/suchipi/first-base.git"
|
|
9
|
+
},
|
|
10
|
+
"author": "Lily Skye <me@suchipi.com>",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"strip-ansi": "^5.0.0",
|
|
14
|
+
"nice-path": "^3.2.2"
|
|
15
|
+
},
|
|
16
|
+
"optionalDependencies": {
|
|
17
|
+
"node-pty": "^1.0.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@babel/cli": "^7.28.6",
|
|
21
|
+
"@babel/core": "^7.29.0",
|
|
22
|
+
"@babel/preset-env": "^7.29.0",
|
|
23
|
+
"babel-core": "^7.0.0-bridge.0",
|
|
24
|
+
"babel-jest": "^30.2.0",
|
|
25
|
+
"eslint": "^8.56.0",
|
|
26
|
+
"eslint-config-unobtrusive": "^1.2.5",
|
|
27
|
+
"eslint-plugin-import": "^2.29.1",
|
|
28
|
+
"jest": "^30.2.0",
|
|
29
|
+
"prettier": "^3.8.1"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "mkdir -p dist; rm -rf dist/*; babel src --out-dir dist && cp src/index.js.flow dist/ && cp src/index.d.ts dist/",
|
|
33
|
+
"test": "jest"
|
|
34
|
+
},
|
|
35
|
+
"jest": {
|
|
36
|
+
"prettierPath": null
|
|
37
|
+
}
|
|
38
|
+
}
|