Skip to content

Commit

Permalink
add tests to cover node's caching
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-shibanov committed Jun 9, 2021
1 parent 1b970e4 commit 3cef065
Show file tree
Hide file tree
Showing 14 changed files with 23,221 additions and 11,708 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
- run: npm run build
- run: npm run format-check
- run: npm test
- run: git add .
- name: Verify no unstaged changes
if: runner.os != 'windows'
run: __tests__/verify-no-unstaged-changes.sh
46 changes: 46 additions & 0 deletions .github/workflows/versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,50 @@ jobs:
architecture: 'x86'
- name: Verify node
run: __tests__/verify-arch.sh "ia32"
shell: bash

node-npm-depencies-caching:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [10, 12, 14]
steps:
- uses: actions/checkout@v2
- name: Setup Node
uses: ./
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install dependencies
run: npm install
- name: Verify node and npm
run: __tests__/verify-node.sh "${{ matrix.node-version }}"
shell: bash


node-yarn-depencies-caching:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [10, 12, 14]
steps:
- uses: actions/checkout@v2
- name: Generate yarn file
run: yarn install
- name: Remove dependencies
shell: pwsh
run: Remove-Item node_modules -Force -Recurse
- name: Setup Node
uses: ./
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
- name: Install dependencies
run: yarn install
- name: Verify node and npm
run: __tests__/verify-node.sh "${{ matrix.node-version }}"
shell: bash
153 changes: 153 additions & 0 deletions __tests__/cache-restore.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import * as core from '@actions/core';
import * as cache from '@actions/cache';
import * as path from 'path';
import * as glob from '@actions/glob';

import * as utils from '../src/cache-utils';
import {restoreCache} from '../src/cache-restore';

describe('cache-restore', () => {
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
if (!process.env.RUNNER_OS) {
process.env.RUNNER_OS = 'Linux';
}
const platform = process.env.RUNNER_OS;
const commonPath = '/some/random/path';
const npmCachePath = `${commonPath}/npm`;
const yarn1CachePath = `${commonPath}/yarn1`;
const yarn2CachePath = `${commonPath}/yarn2`;
const yarnFileHash =
'b8a0bae5243251f7c07dd52d1f78ff78281dfefaded700a176261b6b54fa245b';
const npmFileHash =
'abf7c9b306a3149dcfba4673e2362755503bcceaab46f0e4e6fee0ade493e20c';
const cachesObject = {
[npmCachePath]: npmFileHash,
[yarn1CachePath]: yarnFileHash,
[yarn2CachePath]: yarnFileHash
};

let packageManagerVersion = '1.2.3';

let saveStateSpy: jest.SpyInstance;
let infoSpy: jest.SpyInstance;
let debugSpy: jest.SpyInstance;
let setOutputSpy: jest.SpyInstance;
let getCommandOutputSpy: jest.SpyInstance;
let restoreCacheSpy: jest.SpyInstance;
let hashFilesSpy: jest.SpyInstance;

beforeEach(() => {
// core
infoSpy = jest.spyOn(core, 'info');
infoSpy.mockImplementation(() => undefined);

debugSpy = jest.spyOn(core, 'debug');
debugSpy.mockImplementation(() => undefined);

setOutputSpy = jest.spyOn(core, 'setOutput');
setOutputSpy.mockImplementation(() => undefined);

saveStateSpy = jest.spyOn(core, 'saveState');
saveStateSpy.mockImplementation(() => undefined);

// glob
hashFilesSpy = jest.spyOn(glob, 'hashFiles');
hashFilesSpy.mockImplementation((pattern: string) => {
if (pattern.includes('package-lock.json')) {
return npmFileHash;
} else if (pattern.includes('yarn.lock')) {
return yarnFileHash;
} else {
return '';
}
});

// cache
restoreCacheSpy = jest.spyOn(cache, 'restoreCache');
restoreCacheSpy.mockImplementation(
(cachePaths: Array<string>, key: string) => {
if (!cachePaths || cachePaths.length === 0) {
return undefined;
}

const cachPath = cachePaths[0];
const fileHash = cachesObject[cachPath];

if (key.includes(fileHash)) {
return key;
}

return undefined;
}
);

// cache-utils
getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput');
getCommandOutputSpy.mockImplementation((command: string) => {
if (command.includes('version')) {
return packageManagerVersion;
} else {
switch (command) {
case utils.supportedPackageManagers.npm.getCacheFolderCommand:
return npmCachePath;
case utils.supportedPackageManagers.yarn1.getCacheFolderCommand:
return yarn1CachePath;
case utils.supportedPackageManagers.yarn2.getCacheFolderCommand:
return yarn2CachePath;
default:
return 'packge/not/found';
}
}
});
});
it.each([['npm7'], ['npm7'], ['yarn1'], ['yarn2'], ['random']])(
'it will throw an error because %s is not supported',
async packageManager => {
await expect(restoreCache(packageManager)).rejects.toThrowError(
`Caching for '${packageManager}' is not supported`
);
}
);

it.each([
['yarn', '2.1.2', yarnFileHash],
['yarn', '1.2.3', yarnFileHash],
['npm', '', npmFileHash]
])(
'restored packages for %s',
async (packageManager, toolVersion, fileHash) => {
if (toolVersion) {
packageManagerVersion = toolVersion;
}

await restoreCache(packageManager);
expect(infoSpy).toHaveBeenLastCalledWith(
`Cache restored from key: ${platform}-${packageManager}-${fileHash}`
);
}
);

it.each([
['yarn', '2.1.2', yarnFileHash],
['yarn', '1.2.3', yarnFileHash],
['npm', '', npmFileHash]
])(
'restored packages for %s',
async (packageManager, toolVersion, fileHash) => {
if (toolVersion) {
packageManagerVersion = toolVersion;
}
restoreCacheSpy.mockImplementationOnce(() => undefined);
await restoreCache(packageManager);
expect(infoSpy).not.toHaveBeenLastCalledWith(
`Cache restored from key: ${platform}-${packageManager}-${fileHash}`
);
}
);

afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
//jest.restoreAllMocks();
});
});
Loading

0 comments on commit 3cef065

Please sign in to comment.