This commit is contained in:
Peng Xiao 2024-03-13 21:20:47 +01:00 committed by GitHub
commit 50788d6a53
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 9 deletions

View File

@ -2,16 +2,15 @@ import * as core from '@actions/core';
import * as cache from '@actions/cache'; import * as cache from '@actions/cache';
import * as path from 'path'; import * as path from 'path';
import * as glob from '@actions/glob'; import * as glob from '@actions/glob';
import osm from 'os';
import * as utils from '../src/cache-utils'; import * as utils from '../src/cache-utils';
import {restoreCache} from '../src/cache-restore'; import {restoreCache} from '../src/cache-restore';
describe('cache-restore', () => { describe('cache-restore', () => {
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
if (!process.env.RUNNER_OS) { const platform = 'Linux';
process.env.RUNNER_OS = 'Linux'; const arch = 'arm64';
}
const platform = process.env.RUNNER_OS;
const commonPath = '/some/random/path'; const commonPath = '/some/random/path';
const npmCachePath = `${commonPath}/npm`; const npmCachePath = `${commonPath}/npm`;
const pnpmCachePath = `${commonPath}/pnpm`; const pnpmCachePath = `${commonPath}/pnpm`;
@ -52,6 +51,8 @@ describe('cache-restore', () => {
let getCommandOutputSpy: jest.SpyInstance; let getCommandOutputSpy: jest.SpyInstance;
let restoreCacheSpy: jest.SpyInstance; let restoreCacheSpy: jest.SpyInstance;
let hashFilesSpy: jest.SpyInstance; let hashFilesSpy: jest.SpyInstance;
let archSpy: jest.SpyInstance;
let platformSpy: jest.SpyInstance;
beforeEach(() => { beforeEach(() => {
// core // core
@ -102,6 +103,13 @@ describe('cache-restore', () => {
// cache-utils // cache-utils
getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput'); getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput');
// os
archSpy = jest.spyOn(osm, 'arch');
archSpy.mockImplementation(() => arch);
platformSpy = jest.spyOn(osm, 'platform');
platformSpy.mockImplementation(() => platform);
}); });
describe('Validate provided package manager', () => { describe('Validate provided package manager', () => {
@ -135,7 +143,7 @@ describe('cache-restore', () => {
await restoreCache(packageManager, ''); await restoreCache(packageManager, '');
expect(hashFilesSpy).toHaveBeenCalled(); expect(hashFilesSpy).toHaveBeenCalled();
expect(infoSpy).toHaveBeenCalledWith( expect(infoSpy).toHaveBeenCalledWith(
`Cache restored from key: node-cache-${platform}-${packageManager}-${fileHash}` `Cache restored from key: node-cache-${platform}-${arch}-${packageManager}-${fileHash}`
); );
expect(infoSpy).not.toHaveBeenCalledWith( expect(infoSpy).not.toHaveBeenCalledWith(
`${packageManager} cache is not found` `${packageManager} cache is not found`

6
dist/setup/index.js vendored
View File

@ -93088,6 +93088,7 @@ const core = __importStar(__nccwpck_require__(2186));
const glob = __importStar(__nccwpck_require__(8090)); const glob = __importStar(__nccwpck_require__(8090));
const path_1 = __importDefault(__nccwpck_require__(1017)); const path_1 = __importDefault(__nccwpck_require__(1017));
const fs_1 = __importDefault(__nccwpck_require__(7147)); const fs_1 = __importDefault(__nccwpck_require__(7147));
const os_1 = __importDefault(__nccwpck_require__(2037));
const constants_1 = __nccwpck_require__(9042); const constants_1 = __nccwpck_require__(9042);
const cache_utils_1 = __nccwpck_require__(1678); const cache_utils_1 = __nccwpck_require__(1678);
const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () { const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
@ -93095,7 +93096,8 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0,
if (!packageManagerInfo) { if (!packageManagerInfo) {
throw new Error(`Caching for '${packageManager}' is not supported`); throw new Error(`Caching for '${packageManager}' is not supported`);
} }
const platform = process.env.RUNNER_OS; const platform = os_1.default.platform();
const arch = os_1.default.arch();
const cachePaths = yield (0, cache_utils_1.getCacheDirectories)(packageManagerInfo, cacheDependencyPath); const cachePaths = yield (0, cache_utils_1.getCacheDirectories)(packageManagerInfo, cacheDependencyPath);
core.saveState(constants_1.State.CachePaths, cachePaths); core.saveState(constants_1.State.CachePaths, cachePaths);
const lockFilePath = cacheDependencyPath const lockFilePath = cacheDependencyPath
@ -93105,7 +93107,7 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0,
if (!fileHash) { if (!fileHash) {
throw new Error('Some specified paths were not resolved, unable to cache dependencies.'); throw new Error('Some specified paths were not resolved, unable to cache dependencies.');
} }
const keyPrefix = `node-cache-${platform}-${packageManager}`; const keyPrefix = `node-cache-${platform}-${arch}-${packageManager}`;
const primaryKey = `${keyPrefix}-${fileHash}`; const primaryKey = `${keyPrefix}-${fileHash}`;
core.debug(`primary key is ${primaryKey}`); core.debug(`primary key is ${primaryKey}`);
core.saveState(constants_1.State.CachePrimaryKey, primaryKey); core.saveState(constants_1.State.CachePrimaryKey, primaryKey);

View File

@ -3,6 +3,7 @@ import * as core from '@actions/core';
import * as glob from '@actions/glob'; import * as glob from '@actions/glob';
import path from 'path'; import path from 'path';
import fs from 'fs'; import fs from 'fs';
import os from 'os';
import {State} from './constants'; import {State} from './constants';
import { import {
@ -20,7 +21,8 @@ export const restoreCache = async (
if (!packageManagerInfo) { if (!packageManagerInfo) {
throw new Error(`Caching for '${packageManager}' is not supported`); throw new Error(`Caching for '${packageManager}' is not supported`);
} }
const platform = process.env.RUNNER_OS; const platform = os.platform();
const arch = os.arch();
const cachePaths = await getCacheDirectories( const cachePaths = await getCacheDirectories(
packageManagerInfo, packageManagerInfo,
@ -38,7 +40,7 @@ export const restoreCache = async (
); );
} }
const keyPrefix = `node-cache-${platform}-${packageManager}`; const keyPrefix = `node-cache-${platform}-${arch}-${packageManager}`;
const primaryKey = `${keyPrefix}-${fileHash}`; const primaryKey = `${keyPrefix}-${fileHash}`;
core.debug(`primary key is ${primaryKey}`); core.debug(`primary key is ${primaryKey}`);