Skip to content

Commit

Permalink
fix: reset readline terminal property on close (#12347)
Browse files Browse the repository at this point in the history
* fix: reset readline terminal property on close
* test: ensure closeReadline is called on admin flow
  • Loading branch information
pavellazar authored Mar 31, 2023
1 parent 3b90182 commit 56faf62
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
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;
rl.close();
};

0 comments on commit 56faf62

Please sign in to comment.