| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | import * as core from "@actions/core"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-17 15:46:46 -04:00
										 |  |  | import { Events, Outputs, RefKey, State } from "../src/constants"; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | import * as actionUtils from "../src/utils/actionUtils"; | 
					
						
							| 
									
										
										
										
											2020-06-02 10:21:03 -05:00
										 |  |  | import * as testUtils from "../src/utils/testUtils"; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | jest.mock("@actions/core"); | 
					
						
							| 
									
										
										
										
											2020-03-20 13:02:11 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-02 10:21:03 -05:00
										 |  |  | beforeAll(() => { | 
					
						
							|  |  |  |     jest.spyOn(core, "getInput").mockImplementation((name, options) => { | 
					
						
							|  |  |  |         return jest.requireActual("@actions/core").getInput(name, options); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | afterEach(() => { | 
					
						
							|  |  |  |     delete process.env[Events.Key]; | 
					
						
							| 
									
										
										
										
											2020-04-17 15:46:46 -04:00
										 |  |  |     delete process.env[RefKey]; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  | test("isExactKeyMatch with undefined cache key returns false", () => { | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  |     const key = "linux-rust"; | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     const cacheKey = undefined; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  | test("isExactKeyMatch with empty cache key returns false", () => { | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  |     const key = "linux-rust"; | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     const cacheKey = ""; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("isExactKeyMatch with different keys returns false", () => { | 
					
						
							|  |  |  |     const key = "linux-rust"; | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     const cacheKey = "linux-"; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("isExactKeyMatch with different key accents returns false", () => { | 
					
						
							|  |  |  |     const key = "linux-Ɣccent"; | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     const cacheKey = "linux-accent"; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("isExactKeyMatch with same key returns true", () => { | 
					
						
							|  |  |  |     const key = "linux-rust"; | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     const cacheKey = "linux-rust"; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(true); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("isExactKeyMatch with same key and different casing returns true", () => { | 
					
						
							|  |  |  |     const key = "linux-rust"; | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     const cacheKey = "LINUX-RUST"; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(true); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("setOutputAndState with undefined entry to set cache-hit output", () => { | 
					
						
							|  |  |  |     const key = "linux-rust"; | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     const cacheKey = undefined; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const setOutputMock = jest.spyOn(core, "setOutput"); | 
					
						
							|  |  |  |     const saveStateMock = jest.spyOn(core, "saveState"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     actionUtils.setOutputAndState(key, cacheKey); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "false"); | 
					
						
							|  |  |  |     expect(setOutputMock).toHaveBeenCalledTimes(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(saveStateMock).toHaveBeenCalledTimes(0); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("setOutputAndState with exact match to set cache-hit output and state", () => { | 
					
						
							|  |  |  |     const key = "linux-rust"; | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     const cacheKey = "linux-rust"; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const setOutputMock = jest.spyOn(core, "setOutput"); | 
					
						
							|  |  |  |     const saveStateMock = jest.spyOn(core, "saveState"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     actionUtils.setOutputAndState(key, cacheKey); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "true"); | 
					
						
							|  |  |  |     expect(setOutputMock).toHaveBeenCalledTimes(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-19 13:46:58 -04:00
										 |  |  |     expect(saveStateMock).toHaveBeenCalledWith(State.CacheMatchedKey, cacheKey); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  |     expect(saveStateMock).toHaveBeenCalledTimes(1); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("setOutputAndState with no exact match to set cache-hit output and state", () => { | 
					
						
							|  |  |  |     const key = "linux-rust"; | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     const cacheKey = "linux-rust-bb828da54c148048dd17899ba9fda624811cfb43"; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const setOutputMock = jest.spyOn(core, "setOutput"); | 
					
						
							|  |  |  |     const saveStateMock = jest.spyOn(core, "saveState"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     actionUtils.setOutputAndState(key, cacheKey); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "false"); | 
					
						
							|  |  |  |     expect(setOutputMock).toHaveBeenCalledTimes(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-19 13:46:58 -04:00
										 |  |  |     expect(saveStateMock).toHaveBeenCalledWith(State.CacheMatchedKey, cacheKey); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  |     expect(saveStateMock).toHaveBeenCalledTimes(1); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("getCacheState with no state returns undefined", () => { | 
					
						
							|  |  |  |     const getStateMock = jest.spyOn(core, "getState"); | 
					
						
							|  |  |  |     getStateMock.mockImplementation(() => { | 
					
						
							|  |  |  |         return ""; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const state = actionUtils.getCacheState(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(state).toBe(undefined); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-19 13:46:58 -04:00
										 |  |  |     expect(getStateMock).toHaveBeenCalledWith(State.CacheMatchedKey); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  |     expect(getStateMock).toHaveBeenCalledTimes(1); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("getCacheState with valid state", () => { | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     const cacheKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  |     const getStateMock = jest.spyOn(core, "getState"); | 
					
						
							|  |  |  |     getStateMock.mockImplementation(() => { | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |         return cacheKey; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const state = actionUtils.getCacheState(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 17:27:38 -04:00
										 |  |  |     expect(state).toEqual(cacheKey); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-19 13:46:58 -04:00
										 |  |  |     expect(getStateMock).toHaveBeenCalledWith(State.CacheMatchedKey); | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  |     expect(getStateMock).toHaveBeenCalledTimes(1); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-21 14:37:54 -05:00
										 |  |  | test("logWarning logs a message with a warning prefix", () => { | 
					
						
							|  |  |  |     const message = "A warning occurred."; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const infoMock = jest.spyOn(core, "info"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     actionUtils.logWarning(message); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(infoMock).toHaveBeenCalledWith(`[warning]${message}`); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-17 15:46:46 -04:00
										 |  |  | test("isValidEvent returns false for event that does not have a branch or tag", () => { | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  |     const event = "foo"; | 
					
						
							|  |  |  |     process.env[Events.Key] = event; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const isValidEvent = actionUtils.isValidEvent(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(isValidEvent).toBe(false); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-17 15:46:46 -04:00
										 |  |  | test("isValidEvent returns true for event that has a ref", () => { | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  |     const event = Events.Push; | 
					
						
							|  |  |  |     process.env[Events.Key] = event; | 
					
						
							| 
									
										
										
										
											2020-04-17 15:46:46 -04:00
										 |  |  |     process.env[RefKey] = "ref/heads/feature"; | 
					
						
							| 
									
										
										
										
											2019-11-13 16:13:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const isValidEvent = actionUtils.isValidEvent(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(isValidEvent).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-06-02 10:21:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | test("getInputAsArray returns empty array if not required and missing", () => { | 
					
						
							|  |  |  |     expect(actionUtils.getInputAsArray("foo")).toEqual([]); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("getInputAsArray throws error if required and missing", () => { | 
					
						
							|  |  |  |     expect(() => | 
					
						
							|  |  |  |         actionUtils.getInputAsArray("foo", { required: true }) | 
					
						
							|  |  |  |     ).toThrowError(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("getInputAsArray handles single line correctly", () => { | 
					
						
							|  |  |  |     testUtils.setInput("foo", "bar"); | 
					
						
							|  |  |  |     expect(actionUtils.getInputAsArray("foo")).toEqual(["bar"]); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("getInputAsArray handles multiple lines correctly", () => { | 
					
						
							|  |  |  |     testUtils.setInput("foo", "bar\nbaz"); | 
					
						
							|  |  |  |     expect(actionUtils.getInputAsArray("foo")).toEqual(["bar", "baz"]); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("getInputAsArray handles different new lines correctly", () => { | 
					
						
							|  |  |  |     testUtils.setInput("foo", "bar\r\nbaz"); | 
					
						
							|  |  |  |     expect(actionUtils.getInputAsArray("foo")).toEqual(["bar", "baz"]); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("getInputAsArray handles empty lines correctly", () => { | 
					
						
							|  |  |  |     testUtils.setInput("foo", "\n\nbar\n\nbaz\n\n"); | 
					
						
							|  |  |  |     expect(actionUtils.getInputAsArray("foo")).toEqual(["bar", "baz"]); | 
					
						
							|  |  |  | }); |