Skip to content

Commit 162ddf7

Browse files
Add pnpm caching support
1 parent 38d90ce commit 162ddf7

File tree

11 files changed

+531
-14
lines changed

11 files changed

+531
-14
lines changed

.github/workflows/e2e-cache.yml

+33
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,39 @@ jobs:
3535
run: __tests__/verify-node.sh "${{ matrix.node-version }}"
3636
shell: bash
3737

38+
node-pnpm-depencies-caching:
39+
name: Test pnpm (Node ${{ matrix.node-version}}, ${{ matrix.os }})
40+
runs-on: ${{ matrix.os }}
41+
strategy:
42+
fail-fast: false
43+
matrix:
44+
os: [ubuntu-latest, windows-latest, macos-latest]
45+
node-version: [12, 14, 16]
46+
steps:
47+
- uses: actions/checkout@v2
48+
- name: Install pnpm
49+
uses: pnpm/action-setup@v2
50+
with:
51+
version: 6.9.0
52+
- name: Generate pnpm file
53+
run: pnpm install
54+
- name: Remove dependencies
55+
shell: pwsh
56+
run: Remove-Item node_modules -Force -Recurse
57+
- name: Clean global cache
58+
run: rm -rf ~/.pnpm-store
59+
shell: bash
60+
- name: Setup Node
61+
uses: ./
62+
with:
63+
node-version: ${{ matrix.node-version }}
64+
cache: 'pnpm'
65+
- name: Install dependencies
66+
run: pnpm install
67+
- name: Verify node and pnpm
68+
run: __tests__/verify-node.sh "${{ matrix.node-version }}"
69+
shell: bash
70+
3871
node-yarn1-depencies-caching:
3972
name: Test yarn 1 (Node ${{ matrix.node-version}}, ${{ matrix.os }})
4073
runs-on: ${{ matrix.os }}

README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
This action provides the following functionality for GitHub Actions users:
88

99
- Optionally downloading and caching distribution of the requested Node.js version, and adding it to the PATH
10-
- Optionally caching npm/yarn dependencies
10+
- Optionally caching npm/pnpm/yarn dependencies
1111
- Registering problem matchers for error output
1212
- Configuring authentication for GPR or npm
1313

@@ -41,7 +41,7 @@ nvm lts syntax: `lts/erbium`, `lts/fermium`, `lts/*`
4141

4242
### Caching packages dependencies
4343

44-
The action has a built-in functionality for caching and restoring npm/yarn dependencies. Supported package managers are `npm`, `yarn`. The `cache` input is optional, and caching is turned off by default.
44+
The action has a built-in functionality for caching and restoring npm/yarn dependencies. Supported package managers are `npm`, `pnpm`, `yarn`. The `cache` input is optional, and caching is turned off by default.
4545

4646
**Caching npm dependencies:**
4747
```yaml
@@ -55,6 +55,21 @@ steps:
5555
- run: npm test
5656
```
5757

