1.7 KB48 lines
Blame
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import {processTerminalLines} from '../terminalOutput';
9
10describe('terminalOutput', () => {
11 describe('handles \\r', () => {
12 function lines(s: string): Array<string> {
13 // lines from process output may be flushed at different times, not necessarily only at `\n`
14 return s.split(/([\r\n])/);
15 }
16 it('handles normal lines', () => {
17 expect(processTerminalLines(lines('foo\nbar\nbaz'))).toEqual(['foo', 'bar', 'baz']);
18 expect(processTerminalLines(lines('foo\nbar\nbaz\n'))).toEqual(['foo', 'bar', 'baz']);
19 });
20
21 it('handles \\r\\n as line endings', () => {
22 expect(processTerminalLines(lines('foo\r\nbar\r\nbaz\r\n'))).toEqual(['foo', 'bar', 'baz']);
23 });
24
25 it('erases earlier parts of lines using \\r', () => {
26 expect(
27 processTerminalLines(
28 lines('foo\nProgress 0%\rProgress 33%\rProgress 66%\rProgress 100%\nDone!\n'),
29 ),
30 ).toEqual(['foo', 'Progress 100%', 'Done!']);
31 expect(
32 processTerminalLines(
33 lines('foo\nProgress 0%\rProgress 33%\rProgress 66%\rProgress 100%\r\nDone!\n'),
34 ),
35 ).toEqual(['foo', 'Progress 100%', 'Done!']);
36 });
37
38 it('trailing \\r still shows progress', () => {
39 expect(processTerminalLines(lines('foo\nProgress 0%\rProgress 33%\rProgress 66%\r'))).toEqual(
40 ['foo', 'Progress 66%'],
41 );
42 expect(
43 processTerminalLines(lines('foo\nProgress 0%\rProgress 33%\rProgress 66%\r\n')),
44 ).toEqual(['foo', 'Progress 66%']);
45 });
46 });
47});
48