Skip to content

Commit

Permalink
Fix cacheSaveAlways behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrisbin committed Feb 12, 2024
1 parent 536553c commit 5cc3f1e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 121 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
"@actions/cache": "^3.2.4",
"@actions/core": "^1.6.0",
"@actions/exec": "^1.1.0",
"@actions/github": "^6.0.0",
"@actions/glob": "^0.4.0",
"js-yaml": "^4.1.0",
"minimatch": "^9.0.3",
"shellwords-ts": "^3.0.1"
},
"devDependencies": {
"@actions/glob": "^0.4.0",
"@types/jest": "^27.4.0",
"@types/js-yaml": "^4.0.5",
"@types/node": "^20.11.10",
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function run() {
},
{
...DEFAULT_CACHE_OPTIONS,
skipOnHit: !inputs.cacheSaveAlways,
saveOnError: inputs.cacheSaveAlways,
},
);
});
Expand All @@ -88,6 +88,7 @@ async function run() {
{
...DEFAULT_CACHE_OPTIONS,
skipOnHit: false, // always Build
saveOnError: inputs.cacheSaveAlways,
},
);
});
Expand Down
52 changes: 52 additions & 0 deletions src/with-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ async function testFunction(): Promise<number> {
return 42;
}

async function testFunctionThrows(): Promise<number> {
throw new Error("Boom");
}

