|
1 | 1 | import * as cache from '@actions/cache'; |
2 | 2 | import * as core from '@actions/core'; |
3 | 3 | import * as glob from '@actions/glob'; |
| 4 | +import fs from 'fs'; |
4 | 5 |
|
5 | 6 | import * as cacheRestore from '../src/cache-restore'; |
6 | 7 | import * as cacheUtils from '../src/cache-utils'; |
7 | 8 | import {PackageManagerInfo} from '../src/package-managers'; |
8 | 9 |
|
9 | 10 | describe('restoreCache', () => { |
10 | | - //Arrange |
11 | | - const hashFilesSpy = jest.spyOn(glob, 'hashFiles'); |
12 | | - const getCacheDirectoryPathSpy = jest.spyOn( |
13 | | - cacheUtils, |
14 | | - 'getCacheDirectoryPath' |
15 | | - ); |
16 | | - const restoreCacheSpy = jest.spyOn(cache, 'restoreCache'); |
17 | | - const infoSpy = jest.spyOn(core, 'info'); |
18 | | - const setOutputSpy = jest.spyOn(core, 'setOutput'); |
| 11 | + let hashFilesSpy: jest.SpyInstance; |
| 12 | + let getCacheDirectoryPathSpy: jest.SpyInstance; |
| 13 | + let restoreCacheSpy: jest.SpyInstance; |
| 14 | + let infoSpy: jest.SpyInstance; |
| 15 | + let setOutputSpy: jest.SpyInstance; |
19 | 16 |
|
20 | 17 | const versionSpec = '1.13.1'; |
21 | 18 | const packageManager = 'default'; |
22 | 19 | const cacheDependencyPath = 'path'; |
23 | 20 |
|
| 21 | + let originalWorkspace: string | undefined; |
| 22 | + |
24 | 23 | beforeEach(() => { |
| 24 | + originalWorkspace = process.env.GITHUB_WORKSPACE; |
| 25 | + process.env.GITHUB_WORKSPACE = '/test/workspace'; |
| 26 | + //Arrange |
| 27 | + hashFilesSpy = jest.spyOn(glob, 'hashFiles'); |
| 28 | + getCacheDirectoryPathSpy = jest.spyOn(cacheUtils, 'getCacheDirectoryPath'); |
| 29 | + restoreCacheSpy = jest.spyOn(cache, 'restoreCache'); |
| 30 | + infoSpy = jest.spyOn(core, 'info'); |
| 31 | + setOutputSpy = jest.spyOn(core, 'setOutput'); |
| 32 | + |
25 | 33 | getCacheDirectoryPathSpy.mockImplementation( |
26 | 34 | (PackageManager: PackageManagerInfo) => { |
27 | | - return new Promise<string[]>(resolve => { |
28 | | - resolve(['cache_directory_path', 'cache_directory_path']); |
29 | | - }); |
| 35 | + return Promise.resolve([ |
| 36 | + 'cache_directory_path', |
| 37 | + 'cache_directory_path' |
| 38 | + ]); |
30 | 39 | } |
31 | 40 | ); |
32 | 41 | }); |
33 | 42 |
|
34 | | - it('should throw if dependency file path is not valid', async () => { |
35 | | - //Arrange |
36 | | - hashFilesSpy.mockImplementation((somePath: string) => { |
37 | | - return new Promise<string>(resolve => { |
38 | | - resolve(''); |
39 | | - }); |
40 | | - }); |
| 43 | + afterEach(() => { |
| 44 | + process.env.GITHUB_WORKSPACE = originalWorkspace; |
| 45 | + jest.restoreAllMocks(); |
| 46 | + }); |
41 | 47 |
|
42 | | - //Act + Assert |
43 | | - await expect(async () => { |
44 | | - await cacheRestore.restoreCache( |
| 48 | + it('should throw if dependency file path is not valid', async () => { |
| 49 | + // Arrange |
| 50 | + hashFilesSpy.mockImplementation(() => Promise.resolve('')); |
| 51 | + // Act + Assert |
| 52 | + await expect( |
| 53 | + cacheRestore.restoreCache( |
45 | 54 | versionSpec, |
46 | 55 | packageManager, |
47 | 56 | cacheDependencyPath |
48 | | - ); |
49 | | - }).rejects.toThrow( |
| 57 | + ) |
| 58 | + ).rejects.toThrow( |
50 | 59 | 'Some specified paths were not resolved, unable to cache dependencies.' |
51 | 60 | ); |
52 | 61 | }); |
53 | 62 |
|
54 | | - it('should inform if cache hit is not occured', async () => { |
55 | | - //Arrange |
56 | | - hashFilesSpy.mockImplementation((somePath: string) => { |
57 | | - return new Promise<string>(resolve => { |
58 | | - resolve('file_hash'); |
59 | | - }); |
60 | | - }); |
61 | | - |
62 | | - restoreCacheSpy.mockImplementation(() => { |
63 | | - return new Promise<string>(resolve => { |
64 | | - resolve(''); |
65 | | - }); |
66 | | - }); |
67 | | - |
68 | | - //Act + Assert |
| 63 | + it('should inform if cache hit is not occurred', async () => { |
| 64 | + // Arrange |
| 65 | + hashFilesSpy.mockImplementation(() => Promise.resolve('file_hash')); |
| 66 | + restoreCacheSpy.mockImplementation(() => Promise.resolve('')); |
| 67 | + // Act + Assert |
69 | 68 | await cacheRestore.restoreCache( |
70 | 69 | versionSpec, |
71 | 70 | packageManager, |
72 | 71 | cacheDependencyPath |
73 | 72 | ); |
74 | | - expect(infoSpy).toHaveBeenCalledWith(`Cache is not found`); |
| 73 | + expect(infoSpy).toHaveBeenCalledWith('Cache is not found'); |
75 | 74 | }); |
76 | 75 |
|
77 | | - it('should set output if cache hit is occured', async () => { |
78 | | - //Arrange |
79 | | - hashFilesSpy.mockImplementation((somePath: string) => { |
80 | | - return new Promise<string>(resolve => { |
81 | | - resolve('file_hash'); |
82 | | - }); |
83 | | - }); |
84 | | - |
85 | | - restoreCacheSpy.mockImplementation(() => { |
86 | | - return new Promise<string>(resolve => { |
87 | | - resolve('cache_key'); |
88 | | - }); |
89 | | - }); |
90 | | - |
91 | | - //Act + Assert |
| 76 | + it('should set output if cache hit is occurred', async () => { |
| 77 | + // Arrange |
| 78 | + hashFilesSpy.mockImplementation(() => Promise.resolve('file_hash')); |
| 79 | + restoreCacheSpy.mockImplementation(() => Promise.resolve('cache_key')); |
| 80 | + // Act + Assert |
92 | 81 | await cacheRestore.restoreCache( |
93 | 82 | versionSpec, |
94 | 83 | packageManager, |
95 | 84 | cacheDependencyPath |
96 | 85 | ); |
97 | 86 | expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true); |
98 | 87 | }); |
| 88 | + |
| 89 | + it('should throw if dependency file is not found in workspace', async () => { |
| 90 | + jest.spyOn(fs, 'readdirSync').mockReturnValue(['main.go'] as any); |
| 91 | + |
| 92 | + await expect( |
| 93 | + cacheRestore.restoreCache( |
| 94 | + versionSpec, |
| 95 | + packageManager |
| 96 | + // No cacheDependencyPath |
| 97 | + ) |
| 98 | + ).rejects.toThrow( |
| 99 | + 'Dependencies file is not found in /test/workspace. Supported file pattern: go.mod' |
| 100 | + ); |
| 101 | + }); |
99 | 102 | }); |
0 commit comments