mirror of
				https://github.com/actions/cache.git
				synced 2025-11-04 08:56:37 +07:00 
			
		
		
		
	Compare commits
	
		
			9 Commits
		
	
	
		
			v4.0.2
			...
			kotewar/st
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| f46bb21315 | |||
| 2d5e079d9e | |||
| 667e98af5a | |||
| 5b7eeecaeb | |||
| 0769f2e443 | |||
| 515d10b4fd | |||
| 669e7536d9 | |||
| 29dbbce762 | |||
| ea5981db97 | 
@ -11,12 +11,12 @@ 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, "getInput").mockImplementation(name => {
 | 
			
		||||
        return testUtils.getInput(name);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    jest.spyOn(core, "setOutput").mockImplementation((key, value) => {
 | 
			
		||||
        return jest.requireActual("@actions/core").getInput(key, value);
 | 
			
		||||
    jest.spyOn(core, "getState").mockImplementation(name => {
 | 
			
		||||
        return jest.requireActual("@actions/core").getState(name);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    jest.spyOn(actionUtils, "getInputAsArray").mockImplementation(
 | 
			
		||||
 | 
			
		||||
@ -1,11 +1,12 @@
 | 
			
		||||
import * as core from "@actions/core";
 | 
			
		||||
 | 
			
		||||
import { Events, RefKey, State } from "../src/constants";
 | 
			
		||||
import { Events, Inputs, RefKey, State } from "../src/constants";
 | 
			
		||||
import {
 | 
			
		||||
    IStateProvider,
 | 
			
		||||
    NullStateProvider,
 | 
			
		||||
    StateProvider
 | 
			
		||||
} from "../src/stateProvider";
 | 
			
		||||
import * as testUtils from "../src/utils/testUtils";
 | 
			
		||||
 | 
			
		||||
jest.mock("@actions/core");
 | 
			
		||||
 | 
			
		||||
@ -58,32 +59,43 @@ test("StateProvider saves states", 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
 | 
			
		||||
        .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")
 | 
			
		||||
        .mockImplementation((key, value) => {
 | 
			
		||||
            return jest.requireActual("@actions/core").setOutput(key, value);
 | 
			
		||||
            states.set(key, value);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    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";
 | 
			
		||||
    const cachePrimaryKey = "primary-key";
 | 
			
		||||
    const nullStateProvider: IStateProvider = new NullStateProvider();
 | 
			
		||||
    nullStateProvider.setState(State.CacheMatchedKey, "outputValue");
 | 
			
		||||
    nullStateProvider.setState(State.CachePrimaryKey, cacheMatchedKey);
 | 
			
		||||
    nullStateProvider.getState("outputKey");
 | 
			
		||||
    nullStateProvider.getCacheState();
 | 
			
		||||
    testUtils.setInput(Inputs.Key, cachePrimaryKey);
 | 
			
		||||
    nullStateProvider.setState(State.CachePrimaryKey, cachePrimaryKey);
 | 
			
		||||
    nullStateProvider.setState(State.CacheMatchedKey, cacheMatchedKey);
 | 
			
		||||
    const output1 = nullStateProvider.getState(State.CachePrimaryKey);
 | 
			
		||||
    const output2 = nullStateProvider.getCacheState();
 | 
			
		||||
 | 
			
		||||
    expect(getStateMock).toHaveBeenCalledTimes(0);
 | 
			
		||||
    expect(getInputMock).toHaveBeenCalledTimes(1);
 | 
			
		||||
    expect(output1).toBe("primary-key");
 | 
			
		||||
    expect(output2).toBe(undefined);
 | 
			
		||||
    expect(setOutputMock).toHaveBeenCalledTimes(2);
 | 
			
		||||
    expect(saveStateMock).toHaveBeenCalledTimes(0);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										15
									
								
								dist/restore-only/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										15
									
								
								dist/restore-only/index.js
									
									
									
									
										vendored
									
									
								
							@ -9413,15 +9413,24 @@ 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]
 | 
			
		||||
        ]);
 | 
			
		||||
        this.setState = (key, value) => {
 | 
			
		||||
            core.setOutput(this.stateToOutputMap.get(key), value);
 | 
			
		||||
            if (this.stateToOutputMap.has(key)) {
 | 
			
		||||
                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;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										15
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										15
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							@ -9413,15 +9413,24 @@ 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]
 | 
			
		||||
        ]);
 | 
			
		||||
        this.setState = (key, value) => {
 | 
			
		||||
            core.setOutput(this.stateToOutputMap.get(key), value);
 | 
			
		||||
            if (this.stateToOutputMap.has(key)) {
 | 
			
		||||
                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;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										18
									
								
								dist/save-only/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										18
									
								
								dist/save-only/index.js
									
									
									
									
										vendored
									
									
								
							@ -9469,15 +9469,24 @@ 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]
 | 
			
		||||
        ]);
 | 
			
		||||
        this.setState = (key, value) => {
 | 
			
		||||
            core.setOutput(this.stateToOutputMap.get(key), value);
 | 
			
		||||
            if (this.stateToOutputMap.has(key)) {
 | 
			
		||||
                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;
 | 
			
		||||
@ -41164,8 +41173,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;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										18
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										18
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							@ -9413,15 +9413,24 @@ 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]
 | 
			
		||||
        ]);
 | 
			
		||||
        this.setState = (key, value) => {
 | 
			
		||||
            core.setOutput(this.stateToOutputMap.get(key), value);
 | 
			
		||||
            if (this.stateToOutputMap.has(key)) {
 | 
			
		||||
                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;
 | 
			
		||||
@ -41108,8 +41117,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,14 +33,25 @@ 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]
 | 
			
		||||
    ]);
 | 
			
		||||
 | 
			
		||||
    setState = (key: string, value: string) => {
 | 
			
		||||
        core.setOutput(this.stateToOutputMap.get(key) as string, value);
 | 
			
		||||
        if (this.stateToOutputMap.has(key)) {
 | 
			
		||||
            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) => "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user