From d77303b6780259681f32fd47952d8d7303b45c69 Mon Sep 17 00:00:00 2001 From: Chad Kimes <1936066+chkimes@users.noreply.github.com> Date: Tue, 8 Aug 2023 17:30:54 +0000 Subject: [PATCH] Refactor restore files to have better patterns for testing --- __tests__/restore.test.ts | 16 ++++---- __tests__/restoreImpl.test.ts | 30 +++++++------- __tests__/restoreOnly.test.ts | 10 ++--- dist/restore-only/index.js | 76 +++++++++++++++++------------------ dist/restore/index.js | 76 +++++++++++++++++------------------ src/restore.ts | 27 +------------ src/restoreImpl.ts | 32 +++++++++++++-- src/restoreOnly.ts | 27 +------------ 8 files changed, 137 insertions(+), 157 deletions(-) diff --git a/__tests__/restore.test.ts b/__tests__/restore.test.ts index c51c293..250f7ef 100644 --- a/__tests__/restore.test.ts +++ b/__tests__/restore.test.ts @@ -2,7 +2,7 @@ import * as cache from "@actions/cache"; import * as core from "@actions/core"; import { Events, RefKey } from "../src/constants"; -import run from "../src/restore"; +import { restoreRun } from "../src/restoreImpl"; import * as actionUtils from "../src/utils/actionUtils"; import * as testUtils from "../src/utils/testUtils"; @@ -71,7 +71,7 @@ test("restore with no cache found", async () => { return Promise.resolve(undefined); }); - await run(); + await restoreRun(); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -114,7 +114,7 @@ test("restore with restore keys and no cache found", async () => { return Promise.resolve(undefined); }); - await run(); + await restoreRun(); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -156,7 +156,7 @@ test("restore with cache found for key", async () => { return Promise.resolve(key); }); - await run(); + await restoreRun(); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -201,7 +201,7 @@ test("restore with cache found for restore key", async () => { return Promise.resolve(restoreKey); }); - await run(); + await restoreRun(); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -246,7 +246,7 @@ test("Fail restore when fail on cache miss is enabled and primary + restore keys return Promise.resolve(undefined); }); - await run(); + await restoreRun(); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -289,7 +289,7 @@ test("restore when fail on cache miss is enabled and primary key doesn't match r return Promise.resolve(restoreKey); }); - await run(); + await restoreRun(); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -335,7 +335,7 @@ test("restore with fail on cache miss disabled and no cache found", async () => return Promise.resolve(undefined); }); - await run(); + await restoreRun(); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( diff --git a/__tests__/restoreImpl.test.ts b/__tests__/restoreImpl.test.ts index d6f13ba..8bab894 100644 --- a/__tests__/restoreImpl.test.ts +++ b/__tests__/restoreImpl.test.ts @@ -2,7 +2,7 @@ import * as cache from "@actions/cache"; import * as core from "@actions/core"; import { Events, Inputs, RefKey } from "../src/constants"; -import run from "../src/restoreImpl"; +import { restoreImpl } from "../src/restoreImpl"; import { StateProvider } from "../src/stateProvider"; import * as actionUtils from "../src/utils/actionUtils"; import * as testUtils from "../src/utils/testUtils"; @@ -60,7 +60,7 @@ test("restore with invalid event outputs warning", async () => { const invalidEvent = "commit_comment"; process.env[Events.Key] = invalidEvent; delete process.env[RefKey]; - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(logWarningMock).toHaveBeenCalledWith( `Event Validation Error: The event type ${invalidEvent} is not supported because it's not tied to a branch or tag ref.` ); @@ -76,7 +76,7 @@ test("restore without AC available should no-op", async () => { const restoreCacheMock = jest.spyOn(cache, "restoreCache"); const setCacheHitOutputMock = jest.spyOn(core, "setOutput"); - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(restoreCacheMock).toHaveBeenCalledTimes(0); expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1); @@ -92,7 +92,7 @@ test("restore on GHES without AC available should no-op", async () => { const restoreCacheMock = jest.spyOn(cache, "restoreCache"); const setCacheHitOutputMock = jest.spyOn(core, "setOutput"); - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(restoreCacheMock).toHaveBeenCalledTimes(0); expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1); @@ -119,7 +119,7 @@ test("restore on GHES with AC available ", async () => { return Promise.resolve(key); }); - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -143,7 +143,7 @@ test("restore on GHES with AC available ", async () => { test("restore with no path should fail", async () => { const failedMock = jest.spyOn(core, "setFailed"); const restoreCacheMock = jest.spyOn(cache, "restoreCache"); - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(restoreCacheMock).toHaveBeenCalledTimes(0); // this input isn't necessary for restore b/c tarball contains entries relative to workspace expect(failedMock).not.toHaveBeenCalledWith( @@ -155,7 +155,7 @@ test("restore with no key", async () => { testUtils.setInput(Inputs.Path, "node_modules"); const failedMock = jest.spyOn(core, "setFailed"); const restoreCacheMock = jest.spyOn(cache, "restoreCache"); - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(restoreCacheMock).toHaveBeenCalledTimes(0); expect(failedMock).toHaveBeenCalledWith( "Input required and not supplied: key" @@ -174,7 +174,7 @@ test("restore with too many keys should fail", async () => { }); const failedMock = jest.spyOn(core, "setFailed"); const restoreCacheMock = jest.spyOn(cache, "restoreCache"); - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( [path], @@ -200,7 +200,7 @@ test("restore with large key should fail", async () => { }); const failedMock = jest.spyOn(core, "setFailed"); const restoreCacheMock = jest.spyOn(cache, "restoreCache"); - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( [path], @@ -226,7 +226,7 @@ test("restore with invalid key should fail", async () => { }); const failedMock = jest.spyOn(core, "setFailed"); const restoreCacheMock = jest.spyOn(cache, "restoreCache"); - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( [path], @@ -260,7 +260,7 @@ test("restore with no cache found", async () => { return Promise.resolve(undefined); }); - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -301,7 +301,7 @@ test("restore with restore keys and no cache found", async () => { return Promise.resolve(undefined); }); - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -341,7 +341,7 @@ test("restore with cache found for key", async () => { return Promise.resolve(key); }); - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -383,7 +383,7 @@ test("restore with cache found for restore key", async () => { return Promise.resolve(restoreKey); }); - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -424,7 +424,7 @@ test("restore with lookup-only set", async () => { return Promise.resolve(key); }); - await run(new StateProvider()); + await restoreImpl(new StateProvider()); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( diff --git a/__tests__/restoreOnly.test.ts b/__tests__/restoreOnly.test.ts index 800c2e1..81e5bca 100644 --- a/__tests__/restoreOnly.test.ts +++ b/__tests__/restoreOnly.test.ts @@ -2,7 +2,7 @@ import * as cache from "@actions/cache"; import * as core from "@actions/core"; import { Events, RefKey } from "../src/constants"; -import run from "../src/restoreOnly"; +import { restoreOnlyRun } from "../src/restoreImpl"; import * as actionUtils from "../src/utils/actionUtils"; import * as testUtils from "../src/utils/testUtils"; @@ -72,7 +72,7 @@ test("restore with no cache found", async () => { return Promise.resolve(undefined); }); - await run(); + await restoreOnlyRun(); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -114,7 +114,7 @@ test("restore with restore keys and no cache found", async () => { return Promise.resolve(undefined); }); - await run(); + await restoreOnlyRun(); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -153,7 +153,7 @@ test("restore with cache found for key", async () => { return Promise.resolve(key); }); - await run(); + await restoreOnlyRun(); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( @@ -196,7 +196,7 @@ test("restore with cache found for restore key", async () => { return Promise.resolve(restoreKey); }); - await run(); + await restoreOnlyRun(); expect(restoreCacheMock).toHaveBeenCalledTimes(1); expect(restoreCacheMock).toHaveBeenCalledWith( diff --git a/dist/restore-only/index.js b/dist/restore-only/index.js index 56833dd..c536263 100644 --- a/dist/restore-only/index.js +++ b/dist/restore-only/index.js @@ -37054,44 +37054,9 @@ exports.default = { "use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; Object.defineProperty(exports, "__esModule", { value: true }); -const restoreImpl_1 = __importDefault(__webpack_require__(835)); -const stateProvider_1 = __webpack_require__(309); -function run(earlyExit) { - return __awaiter(this, void 0, void 0, function* () { - try { - yield (0, restoreImpl_1.default)(new stateProvider_1.NullStateProvider()); - } - catch (err) { - console.error(err); - if (earlyExit) { - process.exit(1); - } - } - // node will stay alive if any promises are not resolved, - // which is a possibility if HTTP requests are dangling - // due to retries or timeouts. We know that if we got here - // that all promises that we care about have successfully - // resolved, so simply exit with success. - if (earlyExit) { - process.exit(0); - } - }); -} -run(true); -exports.default = run; +const restoreImpl_1 = __webpack_require__(835); +(0, restoreImpl_1.restoreOnlyRun)(true); /***/ }), @@ -49269,9 +49234,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); +exports.restoreRun = exports.restoreOnlyRun = exports.restoreImpl = void 0; const cache = __importStar(__webpack_require__(692)); const core = __importStar(__webpack_require__(470)); const constants_1 = __webpack_require__(694); +const stateProvider_1 = __webpack_require__(309); const utils = __importStar(__webpack_require__(360)); function restoreImpl(stateProvider) { return __awaiter(this, void 0, void 0, function* () { @@ -49322,7 +49289,40 @@ function restoreImpl(stateProvider) { } }); } -exports.default = restoreImpl; +exports.restoreImpl = restoreImpl; +function run(stateProvider, earlyExit) { + return __awaiter(this, void 0, void 0, function* () { + try { + yield restoreImpl(stateProvider); + } + catch (err) { + console.error(err); + if (earlyExit) { + process.exit(1); + } + } + // node will stay alive if any promises are not resolved, + // which is a possibility if HTTP requests are dangling + // due to retries or timeouts. We know that if we got here + // that all promises that we care about have successfully + // resolved, so simply exit with success. + if (earlyExit) { + process.exit(0); + } + }); +} +function restoreOnlyRun(earlyExit) { + return __awaiter(this, void 0, void 0, function* () { + yield run(new stateProvider_1.NullStateProvider(), earlyExit); + }); +} +exports.restoreOnlyRun = restoreOnlyRun; +function restoreRun(earlyExit) { + return __awaiter(this, void 0, void 0, function* () { + yield run(new stateProvider_1.StateProvider(), earlyExit); + }); +} +exports.restoreRun = restoreRun; /***/ }), diff --git a/dist/restore/index.js b/dist/restore/index.js index 4bef4b4..d88eb2d 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -47610,44 +47610,9 @@ module.exports = function(dst, src) { "use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; Object.defineProperty(exports, "__esModule", { value: true }); -const restoreImpl_1 = __importDefault(__webpack_require__(835)); -const stateProvider_1 = __webpack_require__(309); -function run(earlyExit) { - return __awaiter(this, void 0, void 0, function* () { - try { - yield (0, restoreImpl_1.default)(new stateProvider_1.StateProvider()); - } - catch (err) { - console.error(err); - if (earlyExit) { - process.exit(1); - } - } - // node will stay alive if any promises are not resolved, - // which is a possibility if HTTP requests are dangling - // due to retries or timeouts. We know that if we got here - // that all promises that we care about have successfully - // resolved, so simply exit with success. - if (earlyExit) { - process.exit(0); - } - }); -} -run(true); -exports.default = run; +const restoreImpl_1 = __webpack_require__(835); +(0, restoreImpl_1.restoreRun)(true); /***/ }), @@ -49269,9 +49234,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); +exports.restoreRun = exports.restoreOnlyRun = exports.restoreImpl = void 0; const cache = __importStar(__webpack_require__(692)); const core = __importStar(__webpack_require__(470)); const constants_1 = __webpack_require__(694); +const stateProvider_1 = __webpack_require__(309); const utils = __importStar(__webpack_require__(443)); function restoreImpl(stateProvider) { return __awaiter(this, void 0, void 0, function* () { @@ -49322,7 +49289,40 @@ function restoreImpl(stateProvider) { } }); } -exports.default = restoreImpl; +exports.restoreImpl = restoreImpl; +function run(stateProvider, earlyExit) { + return __awaiter(this, void 0, void 0, function* () { + try { + yield restoreImpl(stateProvider); + } + catch (err) { + console.error(err); + if (earlyExit) { + process.exit(1); + } + } + // node will stay alive if any promises are not resolved, + // which is a possibility if HTTP requests are dangling + // due to retries or timeouts. We know that if we got here + // that all promises that we care about have successfully + // resolved, so simply exit with success. + if (earlyExit) { + process.exit(0); + } + }); +} +function restoreOnlyRun(earlyExit) { + return __awaiter(this, void 0, void 0, function* () { + yield run(new stateProvider_1.NullStateProvider(), earlyExit); + }); +} +exports.restoreOnlyRun = restoreOnlyRun; +function restoreRun(earlyExit) { + return __awaiter(this, void 0, void 0, function* () { + yield run(new stateProvider_1.StateProvider(), earlyExit); + }); +} +exports.restoreRun = restoreRun; /***/ }), diff --git a/src/restore.ts b/src/restore.ts index 312d203..4c53c55 100644 --- a/src/restore.ts +++ b/src/restore.ts @@ -1,26 +1,3 @@ -import restoreImpl from "./restoreImpl"; -import { StateProvider } from "./stateProvider"; +import { restoreRun } from "./restoreImpl"; -async function run(earlyExit?: boolean | undefined): Promise { - try { - await restoreImpl(new StateProvider()); - } catch (err) { - console.error(err); - if (earlyExit) { - process.exit(1); - } - } - - // node will stay alive if any promises are not resolved, - // which is a possibility if HTTP requests are dangling - // due to retries or timeouts. We know that if we got here - // that all promises that we care about have successfully - // resolved, so simply exit with success. - if (earlyExit) { - process.exit(0); - } -} - -run(true); - -export default run; +restoreRun(true); diff --git a/src/restoreImpl.ts b/src/restoreImpl.ts index 797bc74..df50790 100644 --- a/src/restoreImpl.ts +++ b/src/restoreImpl.ts @@ -2,10 +2,10 @@ import * as cache from "@actions/cache"; import * as core from "@actions/core"; import { Events, Inputs, Outputs, State } from "./constants"; -import { IStateProvider } from "./stateProvider"; +import { IStateProvider, NullStateProvider, StateProvider } from "./stateProvider"; import * as utils from "./utils/actionUtils"; -async function restoreImpl( +export async function restoreImpl( stateProvider: IStateProvider ): Promise { try { @@ -82,4 +82,30 @@ async function restoreImpl( } } -export default restoreImpl; +async function run(stateProvider: IStateProvider, earlyExit: boolean | undefined): Promise { + try { + await restoreImpl(stateProvider); + } catch (err) { + console.error(err); + if (earlyExit) { + process.exit(1); + } + } + + // node will stay alive if any promises are not resolved, + // which is a possibility if HTTP requests are dangling + // due to retries or timeouts. We know that if we got here + // that all promises that we care about have successfully + // resolved, so simply exit with success. + if (earlyExit) { + process.exit(0); + } +} + +export async function restoreOnlyRun(earlyExit?: boolean | undefined): Promise { + await run(new NullStateProvider(), earlyExit); +} + +export async function restoreRun(earlyExit?: boolean | undefined): Promise { + await run(new StateProvider(), earlyExit); +} diff --git a/src/restoreOnly.ts b/src/restoreOnly.ts index baf1fd5..c773a4c 100644 --- a/src/restoreOnly.ts +++ b/src/restoreOnly.ts @@ -1,26 +1,3 @@ -import restoreImpl from "./restoreImpl"; -import { NullStateProvider } from "./stateProvider"; +import { restoreOnlyRun } from "./restoreImpl"; -async function run(earlyExit?: boolean | undefined): Promise { - try { - await restoreImpl(new NullStateProvider()); - } catch (err) { - console.error(err); - if (earlyExit) { - process.exit(1); - } - } - - // node will stay alive if any promises are not resolved, - // which is a possibility if HTTP requests are dangling - // due to retries or timeouts. We know that if we got here - // that all promises that we care about have successfully - // resolved, so simply exit with success. - if (earlyExit) { - process.exit(0); - } -} - -run(true); - -export default run; +restoreOnlyRun(true);