From 4bceb75b5b7743784c63c94b81c50a485cbdcda0 Mon Sep 17 00:00:00 2001 From: Dave Hadka Date: Fri, 2 Oct 2020 10:55:30 -0500 Subject: [PATCH] Use parseInt instead of Number to handle empty strings --- __tests__/actionUtils.test.ts | 9 +++++++-- dist/restore/index.js | 4 ++-- dist/save/index.js | 4 ++-- src/utils/actionUtils.ts | 4 ++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/__tests__/actionUtils.test.ts b/__tests__/actionUtils.test.ts index 1eed4e8..419881e 100644 --- a/__tests__/actionUtils.test.ts +++ b/__tests__/actionUtils.test.ts @@ -15,7 +15,6 @@ beforeAll(() => { afterEach(() => { delete process.env[Events.Key]; delete process.env[RefKey]; - testUtils.clearInputs(); }); test("isGhes returns true if server url is not github.com", () => { @@ -215,7 +214,7 @@ test("getInputAsArray handles empty lines correctly", () => { }); test("getInputAsInt returns undefined if input not set", () => { - expect(actionUtils.getInputAsInt("foo")).toBeUndefined(); + expect(actionUtils.getInputAsInt("undefined")).toBeUndefined(); }); test("getInputAsInt returns value if input is valid", () => { @@ -227,3 +226,9 @@ test("getInputAsInt returns undefined if input is invalid or NaN", () => { testUtils.setInput("foo", "bar"); expect(actionUtils.getInputAsInt("foo")).toBeUndefined(); }); + +test("getInputAsInt throws if required and value missing", () => { + expect(() => + actionUtils.getInputAsInt("undefined", { required: true }) + ).toThrowError(); +}); diff --git a/dist/restore/index.js b/dist/restore/index.js index e7ea6bc..87269d9 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -31354,8 +31354,8 @@ function getInputAsArray(name, options) { } exports.getInputAsArray = getInputAsArray; function getInputAsInt(name, options) { - const value = Number(core.getInput(name, options)); - if (Number.isNaN(value) || value < 0) { + const value = parseInt(core.getInput(name, options)); + if (isNaN(value) || value < 0) { return undefined; } return value; diff --git a/dist/save/index.js b/dist/save/index.js index 175ab6f..dda81de 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -31354,8 +31354,8 @@ function getInputAsArray(name, options) { } exports.getInputAsArray = getInputAsArray; function getInputAsInt(name, options) { - const value = Number(core.getInput(name, options)); - if (Number.isNaN(value) || value < 0) { + const value = parseInt(core.getInput(name, options)); + if (isNaN(value) || value < 0) { return undefined; } return value; diff --git a/src/utils/actionUtils.ts b/src/utils/actionUtils.ts index 5f975ec..a4d712d 100644 --- a/src/utils/actionUtils.ts +++ b/src/utils/actionUtils.ts @@ -68,8 +68,8 @@ export function getInputAsInt( name: string, options?: core.InputOptions ): number | undefined { - const value = Number(core.getInput(name, options)); - if (Number.isNaN(value) || value < 0) { + const value = parseInt(core.getInput(name, options)); + if (isNaN(value) || value < 0) { return undefined; } return value;