stateinputprovider with pending test cases fix
This commit is contained in:
		
							parent
							
								
									365406cb70
								
							
						
					
					
						commit
						5b7eeecaeb
					
				| @ -11,12 +11,8 @@ jest.mock("@actions/cache"); | |||||||
| jest.mock("../src/utils/actionUtils"); | jest.mock("../src/utils/actionUtils"); | ||||||
| 
 | 
 | ||||||
| beforeAll(() => { | beforeAll(() => { | ||||||
|     jest.spyOn(core, "getInput").mockImplementation((name, options) => { |     jest.spyOn(core, "getInput").mockImplementation(name => { | ||||||
|         return jest.requireActual("@actions/core").getInput(name, options); |         return testUtils.getInput(name); | ||||||
|     }); |  | ||||||
| 
 |  | ||||||
|     jest.spyOn(core, "setOutput").mockImplementation((key, value) => { |  | ||||||
|         return jest.requireActual("@actions/core").getInput(key, value); |  | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     jest.spyOn(actionUtils, "getInputAsArray").mockImplementation( |     jest.spyOn(actionUtils, "getInputAsArray").mockImplementation( | ||||||
|  | |||||||
| @ -58,11 +58,17 @@ 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 => states.get(key) || ""); | ||||||
|  | 
 | ||||||
|     const getStateMock = jest |     const getStateMock = jest | ||||||
|         .spyOn(core, "getState") |         .spyOn(core, "getState") | ||||||
|         .mockImplementation(name => |         .mockImplementation(key => { | ||||||
|             jest.requireActual("@actions/core").getState(name) |             return jest.requireActual("@actions/core").getState(key); | ||||||
|         ); |         }); | ||||||
| 
 | 
 | ||||||
|     const setOutputMock = jest |     const setOutputMock = jest | ||||||
|         .spyOn(core, "setOutput") |         .spyOn(core, "setOutput") | ||||||
| @ -73,7 +79,7 @@ test("NullStateProvider saves outputs", async () => { | |||||||
|     const saveStateMock = jest |     const saveStateMock = jest | ||||||
|         .spyOn(core, "saveState") |         .spyOn(core, "saveState") | ||||||
|         .mockImplementation((key, value) => { |         .mockImplementation((key, value) => { | ||||||
|             return jest.requireActual("@actions/core").saveState(key, value); |             states.set(key, value); | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|     const cacheMatchedKey = "node-cache"; |     const cacheMatchedKey = "node-cache"; | ||||||
| @ -84,6 +90,7 @@ test("NullStateProvider saves outputs", async () => { | |||||||
|     nullStateProvider.getCacheState(); |     nullStateProvider.getCacheState(); | ||||||
| 
 | 
 | ||||||
|     expect(getStateMock).toHaveBeenCalledTimes(0); |     expect(getStateMock).toHaveBeenCalledTimes(0); | ||||||
|  |     expect(getInputMock).toHaveBeenCalledTimes(2); | ||||||
|     expect(setOutputMock).toHaveBeenCalledTimes(2); |     expect(setOutputMock).toHaveBeenCalledTimes(2); | ||||||
|     expect(saveStateMock).toHaveBeenCalledTimes(0); |     expect(saveStateMock).toHaveBeenCalledTimes(0); | ||||||
| }); | }); | ||||||
|  | |||||||
							
								
								
									
										8
									
								
								dist/restore-only/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										8
									
								
								dist/restore-only/index.js
									
									
									
									
										vendored
									
									
								
							| @ -9405,6 +9405,9 @@ 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] | ||||||
| @ -9412,8 +9415,9 @@ class NullStateProvider extends StateProviderBase { | |||||||
|         this.setState = (key, value) => { |         this.setState = (key, value) => { | ||||||
|             core.setOutput(this.stateToOutputMap.get(key), value); |             core.setOutput(this.stateToOutputMap.get(key), value); | ||||||
|         }; |         }; | ||||||
|         // eslint-disable-next-line @typescript-eslint/no-unused-vars
 |         this.getState = (key) => { | ||||||
|         this.getState = (key) => ""; |             return core.getInput(this.stateToInputMap.get(key)); | ||||||
|  |         }; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| exports.NullStateProvider = NullStateProvider; | exports.NullStateProvider = NullStateProvider; | ||||||
|  | |||||||
							
								
								
									
										8
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										8
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							| @ -9405,6 +9405,9 @@ 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] | ||||||
| @ -9412,8 +9415,9 @@ class NullStateProvider extends StateProviderBase { | |||||||
|         this.setState = (key, value) => { |         this.setState = (key, value) => { | ||||||
|             core.setOutput(this.stateToOutputMap.get(key), value); |             core.setOutput(this.stateToOutputMap.get(key), value); | ||||||
|         }; |         }; | ||||||
|         // eslint-disable-next-line @typescript-eslint/no-unused-vars
 |         this.getState = (key) => { | ||||||
|         this.getState = (key) => ""; |             return core.getInput(this.stateToInputMap.get(key)); | ||||||
|  |         }; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| exports.NullStateProvider = NullStateProvider; | exports.NullStateProvider = NullStateProvider; | ||||||
|  | |||||||
							
								
								
									
										11
									
								
								dist/save-only/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										11
									
								
								dist/save-only/index.js
									
									
									
									
										vendored
									
									
								
							| @ -9461,6 +9461,9 @@ 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] | ||||||
| @ -9468,8 +9471,9 @@ class NullStateProvider extends StateProviderBase { | |||||||
|         this.setState = (key, value) => { |         this.setState = (key, value) => { | ||||||
|             core.setOutput(this.stateToOutputMap.get(key), value); |             core.setOutput(this.stateToOutputMap.get(key), value); | ||||||
|         }; |         }; | ||||||
|         // eslint-disable-next-line @typescript-eslint/no-unused-vars
 |         this.getState = (key) => { | ||||||
|         this.getState = (key) => ""; |             return core.getInput(this.stateToInputMap.get(key)); | ||||||
|  |         }; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| exports.NullStateProvider = NullStateProvider; | exports.NullStateProvider = NullStateProvider; | ||||||
| @ -41059,8 +41063,7 @@ 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; | ||||||
|  | |||||||
							
								
								
									
										11
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										11
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							| @ -9405,6 +9405,9 @@ 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] | ||||||
| @ -9412,8 +9415,9 @@ class NullStateProvider extends StateProviderBase { | |||||||
|         this.setState = (key, value) => { |         this.setState = (key, value) => { | ||||||
|             core.setOutput(this.stateToOutputMap.get(key), value); |             core.setOutput(this.stateToOutputMap.get(key), value); | ||||||
|         }; |         }; | ||||||
|         // eslint-disable-next-line @typescript-eslint/no-unused-vars
 |         this.getState = (key) => { | ||||||
|         this.getState = (key) => ""; |             return core.getInput(this.stateToInputMap.get(key)); | ||||||
|  |         }; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| exports.NullStateProvider = NullStateProvider; | exports.NullStateProvider = NullStateProvider; | ||||||
| @ -41003,8 +41007,7 @@ 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; | ||||||
|  | |||||||
| @ -28,9 +28,7 @@ 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 = |         const primaryKey = stateProvider.getState(State.CachePrimaryKey); | ||||||
|             stateProvider.getState(State.CachePrimaryKey) || |  | ||||||
|             core.getInput(Inputs.Key); |  | ||||||
| 
 | 
 | ||||||
|         if (!primaryKey) { |         if (!primaryKey) { | ||||||
|             utils.logWarning(`Key is not specified.`); |             utils.logWarning(`Key is not specified.`); | ||||||
|  | |||||||
| @ -1,6 +1,6 @@ | |||||||
| import * as core from "@actions/core"; | import * as core from "@actions/core"; | ||||||
| 
 | 
 | ||||||
| import { Outputs, State } from "./constants"; | import { Inputs, Outputs, State } from "./constants"; | ||||||
| 
 | 
 | ||||||
| export interface IStateProvider { | export interface IStateProvider { | ||||||
|     setState(key: string, value: string): void; |     setState(key: string, value: string): void; | ||||||
| @ -33,6 +33,10 @@ 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] | ||||||
| @ -41,6 +45,8 @@ export class NullStateProvider extends StateProviderBase { | |||||||
|     setState = (key: string, value: string) => { |     setState = (key: string, value: string) => { | ||||||
|         core.setOutput(this.stateToOutputMap.get(key) as string, value); |         core.setOutput(this.stateToOutputMap.get(key) as string, value); | ||||||
|     }; |     }; | ||||||
|     // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
 | ||||||
|     getState = (key: string) => ""; |     getState = (key: string) => { | ||||||
|  |         return core.getInput(this.stateToInputMap.get(key) as string); | ||||||
|  |     }; | ||||||
| } | } | ||||||
|  | |||||||
| @ -9,6 +9,10 @@ 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; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user