cliprobe 0.0.1 → 0.2.0
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/README.md +131 -21
- package/dist/cli.js +1420 -0
- package/package.json +90 -44
- package/bin/cliprobe.js +0 -225
package/README.md
CHANGED
|
@@ -7,21 +7,34 @@ npx cliprobe -- esbuild
|
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
```
|
|
10
|
-
cliprobe 0.0
|
|
10
|
+
cliprobe 0.1.0: closed-pipe, color, ctrl-c, control-chars checks for esbuild
|
|
11
11
|
|
|
12
|
+
closed-pipe
|
|
12
13
|
FAIL esbuild --version crashed when stdout was closed (exit 0 -> 1)
|
|
13
14
|
Error: Command failed: .../esbuild --version (at node_modules/esbuild/bin/esbuild:222:28)
|
|
14
15
|
FAIL esbuild --help crashed when stdout was closed (exit 0 -> 1)
|
|
15
16
|
Error: Command failed: .../esbuild --help (at node_modules/esbuild/bin/esbuild:222:28)
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
color
|
|
19
|
+
PASS esbuild --version no color in a pipe or in a terminal
|
|
20
|
+
PASS esbuild --help colors only in a terminal, and NO_COLOR turns it off
|
|
21
|
+
|
|
22
|
+
ctrl-c
|
|
23
|
+
SKIP esbuild needs probe args that keep it running, such as a watch or serve command
|
|
24
|
+
|
|
25
|
+
control-chars
|
|
26
|
+
SKIP esbuild no {fixture} in the arguments; put it where the command takes a path, e.g. ls {fixture}
|
|
27
|
+
|
|
28
|
+
2 failed, 0 warned, 2 passed, 2 skipped
|
|
18
29
|
```
|
|
19
30
|
|
|
20
31
|
## Why
|
|
21
32
|
|
|
22
33
|
When a program's output goes into a reader that stops early (`| head`, `grep -q`, a closed agent pipe), its next write to stdout fails. A well-behaved program exits quietly. Many Node.js CLIs instead crash with `Error: write EPIPE`, a stack trace, and exit code 1. That breaks scripts running under `set -o pipefail` and confuses agents that read stderr.
|
|
23
34
|
|
|
24
|
-
In a census of the 100 most-downloaded npm packages that ship a command, **
|
|
35
|
+
In a census of the 100 most-downloaded npm packages that ship a command (119 commands, run offline in a throwaway container), **9 crashed this way every time**: esbuild, browserslist, update-browserslist-db, autoprefixer, husky, tsconfck, ts-jest, rolldown, and tsx on `--version`. The best-known existing CLI auditor rated the first 8 as passing its SIGPIPE check. The same census found two commands that print color codes into a pipe (`giget --help`, `tsx --help`), and no false failures: every `FAIL` reproduced by hand. (A census of `--help` and `--version` can't exercise ctrl-c or control-chars, which need a command that keeps running or takes a path; their fixtures and real-tool tests are in `test/`.)
|
|
36
|
+
|
|
37
|
+
The other checks cover the rest of what goes wrong between a program and the thing reading it: color codes leaking into logs and agent transcripts, Ctrl-C that is ignored or ends in a stack trace, and file names that can send escape sequences to your terminal.
|
|
25
38
|
|
|
26
39
|
## Usage
|
|
27
40
|
|
|
@@ -30,44 +43,141 @@ npx cliprobe -- <command> # probes "<command> --version" and "<c
|
|
|
30
43
|
npx cliprobe -- <command> [args...] # probes exactly this invocation
|
|
31
44
|
npx cliprobe -- node ./dist/cli.js list # test your own CLI before publishing
|
|
32
45
|
npx cliprobe --json -- <command> # machine-readable results
|
|
46
|
+
npx cliprobe --format junit -o report.xml -- <command> # JUnit file for CI; human report on stdout
|
|
47
|
+
npx cliprobe -- ls {fixture} # {fixture}: a directory of hostile file names
|
|
48
|
+
npx cliprobe --check startup,baseline -- node ./dist/cli.js # info checks, only when named
|
|
33
49
|
```
|
|
34
50
|
|
|
35
|
-
| Option
|
|
36
|
-
|
|
37
|
-
| `--
|
|
38
|
-
| `--
|
|
39
|
-
|
|
|
40
|
-
| `-
|
|
51
|
+
| Option | Meaning |
|
|
52
|
+
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
|
53
|
+
| `--check <name>` | Run only this check; repeat it or use commas for several |
|
|
54
|
+
| `--format <name>` | Output format: `human` (default), `json`, `markdown` (a GitHub job summary or PR comment) or `junit` (XML for CI test reports) |
|
|
55
|
+
| `--json` | Same as `--format json` |
|
|
56
|
+
| `-o`, `--output-file <path>` | Write the `--format` report to a file (atomically, creating parent folders); the human report still prints |
|
|
57
|
+
| `--timeout <ms>` | Kill a probe after this long (default 15000) |
|
|
58
|
+
| `-h`, `--help` | Show help |
|
|
59
|
+
| `-v`, `--version` | Show the version |
|
|
41
60
|
|
|
42
61
|
Exit codes: `0` no failures, `1` a check failed, `2` usage error or the command could not run. That makes it usable as a CI gate.
|
|
43
62
|
|
|
63
|
+
### In CI
|
|
64
|
+
|
|
65
|
+
A failed check exits 1, so the step fails. In GitHub Actions, write a Markdown report to the job summary while the log keeps the human report:
|
|
66
|
+
|
|
67
|
+
```yaml
|
|
68
|
+
- name: Check the CLI for terminal-edge bugs
|
|
69
|
+
run: npx --yes cliprobe@0 --format markdown --output-file "$GITHUB_STEP_SUMMARY" -- node ./dist/cli.js
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
For CI systems that read test results, write JUnit XML instead and keep it even when the step fails:
|
|
73
|
+
|
|
74
|
+
```yaml
|
|
75
|
+
- run: npx --yes cliprobe@0 --format junit --output-file reports/cliprobe.xml -- node ./dist/cli.js
|
|
76
|
+
- uses: actions/upload-artifact@v7
|
|
77
|
+
if: always()
|
|
78
|
+
with:
|
|
79
|
+
name: cliprobe-report
|
|
80
|
+
path: reports/cliprobe.xml
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
In JUnit, each check is a test suite and each probe a test case: `FAIL` is a failure, could-not-run an error, `SKIP` skipped; `WARN`, `INFO` and `PASS` pass, with the verdict and reason in the case's output.
|
|
84
|
+
|
|
44
85
|
## Checks
|
|
45
86
|
|
|
46
|
-
|
|
87
|
+
A default run does the four checks below: closed-pipe, color, ctrl-c and control-chars. Each one reports evidence, never a score. Two info checks, startup and baseline, run only when you name them with `--check`.
|
|
88
|
+
|
|
89
|
+
**closed-pipe**. cliprobe runs the command twice: once normally, then with stdout set to a pipe whose reading end is already closed, so the first write fails every time, even for fast native programs. Results:
|
|
90
|
+
|
|
91
|
+
| Verdict | Meaning |
|
|
92
|
+
| ------- | ------------------------------------------------------------------------------ |
|
|
93
|
+
| `PASS` | Exited quietly, or was stopped by SIGPIPE (the Unix convention) |
|
|
94
|
+
| `FAIL` | Printed an EPIPE error or stack trace and changed its exit code, or hung |
|
|
95
|
+
| `WARN` | Printed a write error but kept its exit code |
|
|
96
|
+
| `SKIP` | Wrote nothing to stdout for this invocation, so pass args that print something |
|
|
97
|
+
|
|
98
|
+
**color**. cliprobe runs the command up to three times: into a pipe, under a real terminal (a pseudo-terminal through the system's `script` command, 80×24), and under a terminal with `NO_COLOR=1`. Every run gets `TERM=xterm-256color` and no `CI` variable, so a program can't pass just because the terminal type looks colorless, and color libraries that force color in CI don't fail it. Color codes in piped output end up in log files and agent transcripts as `\x1b[31m` noise. Results:
|
|
99
|
+
|
|
100
|
+
| Verdict | Meaning |
|
|
101
|
+
| ------- | -------------------------------------------------------------------------------------------- |
|
|
102
|
+
| `PASS` | No color in a pipe; in a terminal, either no color at all or color that `NO_COLOR` turns off |
|
|
103
|
+
| `FAIL` | Printed color or style codes (bold counts, a bare reset doesn't) to stdout in a pipe |
|
|
104
|
+
| `WARN` | Printed color to stderr in a pipe, or kept printing color under `NO_COLOR` |
|
|
105
|
+
| `SKIP` | Printed nothing, waited for input in a terminal, or no usable `script` (BusyBox) |
|
|
106
|
+
|
|
107
|
+
Many classic tools (git, BSD `ls` and `grep`) color correctly but don't implement [`NO_COLOR`](https://no-color.org), so they get a `WARN`, never a `FAIL`. Probe a command that prints color in normal use: almost no CLI colors its `--help`.
|
|
108
|
+
|
|
109
|
+
**ctrl-c**. cliprobe lets the command run for 1 second, then sends SIGINT to its process group, which is what a terminal does when you press Ctrl-C, and gives it 3 seconds to exit. Results:
|
|
110
|
+
|
|
111
|
+
| Verdict | Meaning |
|
|
112
|
+
| ------- | ---------------------------------------------------------------------------------------------- |
|
|
113
|
+
| `PASS` | Stopped by SIGINT, exited 130 (the convention), or exited with another failure code |
|
|
114
|
+
| `FAIL` | Still running 3 seconds later, or printed a stack trace or traceback after the Ctrl-C |
|
|
115
|
+
| `WARN` | Exited 0, so a script or an agent can't tell the work was interrupted |
|
|
116
|
+
| `SKIP` | Finished before the Ctrl-C, as `--version` and `--help` do: probe a command that keeps running |
|
|
117
|
+
|
|
118
|
+
A stack trace printed before the Ctrl-C doesn't count. Python's default `KeyboardInterrupt` traceback is the classic failure here, so `python3 -c 'import time; time.sleep(30)'` fails.
|
|
47
119
|
|
|
48
|
-
|
|
49
|
-
|---|---|
|
|
50
|
-
| `PASS` | Exited quietly, or was stopped by SIGPIPE (the Unix convention) |
|
|
51
|
-
| `FAIL` | Printed an EPIPE error or stack trace and changed its exit code, or hung |
|
|
52
|
-
| `WARN` | Printed a write error but kept its exit code |
|
|
53
|
-
| `SKIP` | Wrote nothing to stdout for this invocation, so pass args that print something |
|
|
120
|
+
**control-chars**. Put `{fixture}` in the arguments where your command takes a path, for example `npx cliprobe -- ls {fixture}` or `npx cliprobe -- node ./dist/cli.js scan --dir={fixture}`. cliprobe replaces it with a temporary directory of file names holding escape sequences (one sets the window title, one clears the screen), a C1 control character, a carriage return, a newline and a bidirectional override. It runs the command under a terminal and looks for those characters, unescaped, in what reaches it. A file name that can clear someone's screen or retitle their window is a terminal injection, the same bug class as the `ls`/`find` escaping that GNU coreutils added years ago. Reports show `{fixture}`, never the temporary path, and the directory is removed afterwards. Results:
|
|
54
121
|
|
|
55
|
-
|
|
122
|
+
| Verdict | Meaning |
|
|
123
|
+
| ------- | ----------------------------------------------------------------------------------------------------------- |
|
|
124
|
+
| `PASS` | Escaped or replaced the control characters (a raw bidi override is mentioned, not judged) |
|
|
125
|
+
| `FAIL` | Sent an escape sequence from a file name to the terminal unescaped |
|
|
126
|
+
| `WARN` | Printed a C1 control, carriage return or newline from a file name unescaped, which can hide or forge output |
|
|
127
|
+
| `SKIP` | No `{fixture}` in the arguments, the command never printed the file names, or no usable `script` |
|
|
128
|
+
|
|
129
|
+
Only the terminal run is judged. Into a pipe, `ls` and `find` print names exactly, by design; that is recorded in the JSON (`pipeRaw`) but doesn't affect the verdict. BSD `find` on macOS fails this check: unlike GNU `find`, it prints names raw even to a terminal.
|
|
130
|
+
|
|
131
|
+
### Info checks
|
|
132
|
+
|
|
133
|
+
These report an observation with the verdict `INFO` and never fail. They run only when named, for example `--check startup,baseline`. With probe args, those args are treated as the program: `npx cliprobe --check baseline -- node ./dist/cli.js` probes `node ./dist/cli.js --version`, and so on.
|
|
134
|
+
|
|
135
|
+
**startup**. The median wall time over 5 runs, after one warm-up, of `<command> --version`, or of the probe exactly as given. It's skipped if that invocation exits non-zero.
|
|
136
|
+
|
|
137
|
+
**baseline**. Appends `--version`, `--help` and `--cliprobe-unknown-flag` to the probe and describes what scripts and agents rely on: one version line on stdout, help on stdout with exit 0, and a non-zero exit with the message on stderr for a flag the program doesn't know. A program that ignores unknown flags will run its default action (it's stopped after 5 seconds), which is why baseline never runs unless you ask for it.
|
|
56
138
|
|
|
57
139
|
## Fixing a failure in a Node.js CLI
|
|
58
140
|
|
|
59
|
-
|
|
141
|
+
**closed-pipe:** handle `EPIPE` on stdout once, near your entry point:
|
|
60
142
|
|
|
61
143
|
```js
|
|
62
|
-
process.stdout.on(
|
|
63
|
-
if (error.code ===
|
|
144
|
+
process.stdout.on('error', (error) => {
|
|
145
|
+
if (error.code === 'EPIPE') process.exit(0);
|
|
64
146
|
throw error;
|
|
65
147
|
});
|
|
66
148
|
```
|
|
67
149
|
|
|
150
|
+
**color:** decide once, from the stream you write to:
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
const useColor = process.stdout.isTTY && !process.env.NO_COLOR;
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
`util.styleText` makes that decision for you on Node 22.13 and later (its `validateStream` option, on by default), and most color libraries check `isTTY` already, but some turn color on whenever `TERM` allows it or `CI` is set.
|
|
157
|
+
|
|
158
|
+
**ctrl-c:** Node's default already does the right thing, dying by SIGINT. If you add a handler to clean up, finish by exiting 130 rather than 0, and don't throw from it:
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
process.once('SIGINT', async () => {
|
|
162
|
+
await cleanUp();
|
|
163
|
+
process.exit(130);
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
**control-chars:** escape names (and any other text you didn't write) before printing them for a person. `JSON.stringify` alone isn't enough: it leaves C1 controls and bidi overrides raw.
|
|
168
|
+
|
|
169
|
+
```js
|
|
170
|
+
const shown = name.replace(
|
|
171
|
+
/[\x00-\x1f\x7f-\x9f]/g,
|
|
172
|
+
(c) => `\\x${c.charCodeAt(0).toString(16).padStart(2, '0')}`
|
|
173
|
+
);
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Also escape U+2028, U+2029 and the bidi controls (U+202A to U+202E, U+2066 to U+2069) if names may come from untrusted sources.
|
|
177
|
+
|
|
68
178
|
## Safety
|
|
69
179
|
|
|
70
|
-
cliprobe runs the target in your current directory without a shell, with stdin closed, a temporary `HOME`, and a minimal environment (no tokens from your shell). It still executes the program, so only probe programs you trust. macOS and Linux only for now.
|
|
180
|
+
cliprobe runs the target in your current directory without a shell, with stdin closed, a temporary `HOME`, and a minimal environment (no tokens from your shell). It still executes the program, so only probe programs you trust. By default cliprobe passes only the arguments you give, or `--version` and `--help`; only the baseline check, when you name it, adds an unknown flag. It kills each run's whole process group on timeout, including under a pseudo-terminal. macOS and Linux only for now.
|
|
71
181
|
|
|
72
182
|
## License
|
|
73
183
|
|