From bb5ff97ab9c85a2085c8968c49b14f9f0530e2a8 Mon Sep 17 00:00:00 2001 From: Jongwoo Han Date: Fri, 16 Dec 2022 23:05:54 +0900 Subject: [PATCH] refactor: Use early return pattern to avoid nested conditions (#302) --- __tests__/cache-utils.test.ts | 7 ++++--- dist/cache-save/index.js | 15 +++++++-------- dist/setup/index.js | 15 +++++++-------- src/cache-utils.ts | 22 +++++++++++----------- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/__tests__/cache-utils.test.ts b/__tests__/cache-utils.test.ts index 1bf7ca3..a21d3aa 100644 --- a/__tests__/cache-utils.test.ts +++ b/__tests__/cache-utils.test.ts @@ -162,7 +162,7 @@ describe('isCacheFeatureAvailable', () => { expect(functionResult).toBeFalsy(); }); - it('should throw when cache feature is unavailable and GHES is used', () => { + it('should warn when cache feature is unavailable and GHES is used', () => { //Arrange isFeatureAvailableSpy.mockImplementation(() => { return false; @@ -170,10 +170,11 @@ describe('isCacheFeatureAvailable', () => { process.env['GITHUB_SERVER_URL'] = 'https://nongithub.com'; - let errorMessage = + let warningMessage = '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.'; //Act + Assert - expect(() => cacheUtils.isCacheFeatureAvailable()).toThrow(errorMessage); + expect(cacheUtils.isCacheFeatureAvailable()).toBeFalsy(); + expect(warningSpy).toHaveBeenCalledWith(warningMessage); }); }); diff --git a/dist/cache-save/index.js b/dist/cache-save/index.js index e6e029d..1c09127 100644 --- a/dist/cache-save/index.js +++ b/dist/cache-save/index.js @@ -60471,16 +60471,15 @@ function isGhes() { } exports.isGhes = isGhes; function isCacheFeatureAvailable() { - if (!cache.isFeatureAvailable()) { - if (isGhes()) { - 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 { - core.warning('The runner was not able to contact the cache service. Caching will be skipped'); - } + if (cache.isFeatureAvailable()) { + return true; + } + if (isGhes()) { + core.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.'); return false; } - return true; + core.warning('The runner was not able to contact the cache service. Caching will be skipped'); + return false; } exports.isCacheFeatureAvailable = isCacheFeatureAvailable; diff --git a/dist/setup/index.js b/dist/setup/index.js index fd06dc2..0c9ac63 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -63144,16 +63144,15 @@ function isGhes() { } exports.isGhes = isGhes; function isCacheFeatureAvailable() { - if (!cache.isFeatureAvailable()) { - if (isGhes()) { - 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 { - core.warning('The runner was not able to contact the cache service. Caching will be skipped'); - } + if (cache.isFeatureAvailable()) { + return true; + } + if (isGhes()) { + core.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.'); return false; } - return true; + core.warning('The runner was not able to contact the cache service. Caching will be skipped'); + return false; } exports.isCacheFeatureAvailable = isCacheFeatureAvailable; diff --git a/src/cache-utils.ts b/src/cache-utils.ts index 5741337..44192bf 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -57,19 +57,19 @@ export function isGhes(): boolean { } export function isCacheFeatureAvailable(): boolean { - if (!cache.isFeatureAvailable()) { - if (isGhes()) { - 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 { - core.warning( - 'The runner was not able to contact the cache service. Caching will be skipped' - ); - } + if (cache.isFeatureAvailable()) { + return true; + } + if (isGhes()) { + core.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.' + ); return false; } - return true; + core.warning( + 'The runner was not able to contact the cache service. Caching will be skipped' + ); + return false; }