@codifycli/plugin-test 1.1.0-beta7 → 2.0.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/.github/workflows/publish.yaml +83 -0
- package/CLAUDE.md +17 -11
- package/dist/plugin-process.d.ts +8 -0
- package/dist/plugin-process.js +38 -3
- package/dist/plugin-process.js.map +1 -1
- package/dist/plugin-tester.d.ts +2 -0
- package/dist/plugin-tester.js +61 -37
- package/dist/plugin-tester.js.map +1 -1
- package/dist/spawn.d.ts +8 -0
- package/dist/spawn.js +59 -20
- package/dist/spawn.js.map +1 -1
- package/dist/terminal-lines.d.ts +19 -0
- package/dist/terminal-lines.js +97 -0
- package/dist/terminal-lines.js.map +1 -0
- package/dist/test-utils.js +14 -4
- package/dist/test-utils.js.map +1 -1
- package/dist/utils.d.ts +9 -1
- package/dist/utils.js +60 -1
- package/dist/utils.js.map +1 -1
- package/package.json +8 -8
- package/src/plugin-process.ts +45 -3
- package/src/plugin-tester.ts +66 -43
- package/src/spawn.ts +70 -22
- package/src/terminal-lines.test.ts +77 -0
- package/src/terminal-lines.ts +115 -0
- package/src/test-utils.test.ts +1 -1
- package/src/test-utils.ts +16 -4
- package/src/utils.test.ts +14 -0
- package/src/utils.ts +70 -1
- package/test/output-plugin.mjs +19 -0
- package/test/plugin-output.test.ts +36 -0
- package/test/plugin-tester.test.ts +48 -3
- package/test/test-plugin.ts +40 -7
- package/.claude/settings.local.json +0 -5
package/dist/spawn.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { SpawnStatus } from '@codifycli/schemas';
|
|
2
|
-
import * as pty from '
|
|
2
|
+
import * as pty from 'node-pty';
|
|
3
3
|
import stripAnsi from 'strip-ansi';
|
|
4
4
|
import { Shell, ShellUtils } from './shell.js';
|
|
5
|
+
import { TerminalLines } from './terminal-lines.js';
|
|
5
6
|
export function testSpawn(cmd, options) {
|
|
6
|
-
return spawnSafe(cmd, { interactive: true, ...options
|
|
7
|
+
return spawnSafe(cmd, { interactive: true, ...options });
|
|
7
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Runs a command in a pty, like the CLI does for the commands plugins request (`src/utils/spawn.ts` in the CLI).
|
|
11
|
+
* Output is printed as the lines a terminal would end up showing (no line per spinner frame), except with `stdin`:
|
|
12
|
+
* then the command has the terminal and gets the output as is.
|
|
13
|
+
*/
|
|
8
14
|
export function spawnSafe(cmd, options) {
|
|
9
15
|
if (cmd.toLowerCase().includes('sudo')) {
|
|
10
16
|
throw new Error('Command must not include sudo');
|
|
11
17
|
}
|
|
12
|
-
console.log(`Running command: ${options?.requiresRoot ? 'sudo' : ''}
|
|
18
|
+
console.log(`Running command: ${options?.requiresRoot ? 'sudo ' : ''}${cmd}` + (options?.cwd ? ` (${options?.cwd})` : ''));
|
|
13
19
|
return new Promise((resolve, reject) => {
|
|
14
20
|
const output = [];
|
|
15
21
|
const historyIgnore = ShellUtils.getShell() === Shell.ZSH ? { HISTORY_IGNORE: '*' } : { HISTIGNORE: '*' };
|
|
@@ -22,41 +28,74 @@ export function spawnSafe(cmd, options) {
|
|
|
22
28
|
COLORTERM: 'truecolor',
|
|
23
29
|
...historyIgnore
|
|
24
30
|
};
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
const disableWrapping = options?.disableWrapping ?? true;
|
|
32
|
+
const columns = () => disableWrapping ? 10000 : process.stdout.columns ?? 80;
|
|
33
|
+
// zsh autocorrect prompts (e.g. "zsh: correct 'config' to '.config' [nyae]?") will hang
|
|
34
|
+
// the pty waiting for interactive input. Can't be disabled via env var; must unset the options explicitly.
|
|
35
|
+
const disableAutocorrect = ShellUtils.getShell() === Shell.ZSH ? 'unsetopt CORRECT CORRECT_ALL 2>/dev/null; ' : '';
|
|
36
|
+
const command = options?.requiresRoot
|
|
37
|
+
? `sudo -E ${ShellUtils.getDefaultShell()} ${options?.interactive ? '-i' : ''} -c "${(disableAutocorrect + cmd).replaceAll('"', '\\"')}"`
|
|
38
|
+
: `${disableAutocorrect}${cmd}`;
|
|
29
39
|
const args = options?.interactive ? ['-i', '-c', command] : ['-c', command];
|
|
30
40
|
// Run the command in a pty for interactivity
|
|
31
41
|
const mPty = pty.spawn(ShellUtils.getDefaultShell(), args, {
|
|
32
|
-
|
|
33
|
-
cols:
|
|
34
|
-
rows:
|
|
35
|
-
env
|
|
42
|
+
cwd: options?.cwd,
|
|
43
|
+
cols: columns(),
|
|
44
|
+
rows: process.stdout.rows ?? 24,
|
|
45
|
+
env,
|
|
36
46
|
});
|
|
47
|
+
const lines = options?.stdin ? undefined : new TerminalLines();
|
|
48
|
+
const print = (text) => {
|
|
49
|
+
if (text.length > 0) {
|
|
50
|
+
process.stdout.write(text.map((line) => `${line}\n`).join(''));
|
|
51
|
+
}
|
|
52
|
+
};
|
|
37
53
|
mPty.onData((data) => {
|
|
38
|
-
|
|
54
|
+
if (lines) {
|
|
55
|
+
print(lines.push(data));
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
process.stdout.write(data);
|
|
59
|
+
}
|
|
39
60
|
output.push(data.toString());
|
|
40
61
|
});
|
|
62
|
+
const resizeListener = () => {
|
|
63
|
+
mPty.resize(columns(), process.stdout.rows ?? 24);
|
|
64
|
+
};
|
|
41
65
|
const stdinListener = (data) => {
|
|
42
|
-
|
|
43
|
-
mPty.write(data.toString());
|
|
66
|
+
mPty.write(data.toString('binary'));
|
|
44
67
|
};
|
|
68
|
+
process.stdout.on('resize', resizeListener);
|
|
45
69
|
if (options?.stdin) {
|
|
46
70
|
process.stdin.on('data', stdinListener);
|
|
47
71
|
}
|
|
48
|
-
|
|
72
|
+
let timer;
|
|
73
|
+
let timedOut = false;
|
|
74
|
+
if (options?.timeout) {
|
|
75
|
+
timer = setTimeout(() => {
|
|
76
|
+
timedOut = true;
|
|
77
|
+
mPty.kill();
|
|
78
|
+
}, options.timeout);
|
|
79
|
+
}
|
|
80
|
+
mPty.onExit((exit) => {
|
|
81
|
+
clearTimeout(timer);
|
|
82
|
+
process.stdout.off('resize', resizeListener);
|
|
49
83
|
if (options?.stdin) {
|
|
50
84
|
process.stdin.off('data', stdinListener);
|
|
51
85
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
86
|
+
if (lines) {
|
|
87
|
+
print([lines.flush()].filter(Boolean));
|
|
88
|
+
}
|
|
89
|
+
// node-pty reports exitCode 0 for a command killed by a signal: use the shell's 128 + n
|
|
90
|
+
const exitCode = timedOut ? -1 : (exit.signal ? 128 + exit.signal : exit.exitCode);
|
|
91
|
+
const raw = stripAnsi(output.join('')).replaceAll('\r\n', '\n').replaceAll('\r', '\n').trim();
|
|
92
|
+
if (options?.throws && exitCode !== 0) {
|
|
93
|
+
reject(new Error(timedOut ? `Command timed out after ${options.timeout}ms: ${cmd}\n${raw}` : raw));
|
|
55
94
|
return;
|
|
56
95
|
}
|
|
57
96
|
resolve({
|
|
58
|
-
status:
|
|
59
|
-
exitCode
|
|
97
|
+
status: exitCode === 0 ? SpawnStatus.SUCCESS : SpawnStatus.ERROR,
|
|
98
|
+
exitCode,
|
|
60
99
|
data: raw,
|
|
61
100
|
});
|
|
62
101
|
});
|
package/dist/spawn.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spawn.js","sourceRoot":"","sources":["../src/spawn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,KAAK,GAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"spawn.js","sourceRoot":"","sources":["../src/spawn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAChC,OAAO,SAAS,MAAM,YAAY,CAAC;AAEnC,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,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AAC,WAAW,CAAC,KAAK;gBAChE,QAAQ;gBACR,IAAI,EAAE,GAAG;aACV,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns command output (chunks as they come from a terminal) into the lines a terminal would end up showing:
|
|
3
|
+
* - a line redrawn with `\r` or cursor-to-line-start (spinners, progress bars) keeps only its final state
|
|
4
|
+
* - spinner-only and blank lines are dropped, escape codes (colors) removed
|
|
5
|
+
* - an escape sequence split between two chunks is put back together
|
|
6
|
+
* - text drawn elsewhere on the screen (apt's progress bar on the last row, between a cursor save and restore) is dropped
|
|
7
|
+
*
|
|
8
|
+
* Only the new chunk is processed: a chunk costs its own size, however long the line in progress is.
|
|
9
|
+
*/
|
|
10
|
+
export declare class TerminalLines {
|
|
11
|
+
private partial;
|
|
12
|
+
private pendingEscape;
|
|
13
|
+
/** Adds a chunk of output and returns the lines it completed */
|
|
14
|
+
push(chunk: string): string[];
|
|
15
|
+
/** The line in progress, as a terminal shows it ('' if there is nothing to show) */
|
|
16
|
+
current(): string;
|
|
17
|
+
/** Ends the line in progress (the command finished): returns it, or '' */
|
|
18
|
+
flush(): string;
|
|
19
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Same as TerminalLines in the Codify CLI (src/plugins/terminal-lines.ts): keep them in sync
|
|
2
|
+
import { stripVTControlCharacters } from 'node:util';
|
|
3
|
+
/**
|
|
4
|
+
* Longest line in progress kept (a progress bar that never prints a newline). It may grow to twice this before it is
|
|
5
|
+
* cut, so a flood of small chunks without newlines doesn't copy the whole line for every chunk.
|
|
6
|
+
*/
|
|
7
|
+
const MAX_PARTIAL_LENGTH = 4096;
|
|
8
|
+
// An escape sequence cut off at the end of a chunk (`ESC`, `ESC[1`, an unfinished `ESC]` title): kept for the next chunk
|
|
9
|
+
// eslint-disable-next-line no-control-regex
|
|
10
|
+
const INCOMPLETE_ESCAPE = /\u001B(?:\[[\d;?]*|\][^\u0007\u001B]{0,256})?$/;
|
|
11
|
+
// Text drawn somewhere else on the screen between saving and restoring the cursor (`ESC 7` … `ESC 8`, `ESC[s` … `ESC[u`):
|
|
12
|
+
// apt's progress bar on the last row. It isn't part of the output's lines
|
|
13
|
+
// eslint-disable-next-line no-control-regex
|
|
14
|
+
const DRAWN_ELSEWHERE = /\u001B7[\s\S]*?\u001B8|\u001B\[s[\s\S]*?\u001B\[u/g;
|
|
15
|
+
// A cursor save without its restore yet (the rest is in the next chunk)
|
|
16
|
+
// eslint-disable-next-line no-control-regex
|
|
17
|
+
const UNFINISHED_DRAWN_ELSEWHERE = /(?:\u001B7|\u001B\[s)(?![\s\S]*(?:\u001B8|\u001B\[u))[\s\S]*$/;
|
|
18
|
+
/** Longest unfinished cursor save kept for the next chunk */
|
|
19
|
+
const MAX_PENDING_LENGTH = 1024;
|
|
20
|
+
// Cursor to the start of the line (`ESC[G`, `ESC[0G`, `ESC[1G`): progress bars redraw the line with it instead of \r
|
|
21
|
+
// eslint-disable-next-line no-control-regex
|
|
22
|
+
const CURSOR_TO_LINE_START = /\u001B\[[01]?G/g;
|
|
23
|
+
// A line that only holds spinner frames (npm's `⠼ ⠴ ⠦`, `| / - \`)
|
|
24
|
+
const SPINNER_LINE = /^[⠀-⣿|/\\-]+$/;
|
|
25
|
+
/**
|
|
26
|
+
* Turns command output (chunks as they come from a terminal) into the lines a terminal would end up showing:
|
|
27
|
+
* - a line redrawn with `\r` or cursor-to-line-start (spinners, progress bars) keeps only its final state
|
|
28
|
+
* - spinner-only and blank lines are dropped, escape codes (colors) removed
|
|
29
|
+
* - an escape sequence split between two chunks is put back together
|
|
30
|
+
* - text drawn elsewhere on the screen (apt's progress bar on the last row, between a cursor save and restore) is dropped
|
|
31
|
+
*
|
|
32
|
+
* Only the new chunk is processed: a chunk costs its own size, however long the line in progress is.
|
|
33
|
+
*/
|
|
34
|
+
export class TerminalLines {
|
|
35
|
+
partial = '';
|
|
36
|
+
pendingEscape = '';
|
|
37
|
+
/** Adds a chunk of output and returns the lines it completed */
|
|
38
|
+
push(chunk) {
|
|
39
|
+
let raw = (this.pendingEscape + chunk).replaceAll(DRAWN_ELSEWHERE, '');
|
|
40
|
+
const incomplete = UNFINISHED_DRAWN_ELSEWHERE.exec(raw) ?? INCOMPLETE_ESCAPE.exec(raw);
|
|
41
|
+
this.pendingEscape = incomplete && incomplete[0].length <= MAX_PENDING_LENGTH ? incomplete[0] : '';
|
|
42
|
+
if (incomplete) {
|
|
43
|
+
raw = raw.slice(0, incomplete.index);
|
|
44
|
+
}
|
|
45
|
+
// \r\n line endings: the \r stays at the end of the line, where cleanLine ignores it
|
|
46
|
+
const parts = stripVTControlCharacters(raw.replaceAll(CURSOR_TO_LINE_START, '\r')).split('\n');
|
|
47
|
+
if (parts.length === 1) {
|
|
48
|
+
this.partial += parts[0];
|
|
49
|
+
if (this.partial.length > MAX_PARTIAL_LENGTH * 2) {
|
|
50
|
+
this.partial = this.partial.slice(-MAX_PARTIAL_LENGTH);
|
|
51
|
+
}
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
const lines = [];
|
|
55
|
+
const add = (text) => {
|
|
56
|
+
const line = cleanLine(text);
|
|
57
|
+
if (line) {
|
|
58
|
+
lines.push(line);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
add(this.partial + parts[0]);
|
|
62
|
+
for (let i = 1; i < parts.length - 1; i++) {
|
|
63
|
+
add(parts[i]);
|
|
64
|
+
}
|
|
65
|
+
this.partial = parts.at(-1);
|
|
66
|
+
if (this.partial.length > MAX_PARTIAL_LENGTH * 2) {
|
|
67
|
+
this.partial = this.partial.slice(-MAX_PARTIAL_LENGTH);
|
|
68
|
+
}
|
|
69
|
+
return lines;
|
|
70
|
+
}
|
|
71
|
+
/** The line in progress, as a terminal shows it ('' if there is nothing to show) */
|
|
72
|
+
current() {
|
|
73
|
+
return cleanLine(this.partial.slice(-MAX_PARTIAL_LENGTH));
|
|
74
|
+
}
|
|
75
|
+
/** Ends the line in progress (the command finished): returns it, or '' */
|
|
76
|
+
flush() {
|
|
77
|
+
const line = this.current();
|
|
78
|
+
this.partial = '';
|
|
79
|
+
this.pendingEscape = '';
|
|
80
|
+
return line;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* What a terminal shows of a line: the text after the last carriage return (progress bars redraw the line), or ''
|
|
85
|
+
* when nothing is left to show (blank, or only spinner frames).
|
|
86
|
+
*/
|
|
87
|
+
function cleanLine(line) {
|
|
88
|
+
const segments = line.split('\r');
|
|
89
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
90
|
+
const segment = segments[i].trimEnd();
|
|
91
|
+
if (segment.trim()) {
|
|
92
|
+
return SPINNER_LINE.test(segment.trim()) ? '' : segment;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return '';
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=terminal-lines.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"terminal-lines.js","sourceRoot":"","sources":["../src/terminal-lines.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAC7F,OAAO,EAAE,wBAAwB,EAAE,MAAM,WAAW,CAAC;AAErD;;;GAGG;AACH,MAAM,kBAAkB,GAAG,IAAI,CAAC;AAEhC,yHAAyH;AACzH,4CAA4C;AAC5C,MAAM,iBAAiB,GAAG,gDAAgD,CAAC;AAE3E,0HAA0H;AAC1H,0EAA0E;AAC1E,4CAA4C;AAC5C,MAAM,eAAe,GAAG,oDAAoD,CAAC;AAE7E,wEAAwE;AACxE,4CAA4C;AAC5C,MAAM,0BAA0B,GAAG,+DAA+D,CAAC;AAEnG,6DAA6D;AAC7D,MAAM,kBAAkB,GAAG,IAAI,CAAC;AAEhC,qHAAqH;AACrH,4CAA4C;AAC5C,MAAM,oBAAoB,GAAG,iBAAiB,CAAC;AAE/C,mEAAmE;AACnE,MAA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AM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC9C,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;QACtC,IAAI,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;YACnB,OAAO,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;QAC1D,CAAC;IACH,CAAC;IAED,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
package/dist/test-utils.js
CHANGED
|
@@ -21,10 +21,7 @@ export const TestUtils = {
|
|
|
21
21
|
resolve(response.data);
|
|
22
22
|
}
|
|
23
23
|
else {
|
|
24
|
-
|
|
25
|
-
const message = errData?.message ?? String(response.data);
|
|
26
|
-
const detail = errData?.data != null ? `\nDetails: ${JSON.stringify(errData.data, null, 2)}` : '';
|
|
27
|
-
reject(new Error(`[${errData?.errorType ?? 'error'}] ${message}${detail}`));
|
|
24
|
+
reject(toError(response.data));
|
|
28
25
|
}
|
|
29
26
|
}
|
|
30
27
|
});
|
|
@@ -123,4 +120,17 @@ export const TestUtils = {
|
|
|
123
120
|
return os.platform() === 'linux';
|
|
124
121
|
},
|
|
125
122
|
};
|
|
123
|
+
/**
|
|
124
|
+
* Errors from plugins: plugin-core >= 2.0 sends `{ errorType, message, data? }` (PluginErrorData), shown as
|
|
125
|
+
* `[errorType] message` with the data (ex: the plan of an apply validation error) under `Details:`, and kept on the
|
|
126
|
+
* error (`errorType`, `data`). Older plugins send a string, used as is.
|
|
127
|
+
*/
|
|
128
|
+
function toError(data) {
|
|
129
|
+
if (data && typeof data === 'object' && typeof data.message === 'string') {
|
|
130
|
+
const { data: errorData, errorType, message } = data;
|
|
131
|
+
const details = errorData === null || errorData === undefined ? '' : `\nDetails: ${JSON.stringify(errorData, null, 2)}`;
|
|
132
|
+
return Object.assign(new Error(`[${errorType ?? 'error'}] ${message}${details}`), { data: errorData, errorType });
|
|
133
|
+
}
|
|
134
|
+
return new Error(String(data));
|
|
135
|
+
}
|
|
126
136
|
//# sourceMappingURL=test-utils.js.map
|
package/dist/test-utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-utils.js","sourceRoot":"","sources":["../src/test-utils.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,EAAE,gBAAgB,EAAgB,aAAa,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAE5G,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC;IAC1B,MAAM,EAAE,IAAI;CACb,CAAC,CAAC;AACH,MAAM,mBAAmB,GAAG,GAAG,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;AAE1D,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,2BAA2B,CAAC,OAAqB,EAAE,OAAqB;QACtE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,QAAsB,EAAE,EAAE;gBAC/C,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC,EAAE,CAAC;oBACnC,MAAM,IAAI,KAAK,CAAC,gCAAgC,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;gBACtF,CAAC;gBAED,4GAA4G;gBAC5G,IAAI,QAAQ,CAAC,SAAS,KAAK,OAAO,CAAC,SAAS,EAAE,CAAC;oBAC7C,IAAI,QAAQ,CAAC,MA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AAE,CAAC,EAAE;YACtD,CAAC,CAAC,GAAG,SAAS,CAAC,eAAe,CAAC,SAAS,SAAS,EAAE,CAAC,EAAE,CAAC;IAC3D,CAAC;IAED;;OAEG;IACH,OAAO;QACL,OAAO,EAAE,CAAC,QAAQ,EAAE,KAAK,QAAQ,CAAC;IACpC,CAAC;IAED;;OAEG;IACH,OAAO;QACL,OAAO,EAAE,CAAC,QAAQ,EAAE,KAAK,OAAO,CAAC;IACnC,CAAC;CACF,CAAC;AAEF;;;;GAIG;AACH,SAAS,OAAO,CAAC,IAAa;IAC5B,IAAI,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,OAAQ,IAA8B,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;QACpG,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,IAA+D,CAAC;QAChH,MAAM,OAAO,GAAG,SAAS,KAAK,IAAI,IAAI,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,cAAc,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;QACxH,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,IAAI,SAAS,IAAI,OAAO,KAAK,OAAO,GAAG,OAAO,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,CAAC;IACpH,CAAC;IAED,OAAO,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AACjC,CAAC"}
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import { ResourceConfig, ResourceOs, StringIndexedObject } from '@codifycli/schemas';
|
|
1
|
+
import { LinuxDistro, ResourceConfig, ResourceOs, StringIndexedObject } from '@codifycli/schemas';
|
|
2
2
|
export declare function splitUserConfig<T extends StringIndexedObject>(config: ResourceConfig & T): {
|
|
3
3
|
parameters: T;
|
|
4
4
|
coreParameters: ResourceConfig;
|
|
5
5
|
};
|
|
6
|
+
/**
|
|
7
|
+
* Whether a config applies to this machine, like the CLI's os and distro filters: `os` must include the current OS,
|
|
8
|
+
* and on Linux `distro` must match the current distribution (`debian-based` / `rpm-based` match their families).
|
|
9
|
+
* `distro` is ignored on other OSes and when the distribution is unknown.
|
|
10
|
+
*/
|
|
11
|
+
export declare function appliesToCurrentPlatform(config: Pick<ResourceConfig, 'distro' | 'os'>): boolean;
|
|
12
|
+
export declare function getLinuxDistro(): LinuxDistro | undefined;
|
|
13
|
+
export declare function distroMatches(filter: LinuxDistro, current: LinuxDistro): boolean;
|
|
6
14
|
export declare function getPlatformOs(): ResourceOs;
|
package/dist/utils.js
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
|
-
import { ResourceOs } from '@codifycli/schemas';
|
|
1
|
+
import { LinuxDistro, ResourceOs } from '@codifycli/schemas';
|
|
2
|
+
import fs from 'node:fs';
|
|
2
3
|
import os from 'node:os';
|
|
4
|
+
const DEBIAN_BASED_DISTROS = [
|
|
5
|
+
LinuxDistro.DEBIAN,
|
|
6
|
+
LinuxDistro.UBUNTU,
|
|
7
|
+
LinuxDistro.MINT,
|
|
8
|
+
LinuxDistro.POP_OS,
|
|
9
|
+
LinuxDistro.ELEMENTARY_OS,
|
|
10
|
+
LinuxDistro.KALI,
|
|
11
|
+
];
|
|
12
|
+
const RPM_BASED_DISTROS = [
|
|
13
|
+
LinuxDistro.FEDORA,
|
|
14
|
+
LinuxDistro.CENTOS,
|
|
15
|
+
LinuxDistro.RHEL,
|
|
16
|
+
LinuxDistro.AMAZON_LINUX,
|
|
17
|
+
LinuxDistro.OPENSUSE,
|
|
18
|
+
LinuxDistro.SUSE,
|
|
19
|
+
];
|
|
3
20
|
export function splitUserConfig(config) {
|
|
4
21
|
const coreParameters = {
|
|
5
22
|
type: config.type,
|
|
@@ -15,6 +32,48 @@ export function splitUserConfig(config) {
|
|
|
15
32
|
coreParameters,
|
|
16
33
|
};
|
|
17
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Whether a config applies to this machine, like the CLI's os and distro filters: `os` must include the current OS,
|
|
37
|
+
* and on Linux `distro` must match the current distribution (`debian-based` / `rpm-based` match their families).
|
|
38
|
+
* `distro` is ignored on other OSes and when the distribution is unknown.
|
|
39
|
+
*/
|
|
40
|
+
export function appliesToCurrentPlatform(config) {
|
|
41
|
+
if (config.os && config.os.length > 0 && !config.os.includes(getPlatformOs())) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
if (!config.distro || config.distro.length === 0 || os.platform() !== 'linux') {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
const current = getLinuxDistro();
|
|
48
|
+
return !current || config.distro.some((d) => distroMatches(d, current));
|
|
49
|
+
}
|
|
50
|
+
export function getLinuxDistro() {
|
|
51
|
+
for (const candidate of ['/etc/os-release', '/usr/lib/os-release']) {
|
|
52
|
+
let osRelease;
|
|
53
|
+
try {
|
|
54
|
+
osRelease = fs.readFileSync(candidate, 'utf8');
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
for (const line of osRelease.split('\n')) {
|
|
60
|
+
if (line.startsWith('ID=')) {
|
|
61
|
+
const distroId = line.slice(3).trim().replaceAll('"', '');
|
|
62
|
+
return Object.values(LinuxDistro).includes(distroId) ? distroId : undefined;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
export function distroMatches(filter, current) {
|
|
69
|
+
if (filter === LinuxDistro.DEBIAN_BASED) {
|
|
70
|
+
return DEBIAN_BASED_DISTROS.includes(current);
|
|
71
|
+
}
|
|
72
|
+
if (filter === LinuxDistro.RPM_BASED) {
|
|
73
|
+
return RPM_BASED_DISTROS.includes(current);
|
|
74
|
+
}
|
|
75
|
+
return filter === current;
|
|
76
|
+
}
|
|
18
77
|
export function getPlatformOs() {
|
|
19
78
|
const currOs = os.platform();
|
|
20
79
|
switch (currOs) {
|
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,UAAU,EAAuB,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAkB,UAAU,EAAuB,MAAM,oBAAoB,CAAC;AAClG,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AAEzB,MAAM,oBAAoB,GAAkB;IAC1C,WAAW,CAAC,MAAM;IAClB,WAAW,CAAC,MAAM;IAClB,WAAW,CAAC,IAAI;IAChB,WAAW,CAAC,MAAM;IAClB,WAAW,CAAC,aAAa;IACzB,WAAW,CAAC,IAAI;CACjB,CAAC;AAEF,MAAM,iBAAiB,GAAkB;IACvC,WAAW,CAAC,MAAM;IAClB,WAAW,CAAC,MAAM;IAClB,WAAW,CAAC,IAAI;IAChB,WAAW,CAAC,YAAY;IACxB,WAAW,CAAC,QAAQ;IACpB,WAAW,CAAC,IAAI;CACjB,CAAC;AAEF,MAAM,UAAU,eAAe,CAC7B,MAA0B;IAE1B,MAAM,cAAc,GAAG;QACrB,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC7C,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5D,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,EAA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;QAC5B,CAAC;QAED,OAAO,CAAC,CAAC,CAAC;YACR,MAAM,IAAI,KAAK,CAAC,mBAAmB,MAAM,EAAE,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codifycli/plugin-test",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Testing framework for Codify plugins with complete lifecycle testing, IPC communication, and cross-platform support",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -27,25 +27,25 @@
|
|
|
27
27
|
"prepublishOnly": "tsc"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@codifycli/schemas": "
|
|
30
|
+
"@codifycli/schemas": "2.0.0",
|
|
31
31
|
"ajv": "^8.18.0",
|
|
32
32
|
"ajv-formats": "^3.0.1",
|
|
33
|
-
"
|
|
33
|
+
"chalk": "^5.4.1",
|
|
34
34
|
"lodash.differencewith": "4.5.0",
|
|
35
|
+
"lodash.matches": "^4.6.0",
|
|
35
36
|
"lodash.unionby": "^4.8.0",
|
|
36
37
|
"nanoid": "^5.0.9",
|
|
37
|
-
"
|
|
38
|
-
"@homebridge/node-pty-prebuilt-multiarch": "^0.13.1",
|
|
38
|
+
"node-pty": "1.2.0-beta.15",
|
|
39
39
|
"strip-ansi": "^7.1.2"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@codifycli/plugin-core": "
|
|
42
|
+
"@codifycli/plugin-core": "3.0.0",
|
|
43
43
|
"@oclif/prettier-config": "^0.2.1",
|
|
44
44
|
"@types/debug": "^4.1.12",
|
|
45
|
-
"@types/node": "^22",
|
|
46
|
-
"@types/lodash.matches": "^4.6.9",
|
|
47
45
|
"@types/lodash.differencewith": "^4.5.9",
|
|
46
|
+
"@types/lodash.matches": "^4.6.9",
|
|
48
47
|
"@types/lodash.unionby": "^4.8.9",
|
|
48
|
+
"@types/node": "^22",
|
|
49
49
|
"eslint": "^8.51.0",
|
|
50
50
|
"eslint-config-oclif": "^5",
|
|
51
51
|
"eslint-config-oclif-typescript": "^3",
|
package/src/plugin-process.ts
CHANGED
|
@@ -7,8 +7,8 @@ import {
|
|
|
7
7
|
ImportRequestData,
|
|
8
8
|
ImportResponseData,
|
|
9
9
|
InitializeResponseData,
|
|
10
|
-
IpcMessageSchema,
|
|
11
10
|
IpcMessageV2,
|
|
11
|
+
IpcMessageV2Schema,
|
|
12
12
|
MessageCmd,
|
|
13
13
|
PlanRequestData,
|
|
14
14
|
PlanResponseData,
|
|
@@ -21,13 +21,19 @@ import { ChildProcess, fork } from 'node:child_process';
|
|
|
21
21
|
import path from 'node:path';
|
|
22
22
|
|
|
23
23
|
import { spawnSafe } from './spawn.js';
|
|
24
|
+
import { TerminalLines } from './terminal-lines.js';
|
|
24
25
|
import { TestUtils } from './test-utils.js';
|
|
25
26
|
|
|
26
27
|
const ajv = new Ajv.default({
|
|
27
28
|
strict: true
|
|
28
29
|
});
|
|
29
30
|
|
|
30
|
-
const ipcMessageValidator = ajv.compile(
|
|
31
|
+
const ipcMessageValidator = ajv.compile<IpcMessageV2>(IpcMessageV2Schema);
|
|
32
|
+
|
|
33
|
+
/** Plugin → CLI, no reply: `{ data }`, the plugin's stdout (plugin-core >= 2.3.0 sends it over IPC, in order with the other messages) */
|
|
34
|
+
export const OUTPUT_MESSAGE_CMD = 'output';
|
|
35
|
+
/** Plugin → CLI, no reply: `{ id, exitCode }` (plugin-core >= 2.2.0) */
|
|
36
|
+
export const COMMAND_FINISHED_MESSAGE_CMD = 'command_finished';
|
|
31
37
|
const commandRequestValidator = ajv.compile(CommandRequestDataSchema);
|
|
32
38
|
|
|
33
39
|
export class PluginProcess {
|
|
@@ -58,7 +64,11 @@ export class PluginProcess {
|
|
|
58
64
|
)
|
|
59
65
|
|
|
60
66
|
this.childProcess.stderr?.pipe(process.stderr);
|
|
61
|
-
|
|
67
|
+
// Command output (older plugins write it to stdout): printed as the lines a terminal would end up showing, so
|
|
68
|
+
// spinners and progress bars don't flood the test logs with a line per redraw
|
|
69
|
+
this.childProcess.stdout?.setEncoding('utf8');
|
|
70
|
+
this.childProcess.stdout?.on('data', (data: string) => this.printOutput(data));
|
|
71
|
+
this.childProcess.on('exit', () => this.printLastLine());
|
|
62
72
|
|
|
63
73
|
this.handleIncomingRequests(this.childProcess);
|
|
64
74
|
}
|
|
@@ -111,6 +121,23 @@ export class PluginProcess {
|
|
|
111
121
|
});
|
|
112
122
|
}
|
|
113
123
|
|
|
124
|
+
private readonly output = new TerminalLines();
|
|
125
|
+
|
|
126
|
+
private printOutput(data: string): void {
|
|
127
|
+
const lines = this.output.push(data);
|
|
128
|
+
if (lines.length > 0) {
|
|
129
|
+
process.stdout.write(lines.map((line) => `${line}\n`).join(''));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The last line of a command when it doesn't end with a newline (a prompt, a progress bar) */
|
|
134
|
+
private printLastLine(): void {
|
|
135
|
+
const line = this.output.flush();
|
|
136
|
+
if (line) {
|
|
137
|
+
process.stdout.write(`${line}\n`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
114
141
|
kill() {
|
|
115
142
|
this.childProcess.kill();
|
|
116
143
|
}
|
|
@@ -122,6 +149,21 @@ export class PluginProcess {
|
|
|
122
149
|
throw new Error(`Invalid message from plugin. ${JSON.stringify(message, null, 2)}`);
|
|
123
150
|
}
|
|
124
151
|
|
|
152
|
+
// The plugin's command output (plugin-core >= 2.3.0). command_started is ignored
|
|
153
|
+
if (message.cmd === OUTPUT_MESSAGE_CMD) {
|
|
154
|
+
const { data } = message.data as { data?: unknown };
|
|
155
|
+
if (typeof data === 'string') {
|
|
156
|
+
this.printOutput(data);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (message.cmd === COMMAND_FINISHED_MESSAGE_CMD) {
|
|
163
|
+
this.printLastLine();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
125
167
|
if (message.cmd === MessageCmd.COMMAND_REQUEST) {
|
|
126
168
|
const { data, requestId } = message;
|
|
127
169
|
if (!commandRequestValidator(data)) {
|