mirror of
				https://github.com/actions/cache.git
				synced 2025-10-31 20:08:36 +08:00 
			
		
		
		
	Add dry-run option
This commit is contained in:
		
							parent
							
								
									eda4a77b7c
								
							
						
					
					
						commit
						1bb6d2503c
					
				|  | @ -74,7 +74,15 @@ test("restore with no cache found", async () => { | |||
|     await run(); | ||||
| 
 | ||||
|     expect(restoreCacheMock).toHaveBeenCalledTimes(1); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith( | ||||
|         [path], | ||||
|         key, | ||||
|         [], | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|     expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key); | ||||
|     expect(stateMock).toHaveBeenCalledTimes(1); | ||||
|  | @ -113,7 +121,9 @@ test("restore with restore keys and no cache found", async () => { | |||
|         [path], | ||||
|         key, | ||||
|         [restoreKey], | ||||
|         {}, | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|  | @ -149,7 +159,15 @@ test("restore with cache found for key", async () => { | |||
|     await run(); | ||||
| 
 | ||||
|     expect(restoreCacheMock).toHaveBeenCalledTimes(1); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith( | ||||
|         [path], | ||||
|         key, | ||||
|         [], | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|     expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key); | ||||
|     expect(stateMock).toHaveBeenCalledWith("CACHE_RESULT", key); | ||||
|  | @ -190,7 +208,9 @@ test("restore with cache found for restore key", async () => { | |||
|         [path], | ||||
|         key, | ||||
|         [restoreKey], | ||||
|         {}, | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|  | @ -205,3 +225,46 @@ test("restore with cache found for restore key", async () => { | |||
|     ); | ||||
|     expect(failedMock).toHaveBeenCalledTimes(0); | ||||
| }); | ||||
| 
 | ||||
| test("restore with dry-run set", async () => { | ||||
|     const path = "node_modules"; | ||||
|     const key = "node-test"; | ||||
|     testUtils.setInputs({ | ||||
|         path: path, | ||||
|         key, | ||||
|         dryRun: "true" | ||||
|     }); | ||||
| 
 | ||||
|     const infoMock = jest.spyOn(core, "info"); | ||||
|     const failedMock = jest.spyOn(core, "setFailed"); | ||||
|     const stateMock = jest.spyOn(core, "saveState"); | ||||
|     const setCacheHitOutputMock = jest.spyOn(core, "setOutput"); | ||||
|     const restoreCacheMock = jest | ||||
|         .spyOn(cache, "restoreCache") | ||||
|         .mockImplementationOnce(() => { | ||||
|             return Promise.resolve(key); | ||||
|         }); | ||||
| 
 | ||||
|     await run(); | ||||
| 
 | ||||
|     expect(restoreCacheMock).toHaveBeenCalledTimes(1); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith( | ||||
|         [path], | ||||
|         key, | ||||
|         [], | ||||
|         { | ||||
|             dryRun: true | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|     expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key); | ||||
|     expect(stateMock).toHaveBeenCalledWith("CACHE_RESULT", key); | ||||
|     expect(stateMock).toHaveBeenCalledTimes(2); | ||||
| 
 | ||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1); | ||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "true"); | ||||
| 
 | ||||
|     expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`); | ||||
|     expect(failedMock).toHaveBeenCalledTimes(0); | ||||
| }); | ||||
|  |  | |||
|  | @ -122,7 +122,15 @@ test("restore on GHES with AC available ", async () => { | |||
|     await run(new StateProvider()); | ||||
| 
 | ||||
|     expect(restoreCacheMock).toHaveBeenCalledTimes(1); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith( | ||||
|         [path], | ||||
|         key, | ||||
|         [], | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|     expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key); | ||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1); | ||||
|  | @ -172,7 +180,9 @@ test("restore with too many keys should fail", async () => { | |||
|         [path], | ||||
|         key, | ||||
|         restoreKeys, | ||||
|         {}, | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
|     expect(failedMock).toHaveBeenCalledWith( | ||||
|  | @ -192,7 +202,15 @@ test("restore with large key should fail", async () => { | |||
|     const restoreCacheMock = jest.spyOn(cache, "restoreCache"); | ||||
|     await run(new StateProvider()); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledTimes(1); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith( | ||||
|         [path], | ||||
|         key, | ||||
|         [], | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
|     expect(failedMock).toHaveBeenCalledWith( | ||||
|         `Key Validation Error: ${key} cannot be larger than 512 characters.` | ||||
|     ); | ||||
|  | @ -210,7 +228,15 @@ test("restore with invalid key should fail", async () => { | |||
|     const restoreCacheMock = jest.spyOn(cache, "restoreCache"); | ||||
|     await run(new StateProvider()); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledTimes(1); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith( | ||||
|         [path], | ||||
|         key, | ||||
|         [], | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
|     expect(failedMock).toHaveBeenCalledWith( | ||||
|         `Key Validation Error: ${key} cannot contain commas.` | ||||
|     ); | ||||
|  | @ -237,7 +263,15 @@ test("restore with no cache found", async () => { | |||
|     await run(new StateProvider()); | ||||
| 
 | ||||
|     expect(restoreCacheMock).toHaveBeenCalledTimes(1); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith( | ||||
|         [path], | ||||
|         key, | ||||
|         [], | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|     expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key); | ||||
|     expect(failedMock).toHaveBeenCalledTimes(0); | ||||
|  | @ -274,7 +308,9 @@ test("restore with restore keys and no cache found", async () => { | |||
|         [path], | ||||
|         key, | ||||
|         [restoreKey], | ||||
|         {}, | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|  | @ -308,7 +344,15 @@ test("restore with cache found for key", async () => { | |||
|     await run(new StateProvider()); | ||||
| 
 | ||||
|     expect(restoreCacheMock).toHaveBeenCalledTimes(1); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith( | ||||
|         [path], | ||||
|         key, | ||||
|         [], | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|     expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key); | ||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1); | ||||
|  | @ -346,7 +390,9 @@ test("restore with cache found for restore key", async () => { | |||
|         [path], | ||||
|         key, | ||||
|         [restoreKey], | ||||
|         {}, | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|  |  | |||
|  | @ -75,7 +75,15 @@ test("restore with no cache found", async () => { | |||
|     await run(); | ||||
| 
 | ||||
|     expect(restoreCacheMock).toHaveBeenCalledTimes(1); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith( | ||||
|         [path], | ||||
|         key, | ||||
|         [], | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|     expect(outputMock).toHaveBeenCalledWith("cache-primary-key", key); | ||||
|     expect(outputMock).toHaveBeenCalledTimes(1); | ||||
|  | @ -113,7 +121,9 @@ test("restore with restore keys and no cache found", async () => { | |||
|         [path], | ||||
|         key, | ||||
|         [restoreKey], | ||||
|         {}, | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|  | @ -146,7 +156,15 @@ test("restore with cache found for key", async () => { | |||
|     await run(); | ||||
| 
 | ||||
|     expect(restoreCacheMock).toHaveBeenCalledTimes(1); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false); | ||||
|     expect(restoreCacheMock).toHaveBeenCalledWith( | ||||
|         [path], | ||||
|         key, | ||||
|         [], | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|     expect(outputMock).toHaveBeenCalledWith("cache-primary-key", key); | ||||
|     expect(outputMock).toHaveBeenCalledWith("cache-hit", "true"); | ||||
|  | @ -185,7 +203,9 @@ test("restore with cache found for restore key", async () => { | |||
|         [path], | ||||
|         key, | ||||
|         [restoreKey], | ||||
|         {}, | ||||
|         { | ||||
|             dryRun: false | ||||
|         }, | ||||
|         false | ||||
|     ); | ||||
| 
 | ||||
|  |  | |||
|  | @ -11,6 +11,10 @@ inputs: | |||
|   restore-keys: | ||||
|     description: 'An ordered list of keys to use for restoring stale cache if no cache hit occurred for key. Note `cache-hit` returns false in this case.' | ||||
|     required: false | ||||
|   dry-run: | ||||
|     description: 'Skip downloading cache. Only check if cache entry exists' | ||||
|     required: false | ||||
|     default: "false" | ||||
|   upload-chunk-size: | ||||
|     description: 'The chunk size used to split up large files during upload, in bytes' | ||||
|     required: false | ||||
|  |  | |||
							
								
								
									
										5
									
								
								dist/restore-only/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								dist/restore-only/index.js
									
									
									
									
										vendored
									
									
								
							|  | @ -4978,7 +4978,8 @@ var Inputs; | |||
|     Inputs["Path"] = "path"; | ||||
|     Inputs["RestoreKeys"] = "restore-keys"; | ||||
|     Inputs["UploadChunkSize"] = "upload-chunk-size"; | ||||
|     Inputs["EnableCrossOsArchive"] = "enableCrossOsArchive"; // Input for cache, restore, save action
 | ||||
|     Inputs["EnableCrossOsArchive"] = "enableCrossOsArchive"; | ||||
|     Inputs["DryRun"] = "dry-run"; // Input for cache, restore action
 | ||||
| })(Inputs = exports.Inputs || (exports.Inputs = {})); | ||||
| var Outputs; | ||||
| (function (Outputs) { | ||||
|  | @ -50504,7 +50505,7 @@ function restoreImpl(stateProvider) { | |||
|                 required: true | ||||
|             }); | ||||
|             const enableCrossOsArchive = utils.getInputAsBool(constants_1.Inputs.EnableCrossOsArchive); | ||||
|             const cacheKey = yield cache.restoreCache(cachePaths, primaryKey, restoreKeys, {}, enableCrossOsArchive); | ||||
|             const cacheKey = yield cache.restoreCache(cachePaths, primaryKey, restoreKeys, { dryRun: core.getBooleanInput(constants_1.Inputs.DryRun) }, enableCrossOsArchive); | ||||
|             if (!cacheKey) { | ||||
|                 core.info(`Cache not found for input keys: ${[ | ||||
|                     primaryKey, | ||||
|  |  | |||
							
								
								
									
										5
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							|  | @ -4978,7 +4978,8 @@ var Inputs; | |||
|     Inputs["Path"] = "path"; | ||||
|     Inputs["RestoreKeys"] = "restore-keys"; | ||||
|     Inputs["UploadChunkSize"] = "upload-chunk-size"; | ||||
|     Inputs["EnableCrossOsArchive"] = "enableCrossOsArchive"; // Input for cache, restore, save action
 | ||||
|     Inputs["EnableCrossOsArchive"] = "enableCrossOsArchive"; | ||||
|     Inputs["DryRun"] = "dry-run"; // Input for cache, restore action
 | ||||
| })(Inputs = exports.Inputs || (exports.Inputs = {})); | ||||
| var Outputs; | ||||
| (function (Outputs) { | ||||
|  | @ -50504,7 +50505,7 @@ function restoreImpl(stateProvider) { | |||
|                 required: true | ||||
|             }); | ||||
|             const enableCrossOsArchive = utils.getInputAsBool(constants_1.Inputs.EnableCrossOsArchive); | ||||
|             const cacheKey = yield cache.restoreCache(cachePaths, primaryKey, restoreKeys, {}, enableCrossOsArchive); | ||||
|             const cacheKey = yield cache.restoreCache(cachePaths, primaryKey, restoreKeys, { dryRun: core.getBooleanInput(constants_1.Inputs.DryRun) }, enableCrossOsArchive); | ||||
|             if (!cacheKey) { | ||||
|                 core.info(`Cache not found for input keys: ${[ | ||||
|                     primaryKey, | ||||
|  |  | |||
							
								
								
									
										3
									
								
								dist/save-only/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								dist/save-only/index.js
									
									
									
									
										vendored
									
									
								
							|  | @ -5034,7 +5034,8 @@ var Inputs; | |||
|     Inputs["Path"] = "path"; | ||||
|     Inputs["RestoreKeys"] = "restore-keys"; | ||||
|     Inputs["UploadChunkSize"] = "upload-chunk-size"; | ||||
|     Inputs["EnableCrossOsArchive"] = "enableCrossOsArchive"; // Input for cache, restore, save action
 | ||||
|     Inputs["EnableCrossOsArchive"] = "enableCrossOsArchive"; | ||||
|     Inputs["DryRun"] = "dry-run"; // Input for cache, restore action
 | ||||
| })(Inputs = exports.Inputs || (exports.Inputs = {})); | ||||
| var Outputs; | ||||
| (function (Outputs) { | ||||
|  |  | |||
							
								
								
									
										3
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							|  | @ -4978,7 +4978,8 @@ var Inputs; | |||
|     Inputs["Path"] = "path"; | ||||
|     Inputs["RestoreKeys"] = "restore-keys"; | ||||
|     Inputs["UploadChunkSize"] = "upload-chunk-size"; | ||||
|     Inputs["EnableCrossOsArchive"] = "enableCrossOsArchive"; // Input for cache, restore, save action
 | ||||
|     Inputs["EnableCrossOsArchive"] = "enableCrossOsArchive"; | ||||
|     Inputs["DryRun"] = "dry-run"; // Input for cache, restore action
 | ||||
| })(Inputs = exports.Inputs || (exports.Inputs = {})); | ||||
| var Outputs; | ||||
| (function (Outputs) { | ||||
|  |  | |||
|  | @ -7,6 +7,7 @@ The restore action, as the name suggest, restores a cache. It acts similar to th | |||
| * `path` - A list of files, directories, and wildcard patterns to cache and restore. See [`@actions/glob`](https://github.com/actions/toolkit/tree/main/packages/glob) for supported patterns. | ||||
| * `key` - String used while saving cache for restoring the cache | ||||
| * `restore-keys` - An ordered list of prefix-matched keys to use for restoring stale cache if no cache hit occurred for key. | ||||
| * `dry-run` - Skip downloading cache. Only check if cache entry exists | ||||
| 
 | ||||
| ## Outputs | ||||
| 
 | ||||
|  |  | |||
|  | @ -15,6 +15,10 @@ inputs: | |||
|     description: 'An optional boolean when enabled, allows windows runners to restore caches that were saved on other platforms' | ||||
|     default: 'false' | ||||
|     required: false | ||||
|   dry-run: | ||||
|     description: 'Skip downloading cache. Only check if cache entry exists' | ||||
|     required: false | ||||
|     default: "false" | ||||
| outputs: | ||||
|   cache-hit: | ||||
|     description: 'A boolean value to indicate an exact match was found for the primary key' | ||||
|  |  | |||
|  | @ -3,7 +3,8 @@ export enum Inputs { | |||
|     Path = "path", // Input for cache, restore, save action
 | ||||
|     RestoreKeys = "restore-keys", // Input for cache, restore action
 | ||||
|     UploadChunkSize = "upload-chunk-size", // Input for cache, save action
 | ||||
|     EnableCrossOsArchive = "enableCrossOsArchive" // Input for cache, restore, save action
 | ||||
|     EnableCrossOsArchive = "enableCrossOsArchive", // Input for cache, restore, save action
 | ||||
|     DryRun = "dry-run" // Input for cache, restore action
 | ||||
| } | ||||
| 
 | ||||
| export enum Outputs { | ||||
|  |  | |||
|  | @ -39,7 +39,7 @@ async function restoreImpl( | |||
|             cachePaths, | ||||
|             primaryKey, | ||||
|             restoreKeys, | ||||
|             {}, | ||||
|             { dryRun: core.getBooleanInput(Inputs.DryRun) }, | ||||
|             enableCrossOsArchive | ||||
|         ); | ||||
| 
 | ||||
|  |  | |||
|  | @ -14,11 +14,13 @@ interface CacheInput { | |||
|     key: string; | ||||
|     restoreKeys?: string[]; | ||||
|     enableCrossOsArchive?: boolean; | ||||
|     dryRun?: string; | ||||
| } | ||||
| 
 | ||||
| export function setInputs(input: CacheInput): void { | ||||
|     setInput(Inputs.Path, input.path); | ||||
|     setInput(Inputs.Key, input.key); | ||||
|     setInput(Inputs.DryRun, "false"); | ||||
|     input.restoreKeys && | ||||
|         setInput(Inputs.RestoreKeys, input.restoreKeys.join("\n")); | ||||
|     input.enableCrossOsArchive !== undefined && | ||||
|  | @ -26,12 +28,14 @@ export function setInputs(input: CacheInput): void { | |||
|             Inputs.EnableCrossOsArchive, | ||||
|             input.enableCrossOsArchive.toString() | ||||
|         ); | ||||
|     input.dryRun && setInput(Inputs.DryRun, input.dryRun); | ||||
| } | ||||
| 
 | ||||
| export function clearInputs(): void { | ||||
|     delete process.env[getInputName(Inputs.Path)]; | ||||
|     delete process.env[getInputName(Inputs.Key)]; | ||||
|     delete process.env[getInputName(Inputs.RestoreKeys)]; | ||||
|     delete process.env[getInputName(Inputs.DryRun)]; | ||||
|     delete process.env[getInputName(Inputs.UploadChunkSize)]; | ||||
|     delete process.env[getInputName(Inputs.EnableCrossOsArchive)]; | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Marc Mueller
						Marc Mueller