From 62e9de96c272bb270d57d36be925e5cdd03a0765 Mon Sep 17 00:00:00 2001 From: Dmitry Shibanov Date: Wed, 30 Mar 2022 13:45:18 +0200 Subject: [PATCH] work on resolving comments --- __tests__/cache-restore.test.ts | 4 --- __tests__/cache-save.test.ts | 4 --- __tests__/cache-utils.test.ts | 19 ++++++------- __tests__/installer.test.ts | 48 +++++++++++++++++++++++++++++++++ dist/cache-save/index.js | 9 ++----- dist/setup/index.js | 14 +++------- src/cache-utils.ts | 10 ++----- src/main.ts | 5 +--- 8 files changed, 64 insertions(+), 49 deletions(-) diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts index 8157cf84..27f6fa27 100644 --- a/__tests__/cache-restore.test.ts +++ b/__tests__/cache-restore.test.ts @@ -50,7 +50,6 @@ describe('cache-restore', () => { let debugSpy: jest.SpyInstance; let setOutputSpy: jest.SpyInstance; let getCommandOutputSpy: jest.SpyInstance; - let isCacheActionAvailable: jest.SpyInstance; let restoreCacheSpy: jest.SpyInstance; let hashFilesSpy: jest.SpyInstance; @@ -103,9 +102,6 @@ describe('cache-restore', () => { // cache-utils getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput'); - - isCacheActionAvailable = jest.spyOn(utils, 'isCacheFeatureAvailable'); - isCacheActionAvailable.mockImplementation(() => true); }); describe('Validate provided package manager', () => { diff --git a/__tests__/cache-save.test.ts b/__tests__/cache-save.test.ts index f8ce3436..82db2195 100644 --- a/__tests__/cache-save.test.ts +++ b/__tests__/cache-save.test.ts @@ -28,7 +28,6 @@ describe('run', () => { let getStateSpy: jest.SpyInstance; let saveCacheSpy: jest.SpyInstance; let getCommandOutputSpy: jest.SpyInstance; - let isCacheActionAvailable: jest.SpyInstance; let hashFilesSpy: jest.SpyInstance; let existsSpy: jest.SpyInstance; @@ -71,9 +70,6 @@ describe('run', () => { // utils getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput'); - - isCacheActionAvailable = jest.spyOn(utils, 'isCacheFeatureAvailable'); - isCacheActionAvailable.mockImplementation(() => true); }); afterEach(() => { diff --git a/__tests__/cache-utils.test.ts b/__tests__/cache-utils.test.ts index df01605f..cb2c4e46 100644 --- a/__tests__/cache-utils.test.ts +++ b/__tests__/cache-utils.test.ts @@ -40,31 +40,28 @@ describe('cache-utils', () => { }); }); - it('isCacheFeatureAvailable is false', () => { + it('isCacheFeatureAvailable for GHES is false', () => { isFeatureAvailable.mockImplementation(() => false); process.env['GITHUB_SERVER_URL'] = 'https://www.test.com'; - expect(isCacheFeatureAvailable()).toBe(false); - expect(info).toHaveBeenCalledWith( - '[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.' + expect(() => isCacheFeatureAvailable()).toThrowError( + '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); process.env['GITHUB_SERVER_URL'] = ''; - expect(isCacheFeatureAvailable()).toBe(false); - expect(info).toHaveBeenCalledWith( - '[warning]An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.' + expect(() => isCacheFeatureAvailable()).toThrowError( + '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); - expect(isCacheFeatureAvailable()).toBe(true); - expect(info).not.toHaveBeenCalled(); + expect(isCacheFeatureAvailable()).toStrictEqual(true); }); afterEach(() => { diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 4565dd4c..54d63da5 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -2,6 +2,7 @@ import * as core from '@actions/core'; import * as io from '@actions/io'; import * as tc from '@actions/tool-cache'; import * as im from '../src/installer'; +import * as cache from '@actions/cache'; import fs from 'fs'; import cp from 'child_process'; import osm = require('os'); @@ -36,6 +37,7 @@ describe('setup-node', () => { let execSpy: jest.SpyInstance; let authSpy: jest.SpyInstance; let parseNodeVersionSpy: jest.SpyInstance; + let isCacheActionAvailable: jest.SpyInstance; beforeEach(() => { // @actions/core @@ -67,6 +69,9 @@ describe('setup-node', () => { existsSpy = jest.spyOn(fs, 'existsSync'); mkdirpSpy = jest.spyOn(io, 'mkdirP'); + // @actions/tool-cache + isCacheActionAvailable = jest.spyOn(cache, 'isFeatureAvailable'); + // disable authentication portion for installer tests authSpy = jest.spyOn(auth, 'configAuthentication'); 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', () => { beforeEach(() => { os.platform = 'linux'; diff --git a/dist/cache-save/index.js b/dist/cache-save/index.js index 143309cb..3f049ebe 100644 --- a/dist/cache-save/index.js +++ b/dist/cache-save/index.js @@ -3873,10 +3873,6 @@ exports.getCacheDirectoryPath = (packageManagerInfo, packageManager) => __awaite core.debug(`${packageManager} path is ${stdOut}`); return stdOut; }); -function logWarning(message) { - const warningPrefix = '[warning]'; - core.info(`${warningPrefix}${message}`); -} function isGhes() { const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; @@ -3885,12 +3881,11 @@ exports.isGhes = isGhes; function isCacheFeatureAvailable() { if (!cache.isFeatureAvailable()) { 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 { - 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; } diff --git a/dist/setup/index.js b/dist/setup/index.js index 57ef4325..8a1610b0 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -6617,10 +6617,7 @@ function run() { if (registryUrl) { auth.configAuthentication(registryUrl, alwaysAuth); } - if (cache) { - if (!cache_utils_1.isCacheFeatureAvailable()) { - throw new Error('Caching is not supported on GHES'); - } + if (cache && cache_utils_1.isCacheFeatureAvailable()) { const cacheDependencyPath = core.getInput('cache-dependency-path'); yield cache_restore_1.restoreCache(cache, cacheDependencyPath); } @@ -46129,10 +46126,6 @@ exports.getCacheDirectoryPath = (packageManagerInfo, packageManager) => __awaite core.debug(`${packageManager} path is ${stdOut}`); return stdOut; }); -function logWarning(message) { - const warningPrefix = '[warning]'; - core.info(`${warningPrefix}${message}`); -} function isGhes() { const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; @@ -46141,12 +46134,11 @@ exports.isGhes = isGhes; function isCacheFeatureAvailable() { if (!cache.isFeatureAvailable()) { 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 { - 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; } diff --git a/src/cache-utils.ts b/src/cache-utils.ts index 6b5e8e2a..31a2eb9f 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -97,11 +97,6 @@ export const getCacheDirectoryPath = async ( return stdOut; }; -function logWarning(message: string): void { - const warningPrefix = '[warning]'; - core.info(`${warningPrefix}${message}`); -} - export function isGhes(): boolean { const ghUrl = new URL( process.env['GITHUB_SERVER_URL'] || 'https://github.com' @@ -112,15 +107,14 @@ export function isGhes(): boolean { export function isCacheFeatureAvailable(): boolean { if (!cache.isFeatureAvailable()) { 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.' ); } 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.' ); } - return false; } return true; diff --git a/src/main.ts b/src/main.ts index 020f8c25..3c5661b5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -45,10 +45,7 @@ export async function run() { auth.configAuthentication(registryUrl, alwaysAuth); } - if (cache) { - if (!isCacheFeatureAvailable()) { - throw new Error('Caching is not supported on GHES'); - } + if (cache && isCacheFeatureAvailable()) { const cacheDependencyPath = core.getInput('cache-dependency-path'); await restoreCache(cache, cacheDependencyPath); }