From 62670df2395c041d13920cdb679bf87f765a5ca8 Mon Sep 17 00:00:00 2001 From: Bryan Clark Date: Wed, 7 Oct 2020 22:31:22 -0700 Subject: [PATCH] test main --- __tests__/main.test.ts | 76 ++++++++++++++++++++++++++++++++++++++++++ src/main.ts | 5 ++- 2 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 __tests__/main.test.ts diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts new file mode 100644 index 0000000..44c8f4e --- /dev/null +++ b/__tests__/main.test.ts @@ -0,0 +1,76 @@ +import osm = require('os'); + +import {run} from '../src/main'; +import * as docker from '../src/docker'; +import * as stateHelper from '../src/state-helper'; + +import * as core from '@actions/core'; + +test('errors when not run on linux platform', async () => { + const platSpy = jest.spyOn(osm, 'platform'); + platSpy.mockImplementation(() => 'netbsd'); + + const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed'); + + await run(); + + expect(coreSpy).toHaveBeenCalledWith('Only supported on linux platform'); +}); + +test('errors without password', async () => { + const platSpy = jest.spyOn(osm, 'platform'); + platSpy.mockImplementation(() => 'linux'); + + const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed'); + + await run(); + + expect(coreSpy).toHaveBeenCalledWith('Input required and not supplied: password'); +}); + +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; + + await run(); + + expect(setRegistrySpy).toHaveBeenCalledWith(''); + expect(setLogoutSpy).toHaveBeenCalledWith(''); + expect(dockerSpy).toHaveBeenCalledWith('', '', password); +}); + +test('calls docker login', 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 username: string = 'dbowie'; + process.env[`INPUT_USERNAME`] = username; + + const password: string = 'groundcontrol'; + process.env[`INPUT_PASSWORD`] = password; + + const registry: string = 'https://ghcr.io'; + process.env[`INPUT_REGISTRY`] = registry; + + const logout: string = 'true'; + process.env['INPUT_LOGOUT'] = logout + + await run(); + + expect(setRegistrySpy).toHaveBeenCalledWith(registry); + expect(setLogoutSpy).toHaveBeenCalledWith(logout); + expect(dockerSpy).toHaveBeenCalledWith(registry, username, password); +}); diff --git a/src/main.ts b/src/main.ts index a62adf8..06ff25c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,11 +4,10 @@ import {getInputs, Inputs} from './context'; import * as docker from './docker'; import * as stateHelper from './state-helper'; -async function run(): Promise { +export async function run(): Promise { try { if (os.platform() !== 'linux') { - core.setFailed('Only supported on linux platform'); - return; + throw new Error('Only supported on linux platform'); } const {registry, username, password, logout} = getInputs();