Compare commits

...

5 Commits

Author SHA1 Message Date
Jason Karns
9f4be578ca
Merge 55b7d827be into 39370e3970 2024-10-24 12:24:19 -04:00
Peng Xiao
39370e3970
fix: add arch to cached path (#843)
* fix: add arch to cached path

* fix: change from using env to os module

* fix: use process.env.RUNNER_OS instead of os.platform()

* fix: remove unused var
2024-10-23 22:31:49 -05:00
Jason Karns
55b7d827be
Refactor and test 2024-05-29 08:52:52 -04:00
Jason Karns
473cb1b506
Make linter happy 2024-05-28 16:13:11 -04:00
Jason Karns
574c6daa52
Spike to default the scope from package.json 2024-05-28 16:13:08 -04:00
5 changed files with 43 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import * as auth from '../src/authutil';
import * as cacheUtils from '../src/cache-utils';
let rcFile: string;
let pkgJson: string;
describe('authutil tests', () => {
const _runnerDir = path.join(__dirname, 'runner');
@ -25,10 +26,12 @@ describe('authutil tests', () => {
process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo';
process.env['RUNNER_TEMP'] = tempDir;
rcFile = path.join(tempDir, '.npmrc');
pkgJson = path.join(tempDir, 'package.json');
}, 100000);
beforeEach(async () => {
await io.rmRF(rcFile);
await io.rmRF(pkgJson);
// if (fs.existsSync(rcFile)) {
// fs.unlinkSync(rcFile);
// }
@ -113,6 +116,15 @@ describe('authutil tests', () => {
expect(rc['always-auth']).toBe('false');
});
it('Automatically configures npm scope from package.json', async () => {
process.env['INPUT_SCOPE'] = '';
fs.writeFileSync(pkgJson, '{"name":"@myscope/mypackage"}');
await auth.configAuthentication('https://registry.npmjs.org', '');
const rc = readRcFile(rcFile);
expect(rc['@myscope:registry']).toBe('https://registry.npmjs.org/');
});
it('Sets up npmrc for always-auth true', async () => {
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
expect(fs.statSync(rcFile)).toBeDefined();

View File

@ -2,6 +2,7 @@ import * as core from '@actions/core';
import * as cache from '@actions/cache';
import * as path from 'path';
import * as glob from '@actions/glob';
import osm from 'os';
import * as utils from '../src/cache-utils';
import {restoreCache} from '../src/cache-restore';
@ -12,6 +13,7 @@ describe('cache-restore', () => {
process.env.RUNNER_OS = 'Linux';
}
const platform = process.env.RUNNER_OS;
const arch = 'arm64';
const commonPath = '/some/random/path';
const npmCachePath = `${commonPath}/npm`;
const pnpmCachePath = `${commonPath}/pnpm`;
@ -52,6 +54,7 @@ describe('cache-restore', () => {
let getCommandOutputSpy: jest.SpyInstance;
let restoreCacheSpy: jest.SpyInstance;
let hashFilesSpy: jest.SpyInstance;
let archSpy: jest.SpyInstance;
beforeEach(() => {
// core
@ -102,6 +105,10 @@ describe('cache-restore', () => {
// cache-utils
getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput');
// os
archSpy = jest.spyOn(osm, 'arch');
archSpy.mockImplementation(() => arch);
});
describe('Validate provided package manager', () => {
@ -135,7 +142,7 @@ describe('cache-restore', () => {
await restoreCache(packageManager, '');
expect(hashFilesSpy).toHaveBeenCalled();
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(
`${packageManager} cache is not found`

4
dist/setup/index.js vendored
View File

@ -93303,6 +93303,7 @@ const core = __importStar(__nccwpck_require__(2186));
const glob = __importStar(__nccwpck_require__(8090));
const path_1 = __importDefault(__nccwpck_require__(1017));
const fs_1 = __importDefault(__nccwpck_require__(7147));
const os_1 = __importDefault(__nccwpck_require__(2037));
const constants_1 = __nccwpck_require__(9042);
const cache_utils_1 = __nccwpck_require__(1678);
const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
@ -93311,6 +93312,7 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0,
throw new Error(`Caching for '${packageManager}' is not supported`);
}
const platform = process.env.RUNNER_OS;
const arch = os_1.default.arch();
const cachePaths = yield (0, cache_utils_1.getCacheDirectories)(packageManagerInfo, cacheDependencyPath);
core.saveState(constants_1.State.CachePaths, cachePaths);
const lockFilePath = cacheDependencyPath
@ -93320,7 +93322,7 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0,
if (!fileHash) {
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}`;
core.debug(`primary key is ${primaryKey}`);
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);

View File

@ -25,6 +25,12 @@ function writeRegistryToFile(
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
scope = github.context.repo.owner;
}
if (!scope) {
const namePrefix = packageJson('name')?.match(/^(@[^/]+)\//);
if (namePrefix) {
scope = namePrefix[1];
}
}
if (scope && scope[0] != '@') {
scope = '@' + scope;
}
@ -57,3 +63,14 @@ function writeRegistryToFile(
process.env.NODE_AUTH_TOKEN || 'XXXXX-XXXXX-XXXXX-XXXXX'
);
}
function packageJson(prop: string){
const pkgPath: string = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), 'package.json');
try {
const json = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
return prop ? json[prop] : json;
} catch(e) {
core.debug(`Unable to read from package.json`);
}
}

View File

@ -3,6 +3,7 @@ import * as core from '@actions/core';
import * as glob from '@actions/glob';
import path from 'path';
import fs from 'fs';
import os from 'os';
import {State} from './constants';
import {
@ -21,6 +22,7 @@ export const restoreCache = async (
throw new Error(`Caching for '${packageManager}' is not supported`);
}
const platform = process.env.RUNNER_OS;
const arch = os.arch();
const cachePaths = await getCacheDirectories(
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}`;
core.debug(`primary key is ${primaryKey}`);