work on resolving comments

This commit is contained in:
Dmitry Shibanov 2022-03-30 13:45:18 +02:00
parent 1c630f9ff8
commit 62e9de96c2
8 changed files with 64 additions and 49 deletions

View file

@ -50,7 +50,6 @@ describe('cache-restore', () => {
let debugSpy: jest.SpyInstance; let debugSpy: jest.SpyInstance;
let setOutputSpy: jest.SpyInstance; let setOutputSpy: jest.SpyInstance;
let getCommandOutputSpy: jest.SpyInstance; let getCommandOutputSpy: jest.SpyInstance;
let isCacheActionAvailable: jest.SpyInstance;
let restoreCacheSpy: jest.SpyInstance; let restoreCacheSpy: jest.SpyInstance;
let hashFilesSpy: jest.SpyInstance; let hashFilesSpy: jest.SpyInstance;
@ -103,9 +102,6 @@ describe('cache-restore', () => {
// cache-utils // cache-utils
getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput'); getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput');
isCacheActionAvailable = jest.spyOn(utils, 'isCacheFeatureAvailable');
isCacheActionAvailable.mockImplementation(() => true);
}); });
describe('Validate provided package manager', () => { describe('Validate provided package manager', () => {

View file

@ -28,7 +28,6 @@ describe('run', () => {
let getStateSpy: jest.SpyInstance; let getStateSpy: jest.SpyInstance;
let saveCacheSpy: jest.SpyInstance; let saveCacheSpy: jest.SpyInstance;
let getCommandOutputSpy: jest.SpyInstance; let getCommandOutputSpy: jest.SpyInstance;
let isCacheActionAvailable: jest.SpyInstance;
let hashFilesSpy: jest.SpyInstance; let hashFilesSpy: jest.SpyInstance;
let existsSpy: jest.SpyInstance; let existsSpy: jest.SpyInstance;
@ -71,9 +70,6 @@ describe('run', () => {
// utils // utils
getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput'); getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput');
isCacheActionAvailable = jest.spyOn(utils, 'isCacheFeatureAvailable');
isCacheActionAvailable.mockImplementation(() => true);
}); });
afterEach(() => { afterEach(() => {

View file

@ -40,31 +40,28 @@ describe('cache-utils', () => {
}); });
}); });
it('isCacheFeatureAvailable is false', () => { it('isCacheFeatureAvailable for GHES is false', () => {
isFeatureAvailable.mockImplementation(() => false); isFeatureAvailable.mockImplementation(() => false);
process.env['GITHUB_SERVER_URL'] = 'https://www.test.com'; process.env['GITHUB_SERVER_URL'] = 'https://www.test.com';
expect(isCacheFeatureAvailable()).toBe(false); expect(() => isCacheFeatureAvailable()).toThrowError(
expect(info).toHaveBeenCalledWith( 'Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'
'[warning]Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'
); );
}); });
it('isCacheFeatureAvailable is false', () => { it('isCacheFeatureAvailable for GHES has an interhal error', () => {
isFeatureAvailable.mockImplementation(() => false); isFeatureAvailable.mockImplementation(() => false);
process.env['GITHUB_SERVER_URL'] = ''; process.env['GITHUB_SERVER_URL'] = '';
expect(isCacheFeatureAvailable()).toBe(false); expect(() => isCacheFeatureAvailable()).toThrowError(
expect(info).toHaveBeenCalledWith( 'An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.'
'[warning]An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.'
); );
}); });
it('isCacheFeatureAvailable is true', () => { it('isCacheFeatureAvailable for GHES is available', () => {
isFeatureAvailable.mockImplementation(() => true); isFeatureAvailable.mockImplementation(() => true);
expect(isCacheFeatureAvailable()).toBe(true); expect(isCacheFeatureAvailable()).toStrictEqual(true);
expect(info).not.toHaveBeenCalled();
}); });
afterEach(() => { afterEach(() => {

View file

@ -2,6 +2,7 @@ import * as core from '@actions/core';
import * as io from '@actions/io'; import * as io from '@actions/io';
import * as tc from '@actions/tool-cache'; import * as tc from '@actions/tool-cache';
import * as im from '../src/installer'; import * as im from '../src/installer';
import * as cache from '@actions/cache';
import fs from 'fs'; import fs from 'fs';
import cp from 'child_process'; import cp from 'child_process';
import osm = require('os'); import osm = require('os');
@ -36,6 +37,7 @@ describe('setup-node', () => {
let execSpy: jest.SpyInstance; let execSpy: jest.SpyInstance;
let authSpy: jest.SpyInstance; let authSpy: jest.SpyInstance;
let parseNodeVersionSpy: jest.SpyInstance; let parseNodeVersionSpy: jest.SpyInstance;
let isCacheActionAvailable: jest.SpyInstance;
beforeEach(() => { beforeEach(() => {
// @actions/core // @actions/core
@ -67,6 +69,9 @@ describe('setup-node', () => {
existsSpy = jest.spyOn(fs, 'existsSync'); existsSpy = jest.spyOn(fs, 'existsSync');
mkdirpSpy = jest.spyOn(io, 'mkdirP'); mkdirpSpy = jest.spyOn(io, 'mkdirP');
// @actions/tool-cache
isCacheActionAvailable = jest.spyOn(cache, 'isFeatureAvailable');
// disable authentication portion for installer tests // disable authentication portion for installer tests
authSpy = jest.spyOn(auth, 'configAuthentication'); authSpy = jest.spyOn(auth, 'configAuthentication');
authSpy.mockImplementation(() => {}); authSpy.mockImplementation(() => {});
@ -644,6 +649,49 @@ describe('setup-node', () => {
); );
}); });
}); });
describe('cache on GHES', () => {
it('Should throw an error, because cache is not supported', async () => {
inputs['node-version'] = '12';
inputs['cache'] = 'npm';
inSpy.mockImplementation(name => inputs[name]);
let toolPath = path.normalize('/cache/node/12.16.1/x64');
findSpy.mockImplementation(() => toolPath);
// expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
process.env['GITHUB_SERVER_URL'] = 'https://www.test.com';
isCacheActionAvailable.mockImplementation(() => false);
await main.run();
expect(cnSpy).toHaveBeenCalledWith(
`::error::Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.${osm.EOL}`
);
});
it('Should throw an internal error', async () => {
inputs['node-version'] = '12';
inputs['cache'] = 'npm';
inSpy.mockImplementation(name => inputs[name]);
let toolPath = path.normalize('/cache/node/12.16.1/x64');
findSpy.mockImplementation(() => toolPath);
// expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
process.env['GITHUB_SERVER_URL'] = '';
isCacheActionAvailable.mockImplementation(() => false);
await main.run();
expect(cnSpy).toHaveBeenCalledWith(
`::error::An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.${osm.EOL}`
);
});
});
describe('LTS version', () => { describe('LTS version', () => {
beforeEach(() => { beforeEach(() => {
os.platform = 'linux'; os.platform = 'linux';

View file

@ -3873,10 +3873,6 @@ exports.getCacheDirectoryPath = (packageManagerInfo, packageManager) => __awaite
core.debug(`${packageManager} path is ${stdOut}`); core.debug(`${packageManager} path is ${stdOut}`);
return stdOut; return stdOut;
}); });
function logWarning(message) {
const warningPrefix = '[warning]';
core.info(`${warningPrefix}${message}`);
}
function isGhes() { function isGhes() {
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
@ -3885,12 +3881,11 @@ exports.isGhes = isGhes;
function isCacheFeatureAvailable() { function isCacheFeatureAvailable() {
if (!cache.isFeatureAvailable()) { if (!cache.isFeatureAvailable()) {
if (isGhes()) { if (isGhes()) {
logWarning('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'); throw new Error('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.');
} }
else { else {
logWarning('An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.'); throw new Error('An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.');
} }
return false;
} }
return true; return true;
} }

14
dist/setup/index.js vendored
View file

@ -6617,10 +6617,7 @@ function run() {
if (registryUrl) { if (registryUrl) {
auth.configAuthentication(registryUrl, alwaysAuth); auth.configAuthentication(registryUrl, alwaysAuth);
} }
if (cache) { if (cache && cache_utils_1.isCacheFeatureAvailable()) {
if (!cache_utils_1.isCacheFeatureAvailable()) {
throw new Error('Caching is not supported on GHES');
}
const cacheDependencyPath = core.getInput('cache-dependency-path'); const cacheDependencyPath = core.getInput('cache-dependency-path');
yield cache_restore_1.restoreCache(cache, cacheDependencyPath); yield cache_restore_1.restoreCache(cache, cacheDependencyPath);
} }
@ -46129,10 +46126,6 @@ exports.getCacheDirectoryPath = (packageManagerInfo, packageManager) => __awaite
core.debug(`${packageManager} path is ${stdOut}`); core.debug(`${packageManager} path is ${stdOut}`);
return stdOut; return stdOut;
}); });
function logWarning(message) {
const warningPrefix = '[warning]';
core.info(`${warningPrefix}${message}`);
}
function isGhes() { function isGhes() {
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
@ -46141,12 +46134,11 @@ exports.isGhes = isGhes;
function isCacheFeatureAvailable() { function isCacheFeatureAvailable() {
if (!cache.isFeatureAvailable()) { if (!cache.isFeatureAvailable()) {
if (isGhes()) { if (isGhes()) {
logWarning('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'); throw new Error('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.');
} }
else { else {
logWarning('An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.'); throw new Error('An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.');
} }
return false;
} }
return true; return true;
} }

View file

@ -97,11 +97,6 @@ export const getCacheDirectoryPath = async (
return stdOut; return stdOut;
}; };
function logWarning(message: string): void {
const warningPrefix = '[warning]';
core.info(`${warningPrefix}${message}`);
}
export function isGhes(): boolean { export function isGhes(): boolean {
const ghUrl = new URL( const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com' process.env['GITHUB_SERVER_URL'] || 'https://github.com'
@ -112,15 +107,14 @@ export function isGhes(): boolean {
export function isCacheFeatureAvailable(): boolean { export function isCacheFeatureAvailable(): boolean {
if (!cache.isFeatureAvailable()) { if (!cache.isFeatureAvailable()) {
if (isGhes()) { if (isGhes()) {
logWarning( throw new Error(
'Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.' 'Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'
); );
} else { } else {
logWarning( throw new Error(
'An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.' 'An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.'
); );
} }
return false;
} }
return true; return true;

View file

@ -45,10 +45,7 @@ export async function run() {
auth.configAuthentication(registryUrl, alwaysAuth); auth.configAuthentication(registryUrl, alwaysAuth);
} }
if (cache) { if (cache && isCacheFeatureAvailable()) {
if (!isCacheFeatureAvailable()) {
throw new Error('Caching is not supported on GHES');
}
const cacheDependencyPath = core.getInput('cache-dependency-path'); const cacheDependencyPath = core.getInput('cache-dependency-path');
await restoreCache(cache, cacheDependencyPath); await restoreCache(cache, cacheDependencyPath);
} }