Repository navigation
Expand file tree
/
Copy pathdaemonGraph.test.ts
More file actions
138 lines (134 loc) · 5.56 KB
/
Copy pathdaemonGraph.test.ts
File metadata and controls
138 lines (134 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { Rush } from '@microsoft/rush-lib';
import {
createDaemonHelloAck,
DAEMON_PROTOCOL_VERSION,
DaemonFrameType,
decodeDaemonControlMessage,
encodeDaemonControlMessage,
encodeDaemonEventFrame,
RUSHD_GRAPH_SNAPSHOT,
type DaemonControlMessage
} from '@rushstack/rush-daemon-protocol';
import { DaemonFrameListener, type DaemonFrameConnection } from '@rushstack/rush-daemon-transport';
import { getDaemonConnectionOptions } from '../daemonConnectionOptions';
import { getTestProcessEnvironment } from './TestProcessEnvironment';
describe('graph client fails closed over the public wire', () => {
it.each(['unsupported', 'missing-snapshot', 'invalid-snapshot', 'old-protocol'])(
'rejects %s without invoking native Rush',
async (mode) => {
const folder: string = fs.mkdtempSync(path.join(os.tmpdir(), 'rush-client-graph-peer-'));
const peers: Set<DaemonFrameConnection> = new Set();
let listener: DaemonFrameListener | undefined;
let requests: number = 0;
try {
fs.writeFileSync(
path.join(folder, 'rush.json'),
JSON.stringify({
rushVersion: Rush.version,
suppressNodeLtsWarning: true,
npmVersion: '10.0.0',
projects: []
})
);
const { paths } = getDaemonConnectionOptions(folder, Rush.version, process.env, false);
const protocolVersion = mode === 'old-protocol' ? { major: 0, minor: 1 } : DAEMON_PROTOCOL_VERSION;
listener = await DaemonFrameListener.listenAsync(paths, {
protocolVersion,
onConnection: (connection) => {
peers.add(connection);
connection.onClosed(() => {
peers.delete(connection);
});
const send = (message: DaemonControlMessage): Promise<void> =>
connection.sendFrameAsync({
kind: DaemonFrameType.controlJson,
payload: encodeDaemonControlMessage(message)
});
connection.onFrame(async (frame) => {
const message = decodeDaemonControlMessage(frame.payload);
if (message.kind === 'hello') {
await send(createDaemonHelloAck(protocolVersion, 'old-graph-peer'));
} else if (message.kind === 'ping') {
await send({ kind: 'pong', payload: { uptimeMs: 1 } });
} else if (message.kind === 'requestStart') {
requests++;
expect(message.payload).toMatchObject({
commandOrigin: 'built-in',
commandName: 'daemon',
argv: ['daemon', 'graph', 'show']
});
const requestId: string = message.payload.requestId;
if (mode === 'unsupported') {
await send({
kind: 'requestRejected',
payload: { requestId, code: 'unsupported', message: 'No graph route.' }
});
return;
}
if (mode === 'invalid-snapshot') {
await connection.sendFrameAsync({
kind: DaemonFrameType.event,
payload: encodeDaemonEventFrame({
protocolVersion,
eventId: 'bad-event',
sessionId: 'old-graph-peer',
sequence: 1,
timestamp: new Date().toISOString(),
source: { packageName: '@rushstack/rush-daemon', packageVersion: '0.0.0' },
type: 'extension',
privacy: 'local-sensitive',
required: true,
payload: {
name: RUSHD_GRAPH_SNAPSHOT,
data: { requestId, snapshot: { initialized: 'invalid' } }
}
})
});
return;
}
await send({
kind: 'requestResult',
payload: { requestId, aborted: false, exitCode: 0, outcome: 'success' }
});
}
});
}
});
const child = spawn(
process.execPath,
[path.resolve(__dirname, '../../bin/rush-client'), 'daemon', 'graph', 'show'],
{
cwd: folder,
env: { ...getTestProcessEnvironment(), RUSH_DAEMON_EXPERIMENTAL: '1' },
stdio: ['ignore', 'pipe', 'pipe']
}
);
let stdout: string = '';
let stderr: string = '';
child.stdout.on('data', (bytes: Buffer) => {
stdout += bytes.toString();
});
child.stderr.on('data', (bytes: Buffer) => {
stderr += bytes.toString();
});
expect((await once(child, 'close'))[0]).toBe(1);
expect(stderr).toBe('');
expect(JSON.parse(stdout)).toMatchObject({ kind: 'graphError', message: expect.any(String) });
expect(stdout).not.toMatch(/using in-process|Usage: rush /);
expect(requests).toBe(mode === 'old-protocol' ? 0 : 1);
} finally {
await Promise.all(Array.from(peers, (peer) => peer.closeAsync()));
await listener?.closeAsync();
fs.rmSync(folder, { recursive: true, force: true });
}
},
15000
);
});