function simulateCacheHit(
_paths: string[],
primaryKey: string,
Expand Down Expand Up @@ -101,3 +105,51 @@ test("withCache can be configured to act and save anyway", async () => {
// This step is still skipped
expect(cache.saveCache).not.toHaveBeenCalled();
});

test("withCache does not save on error", async () => {
const cachePaths = ["/a", "/b"];
const cacheKeys = getCacheKeys(["a-b", "c", "d"]);
cache.restoreCache.mockImplementation(simulateCacheMiss);

await expect(async () => {
await withCache(
cachePaths,
cacheKeys,
testFunctionThrows,
TEST_CACHE_OPTIONS,
);
}).rejects.toThrow();

expect(cache.restoreCache).toHaveBeenCalledWith(
cachePaths,
cacheKeys.primaryKey,
cacheKeys.restoreKeys,
);

// This step is skipped
expect(cache.saveCache).not.toHaveBeenCalled();
});

test("withCache can be configured to save on error", async () => {
const cachePaths = ["/a", "/b"];
const cacheKeys = getCacheKeys(["a-b", "c", "d"]);
cache.restoreCache.mockImplementation(simulateCacheMiss);

await expect(async () => {
await withCache(cachePaths, cacheKeys, testFunctionThrows, {
...TEST_CACHE_OPTIONS,
saveOnError: true,
});
}).rejects.toThrow();

expect(cache.restoreCache).toHaveBeenCalledWith(
cachePaths,
cacheKeys.primaryKey,
cacheKeys.restoreKeys,
);

expect(cache.saveCache).toHaveBeenCalledWith(
cachePaths,
cacheKeys.primaryKey,
);
});
14 changes: 11 additions & 3 deletions src/with-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ export interface CacheDelegate {

export type CacheOptions = {
skipOnHit: boolean;
saveOnError: boolean;
coreDelegate: CoreDelegate;
cacheDelegate: CacheDelegate;
};

export const DEFAULT_CACHE_OPTIONS = {
skipOnHit: true,
saveOnError: false,
coreDelegate: core,
cacheDelegate: cache,
};
Expand All @@ -33,7 +35,7 @@ export async function withCache<T>(
fn: () => Promise<T>,
options: CacheOptions = DEFAULT_CACHE_OPTIONS,
): Promise<T | undefined> {
const { skipOnHit, coreDelegate, cacheDelegate } = options;
const { skipOnHit, saveOnError, coreDelegate, cacheDelegate } = options;

coreDelegate.info(`Paths:\n - ${paths.join("\n - ")}`);
coreDelegate.info(`Primary key: ${keys.primaryKey}`);
Expand All @@ -48,7 +50,7 @@ export async function withCache<T>(
const primaryKeyHit = restoredKey == keys.primaryKey;
coreDelegate.info(`Restored key: ${restoredKey ?? "<none>"}`);

if (primaryKeyHit && skipOnHit) {
if (primaryKeyHit && skipOnHit && !saveOnError) {
coreDelegate.info("Skipping due to primary key hit");
return;
}
Expand All @@ -57,10 +59,16 @@ export async function withCache<T>(

try {
result = await fn();
} finally {

if (!primaryKeyHit) {
await cacheDelegate.saveCache(paths, keys.primaryKey);
}
} catch (ex) {
if (saveOnError && !primaryKeyHit) {
await cacheDelegate.saveCache(paths, keys.primaryKey);
}

throw ex;
}

return result;
Expand Down
116 changes: 2 additions & 114 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@
dependencies:
"@actions/io" "^1.0.1"

"@actions/github@^6.0.0":
version "6.0.0"
resolved "https://registry.yarnpkg.com/@actions/github/-/github-6.0.0.tgz#65883433f9d81521b782a64cc1fd45eef2191ea7"
integrity sha512-alScpSVnYmjNEXboZjarjukQEzgCRmjMv6Xj47fsdnqGS73bjJNDpiiXmp8jr0UZLdUB6d9jW63IcmddUP+l0g==
dependencies:
"@actions/http-client" "^2.2.0"
"@octokit/core" "^5.0.1"
"@octokit/plugin-paginate-rest" "^9.0.0"
"@octokit/plugin-rest-endpoint-methods" "^10.0.0"

"@actions/glob@^0.1.0":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@actions/glob/-/glob-0.1.2.tgz#9685ed2d6583093479c8f137d067c4329d7d0974"
Expand All @@ -59,7 +49,7 @@
"@actions/core" "^1.9.1"
minimatch "^3.0.4"

"@actions/http-client@^2.0.1", "@actions/http-client@^2.1.1", "@actions/http-client@^2.2.0":
"@actions/http-client@^2.0.1", "@actions/http-client@^2.1.1":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@actions/http-client/-/http-client-2.2.0.tgz#f8239f375be6185fcd07765efdcf0031ad5df1a0"
integrity sha512-q+epW0trjVUUHboliPb4UF9g2msf+w61b32tAkFEwL/IwP0DQWgbCMM0Hbe3e3WXSKz5VcUXbzJQgy8Hkra/Lg==
Expand Down Expand Up @@ -714,86 +704,6 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@octokit/auth-token@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@octokit/auth-token/-/auth-token-4.0.0.tgz#40d203ea827b9f17f42a29c6afb93b7745ef80c7"
integrity sha512-tY/msAuJo6ARbK6SPIxZrPBms3xPbfwBrulZe0Wtr/DIY9lje2HeV1uoebShn6mx7SjCHif6EjMvoREj+gZ+SA==

"@octokit/core@^5.0.1":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@octokit/core/-/core-5.1.0.tgz#81dacf0197ed7855e6413f128bd6dd9e121e7d2f"
integrity sha512-BDa2VAMLSh3otEiaMJ/3Y36GU4qf6GI+VivQ/P41NC6GHcdxpKlqV0ikSZ5gdQsmS3ojXeRx5vasgNTinF0Q4g==
dependencies:
"@octokit/auth-token" "^4.0.0"
"@octokit/graphql" "^7.0.0"
"@octokit/request" "^8.0.2"
"@octokit/request-error" "^5.0.0"
"@octokit/types" "^12.0.0"
before-after-hook "^2.2.0"
universal-user-agent "^6.0.0"

"@octokit/endpoint@^9.0.0":
version "9.0.4"
resolved "https://registry.yarnpkg.com/@octokit/endpoint/-/endpoint-9.0.4.tgz#8afda5ad1ffc3073d08f2b450964c610b821d1ea"
integrity sha512-DWPLtr1Kz3tv8L0UvXTDP1fNwM0S+z6EJpRcvH66orY6Eld4XBMCSYsaWp4xIm61jTWxK68BrR7ibO+vSDnZqw==
dependencies:
"@octokit/types" "^12.0.0"
universal-user-agent "^6.0.0"

"@octokit/graphql@^7.0.0":
version "7.0.2"
resolved "https://registry.yarnpkg.com/@octokit/graphql/-/graphql-7.0.2.tgz#3df14b9968192f9060d94ed9e3aa9780a76e7f99"
integrity sha512-OJ2iGMtj5Tg3s6RaXH22cJcxXRi7Y3EBqbHTBRq+PQAqfaS8f/236fUrWhfSn8P4jovyzqucxme7/vWSSZBX2Q==
dependencies:
"@octokit/request" "^8.0.1"
"@octokit/types" "^12.0.0"
universal-user-agent "^6.0.0"

"@octokit/openapi-types@^19.1.0":
version "19.1.0"
resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-19.1.0.tgz#75ec7e64743870fc73e1ab4bc6ec252ecdd624dc"
integrity sha512-6G+ywGClliGQwRsjvqVYpklIfa7oRPA0vyhPQG/1Feh+B+wU0vGH1JiJ5T25d3g1JZYBHzR2qefLi9x8Gt+cpw==

"@octokit/plugin-paginate-rest@^9.0.0":
version "9.1.5"
resolved "https://registry.yarnpkg.com/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-9.1.5.tgz#1705bcef4dcde1f4015ee58a63dc61b68648f480"
integrity sha512-WKTQXxK+bu49qzwv4qKbMMRXej1DU2gq017euWyKVudA6MldaSSQuxtz+vGbhxV4CjxpUxjZu6rM2wfc1FiWVg==
dependencies:
"@octokit/types" "^12.4.0"

"@octokit/plugin-rest-endpoint-methods@^10.0.0":
version "10.2.0"
resolved "https://registry.yarnpkg.com/@octokit/plugin-rest-endpoint-methods/-/plugin-rest-endpoint-methods-10.2.0.tgz#eeaa4de97a2ae26404dea30ce3e17b11928e027c"
integrity sha512-ePbgBMYtGoRNXDyKGvr9cyHjQ163PbwD0y1MkDJCpkO2YH4OeXX40c4wYHKikHGZcpGPbcRLuy0unPUuafco8Q==
dependencies:
"@octokit/types" "^12.3.0"

"@octokit/request-error@^5.0.0":
version "5.0.1"
resolved "https://registry.yarnpkg.com/@octokit/request-error/-/request-error-5.0.1.tgz#277e3ce3b540b41525e07ba24c5ef5e868a72db9"
integrity sha512-X7pnyTMV7MgtGmiXBwmO6M5kIPrntOXdyKZLigNfQWSEQzVxR4a4vo49vJjTWX70mPndj8KhfT4Dx+2Ng3vnBQ==
dependencies:
"@octokit/types" "^12.0.0"
deprecation "^2.0.0"
once "^1.4.0"

"@octokit/request@^8.0.1", "@octokit/request@^8.0.2":
version "8.1.6"
resolved "https://registry.yarnpkg.com/@octokit/request/-/request-8.1.6.tgz#a76a859c30421737a3918b40973c2ff369009571"
integrity sha512-YhPaGml3ncZC1NfXpP3WZ7iliL1ap6tLkAp6MvbK2fTTPytzVUyUesBBogcdMm86uRYO5rHaM1xIWxigWZ17MQ==
dependencies:
"@octokit/endpoint" "^9.0.0"
"@octokit/request-error" "^5.0.0"
"@octokit/types" "^12.0.0"
universal-user-agent "^6.0.0"

"@octokit/types@^12.0.0", "@octokit/types@^12.3.0", "@octokit/types@^12.4.0":
version "12.4.0"
resolved "https://registry.yarnpkg.com/@octokit/types/-/types-12.4.0.tgz#8f97b601e91ce6b9776ed8152217e77a71be7aac"
integrity sha512-FLWs/AvZllw/AGVs+nJ+ELCDZZJk+kY0zMen118xhL2zD0s1etIUHm1odgjP7epxYU1ln7SZxEUWYop5bhsdgQ==
dependencies:
"@octokit/openapi-types" "^19.1.0"

"@opentelemetry/api@^1.0.1":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/api/-/api-1.7.0.tgz#b139c81999c23e3c8d3c0a7234480e945920fc40"
Expand Down Expand Up @@ -1116,11 +1026,6 @@ balanced-match@^1.0.0:
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==

before-after-hook@^2.2.0:
version "2.2.3"
resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-2.2.3.tgz#c51e809c81a4e354084422b9b26bad88249c517c"
integrity sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
Expand Down Expand Up @@ -1382,11 +1287,6 @@ delayed-stream@~1.0.0:
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==

deprecation@^2.0.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/deprecation/-/deprecation-2.3.1.tgz#6368cbdb40abf3373b525ac87e4a260c3a700919"
integrity sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==

detect-newline@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651"
Expand Down Expand Up @@ -2466,13 +2366,6 @@ minimatch@^5.0.1:
dependencies:
brace-expansion "^2.0.1"

minimatch@^9.0.3:
version "9.0.3"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.3.tgz#a6e00c3de44c3a542bfaae70abfc22420a6da825"
integrity sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==
dependencies:
brace-expansion "^2.0.1"

[email protected]:
version "2.1.2"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
Expand Down Expand Up @@ -2522,7 +2415,7 @@ nwsapi@^2.2.0:
resolved "https://registry.yarnpkg.com/nwsapi/-/nwsapi-2.2.7.tgz#738e0707d3128cb750dddcfe90e4610482df0f30"
integrity sha512-ub5E4+FBPKwAZx0UwIQOjYWGHTEq5sPqHQNRN8Z9e4A7u3Tj1weLJsL59yH9vmvqEtBHaOmT6cYQKIZOxp35FQ==

once@^1.3.0, once@^1.4.0:
once@^1.3.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
integrity sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==
Expand Down Expand Up @@ -3038,11 +2931,6 @@ undici@^5.25.4:
dependencies:
"@fastify/busboy" "^2.0.0"

universal-user-agent@^6.0.0:
version "6.0.1"
resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-6.0.1.tgz#15f20f55da3c930c57bddbf1734c6654d5fd35aa"
integrity sha512-yCzhz6FN2wU1NiiQRogkTQszlQSlpWaw8SvVegAc+bDxbzHgh1vX8uIe8OYyMH6DwH+sdTJsgMl36+mSMdRJIQ==

universalify@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.2.0.tgz#6451760566fa857534745ab1dde952d1b1761be0"
Expand Down

0 comments on commit 5cc3f1e

Please sign in to comment.