Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for environments without remote cache #91

Merged
merged 4 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98989,9 +98989,17 @@ function fromRemoteCache(version, packager, customCacheKey) {
// see: https://github.com/actions/toolkit/blob/8a4134761f09d0d97fb15f297705fd8644fef920/packages/tool-cache/src/tool-cache.ts#L401
const target = path_1.default.join(process.env['RUNNER_TOOL_CACHE'] || '', 'expo-cli', version, os_1.default.arch());
const cacheKey = customCacheKey || getRemoteKey(version, packager);
const hit = yield cache_1.restoreCache([target], cacheKey);
if (hit) {
return target;
try {
// When running with nektos/act, or other custom environments, the cache might not be set up.
const hit = yield cache_1.restoreCache([target], cacheKey);
if (hit) {
return target;
}
}
catch (error) {
if (!handleRemoteCacheError(error)) {
throw error;
}
}
});
}
Expand All @@ -99007,11 +99015,7 @@ function toRemoteCache(source, version, packager, customCacheKey) {
yield cache_1.saveCache([source], cacheKey);
}
catch (error) {
if (error instanceof cache_1.ReserveCacheError) {
core.info('Skipping remote cache storage, encountered error:');
core.info(error.message);
}
else {
if (!handleRemoteCacheError(error)) {
throw error;
}
}
Expand All @@ -99024,6 +99028,23 @@ exports.toRemoteCache = toRemoteCache;
function getRemoteKey(version, packager) {
return `expo-cli-${process.platform}-${os_1.default.arch()}-${packager}-${version}`;
}
/**
* Handle any incoming errors from cache methods.
* This can include actual errors like `ReserveCacheErrors` or unavailability errors.
* When the error is handled, it MUST provide feedback for the developer.
*
* @returns If the error was handled properly.
*/
function handleRemoteCacheError(error) {
const isReserveCacheError = error instanceof cache_1.ReserveCacheError;
const isCacheUnavailable = error.message.toLowerCase().includes('cache service url not found');
if (isReserveCacheError || isCacheUnavailable) {
core.warning('Skipping remote cache storage, encountered error:');
core.warning(error.message);
return true;
}
return false;
}


/***/ }),
Expand Down
40 changes: 33 additions & 7 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@ export async function fromRemoteCache(version: string, packager: string, customC
// see: https://github.com/actions/toolkit/blob/8a4134761f09d0d97fb15f297705fd8644fef920/packages/tool-cache/src/tool-cache.ts#L401
const target = path.join(process.env['RUNNER_TOOL_CACHE'] || '', 'expo-cli', version, os.arch());
const cacheKey = customCacheKey || getRemoteKey(version, packager);
const hit = await restoreCache([target], cacheKey);

if (hit) {
return target;
try {
// When running with nektos/act, or other custom environments, the cache might not be set up.
const hit = await restoreCache([target], cacheKey);
if (hit) {
return target;
}
} catch (error) {
if (!handleRemoteCacheError(error)) {
throw error;
}
}
}

Expand All @@ -49,10 +56,7 @@ export async function toRemoteCache(source: string, version: string, packager: s
try {
await saveCache([source], cacheKey);
} catch (error) {
if (error instanceof ReserveCacheError) {
core.info('Skipping remote cache storage, encountered error:');
core.info(error.message);
} else {
if (!handleRemoteCacheError(error)) {
throw error;
}
}
Expand All @@ -64,3 +68,25 @@ export async function toRemoteCache(source: string, version: string, packager: s
function getRemoteKey(version: string, packager: string): string {
return `expo-cli-${process.platform}-${os.arch()}-${packager}-${version}`;
}

/**
* Handle any incoming errors from cache methods.
* This can include actual errors like `ReserveCacheErrors` or unavailability errors.
* When the error is handled, it MUST provide feedback for the developer.
*
* @returns If the error was handled properly.
*/
function handleRemoteCacheError(error: Error): boolean {
const isReserveCacheError = error instanceof ReserveCacheError;
const isCacheUnavailable = error.message.toLowerCase().includes(
'cache service url not found',
);

if (isReserveCacheError || isCacheUnavailable) {
core.warning('Skipping remote cache storage, encountered error:');
core.warning(error.message);
return true;
}

return false;
}
14 changes: 14 additions & 0 deletions tests/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ describe('fromRemoteCache', () => {
spy.restore.mockRejectedValueOnce(error);
await expect(cache.fromRemoteCache('3.20.1', 'yarn')).rejects.toBe(error);
});

it('skips remote cache when unavailable', async () => {
// see: https://github.com/actions/toolkit/blob/9167ce1f3a32ad495fc1dbcb574c03c0e013ae53/packages/cache/src/internal/cacheHttpClient.ts#L41
const error = new Error('Cache Service Url not found, unable to restore cache.');
spy.restore.mockRejectedValueOnce(error);
await expect(cache.fromRemoteCache('3.20.1', 'yarn')).resolves.toBeUndefined();
});
});

describe('toRemoteCache', () => {
Expand Down Expand Up @@ -107,4 +114,11 @@ describe('toRemoteCache', () => {
spy.save.mockRejectedValueOnce(error);
await expect(cache.toRemoteCache(join('local', 'path'), '3.20.1', 'yarn')).rejects.toBe(error);
});

it('skips remote cache when unavailable', async () => {
// see: https://github.com/actions/toolkit/blob/9167ce1f3a32ad495fc1dbcb574c03c0e013ae53/packages/cache/src/internal/cacheHttpClient.ts#L41
const error = new Error('Cache Service Url not found, unable to restore cache.');
spy.save.mockRejectedValueOnce(error);
await expect(cache.toRemoteCache(join('local', 'path'), '3.20.1', 'yarn')).resolves.toBeUndefined();
});
});