Skip to content

Commit 377c6da

Browse files
authored
Merge pull request #639 from akv-platform/v-sdolin/early-return
Use early return pattern to avoid nested conditions
2 parents d1b197b + b28830c commit 377c6da

File tree

5 files changed

+27
-30
lines changed

5 files changed

+27
-30
lines changed

__tests__/cache-utils.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ describe('cache-utils', () => {
4646
isFeatureAvailable.mockImplementation(() => false);
4747
process.env['GITHUB_SERVER_URL'] = 'https://www.test.com';
4848

49-
expect(() => isCacheFeatureAvailable()).toThrowError(
49+
expect(isCacheFeatureAvailable()).toBeFalsy();
50+
expect(warningSpy).toHaveBeenCalledWith(
5051
'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.'
5152
);
5253
});

__tests__/installer.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,9 @@ describe('setup-node', () => {
728728

729729
await main.run();
730730

731-
expect(cnSpy).toHaveBeenCalledWith(
732-
`::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}`
731+
expect(warningSpy).toHaveBeenCalledWith(
732+
// `::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}`
733+
'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.'
733734
);
734735
});
735736

dist/cache-save/index.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61185,16 +61185,14 @@ function isGhes() {
6118561185
}
6118661186
exports.isGhes = isGhes;
6118761187
function isCacheFeatureAvailable() {
61188-
if (!cache.isFeatureAvailable()) {
61189-
if (isGhes()) {
61190-
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.');
61191-
}
61192-
else {
61193-
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
61194-
}
61188+
if (cache.isFeatureAvailable())
61189+
return true;
61190+
if (isGhes()) {
61191+
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.');
6119561192
return false;
6119661193
}
61197-
return true;
61194+
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
61195+
return false;
6119861196
}
6119961197
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
6120061198

dist/setup/index.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73139,16 +73139,14 @@ function isGhes() {
7313973139
}
7314073140
exports.isGhes = isGhes;
7314173141
function isCacheFeatureAvailable() {
73142-
if (!cache.isFeatureAvailable()) {
73143-
if (isGhes()) {
73144-
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.');
73145-
}
73146-
else {
73147-
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
73148-
}
73142+
if (cache.isFeatureAvailable())
73143+
return true;
73144+
if (isGhes()) {
73145+
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.');
7314973146
return false;
7315073147
}
73151-
return true;
73148+
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
73149+
return false;
7315273150
}
7315373151
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
7315473152

src/cache-utils.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,18 @@ export function isGhes(): boolean {
105105
}
106106

107107
export function isCacheFeatureAvailable(): boolean {
108-
if (!cache.isFeatureAvailable()) {
109-
if (isGhes()) {
110-
throw new Error(
111-
'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.'
112-
);
113-
} else {
114-
core.warning(
115-
'The runner was not able to contact the cache service. Caching will be skipped'
116-
);
117-
}
108+
if (cache.isFeatureAvailable()) return true;
118109

110+
if (isGhes()) {
111+
core.warning(
112+
'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.'
113+
);
119114
return false;
120115
}
121116

122-
return true;
117+
core.warning(
118+
'The runner was not able to contact the cache service. Caching will be skipped'
119+
);
120+
121+
return false;
123122
}

0 commit comments

Comments
 (0)