Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add check before writing to terminal #3263

Merged
merged 2 commits into from Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 31 additions & 6 deletions packages/renderer/src/lib/preferences/Util.spec.ts
Expand Up @@ -34,16 +34,41 @@ test('write array object', () => {
expect(xtermMock.write).toBeCalledTimes(2);
});

test('write invalid object', () => {
writeToTerminal(xtermMock, {} as unknown as string[], 'test');
test('write array of array object', () => {
writeToTerminal(
xtermMock,
[
['a', 'b'],
['c', 'd'],
],
'test',
);
// no error reported
expect(xtermMock.write).toBeCalledWith(expect.stringContaining('test'));
expect(xtermMock.write).toBeCalledTimes(4);
lstocchi marked this conversation as resolved.
Show resolved Hide resolved
});

test('write undefined object', () => {
writeToTerminal(xtermMock, undefined as unknown as string[], 'test');
test('write array with mixed values', () => {
writeToTerminal(xtermMock, [undefined, undefined, 'ok'], 'test');
// no error reported
expect(xtermMock.write).toBeCalledTimes(1);
lstocchi marked this conversation as resolved.
Show resolved Hide resolved
});

test('write array of array object', () => {
writeToTerminal(xtermMock, [], 'test');
// no error reported
expect(xtermMock.write).toBeCalledWith(expect.stringContaining('test'));
expect(xtermMock.write).not.toBeCalled();
});

test('write invalid object', () => {
writeToTerminal(xtermMock, {} as unknown as any[], 'test');
// it should not write as xterm.write is called with a valid string
expect(xtermMock.write).not.toBeCalled();
});

test('write undefined object', () => {
writeToTerminal(xtermMock, undefined as unknown as any[], 'test');
// it should not write as xterm.write is called with a valid string
expect(xtermMock.write).not.toBeCalled();
});

test('return default if type is not number', () => {
Expand Down
18 changes: 13 additions & 5 deletions packages/renderer/src/lib/preferences/Util.ts
Expand Up @@ -37,16 +37,24 @@ export interface IConnectionRestart {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function writeToTerminal(xterm: any, data: string[], colorPrefix: string): void {
export function writeToTerminal(xterm: any, data: any[], colorPrefix: string): void {
if (Array.isArray(data)) {
for (const it of data) {
writeMultilineString(xterm, it, colorPrefix);
}
} else {
writeArrayToTerminal(xterm, data, colorPrefix);
} else if (typeof data === 'string') {
writeMultilineString(xterm, data, colorPrefix);
}
}

function writeArrayToTerminal(xterm: any, data: any[], colorPrefix: string) {
for (const content of data) {
if (Array.isArray(content)) {
writeArrayToTerminal(xterm, content, colorPrefix);
} else if (typeof content === 'string') {
writeMultilineString(xterm, content, colorPrefix);
}
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function writeMultilineString(xterm: any, data: string, colorPrefix: string): void {
if (data?.includes?.('\n')) {
Expand Down