From 104f9bb034fd018334f1779dab377811c87bdcb5 Mon Sep 17 00:00:00 2001 From: Ivan Zosimov Date: Thu, 31 Mar 2022 21:19:33 +0200 Subject: [PATCH] Add unit-tests for cache-restore file --- __tests__/cache-restore.test.ts | 86 +++++++++++++++++++++++++++++++++ src/cache-utils.ts | 1 - 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 __tests__/cache-restore.test.ts diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts new file mode 100644 index 0000000..67ce0da --- /dev/null +++ b/__tests__/cache-restore.test.ts @@ -0,0 +1,86 @@ +import * as cache from '@actions/cache'; +import * as core from '@actions/core'; +import * as glob from '@actions/glob'; + +import * as cacheRestore from '../src/cache-restore'; +import * as cacheUtils from '../src/cache-utils'; +import {PackageManagerInfo} from '../src/package-managers'; + +describe('restoreCache', () => { + //Arrange + let hashFilesSpy = jest.spyOn(glob, 'hashFiles'); + let getCacheDirectoryPathSpy = jest.spyOn( + cacheUtils, + 'getCacheDirectoryPath' + ); + let restoreCacheSpy = jest.spyOn(cache, 'restoreCache'); + let infoSpy = jest.spyOn(core, 'info'); + let setOutputSpy = jest.spyOn(core, 'setOutput'); + + const packageManager = 'default'; + const cacheDependencyPath = 'some path'; + + beforeEach(() => { + getCacheDirectoryPathSpy.mockImplementation( + (PackageManager: PackageManagerInfo) => { + return new Promise(resolve => { + resolve('Some cache directory path'); + }); + } + ); + }); + + it('should throw if dependency file path is not valid', async () => { + //Arrange + hashFilesSpy.mockImplementation((somePath: string) => { + return new Promise(resolve => { + resolve(''); + }); + }); + + //Act + Assert + expect(async () => { + await cacheRestore.restoreCache(packageManager, cacheDependencyPath); + }).rejects.toThrowError( + 'Some specified paths were not resolved, unable to cache dependencies.' + ); + }); + + it('should inform if cache hit is not occured', async () => { + //Arrange + hashFilesSpy.mockImplementation((somePath: string) => { + return new Promise(resolve => { + resolve('Some File Hash'); + }); + }); + + restoreCacheSpy.mockImplementation(() => { + return new Promise(resolve => { + resolve(''); + }); + }); + + //Act + Assert + await cacheRestore.restoreCache(packageManager, cacheDependencyPath); + expect(infoSpy).toBeCalledWith(`Cache is not found`); + }); + + it('should set output if cache hit is occured', async () => { + //Arrange + hashFilesSpy.mockImplementation((somePath: string) => { + return new Promise(resolve => { + resolve('Some File Hash'); + }); + }); + + restoreCacheSpy.mockImplementation(() => { + return new Promise(resolve => { + resolve('Some cache key'); + }); + }); + + //Act + Assert + await cacheRestore.restoreCache(packageManager, cacheDependencyPath); + expect(setOutputSpy).toBeCalledWith('cache-hit', true); + }); +}); diff --git a/src/cache-utils.ts b/src/cache-utils.ts index 842ff46..13324c3 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -1,7 +1,6 @@ import * as exec from '@actions/exec'; import {supportedPackageManagers, PackageManagerInfo} from './package-managers'; - export const getCommandOutput = async (toolCommand: string) => { let {stdout, stderr, exitCode} = await exec.getExecOutput( toolCommand,