58+
**Caching pnpm dependencies:**
59+
```yaml
60+
steps:
61+
- uses: actions/checkout@v2
62+
- uses: pnpm/action-setup@v2
63+
with:
64+
version: 6.9.0
65+
- uses: actions/setup-node@v2
66+
with:
67+
node-version: '14'
68+
cache: 'pnpm'
69+
- run: pnpm install
70+
- run: pnpm test
71+
```
72+
5873
**Caching yarn dependencies:**
5974
```yaml
6075
steps:

__tests__/cache-restore.test.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ describe('cache-restore', () => {
1414
const platform = process.env.RUNNER_OS;
1515
const commonPath = '/some/random/path';
1616
const npmCachePath = `${commonPath}/npm`;
17+
const pnpmCachePath = `${commonPath}/pnpm`;
1718
const yarn1CachePath = `${commonPath}/yarn1`;
1819
const yarn2CachePath = `${commonPath}/yarn2`;
1920
const yarnFileHash =
2021
'b8a0bae5243251f7c07dd52d1f78ff78281dfefaded700a176261b6b54fa245b';
2122
const npmFileHash =
2223
'abf7c9b306a3149dcfba4673e2362755503bcceaab46f0e4e6fee0ade493e20c';
24+
const pnpmFileHash =
25+
'26309058093e84713f38869c50cf1cee9b08155ede874ec1b44ce3fca8c68c70';
2326
const cachesObject = {
2427
[npmCachePath]: npmFileHash,
28+
[pnpmCachePath]: pnpmFileHash,
2529
[yarn1CachePath]: yarnFileHash,
2630
[yarn2CachePath]: yarnFileHash
2731
};
@@ -30,6 +34,8 @@ describe('cache-restore', () => {
3034
switch (command) {
3135
case utils.supportedPackageManagers.npm.getCacheFolderCommand:
3236
return npmCachePath;
37+
case utils.supportedPackageManagers.pnpm.getCacheFolderCommand:
38+
return pnpmCachePath;
3339
case utils.supportedPackageManagers.yarn1.getCacheFolderCommand:
3440
return yarn1CachePath;
3541
case utils.supportedPackageManagers.yarn2.getCacheFolderCommand:
@@ -66,6 +72,8 @@ describe('cache-restore', () => {
6672
hashFilesSpy.mockImplementation((pattern: string) => {
6773
if (pattern.includes('package-lock.json')) {
6874
return npmFileHash;
75+
} else if (pattern.includes('pnpm-lock.yaml')) {
76+
return pnpmFileHash;
6977
} else if (pattern.includes('yarn.lock')) {
7078
return yarnFileHash;
7179
} else {
@@ -97,7 +105,7 @@ describe('cache-restore', () => {
97105
});
98106

99107
describe('Validate provided package manager', () => {
100-
it.each([['npm7'], ['npm6'], ['yarn1'], ['yarn2'], ['random']])(
108+
it.each([['npm7'], ['npm6'], ['pnpm6'], ['yarn1'], ['yarn2'], ['random']])(
101109
'Throw an error because %s is not supported',
102110
async packageManager => {
103111
await expect(restoreCache(packageManager)).rejects.toThrowError(
@@ -111,7 +119,8 @@ describe('cache-restore', () => {
111119
it.each([
112120
['yarn', '2.1.2', yarnFileHash],
113121
['yarn', '1.2.3', yarnFileHash],
114-
['npm', '', npmFileHash]
122+
['npm', '', npmFileHash],
123+
['pnpm', '', pnpmFileHash]
115124
])(
116125
'restored dependencies for %s',
117126
async (packageManager, toolVersion, fileHash) => {
@@ -139,7 +148,8 @@ describe('cache-restore', () => {
139148
it.each([
140149
['yarn', '2.1.2', yarnFileHash],
141150
['yarn', '1.2.3', yarnFileHash],
142-
['npm', '', npmFileHash]
151+
['npm', '', npmFileHash],
152+
['pnpm', '', pnpmFileHash]
143153
])(
144154
'dependencies are changed %s',
145155
async (packageManager, toolVersion, fileHash) => {

__tests__/cache-save.test.ts

+46
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ describe('run', () => {
1212
'b8a0bae5243251f7c07dd52d1f78ff78281dfefaded700a176261b6b54fa245b';
1313
const npmFileHash =
1414
'abf7c9b306a3149dcfba4673e2362755503bcceaab46f0e4e6fee0ade493e20c';
15+
const pnpmFileHash =
16+
'26309058093e84713f38869c50cf1cee9b08155ede874ec1b44ce3fca8c68c70';
1517
const commonPath = '/some/random/path';
1618
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
1719

@@ -150,6 +152,23 @@ describe('run', () => {
150152
);
151153
expect(setFailedSpy).not.toHaveBeenCalled();
152154
});
155+
156+
it('should not save cache for npm', async () => {
157+
inputs['cache'] = 'pnpm';
158+
getStateSpy.mockImplementation(() => pnpmFileHash);
159+
getCommandOutputSpy.mockImplementationOnce(() => `${commonPath}/pnpm`);
160+
161+
await run();
162+
163+
expect(getInputSpy).toHaveBeenCalled();
164+
expect(getStateSpy).toHaveBeenCalledTimes(2);
165+
expect(getCommandOutputSpy).toHaveBeenCalledTimes(1);
166+
expect(debugSpy).toHaveBeenCalledWith(`pnpm path is ${commonPath}/pnpm`);
167+
expect(infoSpy).toHaveBeenCalledWith(
168+
`Cache hit occurred on the primary key ${pnpmFileHash}, not saving cache.`
169+
);
170+
expect(setFailedSpy).not.toHaveBeenCalled();
171+
});
153172
});
154173

155174
describe('action saves the cache', () => {
@@ -239,6 +258,33 @@ describe('run', () => {
239258
);
240259
expect(setFailedSpy).not.toHaveBeenCalled();
241260
});
261+
262+
it('saves cache from pnpm', async () => {
263+
inputs['cache'] = 'pnpm';
264+
getStateSpy.mockImplementation((name: string) => {
265+
if (name === State.CacheMatchedKey) {
266+
return pnpmFileHash;
267+
} else {
268+
return npmFileHash;
269+
}
270+
});
271+
getCommandOutputSpy.mockImplementationOnce(() => `${commonPath}/pnpm`);
272+
273+
await run();
274+
275+
expect(getInputSpy).toHaveBeenCalled();
276+
expect(getStateSpy).toHaveBeenCalledTimes(2);
277+
expect(getCommandOutputSpy).toHaveBeenCalledTimes(1);
278+
expect(debugSpy).toHaveBeenCalledWith(`pnpm path is ${commonPath}/pnpm`);
279+
expect(infoSpy).not.toHaveBeenCalledWith(
280+
`Cache hit occurred on the primary key ${pnpmFileHash}, not saving cache.`
281+
);
282+
expect(saveCacheSpy).toHaveBeenCalled();
283+
expect(infoSpy).toHaveBeenLastCalledWith(
284+
`Cache saved with the key: ${npmFileHash}`
285+
);
286+
expect(setFailedSpy).not.toHaveBeenCalled();
287+
});
242288
});
243289

244290
afterEach(() => {

__tests__/cache-utils.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ describe('cache-utils', () => {
1414
function getPackagePath(name: string) {
1515
if (name === utils.supportedPackageManagers.npm.getCacheFolderCommand) {
1616
return `${commonPath}/npm`;
17+
} else if (
18+
name === utils.supportedPackageManagers.pnpm.getCacheFolderCommand
19+
) {
20+
return `${commonPath}/pnpm`;
1721
} else {
1822
if (name === utils.supportedPackageManagers.yarn1.getCacheFolderCommand) {
1923
return `${commonPath}/yarn1`;
@@ -34,6 +38,7 @@ describe('cache-utils', () => {
3438
describe('getPackageManagerInfo', () => {
3539
it.each<[string, PackageManagerInfo | null]>([
3640
['npm', utils.supportedPackageManagers.npm],
41+
['pnpm', utils.supportedPackageManagers.pnpm],
3742
['yarn', utils.supportedPackageManagers.yarn1],
3843
['yarn1', null],
3944
['yarn2', null],

0 commit comments

Comments
 (0)