mirror of
				https://github.com/actions/cache.git
				synced 2025-11-04 07:38:37 +08:00 
			
		
		
		
	feat: force-update
This commit is contained in:
		
							parent
							
								
									c7c46bcb6d
								
							
						
					
					
						commit
						17151cf67e
					
				| 
						 | 
					@ -142,6 +142,38 @@ test("save with exact match returns early", async () => {
 | 
				
			||||||
    expect(failedMock).toHaveBeenCalledTimes(0);
 | 
					    expect(failedMock).toHaveBeenCalledTimes(0);
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("save with force-update", async () => {
 | 
				
			||||||
 | 
					    const failedMock = jest.spyOn(core, "setFailed");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
 | 
				
			||||||
 | 
					    const savedCacheKey = primaryKey;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    jest.spyOn(core, "getState")
 | 
				
			||||||
 | 
					        // Cache Entry State
 | 
				
			||||||
 | 
					        .mockImplementationOnce(() => {
 | 
				
			||||||
 | 
					            return savedCacheKey;
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					        // Cache Key State
 | 
				
			||||||
 | 
					        .mockImplementationOnce(() => {
 | 
				
			||||||
 | 
					            return primaryKey;
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    testUtils.setInput(Inputs.ForceUpdate, "true");
 | 
				
			||||||
 | 
					    testUtils.setInput(Inputs.Path, "node_modules");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const cacheId = 4;
 | 
				
			||||||
 | 
					    const saveCacheMock = jest
 | 
				
			||||||
 | 
					        .spyOn(cache, "saveCache")
 | 
				
			||||||
 | 
					        .mockImplementationOnce(() => {
 | 
				
			||||||
 | 
					            return Promise.resolve(cacheId);
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await run();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    expect(saveCacheMock).toHaveBeenCalledTimes(1);
 | 
				
			||||||
 | 
					    expect(failedMock).toHaveBeenCalledTimes(0);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test("save with missing input outputs warning", async () => {
 | 
					test("save with missing input outputs warning", async () => {
 | 
				
			||||||
    const logWarningMock = jest.spyOn(actionUtils, "logWarning");
 | 
					    const logWarningMock = jest.spyOn(actionUtils, "logWarning");
 | 
				
			||||||
    const failedMock = jest.spyOn(core, "setFailed");
 | 
					    const failedMock = jest.spyOn(core, "setFailed");
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
export enum Inputs {
 | 
					export enum Inputs {
 | 
				
			||||||
 | 
					    ForceUpdate = "force-update",
 | 
				
			||||||
    Key = "key",
 | 
					    Key = "key",
 | 
				
			||||||
    Path = "path",
 | 
					    Path = "path",
 | 
				
			||||||
    RestoreKeys = "restore-keys",
 | 
					    RestoreKeys = "restore-keys",
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -27,8 +27,6 @@ async function run(): Promise<void> {
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const state = utils.getCacheState();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Inputs are re-evaluted before the post action, so we want the original key used for restore
 | 
					        // Inputs are re-evaluted before the post action, so we want the original key used for restore
 | 
				
			||||||
        const primaryKey = core.getState(State.CachePrimaryKey);
 | 
					        const primaryKey = core.getState(State.CachePrimaryKey);
 | 
				
			||||||
        if (!primaryKey) {
 | 
					        if (!primaryKey) {
 | 
				
			||||||
| 
						 | 
					@ -36,7 +34,9 @@ async function run(): Promise<void> {
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (utils.isExactKeyMatch(primaryKey, state)) {
 | 
					        const forceUpdate = core.getInput(Inputs.ForceUpdate) === "true";
 | 
				
			||||||
 | 
					        const state = utils.getCacheState();
 | 
				
			||||||
 | 
					        if (utils.isExactKeyMatch(primaryKey, state) && !forceUpdate) {
 | 
				
			||||||
            core.info(
 | 
					            core.info(
 | 
				
			||||||
                `Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
 | 
					                `Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
 | 
				
			||||||
            );
 | 
					            );
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,6 +23,7 @@ export function setInputs(input: CacheInput): void {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function clearInputs(): void {
 | 
					export function clearInputs(): void {
 | 
				
			||||||
 | 
					    delete process.env[getInputName(Inputs.ForceUpdate)];
 | 
				
			||||||
    delete process.env[getInputName(Inputs.Path)];
 | 
					    delete process.env[getInputName(Inputs.Path)];
 | 
				
			||||||
    delete process.env[getInputName(Inputs.Key)];
 | 
					    delete process.env[getInputName(Inputs.Key)];
 | 
				
			||||||
    delete process.env[getInputName(Inputs.RestoreKeys)];
 | 
					    delete process.env[getInputName(Inputs.RestoreKeys)];
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user