stateinputprovider with pending test cases fix

This commit is contained in:
Sankalp Kotewar 2023-01-02 07:29:20 +00:00 committed by GitHub
parent 365406cb70
commit 5b7eeecaeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 53 additions and 28 deletions

View File

@ -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(

View File

@ -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);
});

View File

@ -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;

View File

@ -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;

View File

@ -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
View File

@ -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;

View File

@ -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.`);

View File

@ -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);
};
}

View File

@ -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;