From d8930c523de4047308b5f09994082f10de6bf863 Mon Sep 17 00:00:00 2001 From: Ivan Zosimov Date: Thu, 7 Apr 2022 13:13:11 +0200 Subject: [PATCH] Add unit tests to the new feature --- __tests__/cache-utils.test.ts | 73 +++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/__tests__/cache-utils.test.ts b/__tests__/cache-utils.test.ts index b9c8ff9..9210d4c 100644 --- a/__tests__/cache-utils.test.ts +++ b/__tests__/cache-utils.test.ts @@ -1,4 +1,6 @@ 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'; @@ -104,3 +106,74 @@ describe('getCacheDirectoryPath', () => { }).rejects.toThrow(); }); }); + +describe('isCacheFeatureAvailable', () => { + //Arrange + let isFeatureAvailableSpy = jest.spyOn(cache, 'isFeatureAvailable'); + let warningSpy = jest.spyOn(core, 'warning'); + + it('should return true when cache feature is available', () => { + //Arrange + isFeatureAvailableSpy.mockImplementation(() => { + return true; + }); + + let functionResult; + + //Act + functionResult = cacheUtils.isCacheFeatureAvailable(); + + //Assert + expect(functionResult).toBeTruthy(); + }); + + it('should warn when cache feature is unavailable and GHES is not used ', () => { + //Arrange + isFeatureAvailableSpy.mockImplementation(() => { + return false; + }); + + process.env['GITHUB_SERVER_URL'] = 'https://github.com'; + + let warningMessage = + 'The runner was not able to contact the cache service. Caching will be skipped'; + + //Act + cacheUtils.isCacheFeatureAvailable(); + + //Assert + expect(warningSpy).toHaveBeenCalledWith(warningMessage); + }); + + it('should return false when cache feature is unavailable', () => { + //Arrange + isFeatureAvailableSpy.mockImplementation(() => { + return false; + }); + + process.env['GITHUB_SERVER_URL'] = 'https://github.com'; + + let functionResult; + + //Act + functionResult = cacheUtils.isCacheFeatureAvailable(); + + //Assert + expect(functionResult).toBeFalsy(); + }); + + it('should throw when cache feature is unavailable and GHES is used', () => { + //Arrange + isFeatureAvailableSpy.mockImplementation(() => { + return false; + }); + + process.env['GITHUB_SERVER_URL'] = 'https://nongithub.com'; + + let errorMessage = + 'Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'; + + //Act + Assert + expect(() => cacheUtils.isCacheFeatureAvailable()).toThrow(errorMessage); + }); +});