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: reset readline terminal property on close #12347

Merged
merged 2 commits into from
Mar 31, 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { $TSContext } from 'amplify-cli-core';
import { adminLoginFlow } from '../admin-login';
import { $TSContext, $TSAny } from 'amplify-cli-core';
import * as adminLogin from '../admin-login';
import { AmplifySpinner } from '@aws-amplify/amplify-prompts';

jest.mock('amplify-cli-core', () => {
Expand Down Expand Up @@ -43,12 +43,22 @@ describe('adminLoginFlow', () => {
const spinnerStartMock = jest.spyOn(AmplifySpinner.prototype, 'start');
const spinnerStopMock = jest.spyOn(AmplifySpinner.prototype, 'stop');

await adminLoginFlow(contextStub, appId, undefined, region);
await adminLogin.adminLoginFlow(contextStub, appId, undefined, region);

expect(spinnerStartMock).toBeCalledTimes(1);
expect(spinnerStartMock).toBeCalledWith('Manually enter your CLI login key:\n');

expect(spinnerStopMock).toBeCalledTimes(1);
expect(spinnerStopMock).toBeCalledWith('Successfully received Amplify Studio tokens.');
});

it('should call closeReadline', async () => {
const appId = 'appid';
const region = 'us-east-2';

const closeReadlineSpy = jest.spyOn(adminLogin as $TSAny, 'closeReadline');
await adminLogin.adminLoginFlow(contextStub, appId, undefined, region);
expect(closeReadlineSpy).toBeCalledTimes(1);
closeReadlineSpy.mockRestore();
});
});
13 changes: 9 additions & 4 deletions packages/amplify-provider-awscloudformation/src/admin-login.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import util from 'util';
import readline from 'readline';
import { Writable } from 'stream';
import { $TSContext, AmplifyError, AMPLIFY_DOCS_URL, open } from 'amplify-cli-core';
import { $TSContext, AmplifyError, AMPLIFY_DOCS_URL, open, $TSAny } from 'amplify-cli-core';
import { printer, AmplifySpinner } from '@aws-amplify/amplify-prompts';
import { adminVerifyUrl, adminBackendMap, isAmplifyAdminApp } from './utils/admin-helpers'; // eslint-disable-line
import { AdminLoginServer } from './utils/admin-login-server';
Expand Down Expand Up @@ -100,12 +100,12 @@ export const adminLoginFlow = async (context: $TSContext, appId: string, envName
await adminLoginServer.storeTokens(tokenJson, appId);
} catch (e) {
printer.error('Provided token was invalid.');
rl.close();
closeReadline(rl);
reject(new Error('Provided token was invalid.'));
return;
}
finished = true;
rl.close();
closeReadline(rl);
resolve();
})
.catch(reject);
Expand All @@ -115,7 +115,7 @@ export const adminLoginFlow = async (context: $TSContext, appId: string, envName
return;
}
finished = true;
rl.close();
closeReadline(rl);
reject();
};
});
Expand All @@ -137,3 +137,8 @@ export const adminLoginFlow = async (context: $TSContext, appId: string, envName
printer.error(`Failed to authenticate with Amplify Studio: ${e?.message || e}`);
}
};

export const closeReadline = (rl: readline.Interface): void => {
(rl as $TSAny).terminal = false;
lazpavel marked this conversation as resolved.
Show resolved Hide resolved
rl.close();
};