From b283174af6d9fb49d472d103b8d65d658e61dfd8 Mon Sep 17 00:00:00 2001 From: Sergey Dolin Date: Wed, 1 Feb 2023 13:10:38 +0100 Subject: [PATCH] Add tests --- __tests__/cache-utils.test.ts | 70 ++++++++++++++++++++++++++++++++++- __tests__/setup-go.test.ts | 26 +++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/__tests__/cache-utils.test.ts b/__tests__/cache-utils.test.ts index 7e67571..eb881e8 100644 --- a/__tests__/cache-utils.test.ts +++ b/__tests__/cache-utils.test.ts @@ -2,7 +2,13 @@ import * as exec from '@actions/exec'; import * as cache from '@actions/cache'; import * as core from '@actions/core'; import * as cacheUtils from '../src/cache-utils'; -import {PackageManagerInfo} from '../src/package-managers'; +import { + getCurrentPackageManager, + PackageManagerInfo +} from '../src/package-managers'; +import fs, {Dirent, PathLike} from 'fs'; +import {getPackageManagerInfo} from '../src/cache-utils'; +import MockInstance = jest.MockInstance; describe('getCommandOutput', () => { //Arrange @@ -213,3 +219,65 @@ describe('isCacheFeatureAvailable', () => { expect(warningSpy).toHaveBeenCalledWith(warningMessage); }); }); + +describe('isCacheEnabled', () => { + let inputs = {} as any; + const inSpy = jest.spyOn(core, 'getInput'); + const getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput'); + const readdirSyncSpy = (jest.spyOn( + fs, + 'readdirSync' + ) as unknown) as MockInstance; + + beforeAll(async () => { + inSpy.mockImplementation(name => inputs[name] || ''); + + getBooleanInputSpy.mockImplementation((name, options) => { + const trueValue = ['true', 'True', 'TRUE']; + const falseValue = ['false', 'False', 'FALSE']; + const val = core.getInput(name, options); + if (trueValue.includes(val)) return true; + if (falseValue.includes(val)) return false; + throw new TypeError( + `Input does not meet YAML 1.2 "Core Schema" specification: ${name}\n` + + `Support boolean input list: \`true | True | TRUE | false | False | FALSE\`` + ); + }); + }); + + afterEach(() => { + jest.clearAllMocks(); + inputs = {}; + }); + + afterAll(async () => { + jest.restoreAllMocks(); + }, 100000); + + it('should return false if `cache` input set to false', async () => { + inputs = {cache: 'false'}; + + const cacheEnabled = await cacheUtils.isCacheEnabled(); + expect(cacheEnabled).toBeFalsy(); + }); + + it('should return false if `cache` input set to true', async () => { + inputs = {cache: 'true'}; + + const cacheEnabled = await cacheUtils.isCacheEnabled(); + expect(cacheEnabled).toBeTruthy(); + }); + + it('should returns false if `cache` input unset and no dependencies file', async () => { + inputs = {}; + process.env['GITHUB_WORKSPACE'] = '/tmp'; + + const packageManager = getCurrentPackageManager(); + const packageManagerInfo = await getPackageManagerInfo(packageManager); + readdirSyncSpy.mockImplementation(() => [ + packageManagerInfo.dependencyFilePattern + ]); + + await expect(cacheUtils.isCacheEnabled()).resolves.toBeTruthy(); + }); +}); diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts index 2188fc9..1736117 100644 --- a/__tests__/setup-go.test.ts +++ b/__tests__/setup-go.test.ts @@ -957,4 +957,30 @@ use . } ); }); + + it('should not throw exception if `cache` is not set', async () => { + process.env['GITHUB_WORKSPACE'] = '/tmp'; + + const arch = 'x64'; + os.platform = 'darwin'; + os.arch = arch; + inputs['go-version'] = '1.17.6'; + inputs['architecture'] = os.arch; + + const setFailedSpy = jest.spyOn(core, 'setFailed'); + setFailedSpy.mockImplementation(line => { + process.stderr.write('log:' + line + '\n'); + }); + + existsSpy.mockImplementation(() => true); + let toolPath = path.normalize('/cache/go/1.17.6/x64'); + findSpy.mockImplementation(() => false); + dlSpy.mockImplementation(() => '/some/temp/path'); + extractTarSpy.mockImplementation(() => '/some/other/temp/path'); + cacheSpy.mockImplementation(() => toolPath); + execSpy.mockImplementation(() => 'go version go1.17.6'); + + await main.run(); + expect(setFailedSpy).not.toHaveBeenCalled(); + }); });