Skip to content

Commit

Permalink
allow 'info' to not error in quarto check
Browse files Browse the repository at this point in the history
  • Loading branch information
cscheid committed Dec 3, 2024
1 parent 8d9f858 commit 5f75aa3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
24 changes: 15 additions & 9 deletions src/command/check/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,20 @@ import { notebookContext } from "../../render/notebook/notebook-context.ts";
import { typstBinaryPath } from "../../core/typst.ts";
import { quartoCacheDir } from "../../core/appdirs.ts";
import { isWindows } from "../../deno_ral/platform.ts";
import { checkStringEnum } from "../../typing/dynamic.ts";

const kIndent = " ";
export const kTargets = [
"install",
"info",
"jupyter",
"knitr",
"versions",
"all",
] as const;
export type Target = typeof kTargets[number];
export const checkTargetType = checkStringEnum(...kTargets);

export type Target =
| "install"
| "jupyter"
| "knitr"
| "versions"
| "info"
| "all";
const kIndent = " ";

export async function check(target: Target): Promise<void> {
const services = renderServices(notebookContext());
Expand All @@ -82,7 +86,9 @@ export async function check(target: Target): Promise<void> {
}
}

// Currently this doesn't check anything, but
// Currently this doesn't check anything
// but it's a placeholder for future checks
// and the message is useful for troubleshooting
async function checkInfo(_services: RenderServices) {
const cacheDir = quartoCacheDir();
completeMessage("Checking environment information...");
Expand Down
18 changes: 4 additions & 14 deletions src/command/check/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
* Copyright (C) 2021-2022 Posit Software, PBC
*/

import { error } from "../../deno_ral/log.ts";

import { Command } from "cliffy/command/mod.ts";
import { check, Target } from "./check.ts";

const kTargets = ["install", "jupyter", "knitr", "versions", "all"];
import { check, checkTargetType } from "./check.ts";

export const checkCommand = new Command()
.name("check")
Expand All @@ -22,13 +18,7 @@ export const checkCommand = new Command()
.example("Check Jupyter engine", "quarto check jupyter")
.example("Check Knitr engine", "quarto check knitr")
.example("Check installation and all engines", "quarto check all")
.action(async (_options: unknown, target?: string) => {
target = target || "all";
if (!kTargets.includes(target)) {
error(
"Invalid target '" + target + "' (valid targets are " +
kTargets.join(", ") + ").",
);
}
await check(target as Target);
.action(async (_options: unknown, targetStr?: string) => {
targetStr = targetStr || "all";
await check(checkTargetType(targetStr));
});
27 changes: 27 additions & 0 deletions src/typing/dynamic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* dynamic.ts
*
* Tools for managing the interface between dynamic and static typing
* in Quarto.
*
* Ideally, every usage of `any` or `as` would appear in this file.
*
* Copyright (C) 2024 Posit Software, PBC
*/

import { DynamicTypeCheckError } from "../core/lib/error.ts";

export const checkStringEnum = <T extends string>(
...values: T[]
): (value: string) => T => {
const valueSet: Set<string> = new Set(values);
return (value: string): T => {
if (!valueSet.has(value)) {
throw new DynamicTypeCheckError(
"Invalid value '" + value + "' (valid values are " +
values.join(", ") + ").",
);
}
return value as unknown as T;
};
};
19 changes: 19 additions & 0 deletions tests/unit/typing/dynamic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* dynamic.test.ts
*
* Copyright (C) 2024 Posit Software, PBC
*
*/

import { checkStringEnum } from "../../../src/typing/dynamic.ts";
import { unitTest } from "../../test.ts";
import { assert, assertThrows } from "testing/asserts";

// deno-lint-ignore require-await
unitTest("checkStringEnum", async () => {
const check = checkStringEnum("a", "b", "c");
assert(check("a") === "a");
assert(check("b") === "b");
assert(check("c") === "c");
assertThrows(() => check("d"), Error, "Invalid value 'd' (valid values are a, b, c).");
});

0 comments on commit 5f75aa3

Please sign in to comment.