From 36ba9a6dd4415ea1dae2ed173172292c927abbf0 Mon Sep 17 00:00:00 2001 From: Sundar Guntnur Date: Tue, 23 Mar 2021 13:42:31 +0530 Subject: [PATCH] Added tests for run function. --- __tests__/run.test.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/__tests__/run.test.ts b/__tests__/run.test.ts index 6de81f5..492546d 100644 --- a/__tests__/run.test.ts +++ b/__tests__/run.test.ts @@ -3,6 +3,7 @@ import * as os from 'os'; import * as toolCache from '@actions/tool-cache'; import * as fs from 'fs'; import * as path from 'path'; +import * as core from '@actions/core'; describe('Testing all functions in run file.', () => { test('getExecutableExtension() - return .exe when os is Windows', () => { @@ -104,4 +105,37 @@ describe('Testing all functions in run file.', () => { expect(fs.chmodSync).toBeCalledWith(path.join('pathToCachedTool', 'kubectl.exe'), '777'); expect(toolCache.downloadTool).not.toBeCalled(); }); + + test('run() - download specified version and set output', async () => { + jest.spyOn(core, 'getInput').mockReturnValue('v1.15.5'); + jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool'); + jest.spyOn(os, 'type').mockReturnValue('Windows_NT'); + jest.spyOn(fs, 'chmodSync').mockImplementation(); + jest.spyOn(core, 'addPath').mockImplementation(); + jest.spyOn(console, 'log').mockImplementation(); + jest.spyOn(core, 'setOutput').mockImplementation(); + + expect(await run.run()).toBeUndefined(); + expect(core.getInput).toBeCalledWith('version', { 'required': true }); + expect(core.addPath).toBeCalledWith('pathToCachedTool'); + expect(core.setOutput).toBeCalledWith('kubectl-path', path.join('pathToCachedTool', 'kubectl.exe')); + }); + + test('run() - get latest version, download it and set output', async () => { + jest.spyOn(core, 'getInput').mockReturnValue('latest'); + jest.spyOn(toolCache, 'downloadTool').mockReturnValue(Promise.resolve('pathToTool')); + jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4'); + jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool'); + jest.spyOn(os, 'type').mockReturnValue('Windows_NT'); + jest.spyOn(fs, 'chmodSync').mockImplementation(); + jest.spyOn(core, 'addPath').mockImplementation(); + jest.spyOn(console, 'log').mockImplementation(); + jest.spyOn(core, 'setOutput').mockImplementation(); + + expect(await run.run()).toBeUndefined(); + expect(toolCache.downloadTool).toBeCalledWith('https://storage.googleapis.com/kubernetes-release/release/stable.txt'); + expect(core.getInput).toBeCalledWith('version', { 'required': true }); + expect(core.addPath).toBeCalledWith('pathToCachedTool'); + expect(core.setOutput).toBeCalledWith('kubectl-path', path.join('pathToCachedTool', 'kubectl.exe')); + }); }); \ No newline at end of file