Repository navigation
Expand file tree
/
Copy pathstartupPendingFallback.test.ts
More file actions
177 lines (164 loc) · 7.71 KB
/
Copy pathstartupPendingFallback.test.ts
File metadata and controls
177 lines (164 loc) · 7.71 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
jest.mock('@microsoft/rush/lib/start', () => ({}));
jest.mock('@rushstack/rush-client-core', () => ({
...jest.requireActual('@rushstack/rush-client-core'),
connectOrAwaitDaemonStartupAsync: jest.fn(),
connectToStartingDaemonAsync: jest.fn()
}));
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {
DaemonClientError,
DaemonStartupPendingError,
connectOrAwaitDaemonStartupAsync,
connectToStartingDaemonAsync,
getDaemonLogFilePath
} from '@rushstack/rush-client-core';
import type { IDaemonPaths } from '@rushstack/rush-daemon-transport';
import { AgentProgressRenderer } from '../AgentProgressRenderer';
import * as connectionOptions from '../daemonConnectionOptions';
import { launchClientAsync } from '../launchClient';
describe('a daemon startup failure', () => {
let folder: string;
let originalArgv: string[];
let originalEnvironment: NodeJS.ProcessEnv;
let output: jest.SpyInstance;
beforeEach(() => {
folder = fs.mkdtempSync(path.join(os.tmpdir(), 'rush-client-startup-pending-'));
originalArgv = process.argv;
originalEnvironment = process.env;
fs.writeFileSync(
path.join(folder, 'rush.json'),
JSON.stringify({ rushVersion: '5.178.1', pnpmVersion: '10.27.0', projects: [] })
);
jest.spyOn(connectionOptions, 'getDaemonConnectionOptionsAsync').mockResolvedValue({
paths: {
runtimeDir: folder,
socketPath: path.join(folder, 'd.sock'),
lockfilePath: path.join(folder, 'daemon.pid.json')
}
});
output = jest.spyOn(process.stderr, 'write').mockReturnValue(true);
jest.spyOn(process, 'cwd').mockReturnValue(folder);
process.argv = [process.execPath, 'rush-client', 'build', '--to', 'project'];
process.env = { ...originalEnvironment, CI: 'false', RUSH_DAEMON: '1', RUSH_REPORTER: 'legacy' };
delete process.env.RUSH_PREVIEW_VERSION;
});
afterEach(() => {
process.argv = originalArgv;
process.env = originalEnvironment;
jest.restoreAllMocks();
jest.mocked(connectOrAwaitDaemonStartupAsync).mockReset();
jest.mocked(connectToStartingDaemonAsync).mockReset();
fs.rmSync(folder, { recursive: true });
});
it('fails the command instead of running Rush in-process while daemon startup is pending', async () => {
jest
.mocked(connectOrAwaitDaemonStartupAsync)
.mockRejectedValue(new DaemonStartupPendingError('Another client is still starting the daemon.'));
await expect(launchClientAsync(false)).rejects.toThrow('Another client is still starting the daemon.');
expect(connectOrAwaitDaemonStartupAsync).toHaveBeenCalledTimes(1);
expect(process.argv).toEqual([process.execPath, 'rush-client', 'build', '--to', 'project']);
expect(output).not.toHaveBeenCalledWith(expect.stringContaining('using in-process Rush'));
});
it('says why it keeps waiting for a daemon that is still starting', async () => {
const pending: DaemonStartupPendingError = new DaemonStartupPendingError('Still starting.');
jest.mocked(connectOrAwaitDaemonStartupAsync).mockImplementation(async (options) => {
options.onAwaitStartup?.('Its startup helper (PID 42) is still waiting for the daemon', 15000);
throw pending;
});
await expect(launchClientAsync(false)).rejects.toBe(pending);
expect(output).toHaveBeenCalledWith(
'rush-client: The daemon is not ready yet. Its startup helper (PID 42) is still waiting for the daemon, ' +
'so this command waits up to 15 s more for it instead of running Rush in-process.\n'
);
// Agent output shows it as the phase of its progress line instead, and names the live process there.
output.mockClear();
const write: jest.Mock = jest.fn();
const renderer: AgentProgressRenderer = new AgentProgressRenderer({
commandName: 'build',
isTTY: false,
columns: 80,
write
});
await expect(launchClientAsync(false, renderer)).rejects.toBe(pending);
expect(write).toHaveBeenCalledWith(
expect.stringContaining(
'rushd is still starting; waiting for it (up to 15s more) because its startup helper (PID 42) is ' +
'still waiting for the daemon'
)
);
expect(output).not.toHaveBeenCalledWith(expect.stringContaining('The daemon is not ready yet'));
});
it('still runs Rush in-process after a startup failure that no live process owns', async () => {
jest
.mocked(connectOrAwaitDaemonStartupAsync)
.mockRejectedValue(new DaemonClientError('startupFailed', 'No ready daemon; auto-start is disabled.'));
await launchClientAsync(false);
expect(process.argv[1]).toBe(
path.join(path.dirname(require.resolve('@microsoft/rush/package.json')), 'bin/rush')
);
expect(process.argv.slice(2)).toEqual(['build', '--to', 'project']);
expect(output).toHaveBeenCalledWith(
'rush-client: No ready daemon; auto-start is disabled; using in-process Rush.\n'
);
});
it('fails daemon stop without stopping a daemon that is still starting, and says to run stop again', async () => {
process.argv = [process.execPath, 'rush-client', 'daemon', 'stop'];
const pending: string = `The daemon at ${path.join(folder, 'd.sock')} is still starting after 15 s. Another client is still starting the daemon.`;
jest.mocked(connectToStartingDaemonAsync).mockImplementation(async (options) => {
options.onAwaitStartup?.('Another client is still starting the daemon', 15000);
throw new DaemonStartupPendingError(pending);
});
const error: Error = await launchClientAsync(false).then(
() => new Error('Expected daemon stop to fail.'),
(failure: Error) => failure
);
expect(error.message).toBe(
`${pending} It was not stopped; run "rush-client daemon stop" again once "rush-client daemon status" reports it ready.`
);
expect(connectToStartingDaemonAsync).toHaveBeenCalledTimes(1);
expect(output.mock.calls).toEqual([
[
'rush-client: The daemon is not ready yet. Another client is still starting the daemon, ' +
'so stop waits up to 15 s for it to become ready and then stops it.\n'
]
]);
expect(connectOrAwaitDaemonStartupAsync).not.toHaveBeenCalled();
});
(process.platform === 'win32' ? it.skip : it)(
'runs Rush in-process when auto-start cannot open the launcher log, and says why',
async () => {
jest
.mocked(connectOrAwaitDaemonStartupAsync)
.mockImplementation(
jest.requireActual('@rushstack/rush-client-core').connectOrAwaitDaemonStartupAsync
);
const paths: IDaemonPaths = {
runtimeDir: folder,
socketPath: path.join(folder, 'd.sock'),
lockfilePath: path.join(folder, 'daemon.pid.json')
};
jest.mocked(connectionOptions.getDaemonConnectionOptionsAsync).mockResolvedValue({
paths,
startCommand: { command: process.execPath, args: ['-e', ''], cwd: folder, environment: {} }
});
const logFilePath: string = getDaemonLogFilePath(paths);
fs.symlinkSync(`${logFilePath}.target`, logFilePath);
await launchClientAsync(false);
expect(process.argv[1]).toBe(
path.join(path.dirname(require.resolve('@microsoft/rush/package.json')), 'bin/rush')
);
expect(process.argv.slice(2)).toEqual(['build', '--to', 'project']);
// The reason and the path; the tests above pin the rest of the fallback line.
expect(output).toHaveBeenCalledWith(
expect.stringContaining(
`rush-client: Launcher log cannot be opened for writing (ELOOP): ${logFilePath}`
)
);
expect(fs.existsSync(`${logFilePath}.target`)).toBe(false);
}
);
});