mirror of
				https://kkgithub.com/actions/cache.git
				synced 2025-11-04 12:41:50 +08:00 
			
		
		
		
	Fixed test cases
This commit is contained in:
		@ -15,6 +15,10 @@ beforeAll(() => {
 | 
				
			|||||||
        return testUtils.getInput(name);
 | 
					        return testUtils.getInput(name);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    jest.spyOn(core, "getState").mockImplementation(name => {
 | 
				
			||||||
 | 
					        return jest.requireActual("@actions/core").getState(name);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    jest.spyOn(actionUtils, "getInputAsArray").mockImplementation(
 | 
					    jest.spyOn(actionUtils, "getInputAsArray").mockImplementation(
 | 
				
			||||||
        (name, options) => {
 | 
					        (name, options) => {
 | 
				
			||||||
            return jest
 | 
					            return jest
 | 
				
			||||||
 | 
				
			|||||||
@ -1,11 +1,12 @@
 | 
				
			|||||||
import * as core from "@actions/core";
 | 
					import * as core from "@actions/core";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { Events, RefKey, State } from "../src/constants";
 | 
					import { Events, Inputs, 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");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -62,7 +63,7 @@ test("NullStateProvider saves outputs", async () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const getInputMock = jest
 | 
					    const getInputMock = jest
 | 
				
			||||||
        .spyOn(core, "getInput")
 | 
					        .spyOn(core, "getInput")
 | 
				
			||||||
        .mockImplementation(key => states.get(key) || "");
 | 
					        .mockImplementation(key => testUtils.getInput(key));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const getStateMock = jest
 | 
					    const getStateMock = jest
 | 
				
			||||||
        .spyOn(core, "getState")
 | 
					        .spyOn(core, "getState")
 | 
				
			||||||
@ -73,7 +74,7 @@ test("NullStateProvider saves outputs", async () => {
 | 
				
			|||||||
    const setOutputMock = jest
 | 
					    const setOutputMock = jest
 | 
				
			||||||
        .spyOn(core, "setOutput")
 | 
					        .spyOn(core, "setOutput")
 | 
				
			||||||
        .mockImplementation((key, value) => {
 | 
					        .mockImplementation((key, value) => {
 | 
				
			||||||
            return jest.requireActual("@actions/core").setOutput(key, value);
 | 
					            states.set(key, value);
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const saveStateMock = jest
 | 
					    const saveStateMock = jest
 | 
				
			||||||
@ -83,14 +84,18 @@ test("NullStateProvider saves outputs", async () => {
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const cacheMatchedKey = "node-cache";
 | 
					    const cacheMatchedKey = "node-cache";
 | 
				
			||||||
 | 
					    const cachePrimaryKey = "primary-key";
 | 
				
			||||||
    const nullStateProvider: IStateProvider = new NullStateProvider();
 | 
					    const nullStateProvider: IStateProvider = new NullStateProvider();
 | 
				
			||||||
    nullStateProvider.setState(State.CacheMatchedKey, "outputValue");
 | 
					    testUtils.setInput(Inputs.Key, cachePrimaryKey);
 | 
				
			||||||
    nullStateProvider.setState(State.CachePrimaryKey, cacheMatchedKey);
 | 
					    nullStateProvider.setState(State.CachePrimaryKey, cachePrimaryKey);
 | 
				
			||||||
    nullStateProvider.getState("outputKey");
 | 
					    nullStateProvider.setState(State.CacheMatchedKey, cacheMatchedKey);
 | 
				
			||||||
    nullStateProvider.getCacheState();
 | 
					    const output1 = nullStateProvider.getState(State.CachePrimaryKey);
 | 
				
			||||||
 | 
					    const output2 = nullStateProvider.getCacheState();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    expect(getStateMock).toHaveBeenCalledTimes(0);
 | 
					    expect(getStateMock).toHaveBeenCalledTimes(0);
 | 
				
			||||||
    expect(getInputMock).toHaveBeenCalledTimes(2);
 | 
					    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);
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										7
									
								
								dist/restore-only/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										7
									
								
								dist/restore-only/index.js
									
									
									
									
										vendored
									
									
								
							@ -9413,9 +9413,14 @@ class NullStateProvider extends StateProviderBase {
 | 
				
			|||||||
            [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
 | 
					            [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
        this.setState = (key, value) => {
 | 
					        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) => {
 | 
					        this.getState = (key) => {
 | 
				
			||||||
 | 
					            if (!this.stateToInputMap.has(key)) {
 | 
				
			||||||
 | 
					                return "";
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            return core.getInput(this.stateToInputMap.get(key));
 | 
					            return core.getInput(this.stateToInputMap.get(key));
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										7
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										7
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							@ -9413,9 +9413,14 @@ class NullStateProvider extends StateProviderBase {
 | 
				
			|||||||
            [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
 | 
					            [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
        this.setState = (key, value) => {
 | 
					        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) => {
 | 
					        this.getState = (key) => {
 | 
				
			||||||
 | 
					            if (!this.stateToInputMap.has(key)) {
 | 
				
			||||||
 | 
					                return "";
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            return core.getInput(this.stateToInputMap.get(key));
 | 
					            return core.getInput(this.stateToInputMap.get(key));
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										7
									
								
								dist/save-only/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										7
									
								
								dist/save-only/index.js
									
									
									
									
										vendored
									
									
								
							@ -9469,9 +9469,14 @@ class NullStateProvider extends StateProviderBase {
 | 
				
			|||||||
            [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
 | 
					            [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
        this.setState = (key, value) => {
 | 
					        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) => {
 | 
					        this.getState = (key) => {
 | 
				
			||||||
 | 
					            if (!this.stateToInputMap.has(key)) {
 | 
				
			||||||
 | 
					                return "";
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            return core.getInput(this.stateToInputMap.get(key));
 | 
					            return core.getInput(this.stateToInputMap.get(key));
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										7
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										7
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							@ -9413,9 +9413,14 @@ class NullStateProvider extends StateProviderBase {
 | 
				
			|||||||
            [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
 | 
					            [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
        this.setState = (key, value) => {
 | 
					        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) => {
 | 
					        this.getState = (key) => {
 | 
				
			||||||
 | 
					            if (!this.stateToInputMap.has(key)) {
 | 
				
			||||||
 | 
					                return "";
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            return core.getInput(this.stateToInputMap.get(key));
 | 
					            return core.getInput(this.stateToInputMap.get(key));
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -43,10 +43,15 @@ export class NullStateProvider extends StateProviderBase {
 | 
				
			|||||||
    ]);
 | 
					    ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    setState = (key: string, value: string) => {
 | 
					    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) => {
 | 
					    getState = (key: string) => {
 | 
				
			||||||
 | 
					        if (!this.stateToInputMap.has(key)) {
 | 
				
			||||||
 | 
					            return "";
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        return core.getInput(this.stateToInputMap.get(key) as string);
 | 
					        return core.getInput(this.stateToInputMap.get(key) as string);
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user