From a71ab2548dce0cc2cb58fb3ef60a82af11baed01 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Fri, 8 May 2020 10:33:42 +0200 Subject: [PATCH 1/2] refactor: add and use restore env utils for testing --- build/index.js | 11 ++++++++--- src/expo.ts | 16 ++++++++++------ tests/expo.test.ts | 7 ++++--- tests/install.test.ts | 9 +++++++-- tests/system.test.ts | 4 ++-- tests/utils.ts | 19 ++++++++++++++++++- 6 files changed, 49 insertions(+), 17 deletions(-) diff --git a/build/index.js b/build/index.js index 3b5cdf6c..bd409857 100644 --- a/build/index.js +++ b/build/index.js @@ -20413,9 +20413,14 @@ function authenticate(username, password) { const bin = process.platform === 'win32' ? 'expo.cmd' : 'expo'; - yield cli.exec(bin, ['login', `--username=${username}`], { - env: Object.assign(Object.assign({}, process.env), { EXPO_CLI_PASSWORD: password }), - }); + try { + yield cli.exec(bin, ['login', `--username=${username}`], { + env: Object.assign(Object.assign({}, process.env), { EXPO_CLI_PASSWORD: password }), + }); + } + catch (error) { + core.setFailed(error); + } }); } exports.authenticate = authenticate; diff --git a/src/expo.ts b/src/expo.ts index 6e875724..ac9a8eb9 100644 --- a/src/expo.ts +++ b/src/expo.ts @@ -16,10 +16,14 @@ export async function authenticate(username: string, password: string) { ? 'expo.cmd' : 'expo'; - await cli.exec(bin, ['login', `--username=${username}`], { - env: { - ...process.env, - EXPO_CLI_PASSWORD: password, - }, - }); + try { + await cli.exec(bin, ['login', `--username=${username}`], { + env: { + ...process.env, + EXPO_CLI_PASSWORD: password, + }, + }); + } catch (error) { + core.setFailed(error); + } } diff --git a/tests/expo.test.ts b/tests/expo.test.ts index 73512627..2ecc37ff 100644 --- a/tests/expo.test.ts +++ b/tests/expo.test.ts @@ -1,7 +1,7 @@ import * as core from '@actions/core'; import * as cli from '@actions/exec'; import * as expo from '../src/expo'; -import { setPlatform, resetPlatform } from './utils'; +import { setPlatform, restorePlatform, setEnv, restoreEnv } from './utils'; describe('authenticate', () => { const spy = { @@ -22,7 +22,7 @@ describe('authenticate', () => { }); it('executes login command with password through environment', async () => { - process.env['TEST_INCLUDED'] = 'hellyeah'; + setEnv('TEST_INCLUDED', 'hellyeah'); await expo.authenticate('bycedric', 'mypassword'); expect(spy.exec).toBeCalled(); expect(spy.exec.mock.calls[0][0]).toBe('expo'); @@ -33,6 +33,7 @@ describe('authenticate', () => { EXPO_CLI_PASSWORD: 'mypassword', }, }); + restoreEnv(); }); it('executes login command with `.cmd` suffix on windows', async () => { @@ -40,6 +41,6 @@ describe('authenticate', () => { await expo.authenticate('bycedric', 'mypassword'); expect(spy.exec).toBeCalled(); expect(spy.exec.mock.calls[0][0]).toBe('expo.cmd'); - resetPlatform(); + restorePlatform(); }); }); diff --git a/tests/install.test.ts b/tests/install.test.ts index ac2bd377..122c50bc 100644 --- a/tests/install.test.ts +++ b/tests/install.test.ts @@ -14,6 +14,7 @@ jest.mock('libnpm', () => registry); jest.mock('../src/cache', () => cache); import * as install from '../src/install'; +import { setEnv, restoreEnv } from './utils'; describe('resolve', () => { it('fetches exact version of expo-cli', async () => { @@ -41,19 +42,23 @@ describe('install', () => { }); describe('fromPackager', () => { + afterEach(() => { + restoreEnv(); + }); + it('resolves tool path', async () => { await install.fromPackager('3.0.10', 'npm'); expect(io.which).toBeCalledWith('npm'); }); it('creates temporary folder', async () => { - process.env['RUNNER_TEMP'] = '/temp/path'; + setEnv('RUNNER_TEMP', '/temp/path'); await install.fromPackager('latest', 'yarn'); expect(io.mkdirP).toBeCalledWith('/temp/path'); }); it('installs expo with tool', async () => { - process.env['RUNNER_TEMP'] = '/temp/path'; + setEnv('RUNNER_TEMP', '/temp/path'); io.which.mockResolvedValue('npm'); const expoPath = await install.fromPackager('beta', 'npm'); expect(expoPath).toBe('/temp/path'); diff --git a/tests/system.test.ts b/tests/system.test.ts index b143c6a8..2bebd0c8 100644 --- a/tests/system.test.ts +++ b/tests/system.test.ts @@ -1,7 +1,7 @@ import * as core from '@actions/core'; import * as cli from '@actions/exec'; import * as system from '../src/system'; -import { setPlatform, resetPlatform } from './utils'; +import { setPlatform, restorePlatform } from './utils'; describe('patchWatchers', () => { const spy = { @@ -11,7 +11,7 @@ describe('patchWatchers', () => { }; afterEach(() => { - resetPlatform(); + restorePlatform(); }); it('increses fs inotify settings with sysctl', async () => { diff --git a/tests/utils.ts b/tests/utils.ts index 9cdb9a4a..23091e5e 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -12,6 +12,23 @@ export function setPlatform(platform: NodeJS.Platform) { /** * Revert the platform to the original one. */ -export function resetPlatform() { +export function restorePlatform() { setPlatform(originalPlatform); } + +// keep track of the original environment variables +const originalEnv = { ...process.env }; + +/** + * Change the environment variable for testing purposes. + */ +export function setEnv(name: string, value: string) { + process.env[name] = value; +} + +/** + * Revert the environment variable changes. + */ +export function restoreEnv() { + process.env = originalEnv; +} From 9758573a25c5da3ec685ec3b3b757b5686b2e8aa Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Fri, 8 May 2020 10:38:40 +0200 Subject: [PATCH 2/2] feat: fail when provided credentials are incorrect --- build/index.js | 1 + src/expo.ts | 1 + tests/expo.test.ts | 10 ++++++++++ 3 files changed, 12 insertions(+) diff --git a/build/index.js b/build/index.js index bd409857..8b987b93 100644 --- a/build/index.js +++ b/build/index.js @@ -20420,6 +20420,7 @@ function authenticate(username, password) { } catch (error) { core.setFailed(error); + throw error; } }); } diff --git a/src/expo.ts b/src/expo.ts index ac9a8eb9..5a148f01 100644 --- a/src/expo.ts +++ b/src/expo.ts @@ -25,5 +25,6 @@ export async function authenticate(username: string, password: string) { }); } catch (error) { core.setFailed(error); + throw error; } } diff --git a/tests/expo.test.ts b/tests/expo.test.ts index 2ecc37ff..52d16924 100644 --- a/tests/expo.test.ts +++ b/tests/expo.test.ts @@ -7,17 +7,20 @@ describe('authenticate', () => { const spy = { exec: jest.spyOn(cli, 'exec').mockImplementation(), info: jest.spyOn(core, 'info').mockImplementation(), + fail: jest.spyOn(core, 'setFailed').mockImplementation(), }; it('skips authentication without credentials', async () => { await expo.authenticate('', ''); expect(spy.exec).not.toBeCalled(); + expect(spy.fail).not.toBeCalled(); expect(spy.info).toBeCalled(); }); it('skips authentication without password', async () => { await expo.authenticate('bycedric', ''); expect(spy.exec).not.toBeCalled(); + expect(spy.fail).not.toBeCalled(); expect(spy.info).toBeCalled(); }); @@ -43,4 +46,11 @@ describe('authenticate', () => { expect(spy.exec.mock.calls[0][0]).toBe('expo.cmd'); restorePlatform(); }); + + it('fails when credentials are incorrect', async () => { + const error = new Error('Invalid username/password. Please try again.'); + spy.exec.mockRejectedValue(error); + await expect(expo.authenticate('bycedric', 'incorrect')).rejects.toBe(error); + expect(spy.fail).toBeCalledWith(error); + }); });