Skip to content

Commit 01aec7b

Browse files
add tests to cover node's caching
1 parent 38d335a commit 01aec7b

File tree

15 files changed

+23229
-11714
lines changed

15 files changed

+23229
-11714
lines changed

.github/workflows/build-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
- run: npm run build
2828
- run: npm run format-check
2929
- run: npm test
30+
- run: git add .
3031
- name: Verify no unstaged changes
3132
if: runner.os != 'windows'
3233
run: __tests__/verify-no-unstaged-changes.sh

.github/workflows/versions.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,50 @@ jobs:
109109
architecture: 'x86'
110110
- name: Verify node
111111
run: __tests__/verify-arch.sh "ia32"
112+
shell: bash
113+
114+
node-npm-depencies-caching:
115+
runs-on: ${{ matrix.os }}
116+
strategy:
117+
fail-fast: false
118+
matrix:
119+
os: [ubuntu-latest, windows-latest, macos-latest]
120+
node-version: [10, 12, 14]
121+
steps:
122+
- uses: actions/checkout@v2
123+
- name: Setup Node
124+
uses: ./
125+
with:
126+
node-version: ${{ matrix.node-version }}
127+
cache: 'npm'
128+
- name: Install dependencies
129+
run: npm install
130+
- name: Verify node and npm
131+
run: __tests__/verify-node.sh "${{ matrix.node-version }}"
132+
shell: bash
133+
134+
135+
node-yarn-depencies-caching:
136+
runs-on: ${{ matrix.os }}
137+
strategy:
138+
fail-fast: false
139+
matrix:
140+
os: [ubuntu-latest, windows-latest, macos-latest]
141+
node-version: [10, 12, 14]
142+
steps:
143+
- uses: actions/checkout@v2
144+
- name: Generate yarn file
145+
run: yarn install
146+
- name: Remove dependencies
147+
shell: pwsh
148+
run: Remove-Item node_modules -Force -Recurse
149+
- name: Setup Node
150+
uses: ./
151+
with:
152+
node-version: ${{ matrix.node-version }}
153+
cache: 'yarn'
154+
- name: Install dependencies
155+
run: yarn install
156+
- name: Verify node and npm
157+
run: __tests__/verify-node.sh "${{ matrix.node-version }}"
112158
shell: bash

