Update non-null state provider to also output primary/matched keys

This commit is contained in:
Stuart Leeks 2025-05-08 16:51:55 +00:00
parent 480d890516
commit 91afe36e0a
4 changed files with 51 additions and 33 deletions

View File

@ -149,7 +149,7 @@ test("restore with cache found for key", async () => {
const infoMock = jest.spyOn(core, "info");
const failedMock = jest.spyOn(core, "setFailed");
const stateMock = jest.spyOn(core, "saveState");
const setCacheHitOutputMock = jest.spyOn(core, "setOutput");
const setOutputMock = jest.spyOn(core, "setOutput");
const restoreCacheMock = jest
.spyOn(cache, "restoreCache")
.mockImplementationOnce(() => {
@ -173,8 +173,10 @@ test("restore with cache found for key", async () => {
expect(stateMock).toHaveBeenCalledWith("CACHE_RESULT", key);
expect(stateMock).toHaveBeenCalledTimes(2);
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "true");
expect(setOutputMock).toHaveBeenCalledTimes(3);
expect(setOutputMock).toHaveBeenCalledWith("cache-hit", "true");
expect(setOutputMock).toHaveBeenCalledWith("cache-primary-key", key);
expect(setOutputMock).toHaveBeenCalledWith("cache-matched-key", key);
expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`);
expect(failedMock).toHaveBeenCalledTimes(0);
@ -194,7 +196,7 @@ test("restore with cache found for restore key", async () => {
const infoMock = jest.spyOn(core, "info");
const failedMock = jest.spyOn(core, "setFailed");
const stateMock = jest.spyOn(core, "saveState");
const setCacheHitOutputMock = jest.spyOn(core, "setOutput");
const setOutputMock = jest.spyOn(core, "setOutput");
const restoreCacheMock = jest
.spyOn(cache, "restoreCache")
.mockImplementationOnce(() => {
@ -218,8 +220,10 @@ test("restore with cache found for restore key", async () => {
expect(stateMock).toHaveBeenCalledWith("CACHE_RESULT", restoreKey);
expect(stateMock).toHaveBeenCalledTimes(2);
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "false");
expect(setOutputMock).toHaveBeenCalledTimes(3);
expect(setOutputMock).toHaveBeenCalledWith("cache-hit", "false");
expect(setOutputMock).toHaveBeenCalledWith("cache-primary-key", key);
expect(setOutputMock).toHaveBeenCalledWith("cache-matched-key", restoreKey);
expect(infoMock).toHaveBeenCalledWith(
`Cache restored from key: ${restoreKey}`
);
@ -239,7 +243,7 @@ test("Fail restore when fail on cache miss is enabled and primary + restore keys
const failedMock = jest.spyOn(core, "setFailed");
const stateMock = jest.spyOn(core, "saveState");
const setCacheHitOutputMock = jest.spyOn(core, "setOutput");
const setOutputMock = jest.spyOn(core, "setOutput");
const restoreCacheMock = jest
.spyOn(cache, "restoreCache")
.mockImplementationOnce(() => {
@ -260,7 +264,8 @@ test("Fail restore when fail on cache miss is enabled and primary + restore keys
);
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(0);
expect(setOutputMock).toHaveBeenCalledTimes(1);
expect(setOutputMock).toHaveBeenCalledWith("cache-primary-key", key);
expect(failedMock).toHaveBeenCalledWith(
`Failed to restore cache entry. Exiting as fail-on-cache-miss is set. Input key: ${key}`
@ -282,7 +287,7 @@ test("restore when fail on cache miss is enabled and primary key doesn't match r
const infoMock = jest.spyOn(core, "info");
const failedMock = jest.spyOn(core, "setFailed");
const stateMock = jest.spyOn(core, "saveState");
const setCacheHitOutputMock = jest.spyOn(core, "setOutput");
const setOutputMock = jest.spyOn(core, "setOutput");
const restoreCacheMock = jest
.spyOn(cache, "restoreCache")
.mockImplementationOnce(() => {
@ -306,8 +311,10 @@ test("restore when fail on cache miss is enabled and primary key doesn't match r
expect(stateMock).toHaveBeenCalledWith("CACHE_RESULT", restoreKey);
expect(stateMock).toHaveBeenCalledTimes(2);
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "false");
expect(setOutputMock).toHaveBeenCalledTimes(3);
expect(setOutputMock).toHaveBeenCalledWith("cache-hit", "false");
expect(setOutputMock).toHaveBeenCalledWith("cache-primary-key", key);
expect(setOutputMock).toHaveBeenCalledWith("cache-matched-key", restoreKey);
expect(infoMock).toHaveBeenCalledWith(
`Cache restored from key: ${restoreKey}`

View File

@ -112,7 +112,7 @@ test("restore on GHES with AC available ", async () => {
const infoMock = jest.spyOn(core, "info");
const failedMock = jest.spyOn(core, "setFailed");
const stateMock = jest.spyOn(core, "saveState");
const setCacheHitOutputMock = jest.spyOn(core, "setOutput");
const setOutputMock = jest.spyOn(core, "setOutput");
const restoreCacheMock = jest
.spyOn(cache, "restoreCache")
.mockImplementationOnce(() => {
@ -133,8 +133,10 @@ test("restore on GHES with AC available ", async () => {
);
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "true");
expect(setOutputMock).toHaveBeenCalledTimes(3);
expect(setOutputMock).toHaveBeenCalledWith("cache-hit", "true");
expect(setOutputMock).toHaveBeenCalledWith("cache-primary-key", key);
expect(setOutputMock).toHaveBeenCalledWith("cache-matched-key", key);
expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`);
expect(failedMock).toHaveBeenCalledTimes(0);
@ -334,7 +336,7 @@ test("restore with cache found for key", async () => {
const infoMock = jest.spyOn(core, "info");
const failedMock = jest.spyOn(core, "setFailed");
const stateMock = jest.spyOn(core, "saveState");
const setCacheHitOutputMock = jest.spyOn(core, "setOutput");
const setOutputMock = jest.spyOn(core, "setOutput");
const restoreCacheMock = jest
.spyOn(cache, "restoreCache")
.mockImplementationOnce(() => {
@ -355,8 +357,11 @@ test("restore with cache found for key", async () => {
);
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "true");
expect(setOutputMock).toHaveBeenCalledTimes(3);
expect(setOutputMock).toHaveBeenCalledWith("cache-hit", "true");
expect(setOutputMock).toHaveBeenCalledWith("cache-primary-key", key);
expect(setOutputMock).toHaveBeenCalledWith("cache-matched-key", key);
expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`);
expect(failedMock).toHaveBeenCalledTimes(0);
@ -376,7 +381,7 @@ test("restore with cache found for restore key", async () => {
const infoMock = jest.spyOn(core, "info");
const failedMock = jest.spyOn(core, "setFailed");
const stateMock = jest.spyOn(core, "saveState");
const setCacheHitOutputMock = jest.spyOn(core, "setOutput");
const setOutputMock = jest.spyOn(core, "setOutput");
const restoreCacheMock = jest
.spyOn(cache, "restoreCache")
.mockImplementationOnce(() => {
@ -397,8 +402,10 @@ test("restore with cache found for restore key", async () => {
);
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "false");
expect(setOutputMock).toHaveBeenCalledTimes(3);
expect(setOutputMock).toHaveBeenCalledWith("cache-hit", "false");
expect(setOutputMock).toHaveBeenCalledWith("cache-primary-key", key);
expect(setOutputMock).toHaveBeenCalledWith("cache-matched-key", restoreKey);
expect(infoMock).toHaveBeenCalledWith(
`Cache restored from key: ${restoreKey}`
);
@ -417,7 +424,7 @@ test("restore with lookup-only set", async () => {
const infoMock = jest.spyOn(core, "info");
const failedMock = jest.spyOn(core, "setFailed");
const stateMock = jest.spyOn(core, "saveState");
const setCacheHitOutputMock = jest.spyOn(core, "setOutput");
const setOutputMock = jest.spyOn(core, "setOutput");
const restoreCacheMock = jest
.spyOn(cache, "restoreCache")
.mockImplementationOnce(() => {
@ -441,8 +448,10 @@ test("restore with lookup-only set", async () => {
expect(stateMock).toHaveBeenCalledWith("CACHE_RESULT", key);
expect(stateMock).toHaveBeenCalledTimes(2);
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "true");
expect(setOutputMock).toHaveBeenCalledTimes(3);
expect(setOutputMock).toHaveBeenCalledWith("cache-hit", "true");
expect(setOutputMock).toHaveBeenCalledWith("cache-primary-key", key);
expect(setOutputMock).toHaveBeenCalledWith("cache-matched-key", key);
expect(infoMock).toHaveBeenCalledWith(
`Cache found and can be restored from key: ${key}`

View File

@ -54,7 +54,7 @@ test("StateProvider saves states", async () => {
expect(cacheStateValue).toBe(cacheMatchedKey);
expect(getStateMock).toHaveBeenCalledTimes(2);
expect(saveStateMock).toHaveBeenCalledTimes(2);
expect(setOutputMock).toHaveBeenCalledTimes(0);
expect(setOutputMock).toHaveBeenCalledTimes(2);
});
test("NullStateProvider saves outputs", async () => {

View File

@ -21,26 +21,28 @@ class StateProviderBase implements IStateProvider {
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
setState = (key: string, value: string) => {};
setState = (key: string, value: string) => { };
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getState = (key: string) => "";
}
export class StateProvider extends StateProviderBase {
setState = core.saveState;
setState = (key: string, value: string) => { core.saveState(key, value); stateToOutput(key, value); };
getState = core.getState;
}
const stateToOutputMap = new Map<string, string>([
[State.CacheMatchedKey, Outputs.CacheMatchedKey],
[State.CachePrimaryKey, Outputs.CachePrimaryKey]
]);
function stateToOutput(key: string, value: string) {
core.setOutput(stateToOutputMap.get(key) as string, value);
}
export class NullStateProvider extends StateProviderBase {
stateToOutputMap = new Map<string, string>([
[State.CacheMatchedKey, Outputs.CacheMatchedKey],
[State.CachePrimaryKey, Outputs.CachePrimaryKey]
]);
setState = (key: string, value: string) => {
core.setOutput(this.stateToOutputMap.get(key) as string, value);
};
setState = stateToOutput;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getState = (key: string) => "";
}