Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ describe("config", () => {
);
});

test("should not throw if value is empty and plugin is disabled", () => {
expect(() => parseConfig(config({ enabled: false, token: "" }))).not.toThrow();
});

test("should set passed value", () => {
expect(parseConfig(config({ token: "123456789" })).token).toBe("123456789");
});
Expand All @@ -54,6 +58,10 @@ describe("config", () => {
expect(() => parseConfig(config({ help: "" }))).toThrow(/'help' option must be of a non empty string type/);
});

test("should not throw if value is an empty string and plugin is disabled", () => {
expect(() => parseConfig(config({ enabled: false, help: "" }))).not.toThrow();
});

test("should set passed value", () => {
expect(parseConfig(config({ help: "https://<help>" })).help).toBe("https://<help>");
});
Expand Down
25 changes: 12 additions & 13 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import type { Parser } from "gemini-configparser";

import { PluginOptionTypeError, PluginTokenOptionAbsenceError } from "./errors";

export type PluginConfig = {
enabled: boolean;
token: string;
help: string;
};
export type PluginConfig =
| { enabled: true; token: string; help: string }
| { enabled: false; token?: string; help?: string };

const isNonEmptyString = (v: unknown): boolean => typeof v === "string" && v !== "";

Expand All @@ -27,12 +25,6 @@ const boolean = (name: string): Parser<boolean> =>
validate: assertType(name, v => typeof v === "boolean", "boolean"),
});

const nonEmptyString = (name: string): Parser<string> =>
option({
defaultValue: "",
validate: assertType(name, isNonEmptyString, "non empty string"),
});

export function parseConfig(options: Record<string, unknown>): PluginConfig {
const { env, argv } = process;

Expand All @@ -42,12 +34,19 @@ export function parseConfig(options: Record<string, unknown>): PluginConfig {
token: option({
defaultValue: "",
validate: (v, config) => {
if (!isNonEmptyString(v)) {
if (!isNonEmptyString(v) && config.enabled) {
throw new PluginTokenOptionAbsenceError(config.help);
}
},
}),
help: nonEmptyString("help"),
help: option({
defaultValue: "",
validate: (v, config) => {
if (config.enabled) {
assertType("help", isNonEmptyString, "non empty string")(v);
}
},
}),
}),
{ envPrefix: "testplane_oauth_", cliPrefix: "--oauth-" },
);
Expand Down
Loading