test: explain expected behaviour by Jest

This commit is contained in:
Kengo TODA 2021-09-06 01:59:45 +00:00
parent 3bc31aaf88
commit 7b921a1561
4 changed files with 35 additions and 5 deletions

View file

@ -1,7 +1,7 @@
import { mkdtempSync } from 'fs'; import { mkdtempSync } from 'fs';
import { tmpdir } from 'os'; import { tmpdir } from 'os';
import { join } from 'path'; import { join } from 'path';
import { restore, save } from '../src/cache'; import { computeCacheKey, findPackageManager, restore, save } from '../src/cache';
import * as fs from 'fs'; import * as fs from 'fs';
import * as os from 'os'; import * as os from 'os';
import * as core from '@actions/core'; import * as core from '@actions/core';
@ -92,6 +92,19 @@ describe('dependency cache', () => {
expect(spyWarning).not.toBeCalled(); expect(spyWarning).not.toBeCalled();
expect(spyInfo).toBeCalledWith('maven cache is not found'); expect(spyInfo).toBeCalledWith('maven cache is not found');
}); });
it('generates same cache key for every supported OS', async () => {
const packageManager = findPackageManager('maven');
process.env['RUNNER_OS'] = 'Windows';
const keyForWin = await computeCacheKey(packageManager);
process.env['RUNNER_OS'] = 'Linux';
const keyForLinux = await computeCacheKey(packageManager);
process.env['RUNNER_OS'] = 'macOS';
const keyForMacOS = await computeCacheKey(packageManager);
expect(keyForWin).toBe(keyForLinux);
expect(keyForWin).toBe(keyForMacOS);
});
}); });
describe('for gradle', () => { describe('for gradle', () => {
it('throws error if no build.gradle found', async () => { it('throws error if no build.gradle found', async () => {
@ -117,6 +130,19 @@ describe('dependency cache', () => {
expect(spyWarning).not.toBeCalled(); expect(spyWarning).not.toBeCalled();
expect(spyInfo).toBeCalledWith('gradle cache is not found'); expect(spyInfo).toBeCalledWith('gradle cache is not found');
}); });
it('generates same cache key for every supported OS', async () => {
const packageManager = findPackageManager('gradle');
process.env['RUNNER_OS'] = 'Windows';
const keyForWin = await computeCacheKey(packageManager);
process.env['RUNNER_OS'] = 'Linux';
const keyForLinux = await computeCacheKey(packageManager);
process.env['RUNNER_OS'] = 'macOS';
const keyForMacOS = await computeCacheKey(packageManager);
expect(keyForWin).toEqual(keyForLinux);
expect(keyForWin).toEqual(keyForMacOS);
});
}); });
}); });
describe('save', () => { describe('save', () => {

View file

@ -64536,7 +64536,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.save = exports.restore = void 0; exports.save = exports.restore = exports.computeCacheKey = exports.findPackageManager = void 0;
const path_1 = __webpack_require__(622); const path_1 = __webpack_require__(622);
const os_1 = __importDefault(__webpack_require__(87)); const os_1 = __importDefault(__webpack_require__(87));
const cache = __importStar(__webpack_require__(692)); const cache = __importStar(__webpack_require__(692));
@ -64566,6 +64566,7 @@ function findPackageManager(id) {
} }
return packageManager; return packageManager;
} }
exports.findPackageManager = findPackageManager;
/** /**
* A function that generates a cache key to use. * A function that generates a cache key to use.
* Format of the generated key will be "${{ platform }}-${{ id }}-${{ fileHash }}"". * Format of the generated key will be "${{ platform }}-${{ id }}-${{ fileHash }}"".
@ -64578,6 +64579,7 @@ function computeCacheKey(packageManager) {
return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${hash}`; return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${hash}`;
}); });
} }
exports.computeCacheKey = computeCacheKey;
/** /**
* Restore the dependency cache * Restore the dependency cache
* @param id ID of the package manager, should be "maven" or "gradle" * @param id ID of the package manager, should be "maven" or "gradle"

4
dist/setup/index.js vendored
View file

@ -18923,7 +18923,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.save = exports.restore = void 0; exports.save = exports.restore = exports.computeCacheKey = exports.findPackageManager = void 0;
const path_1 = __webpack_require__(622); const path_1 = __webpack_require__(622);
const os_1 = __importDefault(__webpack_require__(87)); const os_1 = __importDefault(__webpack_require__(87));
const cache = __importStar(__webpack_require__(692)); const cache = __importStar(__webpack_require__(692));
@ -18953,6 +18953,7 @@ function findPackageManager(id) {
} }
return packageManager; return packageManager;
} }
exports.findPackageManager = findPackageManager;
/** /**
* A function that generates a cache key to use. * A function that generates a cache key to use.
* Format of the generated key will be "${{ platform }}-${{ id }}-${{ fileHash }}"". * Format of the generated key will be "${{ platform }}-${{ id }}-${{ fileHash }}"".
@ -18965,6 +18966,7 @@ function computeCacheKey(packageManager) {
return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${hash}`; return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${hash}`;
}); });
} }
exports.computeCacheKey = computeCacheKey;
/** /**
* Restore the dependency cache * Restore the dependency cache
* @param id ID of the package manager, should be "maven" or "gradle" * @param id ID of the package manager, should be "maven" or "gradle"

View file

@ -35,7 +35,7 @@ const supportedPackageManager: PackageManager[] = [
} }
]; ];
function findPackageManager(id: string): PackageManager { export function findPackageManager(id: string): PackageManager {
const packageManager = supportedPackageManager.find(packageManager => packageManager.id === id); const packageManager = supportedPackageManager.find(packageManager => packageManager.id === id);
if (packageManager === undefined) { if (packageManager === undefined) {
throw new Error(`unknown package manager specified: ${id}`); throw new Error(`unknown package manager specified: ${id}`);
@ -49,7 +49,7 @@ function findPackageManager(id: string): PackageManager {
* If there is no file matched to {@link PackageManager.path}, the generated key ends with a dash (-). * If there is no file matched to {@link PackageManager.path}, the generated key ends with a dash (-).
* @see {@link https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows#matching-a-cache-key|spec of cache key} * @see {@link https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows#matching-a-cache-key|spec of cache key}
*/ */
async function computeCacheKey(packageManager: PackageManager) { export async function computeCacheKey(packageManager: PackageManager) {
const hash = await glob.hashFiles(packageManager.pattern.join('\n')); const hash = await glob.hashFiles(packageManager.pattern.join('\n'));
return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${hash}`; return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${hash}`;
} }