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: don't render spinner in non-TTY terminal #13242

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions packages/amplify-prompts/src/__tests__/spinner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { AmplifySpinner } from '../progressbars/spinner';
import { AmplifyTerminal } from '../progressbars/terminal';

jest.mock('../progressbars/terminal');
const amplifyTerminalMocked = jest.mocked(AmplifyTerminal);

beforeEach(() => {
jest.resetAllMocks();
});

describe('in non-TTY terminal', () => {
beforeEach(() => {
jest.spyOn(AmplifyTerminal.prototype, 'isTTY').mockReturnValue(false);
});

it('is noop', () => {
const spinner = new AmplifySpinner();

spinner.start('initial message');
spinner.resetMessage('other message');
spinner.stop('last message');

expect(amplifyTerminalMocked.mock.instances.length).toEqual(1);
expect(AmplifyTerminal).toHaveBeenCalledTimes(1);
expect(amplifyTerminalMocked.mock.instances[0].writeLines).toHaveBeenCalledTimes(0);
expect(amplifyTerminalMocked.mock.instances[0].cursor).toHaveBeenCalledTimes(0);
expect(amplifyTerminalMocked.mock.instances[0].getLastHeight).toHaveBeenCalledTimes(0);
expect(amplifyTerminalMocked.mock.instances[0].newLine).toHaveBeenCalledTimes(0);
});
});

describe('in TTY terminal', () => {
let spinner: AmplifySpinner;
beforeEach(() => {
jest.spyOn(AmplifyTerminal.prototype, 'isTTY').mockReturnValue(true);
spinner = new AmplifySpinner();
});

afterEach(() => {
// always stop the spinner to clear internal timer.
spinner.stop();
});

it('starts spinner renders initial message, and stops spinner', async () => {
const initialMessage = 'initial message';
const intermediateMessage = 'intermediate message';
const lastMessage = 'last message';
spinner.start(initialMessage);
spinner.resetMessage(intermediateMessage);
// wait for default rate of 50ms plus small delta to let spinner render new message.
await new Promise((resolve) => setTimeout(resolve, 50 + 10));
spinner.stop(lastMessage);
expect(amplifyTerminalMocked.mock.instances.length).toEqual(1);
expect(AmplifyTerminal).toHaveBeenCalledTimes(1);
expect(amplifyTerminalMocked.mock.instances[0].writeLines).toHaveBeenCalledTimes(3);
expect(amplifyTerminalMocked.mock.instances[0].writeLines).toHaveBeenCalledWith([{ color: '', renderString: `⠋ ${initialMessage}` }]);
expect(amplifyTerminalMocked.mock.instances[0].writeLines).toHaveBeenCalledWith([
{ color: '', renderString: `⠙ ${intermediateMessage}` },
]);
expect(amplifyTerminalMocked.mock.instances[0].writeLines).toHaveBeenCalledWith([{ color: 'green', renderString: lastMessage }]);
expect(amplifyTerminalMocked.mock.instances[0].cursor).toHaveBeenCalledTimes(2);
expect(amplifyTerminalMocked.mock.instances[0].cursor).toHaveBeenCalledWith(false);
expect(amplifyTerminalMocked.mock.instances[0].getLastHeight).toHaveBeenCalledTimes(0);
expect(amplifyTerminalMocked.mock.instances[0].newLine).toHaveBeenCalledTimes(0);
});
});
27 changes: 16 additions & 11 deletions packages/amplify-prompts/src/progressbars/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class AmplifySpinner {
* Render function
*/
private render(): void {
if (!this.terminal) {
if (!this.terminal || !this.terminal.isTTY()) {
return;
}
if (this.timer) {
Expand All @@ -52,6 +52,9 @@ export class AmplifySpinner {
if (!this.terminal) {
this.terminal = new AmplifyTerminal();
}
if (!this.terminal.isTTY()) {
return;
}
this.prefixText = text ? text.replace('\n', '') : this.prefixText;
this.terminal.cursor(false);
this.render();
Expand All @@ -61,7 +64,7 @@ export class AmplifySpinner {
* Reset spinner message
*/
resetMessage(text: string | null): void {
if (!this.terminal) {
if (!this.terminal || !this.terminal.isTTY()) {
this.start(text);
return;
}
Expand All @@ -75,16 +78,18 @@ export class AmplifySpinner {
if (!this.terminal) {
return;
}
const lines: TerminalLine[] = [
{
renderString: text || '',
color: success ? 'green' : 'red',
},
];
if (this.terminal.isTTY()) {
const lines: TerminalLine[] = [
{
renderString: text || '',
color: success ? 'green' : 'red',
},
];

clearTimeout(this.timer);
this.terminal.writeLines(lines);
this.terminal.cursor(true);
clearTimeout(this.timer);
this.terminal.writeLines(lines);
this.terminal.cursor(true);
}
this.terminal = null;
}
}