From 01229828ffa049a8dee4db27bcb23ed33f2b451f Mon Sep 17 00:00:00 2001
From: Tatyana Kostromskaya <32135588+takost@users.noreply.github.com>
Date: Wed, 10 Jan 2024 15:36:58 +0000
Subject: [PATCH] Apply workaround for earlyExit

---
 __tests__/save.test.ts     |   4 +-
 __tests__/saveImpl.test.ts |  24 ++++----
 __tests__/saveOnly.test.ts |   6 +-
 dist/save-only/index.js    | 121 ++++++++++++++++++-------------------
 dist/save/index.js         | 102 +++++++++++++++++++------------
 src/save.ts                |  11 +---
 src/saveImpl.ts            |  52 +++++++++++++++-
 src/saveOnly.ts            |  16 +----
 8 files changed, 192 insertions(+), 144 deletions(-)

diff --git a/__tests__/save.test.ts b/__tests__/save.test.ts
index 1eb9c7b..4678c43 100644
--- a/__tests__/save.test.ts
+++ b/__tests__/save.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/save";
+import { saveRun } from "../src/saveImpl";
 import * as actionUtils from "../src/utils/actionUtils";
 import * as testUtils from "../src/utils/testUtils";
 
@@ -100,7 +100,7 @@ test("save with valid inputs uploads a cache", async () => {
             return Promise.resolve(cacheId);
         });
 
