4
import * as p from '@clack/prompts';
5
import { CANCEL_SYMBOL } from '@clack/core';
6
import stringWidth from 'fast-string-width';
7
-import { Data, Effect } from 'effect';
7
+import { assert } from '@effect/vitest';
8
+import { Cause, Data, Effect, Exit, Fiber } from 'effect';
9
import {
10
clampSpinnerMessage,
11
getTerminalCapabilities,
186
);
187
});
188
189
+ describe('a stdout write that fails', () => {
190
+ /**
191
+ * A redirected stdout whose every write fails with `code`, as the OS reports it. An `error`
192
+ * event nothing listens to, which would crash the process, is recorded in `unhandled`.
193
+ */
194
+ const failingStdout = (code: string) => {
195
+ const attempts: string[] = [];
196
+ const unhandled: unknown[] = [];
197
+ const sink = new Writable({
198
+ write(chunk, _encoding, callback) {
199
+ attempts.push(String(chunk));
200
+ callback(Object.assign(new Error(`${code}: write failed`), { code }));
201
+ },
202
+ });
203
+ const emit = sink.emit.bind(sink);
204
+ sink.emit = ((event: string | symbol, ...args: unknown[]) => {
205
+ if (event === 'error' && sink.listenerCount('error') === 0) {
206
+ unhandled.push(args[0]);
207
+ return false;
208
+ }
209
+ return emit(event, ...args);
210
+ }) as typeof sink.emit;
211
+ return Object.assign(sink, { isTTY: false, attempts, unhandled });
212
+ };
213
+
214
+ /** Lets the stream emit what it deferred to the next turn of the event loop. */
215
+ const settle = Effect.promise(() => new Promise<void>(resolve => setImmediate(resolve)));
216
+
217
+ it.effect('drops the data once the reader closed stdout (EPIPE), and the command goes on', () =>
218
+ Effect.gen(function* () {
219
+ const stdout = failingStdout('EPIPE');
220
+ const ui = makeTerminalUI({ stdin: { isTTY: false }, stdout, stderr: makeSink(false) });
221
+
222
+ yield* ui.output('{"first":true}');
223
+ yield* ui.output('{"second":true}');
224
+
225
+ yield* settle;
226
+
227
+ // Nothing more can reach a reader that went away, and its error does not crash the CLI.
228
+ expect(stdout.attempts).toEqual(['{"first":true}\n']);
229
+ expect(stdout.unhandled).toEqual([]);
230
+ })
231
+ );
232
+
233
+ it.effect('completes stdoutClosed once the reader closed stdout (EPIPE)', () =>
234
+ Effect.gen(function* () {
235
+ const ui = makeTerminalUI({
236
+ stdin: { isTTY: false },
237
+ stdout: failingStdout('EPIPE'),
238
+ stderr: makeSink(false),
239
+ });
240
+ const closed = yield* Effect.forkChild(ui.stdoutClosed);
241
+ yield* Effect.yieldNow;
242
+ expect(closed.pollUnsafe()).toBeUndefined();
243
+
244
+ yield* ui.output('{"first":true}');
245
+
246
+ expect(Exit.isSuccess(yield* Fiber.await(closed))).toBe(true);
247
+ })
248
+ );
249
+
250
+ it.effect('leaves stdoutClosed pending while the reader takes the data', () =>
251
+ Effect.gen(function* () {
252
+ const ui = makeTerminalUI({
253
+ stdin: { isTTY: false },
254
+ stdout: makeSink(false),
255
+ stderr: makeSink(false),
256
+ });
257
+ const closed = yield* Effect.forkChild(ui.stdoutClosed);
258
+
259
+ yield* ui.output('{"first":true}');
260
+ yield* settle;
261
+
262
+ expect(closed.pollUnsafe()).toBeUndefined();
263
+ yield* Fiber.interrupt(closed);
264
+ })
265
+ );
266
+
267
+ it.effect('is a defect for any other failure', () =>
268
+ Effect.gen(function* () {
269
+ const stdout = failingStdout('EIO');
270
+ const ui = makeTerminalUI({ stdin: { isTTY: false }, stdout, stderr: makeSink(false) });
271
+
272
+ const exit = yield* Effect.exit(ui.output('{"machine":"data"}'));
273
+
274
+ assert(Exit.isFailure(exit));
275
+ expect(Cause.hasDies(exit.cause)).toBe(true);
276
+ expect(Cause.squash(exit.cause)).toMatchObject({ code: 'EIO' });
277
+ })
278
+ );
279
+ });
280
+
281
describe('clampSpinnerMessage keeps live spinner messages on one terminal row', () => {
282
it('leaves a message that fits untouched', () => {
283
expect(clampSpinnerMessage(80, 'Checking for updates')).toBe('Checking for updates');