From 1c2cf9942d6eada93114da8ec515b2a291397671 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Fri, 16 Oct 2020 18:24:41 +0200 Subject: [PATCH 1/2] Username required Signed-off-by: CrazyMax --- __tests__/main.test.ts | 30 +++++++++++++++--------------- action.yml | 2 +- dist/index.js | 36 +++++++++++++----------------------- src/context.ts | 2 +- 4 files changed, 30 insertions(+), 40 deletions(-) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 44c8f4e..0b207f0 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -29,22 +29,22 @@ test('errors without password', async () => { }); test('successful with only password', async () => { - const platSpy = jest.spyOn(osm, 'platform'); - platSpy.mockImplementation(() => 'linux'); - - const setRegistrySpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setRegistry'); - const setLogoutSpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setLogout'); - const dockerSpy: jest.SpyInstance = jest.spyOn(docker, 'login'); - dockerSpy.mockImplementation(() => {}); - - const password: string = 'groundcontrol'; - process.env[`INPUT_PASSWORD`] = password; + const platSpy = jest.spyOn(osm, 'platform'); + platSpy.mockImplementation(() => 'linux'); - await run(); + const setRegistrySpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setRegistry'); + const setLogoutSpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setLogout'); + const dockerSpy: jest.SpyInstance = jest.spyOn(docker, 'login'); + dockerSpy.mockImplementation(() => {}); - expect(setRegistrySpy).toHaveBeenCalledWith(''); - expect(setLogoutSpy).toHaveBeenCalledWith(''); - expect(dockerSpy).toHaveBeenCalledWith('', '', password); + const password: string = 'groundcontrol'; + process.env[`INPUT_PASSWORD`] = password; + + await run(); + + expect(setRegistrySpy).toHaveBeenCalledWith(''); + expect(setLogoutSpy).toHaveBeenCalledWith(''); + expect(dockerSpy).toHaveBeenCalledWith('', '', password); }); test('calls docker login', async () => { @@ -66,7 +66,7 @@ test('calls docker login', async () => { process.env[`INPUT_REGISTRY`] = registry; const logout: string = 'true'; - process.env['INPUT_LOGOUT'] = logout + process.env['INPUT_LOGOUT'] = logout; await run(); diff --git a/action.yml b/action.yml index 07c82e8..3766856 100644 --- a/action.yml +++ b/action.yml @@ -12,7 +12,7 @@ inputs: required: false username: description: 'Username used to log against the Docker registry' - required: false + required: true password: description: 'Password or personal access token used to log against the Docker registry' required: true diff --git a/dist/index.js b/dist/index.js index 712a13e..aec5702 100644 --- a/dist/index.js +++ b/dist/index.js @@ -496,6 +496,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); +exports.run = void 0; const os = __importStar(__webpack_require__(87)); const core = __importStar(__webpack_require__(186)); const context_1 = __webpack_require__(842); @@ -505,19 +506,19 @@ function run() { return __awaiter(this, void 0, void 0, function* () { try { if (os.platform() !== 'linux') { - core.setFailed('Only supported on linux platform'); - return; + throw new Error('Only supported on linux platform'); } - let inputs = yield context_1.getInputs(); - stateHelper.setRegistry(inputs.registry); - stateHelper.setLogout(inputs.logout); - yield docker.login(inputs.registry, inputs.username, inputs.password); + const { registry, username, password, logout } = context_1.getInputs(); + stateHelper.setRegistry(registry); + stateHelper.setLogout(logout); + yield docker.login(registry, username, password); } catch (error) { core.setFailed(error.message); } }); } +exports.run = run; function logout() { return __awaiter(this, void 0, void 0, function* () { if (!stateHelper.logout) { @@ -3640,27 +3641,16 @@ var __importStar = (this && this.__importStar) || function (mod) { __setModuleDefault(result, mod); return result; }; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; Object.defineProperty(exports, "__esModule", { value: true }); exports.getInputs = void 0; const core = __importStar(__webpack_require__(186)); function getInputs() { - return __awaiter(this, void 0, void 0, function* () { - return { - registry: core.getInput('registry'), - username: core.getInput('username'), - password: core.getInput('password', { required: true }), - logout: core.getInput('logout') - }; - }); + return { + registry: core.getInput('registry'), + username: core.getInput('username', { required: true }), + password: core.getInput('password', { required: true }), + logout: core.getInput('logout') + }; } exports.getInputs = getInputs; //# sourceMappingURL=context.js.map diff --git a/src/context.ts b/src/context.ts index 3d24754..953ea21 100644 --- a/src/context.ts +++ b/src/context.ts @@ -10,7 +10,7 @@ export interface Inputs { export function getInputs(): Inputs { return { registry: core.getInput('registry'), - username: core.getInput('username'), + username: core.getInput('username', {required: true}), password: core.getInput('password', {required: true}), logout: core.getInput('logout') }; From 1c402b7c97716a196958e5239fb045241f9d3d95 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Fri, 16 Oct 2020 18:34:48 +0200 Subject: [PATCH 2/2] Fix tests Signed-off-by: CrazyMax --- __tests__/context.test.ts | 10 +++++++++- __tests__/main.test.ts | 23 ++++++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index a77f44b..886619b 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -2,13 +2,21 @@ import osm = require('os'); import {getInputs} from '../src/context'; +test('without username getInputs throws errors', async () => { + expect(() => { + getInputs(); + }).toThrowError('Input required and not supplied: username'); +}); + test('without password getInputs throws errors', async () => { + process.env['INPUT_USERNAME'] = 'dbowie'; expect(() => { getInputs(); }).toThrowError('Input required and not supplied: password'); }); -test('with password getInputs does not error', async () => { +test('with password and username getInputs does not error', async () => { + process.env['INPUT_USERNAME'] = 'dbowie'; process.env['INPUT_PASSWORD'] = 'groundcontrol'; expect(() => { getInputs(); diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 0b207f0..062c7c2 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -17,7 +17,7 @@ test('errors when not run on linux platform', async () => { expect(coreSpy).toHaveBeenCalledWith('Only supported on linux platform'); }); -test('errors without password', async () => { +test('errors without username', async () => { const platSpy = jest.spyOn(osm, 'platform'); platSpy.mockImplementation(() => 'linux'); @@ -25,10 +25,24 @@ test('errors without password', async () => { await run(); + expect(coreSpy).toHaveBeenCalledWith('Input required and not supplied: username'); +}); + +test('errors without password', async () => { + const platSpy = jest.spyOn(osm, 'platform'); + platSpy.mockImplementation(() => 'linux'); + + const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed'); + + const username: string = 'dbowie'; + process.env[`INPUT_USERNAME`] = username; + + await run(); + expect(coreSpy).toHaveBeenCalledWith('Input required and not supplied: password'); }); -test('successful with only password', async () => { +test('successful with username and password', async () => { const platSpy = jest.spyOn(osm, 'platform'); platSpy.mockImplementation(() => 'linux'); @@ -37,6 +51,9 @@ test('successful with only password', async () => { const dockerSpy: jest.SpyInstance = jest.spyOn(docker, 'login'); dockerSpy.mockImplementation(() => {}); + const username: string = 'dbowie'; + process.env[`INPUT_USERNAME`] = username; + const password: string = 'groundcontrol'; process.env[`INPUT_PASSWORD`] = password; @@ -44,7 +61,7 @@ test('successful with only password', async () => { expect(setRegistrySpy).toHaveBeenCalledWith(''); expect(setLogoutSpy).toHaveBeenCalledWith(''); - expect(dockerSpy).toHaveBeenCalledWith('', '', password); + expect(dockerSpy).toHaveBeenCalledWith('', username, password); }); test('calls docker login', async () => {