Skip to content

Commit

Permalink
Merge pull request #639 from akv-platform/v-sdolin/early-return
Browse files Browse the repository at this point in the history
Use early return pattern to avoid nested conditions
  • Loading branch information
dsame committed Dec 12, 2022
2 parents d1b197b + b28830c commit 377c6da
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
3 changes: 2 additions & 1 deletion __tests__/cache-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ describe('cache-utils', () => {
isFeatureAvailable.mockImplementation(() => false);
process.env['GITHUB_SERVER_URL'] = 'https://www.test.com';

expect(() => isCacheFeatureAvailable()).toThrowError(
expect(isCacheFeatureAvailable()).toBeFalsy();
expect(warningSpy).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.'
);
});
Expand Down
5 changes: 3 additions & 2 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,9 @@ describe('setup-node', () => {

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}`
expect(warningSpy).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}`
'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.'
);
});

Expand Down
14 changes: 6 additions & 8 deletions dist/cache-save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61185,16 +61185,14 @@ 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
14 changes: 6 additions & 8 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73139,16 +73139,14 @@ 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
21 changes: 10 additions & 11 deletions src/cache-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,18 @@ 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 377c6da

Please sign in to comment.