__tests__/cache-restore.test.ts

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import * as core from '@actions/core';
2+
import * as cache from '@actions/cache';
3+
import * as path from 'path';
4+
import * as glob from '@actions/glob';
5+
6+
import * as utils from '../src/cache-utils';
7+
import {restoreCache} from '../src/cache-restore';
8+
9+
describe('cache-restore', () => {
10+
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
11+
if (!process.env.RUNNER_OS) {
12+
process.env.RUNNER_OS = 'Linux';
13+
}
14+
const platform = process.env.RUNNER_OS;
15+
const commonPath = '/some/random/path';
16+
const npmCachePath = `${commonPath}/npm`;
17+
const yarn1CachePath = `${commonPath}/yarn1`;
18+
const yarn2CachePath = `${commonPath}/yarn2`;
19+
const yarnFileHash =
20+
'b8a0bae5243251f7c07dd52d1f78ff78281dfefaded700a176261b6b54fa245b';
21+
const npmFileHash =
22+
'abf7c9b306a3149dcfba4673e2362755503bcceaab46f0e4e6fee0ade493e20c';
23+
const cachesObject = {
24+
[npmCachePath]: npmFileHash,
25+
[yarn1CachePath]: yarnFileHash,
26+
[yarn2CachePath]: yarnFileHash
27+
};
28+
29+
let packageManagerVersion = '1.2.3';
30+
31+
let saveStateSpy: jest.SpyInstance;
32+
let infoSpy: jest.SpyInstance;
33+
let debugSpy: jest.SpyInstance;
34+
let setOutputSpy: jest.SpyInstance;
35+
let getCommandOutputSpy: jest.SpyInstance;
36+
let restoreCacheSpy: jest.SpyInstance;
37+
let hashFilesSpy: jest.SpyInstance;
38+
39+
beforeEach(() => {
40+
// core
41+
infoSpy = jest.spyOn(core, 'info');
42+
infoSpy.mockImplementation(() => undefined);
43+
44+
debugSpy = jest.spyOn(core, 'debug');
45+
debugSpy.mockImplementation(() => undefined);
46+
47+
setOutputSpy = jest.spyOn(core, 'setOutput');
48+
setOutputSpy.mockImplementation(() => undefined);
49+
50+
saveStateSpy = jest.spyOn(core, 'saveState');
51+
saveStateSpy.mockImplementation(() => undefined);
52+
53+
// glob
54+
hashFilesSpy = jest.spyOn(glob, 'hashFiles');
55+
hashFilesSpy.mockImplementation((pattern: string) => {
56+
if (pattern.includes('package-lock.json')) {
57+
return npmFileHash;
58+
} else if (pattern.includes('yarn.lock')) {
59+
return yarnFileHash;
60+
} else {
61+
return '';
62+
}
63+
});
64+
65+
// cache
66+
restoreCacheSpy = jest.spyOn(cache, 'restoreCache');
67+
restoreCacheSpy.mockImplementation(
68+
(cachePaths: Array<string>, key: string) => {
69+
if (!cachePaths || cachePaths.length === 0) {
70+
return undefined;
71+
}
72+
73+
const cachPath = cachePaths[0];
74+
const fileHash = cachesObject[cachPath];
75+
76+
if (key.includes(fileHash)) {
77+
return key;
78+
}
79+
80+
return undefined;
81+
}
82+
);
83+
84+
// cache-utils
85+
getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput');
86+
getCommandOutputSpy.mockImplementation((command: string) => {
87+
if (command.includes('version')) {
88+
return packageManagerVersion;
89+
} else {
90+
switch (command) {
91+
case utils.supportedPackageManagers.npm.getCacheFolderCommand:
92+
return npmCachePath;
93+
case utils.supportedPackageManagers.yarn1.getCacheFolderCommand:
94+
return yarn1CachePath;
95+
case utils.supportedPackageManagers.yarn2.getCacheFolderCommand:
96+
return yarn2CachePath;
97+
default:
98+
return 'packge/not/found';
99+
}
100+
}
101+
});
102+
});
103+
it.each([['npm7'], ['npm7'], ['yarn1'], ['yarn2'], ['random']])(
104+
'it will throw an error because %s is not supported',
105+
async packageManager => {
106+
await expect(restoreCache(packageManager)).rejects.toThrowError(
107+
`Caching for '${packageManager}' is not supported`
108+
);
109+
}
110+
);
111+
112+
it.each([
113+
['yarn', '2.1.2', yarnFileHash],
114+
['yarn', '1.2.3', yarnFileHash],
115+
['npm', '', npmFileHash]
116+
])(
117+
'restored packages for %s',
118+
async (packageManager, toolVersion, fileHash) => {
119+
if (toolVersion) {
120+
packageManagerVersion = toolVersion;
121+
}
122+
123+
await restoreCache(packageManager);
124+
expect(infoSpy).toHaveBeenLastCalledWith(
125+
`Cache restored from key: ${platform}-${packageManager}-${fileHash}`
126+
);
127+
}
128+
);
129+
130+
it.each([
131+
['yarn', '2.1.2', yarnFileHash],
132+
['yarn', '1.2.3', yarnFileHash],
133+
['npm', '', npmFileHash]
134+
])(
135+
'restored packages for %s',
136+
async (packageManager, toolVersion, fileHash) => {
137+
if (toolVersion) {
138+
packageManagerVersion = toolVersion;
139+
}
140+
restoreCacheSpy.mockImplementationOnce(() => undefined);
141+
await restoreCache(packageManager);
142+
expect(infoSpy).not.toHaveBeenLastCalledWith(
143+
`Cache restored from key: ${platform}-${packageManager}-${fileHash}`
144+
);
145+
}
146+
);
147+
148+
afterEach(() => {
149+
jest.resetAllMocks();
150+
jest.clearAllMocks();
151+
//jest.restoreAllMocks();
152+
});
153+
});

0 commit comments

Comments
 (0)