Skip to content

Commit

Permalink
refactor: Use early return pattern to avoid nested conditions (action…
Browse files Browse the repository at this point in the history
  • Loading branch information
jongwooo authored and adilhusain-s committed Feb 6, 2023
1 parent 14a3cdd commit d97a2e5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
7 changes: 4 additions & 3 deletions __tests__/cache-utils.test.ts
Expand Up @@ -162,18 +162,19 @@ 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;
});

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);
});
});
15 changes: 7 additions & 8 deletions dist/cache-save/index.js
Expand Up @@ -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;

Expand Down
15 changes: 7 additions & 8 deletions dist/setup/index.js
Expand Up @@ -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;

Expand Down
22 changes: 11 additions & 11 deletions src/cache-utils.ts
Expand Up @@ -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;
}

0 comments on commit d97a2e5

Please sign in to comment.