mirror of
https://code.forgejo.org/actions/cache.git
synced 2024-11-29 18:00:59 -05:00
stateinputprovider with pending test cases fix
This commit is contained in:
parent
365406cb70
commit
5b7eeecaeb
9 changed files with 53 additions and 28 deletions
|
@ -11,12 +11,8 @@ jest.mock("@actions/cache");
|
|||
jest.mock("../src/utils/actionUtils");
|
||||
|
||||
beforeAll(() => {
|
||||
jest.spyOn(core, "getInput").mockImplementation((name, options) => {
|
||||
return jest.requireActual("@actions/core").getInput(name, options);
|
||||
});
|
||||
|
||||
jest.spyOn(core, "setOutput").mockImplementation((key, value) => {
|
||||
return jest.requireActual("@actions/core").getInput(key, value);
|
||||
jest.spyOn(core, "getInput").mockImplementation(name => {
|
||||
return testUtils.getInput(name);
|
||||
});
|
||||
|
||||
jest.spyOn(actionUtils, "getInputAsArray").mockImplementation(
|
||||
|
|
|
@ -58,11 +58,17 @@ test("StateProvider saves states", async () => {
|
|||
});
|
||||
|
||||
test("NullStateProvider saves outputs", async () => {
|
||||
const states = new Map<string, string>();
|
||||
|
||||
const getInputMock = jest
|
||||
.spyOn(core, "getInput")
|
||||
.mockImplementation(key => states.get(key) || "");
|
||||
|
||||
const getStateMock = jest
|
||||
.spyOn(core, "getState")
|
||||
.mockImplementation(name =>
|
||||
jest.requireActual("@actions/core").getState(name)
|
||||
);
|
||||
.mockImplementation(key => {
|
||||
return jest.requireActual("@actions/core").getState(key);
|
||||
});
|
||||
|
||||
const setOutputMock = jest
|
||||
.spyOn(core, "setOutput")
|
||||
|
@ -73,7 +79,7 @@ test("NullStateProvider saves outputs", async () => {
|
|||
const saveStateMock = jest
|
||||
.spyOn(core, "saveState")
|
||||
.mockImplementation((key, value) => {
|
||||
return jest.requireActual("@actions/core").saveState(key, value);
|
||||
states.set(key, value);
|
||||
});
|
||||
|
||||
const cacheMatchedKey = "node-cache";
|
||||
|
@ -84,6 +90,7 @@ test("NullStateProvider saves outputs", async () => {
|
|||
nullStateProvider.getCacheState();
|
||||
|
||||
expect(getStateMock).toHaveBeenCalledTimes(0);
|
||||
expect(getInputMock).toHaveBeenCalledTimes(2);
|
||||
expect(setOutputMock).toHaveBeenCalledTimes(2);
|
||||
expect(saveStateMock).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
|
8
dist/restore-only/index.js
vendored
8
dist/restore-only/index.js
vendored
|
@ -9405,6 +9405,9 @@ exports.StateProvider = StateProvider;
|
|||
class NullStateProvider extends StateProviderBase {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.stateToInputMap = new Map([
|
||||
[constants_1.State.CachePrimaryKey, constants_1.Inputs.Key]
|
||||
]);
|
||||
this.stateToOutputMap = new Map([
|
||||
[constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey],
|
||||
[constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
|
||||
|
@ -9412,8 +9415,9 @@ class NullStateProvider extends StateProviderBase {
|
|||
this.setState = (key, value) => {
|
||||
core.setOutput(this.stateToOutputMap.get(key), value);
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
this.getState = (key) => "";
|
||||
this.getState = (key) => {
|
||||
return core.getInput(this.stateToInputMap.get(key));
|
||||
};
|
||||
}
|
||||
}
|
||||
exports.NullStateProvider = NullStateProvider;
|
||||
|
|
8
dist/restore/index.js
vendored
8
dist/restore/index.js
vendored
|
@ -9405,6 +9405,9 @@ exports.StateProvider = StateProvider;
|
|||
class NullStateProvider extends StateProviderBase {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.stateToInputMap = new Map([
|
||||
[constants_1.State.CachePrimaryKey, constants_1.Inputs.Key]
|
||||
]);
|
||||
this.stateToOutputMap = new Map([
|
||||
[constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey],
|
||||
[constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
|
||||
|
@ -9412,8 +9415,9 @@ class NullStateProvider extends StateProviderBase {
|
|||
this.setState = (key, value) => {
|
||||
core.setOutput(this.stateToOutputMap.get(key), value);
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
this.getState = (key) => "";
|
||||
this.getState = (key) => {
|
||||
return core.getInput(this.stateToInputMap.get(key));
|
||||
};
|
||||
}
|
||||
}
|
||||
exports.NullStateProvider = NullStateProvider;
|
||||
|
|
11
dist/save-only/index.js
vendored
11
dist/save-only/index.js
vendored
|
@ -9461,6 +9461,9 @@ exports.StateProvider = StateProvider;
|
|||
class NullStateProvider extends StateProviderBase {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.stateToInputMap = new Map([
|
||||
[constants_1.State.CachePrimaryKey, constants_1.Inputs.Key]
|
||||
]);
|
||||
this.stateToOutputMap = new Map([
|
||||
[constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey],
|
||||
[constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
|
||||
|
@ -9468,8 +9471,9 @@ class NullStateProvider extends StateProviderBase {
|
|||
this.setState = (key, value) => {
|
||||
core.setOutput(this.stateToOutputMap.get(key), value);
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
this.getState = (key) => "";
|
||||
this.getState = (key) => {
|
||||
return core.getInput(this.stateToInputMap.get(key));
|
||||
};
|
||||
}
|
||||
}
|
||||
exports.NullStateProvider = NullStateProvider;
|
||||
|
@ -41059,8 +41063,7 @@ function saveImpl(stateProvider) {
|
|||
}
|
||||
// If restore has stored a primary key in state, reuse that
|
||||
// Else re-evaluate from inputs
|
||||
const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey) ||
|
||||
core.getInput(constants_1.Inputs.Key);
|
||||
const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey);
|
||||
if (!primaryKey) {
|
||||
utils.logWarning(`Key is not specified.`);
|
||||
return;
|
||||
|
|
11
dist/save/index.js
vendored
11
dist/save/index.js
vendored
|
@ -9405,6 +9405,9 @@ exports.StateProvider = StateProvider;
|
|||
class NullStateProvider extends StateProviderBase {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.stateToInputMap = new Map([
|
||||
[constants_1.State.CachePrimaryKey, constants_1.Inputs.Key]
|
||||
]);
|
||||
this.stateToOutputMap = new Map([
|
||||
[constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey],
|
||||
[constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
|
||||
|
@ -9412,8 +9415,9 @@ class NullStateProvider extends StateProviderBase {
|
|||
this.setState = (key, value) => {
|
||||
core.setOutput(this.stateToOutputMap.get(key), value);
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
this.getState = (key) => "";
|
||||
this.getState = (key) => {
|
||||
return core.getInput(this.stateToInputMap.get(key));
|
||||
};
|
||||
}
|
||||
}
|
||||
exports.NullStateProvider = NullStateProvider;
|
||||
|
@ -41003,8 +41007,7 @@ function saveImpl(stateProvider) {
|
|||
}
|
||||
// If restore has stored a primary key in state, reuse that
|
||||
// Else re-evaluate from inputs
|
||||
const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey) ||
|
||||
core.getInput(constants_1.Inputs.Key);
|
||||
const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey);
|
||||
if (!primaryKey) {
|
||||
utils.logWarning(`Key is not specified.`);
|
||||
return;
|
||||
|
|
|
@ -28,9 +28,7 @@ async function saveImpl(stateProvider: IStateProvider): Promise<number | void> {
|
|||
|
||||
// If restore has stored a primary key in state, reuse that
|
||||
// Else re-evaluate from inputs
|
||||
const primaryKey =
|
||||
stateProvider.getState(State.CachePrimaryKey) ||
|
||||
core.getInput(Inputs.Key);
|
||||
const primaryKey = stateProvider.getState(State.CachePrimaryKey);
|
||||
|
||||
if (!primaryKey) {
|
||||
utils.logWarning(`Key is not specified.`);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import * as core from "@actions/core";
|
||||
|
||||
import { Outputs, State } from "./constants";
|
||||
import { Inputs, Outputs, State } from "./constants";
|
||||
|
||||
export interface IStateProvider {
|
||||
setState(key: string, value: string): void;
|
||||
|
@ -33,6 +33,10 @@ export class StateProvider extends StateProviderBase {
|
|||
}
|
||||
|
||||
export class NullStateProvider extends StateProviderBase {
|
||||
stateToInputMap = new Map<string, string>([
|
||||
[State.CachePrimaryKey, Inputs.Key]
|
||||
]);
|
||||
|
||||
stateToOutputMap = new Map<string, string>([
|
||||
[State.CacheMatchedKey, Outputs.CacheMatchedKey],
|
||||
[State.CachePrimaryKey, Outputs.CachePrimaryKey]
|
||||
|
@ -41,6 +45,8 @@ export class NullStateProvider extends StateProviderBase {
|
|||
setState = (key: string, value: string) => {
|
||||
core.setOutput(this.stateToOutputMap.get(key) as string, value);
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
getState = (key: string) => "";
|
||||
|
||||
getState = (key: string) => {
|
||||
return core.getInput(this.stateToInputMap.get(key) as string);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -9,6 +9,10 @@ export function setInput(name: string, value: string): void {
|
|||
process.env[getInputName(name)] = value;
|
||||
}
|
||||
|
||||
export function getInput(name: string): string {
|
||||
return process.env[getInputName(name)] as string;
|
||||
}
|
||||
|
||||
interface CacheInput {
|
||||
path: string;
|
||||
key: string;
|
||||
|
|
Loading…
Reference in a new issue