Skip to content

Commit a103ca0

Browse files
committed
chore: introduce CacheOptions{silent} to avoid logging in tests
Now that it's an esm module, we can't mock `core` like we were.
1 parent 38b2d30 commit a103ca0

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

src/with-cache.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const cache: CacheDelegate = {
1414
};
1515

1616
const restoreCacheMock = jest.spyOn(cache, "restoreCache");
17-
jest.spyOn(core, "info");
1817

1918
async function testFunction(): Promise<number> {
2019
return 42;
@@ -49,7 +48,7 @@ test("withCache skips on primary-key hit", async () => {
4948
cachePaths,
5049
cacheKeys,
5150
testFunction,
52-
DEFAULT_CACHE_OPTIONS,
51+
{ ...DEFAULT_CACHE_OPTIONS, silent: true },
5352
cache,
5453
);
5554

@@ -71,7 +70,7 @@ test("withCache acts and saves if no primary-key hit", async () => {
7170
cachePaths,
7271
cacheKeys,
7372
testFunction,
74-
DEFAULT_CACHE_OPTIONS,
73+
{ ...DEFAULT_CACHE_OPTIONS, silent: true },
7574
cache,
7675
);
7776

@@ -99,6 +98,7 @@ test("withCache can be configured to act and save anyway", async () => {
9998
{
10099
...DEFAULT_CACHE_OPTIONS,
101100
skipOnHit: false,
101+
silent: true,
102102
},
103103
cache,
104104
);
@@ -124,7 +124,7 @@ test("withCache does not save on error", async () => {
124124
cachePaths,
125125
cacheKeys,
126126
testFunctionThrows,
127-
DEFAULT_CACHE_OPTIONS,
127+
{ ...DEFAULT_CACHE_OPTIONS, silent: true },
128128
cache,
129129
);
130130
}).rejects.toThrow();
@@ -152,6 +152,7 @@ test("withCache can be configured to save on error", async () => {
152152
{
153153
...DEFAULT_CACHE_OPTIONS,
154154
saveOnError: true,
155+
silent: true,
155156
},
156157
cache,
157158
);

src/with-cache.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import type { CacheKeys } from "./get-cache-keys.js";
55
export type CacheOptions = {
66
skipOnHit: boolean;
77
saveOnError: boolean;
8+
silent: boolean;
89
};
910

1011
export const DEFAULT_CACHE_OPTIONS = {
1112
skipOnHit: true,
1213
saveOnError: false,
14+
silent: false,
1315
};
1416

1517
export interface CacheDelegate {
@@ -29,11 +31,13 @@ export async function withCache<T>(
2931
cache?: CacheDelegate,
3032
): Promise<T | undefined> {
3133
const cacheImpl = cache ?? realCache;
32-
const { skipOnHit, saveOnError } = options;
34+
const { skipOnHit, saveOnError, silent } = options;
3335

34-
core.info(`Cached paths:\n - ${paths.join("\n - ")}`);
35-
core.info(`Cache key: ${keys.primaryKey}`);
36-
core.info(`Cache restore keys:\n - ${keys.restoreKeys.join("\n - ")}`);
36+
if (!silent) {
37+
core.info(`Cached paths:\n - ${paths.join("\n - ")}`);
38+
core.info(`Cache key: ${keys.primaryKey}`);
39+
core.info(`Cache restore keys:\n - ${keys.restoreKeys.join("\n - ")}`);
40+
}
3741

3842
const restoredKey = await cacheImpl.restoreCache(
3943
paths,
@@ -44,13 +48,17 @@ export async function withCache<T>(
4448
const primaryKeyHit = restoredKey == keys.primaryKey;
4549

4650
if (restoredKey) {
47-
core.info(`Cache restored from key: ${restoredKey}`);
51+
if (!silent) {
52+
core.info(`Cache restored from key: ${restoredKey}`);
53+
}
4854
} else {
4955
core.warning("No cache found");
5056
}
5157

5258
if (primaryKeyHit && skipOnHit && !saveOnError) {
53-
core.info("Skipping due to primary key hit");
59+
if (!silent) {
60+
core.info("Skipping due to primary key hit");
61+
}
5462
return;
5563
}
5664

0 commit comments

Comments
 (0)