-    await run();
+    await saveRun();
 
     expect(saveCacheMock).toHaveBeenCalledTimes(1);
     expect(saveCacheMock).toHaveBeenCalledWith(
diff --git a/__tests__/saveImpl.test.ts b/__tests__/saveImpl.test.ts
index 2a048b0..ad154f6 100644
--- a/__tests__/saveImpl.test.ts
+++ b/__tests__/saveImpl.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/saveImpl";
+import { saveImpl } from "../src/saveImpl";
 import { StateProvider } from "../src/stateProvider";
 import * as actionUtils from "../src/utils/actionUtils";
 import * as testUtils from "../src/utils/testUtils";
@@ -77,7 +77,7 @@ test("save with invalid event outputs warning", async () => {
     const invalidEvent = "commit_comment";
     process.env[Events.Key] = invalidEvent;
     delete process.env[RefKey];
-    await run(new StateProvider());
+    await saveImpl(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.`
     );
@@ -100,7 +100,7 @@ test("save with no primary key in state outputs warning", async () => {
         });
     const saveCacheMock = jest.spyOn(cache, "saveCache");
 
-    await run(new StateProvider());
+    await saveImpl(new StateProvider());
 
     expect(saveCacheMock).toHaveBeenCalledTimes(0);
     expect(logWarningMock).toHaveBeenCalledWith(`Key is not specified.`);
@@ -115,7 +115,7 @@ test("save without AC available should no-op", async () => {
 
     const saveCacheMock = jest.spyOn(cache, "saveCache");
 
-    await run(new StateProvider());
+    await saveImpl(new StateProvider());
 
     expect(saveCacheMock).toHaveBeenCalledTimes(0);
 });
@@ -128,7 +128,7 @@ test("save on ghes without AC available should no-op", async () => {
 
     const saveCacheMock = jest.spyOn(cache, "saveCache");
 
-    await run(new StateProvider());
+    await saveImpl(new StateProvider());
 
     expect(saveCacheMock).toHaveBeenCalledTimes(0);
 });
@@ -161,7 +161,7 @@ test("save on GHES with AC available", async () => {
             return Promise.resolve(cacheId);
         });
 
-    await run(new StateProvider());
+    await saveImpl(new StateProvider());
 
     expect(saveCacheMock).toHaveBeenCalledTimes(1);
     expect(saveCacheMock).toHaveBeenCalledWith(
@@ -194,7 +194,7 @@ test("save with exact match returns early", async () => {
         });
     const saveCacheMock = jest.spyOn(cache, "saveCache");
 
-    await run(new StateProvider());
+    await saveImpl(new StateProvider());
 
     expect(saveCacheMock).toHaveBeenCalledTimes(0);
     expect(infoMock).toHaveBeenCalledWith(
@@ -221,7 +221,7 @@ test("save with missing input outputs warning", async () => {
         });
     const saveCacheMock = jest.spyOn(cache, "saveCache");
 
-    await run(new StateProvider());
+    await saveImpl(new StateProvider());
 
     expect(saveCacheMock).toHaveBeenCalledTimes(0);
     expect(logWarningMock).toHaveBeenCalledWith(
@@ -259,7 +259,7 @@ test("save with large cache outputs warning", async () => {
             );
         });
 
-    await run(new StateProvider());
+    await saveImpl(new StateProvider());
 
     expect(saveCacheMock).toHaveBeenCalledTimes(1);
     expect(saveCacheMock).toHaveBeenCalledWith(
@@ -306,7 +306,7 @@ test("save with reserve cache failure outputs warning", async () => {
             throw error;
         });
 
-    await run(new StateProvider());
+    await saveImpl(new StateProvider());
 
     expect(saveCacheMock).toHaveBeenCalledTimes(1);
     expect(saveCacheMock).toHaveBeenCalledWith(
@@ -349,7 +349,7 @@ test("save with server error outputs warning", async () => {
             throw new Error("HTTP Error Occurred");
         });
 
-    await run(new StateProvider());
+    await saveImpl(new StateProvider());
 
     expect(saveCacheMock).toHaveBeenCalledTimes(1);
     expect(saveCacheMock).toHaveBeenCalledWith(
@@ -392,7 +392,7 @@ test("save with valid inputs uploads a cache", async () => {
             return Promise.resolve(cacheId);
         });
 
-    await run(new StateProvider());
+    await saveImpl(new StateProvider());
 
     expect(saveCacheMock).toHaveBeenCalledTimes(1);
     expect(saveCacheMock).toHaveBeenCalledWith(
diff --git a/__tests__/saveOnly.test.ts b/__tests__/saveOnly.test.ts
index 5a7f8b0..0437739 100644
--- a/__tests__/saveOnly.test.ts
+++ b/__tests__/saveOnly.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/saveOnly";
+import { saveOnlyRun } from "../src/saveImpl";
 import * as actionUtils from "../src/utils/actionUtils";
 import * as testUtils from "../src/utils/testUtils";
 
@@ -90,7 +90,7 @@ test("save with valid inputs uploads a cache", async () => {
             return Promise.resolve(cacheId);
         });
 
-    await run();
+    await saveOnlyRun();
 
     expect(saveCacheMock).toHaveBeenCalledTimes(1);
     expect(saveCacheMock).toHaveBeenCalledWith(
@@ -122,7 +122,7 @@ test("save failing logs the warning message", async () => {
             return Promise.resolve(cacheId);
         });
 
-    await run();
+    await saveOnlyRun();
 
     expect(saveCacheMock).toHaveBeenCalledTimes(1);
     expect(saveCacheMock).toHaveBeenCalledWith(
diff --git a/dist/save-only/index.js b/dist/save-only/index.js
index f88b11d..4bb48d7 100644
--- a/dist/save-only/index.js
+++ b/dist/save-only/index.js
@@ -59387,9 +59387,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
     });
 };
 Object.defineProperty(exports, "__esModule", ({ value: true }));
+exports.saveRun = exports.saveOnlyRun = exports.saveImpl = void 0;
 const cache = __importStar(__nccwpck_require__(7799));
 const core = __importStar(__nccwpck_require__(2186));
 const constants_1 = __nccwpck_require__(9042);
+const stateProvider_1 = __nccwpck_require__(1527);
 const utils = __importStar(__nccwpck_require__(6850));
 // Catch and log any unhandled exceptions.  These exceptions can leak out of the uploadChunk method in
 // @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
@@ -59436,65 +59438,54 @@ function saveImpl(stateProvider) {
         return cacheId;
     });
 }
-exports["default"] = saveImpl;
-
-
-/***/ }),
-
-/***/ 3160:
-/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
-
-"use strict";
-
-var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
-    if (k2 === undefined) k2 = k;
-    var desc = Object.getOwnPropertyDescriptor(m, k);
-    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
-      desc = { enumerable: true, get: function() { return m[k]; } };
-    }
-    Object.defineProperty(o, k2, desc);
-}) : (function(o, m, k, k2) {
-    if (k2 === undefined) k2 = k;
-    o[k2] = m[k];
-}));
-var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
-    Object.defineProperty(o, "default", { enumerable: true, value: v });
-}) : function(o, v) {
-    o["default"] = v;
-});
-var __importStar = (this && this.__importStar) || function (mod) {
-    if (mod && mod.__esModule) return mod;
-    var result = {};
-    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
-    __setModuleDefault(result, mod);
-    return result;
-};
-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 core = __importStar(__nccwpck_require__(2186));
-const saveImpl_1 = __importDefault(__nccwpck_require__(6589));
-const stateProvider_1 = __nccwpck_require__(1527);
-function run() {
+exports.saveImpl = saveImpl;
+function saveOnlyRun(earlyExit) {
     return __awaiter(this, void 0, void 0, function* () {
-        const cacheId = yield (0, saveImpl_1.default)(new stateProvider_1.NullStateProvider());
-        if (cacheId === -1) {
-            core.warning(`Cache save failed.`);
+        try {
+            const cacheId = yield saveImpl(new stateProvider_1.NullStateProvider());
+            if (cacheId === -1) {
+                core.warning(`Cache save failed.`);
+            }
+        }
+        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();
-exports["default"] = run;
+exports.saveOnlyRun = saveOnlyRun;
+function saveRun(earlyExit) {
+    return __awaiter(this, void 0, void 0, function* () {
+        try {
+            yield saveImpl(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);
+        }
+    });
+}
+exports.saveRun = saveRun;
 
 
 /***/ }),
@@ -59882,12 +59873,18 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"]
 /******/ 	if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
 /******/ 	
 /************************************************************************/
-/******/ 	
-/******/ 	// startup
-/******/ 	// Load entry module and return exports
-/******/ 	// This entry module is referenced by other modules so it can't be inlined
-/******/ 	var __webpack_exports__ = __nccwpck_require__(3160);
-/******/ 	module.exports = __webpack_exports__;
-/******/ 	
+var __webpack_exports__ = {};
+// This entry need to be wrapped in an IIFE because it need to be in strict mode.
+(() => {
+"use strict";
+var exports = __webpack_exports__;
+
+Object.defineProperty(exports, "__esModule", ({ value: true }));
+const saveImpl_1 = __nccwpck_require__(6589);
+(0, saveImpl_1.saveOnlyRun)(true);
+
+})();
+
+module.exports = __webpack_exports__;
 /******/ })()
 ;
\ No newline at end of file
diff --git a/dist/save/index.js b/dist/save/index.js
index a006e7d..0c2a02e 100644
--- a/dist/save/index.js
+++ b/dist/save/index.js
@@ -59347,37 +59347,6 @@ var Events;
 exports.RefKey = "GITHUB_REF";
 
 
-/***/ }),
-
-/***/ 5131:
-/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
-
-"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 saveImpl_1 = __importDefault(__nccwpck_require__(6589));
-const stateProvider_1 = __nccwpck_require__(1527);
-function run() {
-    return __awaiter(this, void 0, void 0, function* () {
-        yield (0, saveImpl_1.default)(new stateProvider_1.StateProvider());
-    });
-}
-run();
-exports["default"] = run;
-
-
 /***/ }),
 
 /***/ 6589:
@@ -59418,9 +59387,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
     });
 };
 Object.defineProperty(exports, "__esModule", ({ value: true }));
+exports.saveRun = exports.saveOnlyRun = exports.saveImpl = void 0;
 const cache = __importStar(__nccwpck_require__(7799));
 const core = __importStar(__nccwpck_require__(2186));
 const constants_1 = __nccwpck_require__(9042);
+const stateProvider_1 = __nccwpck_require__(1527);
 const utils = __importStar(__nccwpck_require__(6850));
 // Catch and log any unhandled exceptions.  These exceptions can leak out of the uploadChunk method in
 // @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
@@ -59467,7 +59438,54 @@ function saveImpl(stateProvider) {
         return cacheId;
     });
 }
-exports["default"] = saveImpl;
+exports.saveImpl = saveImpl;
+function saveOnlyRun(earlyExit) {
+    return __awaiter(this, void 0, void 0, function* () {
+        try {
+            const cacheId = yield saveImpl(new stateProvider_1.NullStateProvider());
+            if (cacheId === -1) {
+                core.warning(`Cache save failed.`);
+            }
+        }
+        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);
+        }
+    });
+}
+exports.saveOnlyRun = saveOnlyRun;
+function saveRun(earlyExit) {
+    return __awaiter(this, void 0, void 0, function* () {
+        try {
+            yield saveImpl(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);
+        }
+    });
+}
+exports.saveRun = saveRun;
 
 
 /***/ }),
@@ -59855,12 +59873,18 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"]
 /******/ 	if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
 /******/ 	
 /************************************************************************/
-/******/ 	
-/******/ 	// startup
-/******/ 	// Load entry module and return exports
-/******/ 	// This entry module is referenced by other modules so it can't be inlined
-/******/ 	var __webpack_exports__ = __nccwpck_require__(5131);
-/******/ 	module.exports = __webpack_exports__;
-/******/ 	
+var __webpack_exports__ = {};
+// This entry need to be wrapped in an IIFE because it need to be in strict mode.
+(() => {
+"use strict";
+var exports = __webpack_exports__;
+
+Object.defineProperty(exports, "__esModule", ({ value: true }));
+const saveImpl_1 = __nccwpck_require__(6589);
+(0, saveImpl_1.saveRun)(true);
+
+})();
+
+module.exports = __webpack_exports__;
 /******/ })()
 ;
\ No newline at end of file
diff --git a/src/save.ts b/src/save.ts
index 222c87e..34f7e69 100644
--- a/src/save.ts
+++ b/src/save.ts
@@ -1,10 +1,3 @@
-import saveImpl from "./saveImpl";
-import { StateProvider } from "./stateProvider";
+import { saveRun } from "./saveImpl";
 
-async function run(): Promise<void> {
-    await saveImpl(new StateProvider());
-}
-
-run();
-
-export default run;
+saveRun(true);
diff --git a/src/saveImpl.ts b/src/saveImpl.ts
index 5fd4513..640df8b 100644
--- a/src/saveImpl.ts
+++ b/src/saveImpl.ts
@@ -2,7 +2,11 @@ import * as cache from "@actions/cache";
 import * as core from "@actions/core";
 
 import { Events, Inputs, State } from "./constants";
-import { IStateProvider } from "./stateProvider";
+import {
+    IStateProvider,
+    NullStateProvider,
+    StateProvider
+} from "./stateProvider";
 import * as utils from "./utils/actionUtils";
 
 // Catch and log any unhandled exceptions.  These exceptions can leak out of the uploadChunk method in
@@ -10,7 +14,7 @@ import * as utils from "./utils/actionUtils";
 // throw an uncaught exception.  Instead of failing this action, just warn.
 process.on("uncaughtException", e => utils.logWarning(e.message));
 
-async function saveImpl(stateProvider: IStateProvider): Promise<number | void> {
+export async function saveImpl(stateProvider: IStateProvider): Promise<number | void> {
     let cacheId = -1;
     try {
         if (!utils.isCacheFeatureAvailable()) {
@@ -72,4 +76,46 @@ async function saveImpl(stateProvider: IStateProvider): Promise<number | void> {
     return cacheId;
 }
 
-export default saveImpl;
+export async function saveOnlyRun(earlyExit?: boolean | undefined): Promise<void> {
+    try {
+        const cacheId = await saveImpl(new NullStateProvider());
+        if (cacheId === -1) {
+            core.warning(`Cache save failed.`);
+        }
+    } 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 saveRun(earlyExit?: boolean | undefined): Promise<void> {
+    try {
+        await saveImpl(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);
+    }
+}
+
diff --git a/src/saveOnly.ts b/src/saveOnly.ts
index bdc3f77..171240b 100644
--- a/src/saveOnly.ts
+++ b/src/saveOnly.ts
@@ -1,15 +1,3 @@
-import * as core from "@actions/core";
+import { saveOnlyRun } from "./saveImpl";
 
-import saveImpl from "./saveImpl";
-import { NullStateProvider } from "./stateProvider";
-
-async function run(): Promise<void> {
-    const cacheId = await saveImpl(new NullStateProvider());
-    if (cacheId === -1) {
-        core.warning(`Cache save failed.`);
-    }
-}
-
-run();
-
-export default run;
+saveOnlyRun(true);
\ No newline at end of file