-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
180 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { beforeEach, describe, expect, it } from "vitest"; | ||
import { getBoolean, getMultiline, getString } from "../inputs"; | ||
|
||
const testEnvVars = { | ||
// Set inputs | ||
INPUT_STRING: "string value", | ||
INPUT_BOOLEAN_TRUE: "true", | ||
INPUT_BOOLEAN_FALSE: "false", | ||
INPUT_LIST: "val1\nval2\nval3", | ||
}; | ||
|
||
describe("@actions/core", () => { | ||
beforeEach(() => { | ||
for (const key in testEnvVars) { | ||
process.env[key] = testEnvVars[key as keyof typeof testEnvVars]; | ||
} | ||
}); | ||
|
||
describe("get inputs", () => { | ||
it("getString", async () => { | ||
expect(getString({ name: "string", required: true })).toBe("string value"); | ||
expect(() => getString({ name: "string required", required: true })).toThrowError(); | ||
expect(getString({ name: "string optional", required: false, defaultValue: "default" })).toBe("default"); | ||
}); | ||
|
||
it("getBooleanInput", async () => { | ||
expect(getBoolean({ name: "boolean true", required: true })).toBe(true); | ||
expect(getBoolean({ name: "boolean false", required: true })).toBe(false); | ||
expect(() => getBoolean({ name: "boolean required", required: true })).toThrowError(); | ||
expect(getBoolean({ name: "boolean optional", required: false, defaultValue: false })).toBe(false); | ||
}); | ||
|
||
it("getMultiline", async () => { | ||
expect(getMultiline({ name: "list", required: true })).toMatchObject(["val1", "val2", "val3"]); | ||
expect(() => getMultiline({ name: "list required", required: true })).toThrowError(); | ||
expect(getMultiline({ name: "list optional", required: false, defaultValue: ["default"] })).toMatchObject(["default"]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as core from "@actions/core"; | ||
|
||
type GetStringOptions = { name: string; required: true } | { name: string; required: false; defaultValue: string }; | ||
|
||
export function getString(options: GetStringOptions): string { | ||
const { name, required } = options; | ||
|
||
if (required) { | ||
return core.getInput(name, { required }); | ||
} else { | ||
return core.getInput(name, { required }) || options.defaultValue; | ||
} | ||
} | ||
|
||
type GetBooleanOptions = { name: string; required: true } | { name: string; required: false; defaultValue: boolean }; | ||
|
||
export function getBoolean(options: GetBooleanOptions): boolean { | ||
const { name, required } = options; | ||
|
||
if (required) { | ||
return core.getBooleanInput(name, { required }); | ||
} else { | ||
try { | ||
return core.getBooleanInput(name, { required }); | ||
} catch (error) { | ||
return options.defaultValue; | ||
} | ||
} | ||
} | ||
|
||
type GetMultilineOptions = { name: string; required: true } | { name: string; required: false; defaultValue: string[] }; | ||
|
||
export function getMultiline(options: GetMultilineOptions): string[] { | ||
const { name, required } = options; | ||
|
||
if (required) { | ||
return core.getMultilineInput(name, { required }); | ||
} else { | ||
const value = core.getMultilineInput(name, { required }); | ||
|
||
if (value.length === 0) { | ||
return options.defaultValue; | ||
} else { | ||
return value; | ||
} | ||
} | ||
} |