Compare commits

..

1 Commits

Author SHA1 Message Date
e630eda107 Fix json5 package version 2023-01-09 10:28:50 +00:00
10 changed files with 49 additions and 108 deletions

View File

@ -11,12 +11,12 @@ jest.mock("@actions/cache");
jest.mock("../src/utils/actionUtils"); jest.mock("../src/utils/actionUtils");
beforeAll(() => { beforeAll(() => {
jest.spyOn(core, "getInput").mockImplementation(name => { jest.spyOn(core, "getInput").mockImplementation((name, options) => {
return testUtils.getInput(name); return jest.requireActual("@actions/core").getInput(name, options);
}); });
jest.spyOn(core, "getState").mockImplementation(name => { jest.spyOn(core, "setOutput").mockImplementation((key, value) => {
return jest.requireActual("@actions/core").getState(name); return jest.requireActual("@actions/core").getInput(key, value);
}); });
jest.spyOn(actionUtils, "getInputAsArray").mockImplementation( jest.spyOn(actionUtils, "getInputAsArray").mockImplementation(

View File

@ -1,12 +1,11 @@
import * as core from "@actions/core"; import * as core from "@actions/core";
import { Events, Inputs, RefKey, State } from "../src/constants"; import { Events, RefKey, State } from "../src/constants";
import { import {
IStateProvider, IStateProvider,
NullStateProvider, NullStateProvider,
StateProvider StateProvider
} from "../src/stateProvider"; } from "../src/stateProvider";
import * as testUtils from "../src/utils/testUtils";
jest.mock("@actions/core"); jest.mock("@actions/core");
@ -59,43 +58,32 @@ test("StateProvider saves states", async () => {
}); });
test("NullStateProvider saves outputs", async () => { test("NullStateProvider saves outputs", async () => {
const states = new Map<string, string>();
const getInputMock = jest
.spyOn(core, "getInput")
.mockImplementation(key => testUtils.getInput(key));
const getStateMock = jest const getStateMock = jest
.spyOn(core, "getState") .spyOn(core, "getState")
.mockImplementation(key => { .mockImplementation(name =>
return jest.requireActual("@actions/core").getState(key); jest.requireActual("@actions/core").getState(name)
}); );
const setOutputMock = jest const setOutputMock = jest
.spyOn(core, "setOutput") .spyOn(core, "setOutput")
.mockImplementation((key, value) => { .mockImplementation((key, value) => {
states.set(key, value); return jest.requireActual("@actions/core").setOutput(key, value);
}); });
const saveStateMock = jest const saveStateMock = jest
.spyOn(core, "saveState") .spyOn(core, "saveState")
.mockImplementation((key, value) => { .mockImplementation((key, value) => {
states.set(key, value); return jest.requireActual("@actions/core").saveState(key, value);
}); });
const cacheMatchedKey = "node-cache"; const cacheMatchedKey = "node-cache";
const cachePrimaryKey = "primary-key";
const nullStateProvider: IStateProvider = new NullStateProvider(); const nullStateProvider: IStateProvider = new NullStateProvider();
testUtils.setInput(Inputs.Key, cachePrimaryKey); nullStateProvider.setState(State.CacheMatchedKey, "outputValue");
nullStateProvider.setState(State.CachePrimaryKey, cachePrimaryKey); nullStateProvider.setState(State.CachePrimaryKey, cacheMatchedKey);
nullStateProvider.setState(State.CacheMatchedKey, cacheMatchedKey); nullStateProvider.getState("outputKey");
const output1 = nullStateProvider.getState(State.CachePrimaryKey); nullStateProvider.getCacheState();
const output2 = nullStateProvider.getCacheState();
expect(getStateMock).toHaveBeenCalledTimes(0); expect(getStateMock).toHaveBeenCalledTimes(0);
expect(getInputMock).toHaveBeenCalledTimes(1);
expect(output1).toBe("primary-key");
expect(output2).toBe(undefined);
expect(setOutputMock).toHaveBeenCalledTimes(2); expect(setOutputMock).toHaveBeenCalledTimes(2);
expect(saveStateMock).toHaveBeenCalledTimes(0); expect(saveStateMock).toHaveBeenCalledTimes(0);
}); });

View File

@ -9413,24 +9413,15 @@ exports.StateProvider = StateProvider;
class NullStateProvider extends StateProviderBase { class NullStateProvider extends StateProviderBase {
constructor() { constructor() {
super(...arguments); super(...arguments);
this.stateToInputMap = new Map([
[constants_1.State.CachePrimaryKey, constants_1.Inputs.Key]
]);
this.stateToOutputMap = new Map([ this.stateToOutputMap = new Map([
[constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey], [constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey],
[constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
]); ]);
this.setState = (key, value) => { this.setState = (key, value) => {
if (this.stateToOutputMap.has(key)) { core.setOutput(this.stateToOutputMap.get(key), value);
core.setOutput(this.stateToOutputMap.get(key), value);
}
};
this.getState = (key) => {
if (!this.stateToInputMap.has(key)) {
return "";
}
return core.getInput(this.stateToInputMap.get(key));
}; };
// eslint-disable-next-line @typescript-eslint/no-unused-vars
this.getState = (key) => "";
} }
} }
exports.NullStateProvider = NullStateProvider; exports.NullStateProvider = NullStateProvider;

15
dist/restore/index.js vendored
View File

@ -9413,24 +9413,15 @@ exports.StateProvider = StateProvider;
class NullStateProvider extends StateProviderBase { class NullStateProvider extends StateProviderBase {
constructor() { constructor() {
super(...arguments); super(...arguments);
this.stateToInputMap = new Map([
[constants_1.State.CachePrimaryKey, constants_1.Inputs.Key]
]);
this.stateToOutputMap = new Map([ this.stateToOutputMap = new Map([
[constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey], [constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey],
[constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
]); ]);
this.setState = (key, value) => { this.setState = (key, value) => {
if (this.stateToOutputMap.has(key)) { core.setOutput(this.stateToOutputMap.get(key), value);
core.setOutput(this.stateToOutputMap.get(key), value);
}
};
this.getState = (key) => {
if (!this.stateToInputMap.has(key)) {
return "";
}
return core.getInput(this.stateToInputMap.get(key));
}; };
// eslint-disable-next-line @typescript-eslint/no-unused-vars
this.getState = (key) => "";
} }
} }
exports.NullStateProvider = NullStateProvider; exports.NullStateProvider = NullStateProvider;

View File

@ -9469,24 +9469,15 @@ exports.StateProvider = StateProvider;
class NullStateProvider extends StateProviderBase { class NullStateProvider extends StateProviderBase {
constructor() { constructor() {
super(...arguments); super(...arguments);
this.stateToInputMap = new Map([
[constants_1.State.CachePrimaryKey, constants_1.Inputs.Key]
]);
this.stateToOutputMap = new Map([ this.stateToOutputMap = new Map([
[constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey], [constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey],
[constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
]); ]);
this.setState = (key, value) => { this.setState = (key, value) => {
if (this.stateToOutputMap.has(key)) { core.setOutput(this.stateToOutputMap.get(key), value);
core.setOutput(this.stateToOutputMap.get(key), value);
}
};
this.getState = (key) => {
if (!this.stateToInputMap.has(key)) {
return "";
}
return core.getInput(this.stateToInputMap.get(key));
}; };
// eslint-disable-next-line @typescript-eslint/no-unused-vars
this.getState = (key) => "";
} }
} }
exports.NullStateProvider = NullStateProvider; exports.NullStateProvider = NullStateProvider;
@ -41173,7 +41164,8 @@ function saveImpl(stateProvider) {
} }
// If restore has stored a primary key in state, reuse that // If restore has stored a primary key in state, reuse that
// Else re-evaluate from inputs // Else re-evaluate from inputs
const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey); const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey) ||
core.getInput(constants_1.Inputs.Key);
if (!primaryKey) { if (!primaryKey) {
utils.logWarning(`Key is not specified.`); utils.logWarning(`Key is not specified.`);
return; return;

18
dist/save/index.js vendored
View File

@ -9413,24 +9413,15 @@ exports.StateProvider = StateProvider;
class NullStateProvider extends StateProviderBase { class NullStateProvider extends StateProviderBase {
constructor() { constructor() {
super(...arguments); super(...arguments);
this.stateToInputMap = new Map([
[constants_1.State.CachePrimaryKey, constants_1.Inputs.Key]
]);
this.stateToOutputMap = new Map([ this.stateToOutputMap = new Map([
[constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey], [constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey],
[constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
]); ]);
this.setState = (key, value) => { this.setState = (key, value) => {
if (this.stateToOutputMap.has(key)) { core.setOutput(this.stateToOutputMap.get(key), value);
core.setOutput(this.stateToOutputMap.get(key), value);
}
};
this.getState = (key) => {
if (!this.stateToInputMap.has(key)) {
return "";
}
return core.getInput(this.stateToInputMap.get(key));
}; };
// eslint-disable-next-line @typescript-eslint/no-unused-vars
this.getState = (key) => "";
} }
} }
exports.NullStateProvider = NullStateProvider; exports.NullStateProvider = NullStateProvider;
@ -41117,7 +41108,8 @@ function saveImpl(stateProvider) {
} }
// If restore has stored a primary key in state, reuse that // If restore has stored a primary key in state, reuse that
// Else re-evaluate from inputs // Else re-evaluate from inputs
const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey); const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey) ||
core.getInput(constants_1.Inputs.Key);
if (!primaryKey) { if (!primaryKey) {
utils.logWarning(`Key is not specified.`); utils.logWarning(`Key is not specified.`);
return; return;

24
package-lock.json generated
View File

@ -8146,9 +8146,9 @@
"dev": true "dev": true
}, },
"node_modules/json5": { "node_modules/json5": {
"version": "2.2.1", "version": "2.2.3",
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz", "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
"integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==", "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==",
"dev": true, "dev": true,
"bin": { "bin": {
"json5": "lib/cli.js" "json5": "lib/cli.js"
@ -9406,9 +9406,9 @@
} }
}, },
"node_modules/tsconfig-paths/node_modules/json5": { "node_modules/tsconfig-paths/node_modules/json5": {
"version": "1.0.1", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.2.tgz",
"integrity": "sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==", "integrity": "sha512-g1MWMLBiz8FKi1e4w0UyVL3w+iJceWAFBAaBnnGKOpNa5f8TLktkbre1+s6oICydWAm+HRUGTmI+//xv2hvXYA==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"minimist": "^1.2.0" "minimist": "^1.2.0"
@ -16062,9 +16062,9 @@
"dev": true "dev": true
}, },
"json5": { "json5": {
"version": "2.2.1", "version": "2.2.3",
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz", "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
"integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==", "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==",
"dev": true "dev": true
}, },
"kleur": { "kleur": {
@ -16967,9 +16967,9 @@
}, },
"dependencies": { "dependencies": {
"json5": { "json5": {
"version": "1.0.1", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.2.tgz",
"integrity": "sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==", "integrity": "sha512-g1MWMLBiz8FKi1e4w0UyVL3w+iJceWAFBAaBnnGKOpNa5f8TLktkbre1+s6oICydWAm+HRUGTmI+//xv2hvXYA==",
"dev": true, "dev": true,
"requires": { "requires": {
"minimist": "^1.2.0" "minimist": "^1.2.0"

View File

@ -28,7 +28,9 @@ async function saveImpl(stateProvider: IStateProvider): Promise<number | void> {
// If restore has stored a primary key in state, reuse that // If restore has stored a primary key in state, reuse that
// Else re-evaluate from inputs // Else re-evaluate from inputs
const primaryKey = stateProvider.getState(State.CachePrimaryKey); const primaryKey =
stateProvider.getState(State.CachePrimaryKey) ||
core.getInput(Inputs.Key);
if (!primaryKey) { if (!primaryKey) {
utils.logWarning(`Key is not specified.`); utils.logWarning(`Key is not specified.`);

View File

@ -1,6 +1,6 @@
import * as core from "@actions/core"; import * as core from "@actions/core";
import { Inputs, Outputs, State } from "./constants"; import { Outputs, State } from "./constants";
export interface IStateProvider { export interface IStateProvider {
setState(key: string, value: string): void; setState(key: string, value: string): void;
@ -33,25 +33,14 @@ export class StateProvider extends StateProviderBase {
} }
export class NullStateProvider extends StateProviderBase { export class NullStateProvider extends StateProviderBase {
stateToInputMap = new Map<string, string>([
[State.CachePrimaryKey, Inputs.Key]
]);
stateToOutputMap = new Map<string, string>([ stateToOutputMap = new Map<string, string>([
[State.CacheMatchedKey, Outputs.CacheMatchedKey], [State.CacheMatchedKey, Outputs.CacheMatchedKey],
[State.CachePrimaryKey, Outputs.CachePrimaryKey] [State.CachePrimaryKey, Outputs.CachePrimaryKey]
]); ]);
setState = (key: string, value: string) => { setState = (key: string, value: string) => {
if (this.stateToOutputMap.has(key)) { core.setOutput(this.stateToOutputMap.get(key) as string, value);
core.setOutput(this.stateToOutputMap.get(key) as string, value);
}
};
getState = (key: string) => {
if (!this.stateToInputMap.has(key)) {
return "";
}
return core.getInput(this.stateToInputMap.get(key) as string);
}; };
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getState = (key: string) => "";
} }

View File

@ -9,10 +9,6 @@ export function setInput(name: string, value: string): void {
process.env[getInputName(name)] = value; process.env[getInputName(name)] = value;
} }
export function getInput(name: string): string {
return process.env[getInputName(name)] as string;
}
interface CacheInput { interface CacheInput {
path: string; path: string;
key: string; key: string;