diff --git a/__tests__/cache.test.ts b/__tests__/cache.test.ts index b748e8027..c2e1f7773 100644 --- a/__tests__/cache.test.ts +++ b/__tests__/cache.test.ts @@ -1,7 +1,7 @@ import { mkdtempSync } from 'fs'; import { tmpdir } from 'os'; 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 os from 'os'; import * as core from '@actions/core'; @@ -92,6 +92,19 @@ describe('dependency cache', () => { expect(spyWarning).not.toBeCalled(); 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', () => { it('throws error if no build.gradle found', async () => { @@ -117,6 +130,19 @@ describe('dependency cache', () => { expect(spyWarning).not.toBeCalled(); 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', () => { diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js index bbc65296e..a8f2f1db1 100644 --- a/dist/cleanup/index.js +++ b/dist/cleanup/index.js @@ -64536,7 +64536,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; 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 os_1 = __importDefault(__webpack_require__(87)); const cache = __importStar(__webpack_require__(692)); @@ -64566,18 +64566,20 @@ function findPackageManager(id) { } return packageManager; } +exports.findPackageManager = findPackageManager; /** * 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 "setup-java-${{ id }}-${{ fileHash }}"". * 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} */ function computeCacheKey(packageManager) { return __awaiter(this, void 0, void 0, function* () { const hash = yield glob.hashFiles(packageManager.pattern.join('\n')); - return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${hash}`; + return `${CACHE_KEY_PREFIX}-${packageManager.id}-${hash}`; }); } +exports.computeCacheKey = computeCacheKey; /** * Restore the dependency cache * @param id ID of the package manager, should be "maven" or "gradle" @@ -64592,7 +64594,7 @@ function restore(id) { throw new Error(`No file in ${process.cwd()} matched to [${packageManager.pattern}], make sure you have checked out the target repository`); } const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey, [ - `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${id}` + `${CACHE_KEY_PREFIX}-${id}` ]); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); diff --git a/dist/setup/index.js b/dist/setup/index.js index d8f0e773e..591b7b01f 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -18923,7 +18923,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; 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 os_1 = __importDefault(__webpack_require__(87)); const cache = __importStar(__webpack_require__(692)); @@ -18953,18 +18953,20 @@ function findPackageManager(id) { } return packageManager; } +exports.findPackageManager = findPackageManager; /** * 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 "setup-java-${{ id }}-${{ fileHash }}"". * 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} */ function computeCacheKey(packageManager) { return __awaiter(this, void 0, void 0, function* () { const hash = yield glob.hashFiles(packageManager.pattern.join('\n')); - return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${hash}`; + return `${CACHE_KEY_PREFIX}-${packageManager.id}-${hash}`; }); } +exports.computeCacheKey = computeCacheKey; /** * Restore the dependency cache * @param id ID of the package manager, should be "maven" or "gradle" @@ -18979,7 +18981,7 @@ function restore(id) { throw new Error(`No file in ${process.cwd()} matched to [${packageManager.pattern}], make sure you have checked out the target repository`); } const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey, [ - `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${id}` + `${CACHE_KEY_PREFIX}-${id}` ]); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); diff --git a/src/cache.ts b/src/cache.ts index fb97fb0b1..b277cd605 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -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); if (packageManager === undefined) { throw new Error(`unknown package manager specified: ${id}`); @@ -45,13 +45,13 @@ function findPackageManager(id: string): PackageManager { /** * 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 "setup-java-${{ id }}-${{ fileHash }}"". * 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} */ -async function computeCacheKey(packageManager: PackageManager) { +export async function computeCacheKey(packageManager: PackageManager) { const hash = await glob.hashFiles(packageManager.pattern.join('\n')); - return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${hash}`; + return `${CACHE_KEY_PREFIX}-${packageManager.id}-${hash}`; } /** @@ -73,7 +73,7 @@ export async function restore(id: string) { } const matchedKey = await cache.restoreCache(packageManager.path, primaryKey, [ - `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${id}` + `${CACHE_KEY_PREFIX}-${id}` ]); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey);