Skip to content

Commit 54e0a47

Browse files
committed
Add testing for prompter interface
1 parent 9cc7074 commit 54e0a47

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

tests/cli_test.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// review
44
// run
55

6-
import { assertEquals } from "jsr:@std/assert";
6+
import { assertEquals, assertArrayIncludes } from "jsr:@std/assert";
77
import { assertSnapshot } from "jsr:@std/testing/snapshot";
88
import { $ } from "jsr:@david/dax"
99

@@ -81,9 +81,12 @@ Deno.test.ignore("diff", async (t) => {
8181

8282
Deno.test("init", async (t) => {
8383
const {dir} = await setup();
84-
const files = await Array.fromAsync(Deno.readDir(dir));
85-
const filenames = files.map(f => f.name).sort();
86-
assertSnapshot(t, filenames);
84+
const files = (await getAllFiles(dir)).map(f => f.replace(dir, ""));
85+
assertEquals(files, [
86+
"/runbooks/binder/_template-deno.ipynb",
87+
"/runbooks/binder/_template-python.ipynb",
88+
"/runbooks/.runbook.json",
89+
])
8790
});
8891

8992
Deno.test("list", async (t) => {
@@ -99,6 +102,27 @@ Deno.test("show", async (t) => {
99102
assertSnapshot(t, { stdout: cmd.stdout, stderr: cmd.stderr, exitCode: cmd.code });
100103
});
101104

105+
// plan
106+
Deno.test("plan: prompter interface", async (t) => {
107+
const cwd = Deno.cwd();
108+
const {runbook, dir } = await setup();
109+
const cmd = await runbook(["plan", "runbooks/binder/_template-deno.ipynb", "--prompter", [cwd, "tests/fixtures/prompters/echoer"].join("/")]);
110+
assertEquals(cmd.code, 0);
111+
112+
const files = await getAllFiles($.path(dir).join("runbooks/runs").toString());
113+
const planFile = files.find(f => f.endsWith("_template-deno/_template-deno.ipynb"));
114+
if(!planFile) {
115+
throw new Error("Plan file not found");
116+
}
117+
const json = await Deno.readTextFile(planFile);
118+
const plan = JSON.parse(json);
119+
const maybeParamCells = plan.cells.filter((c: any) => c.cell_type === "code" && c.metadata?.tags?.includes("injected-parameters"));
120+
assertEquals(maybeParamCells.length, 1);
121+
const paramCell = maybeParamCells[0];
122+
assertArrayIncludes(paramCell.source, [`server = "main.xargs.io";\n`, `arg = 1;\n`, `anArray = ["a", "b"];\n`]);
123+
// assertSnapshot(t, { stdout: cmd.stdout, stderr: cmd.stderr, exitCode: cmd.code });
124+
});
125+
102126
// run
103127
/* failing on nested dax commands
104128
Exception encountered at "In [3]":
@@ -122,3 +146,22 @@ Deno.test("version", async (t) => {
122146
assertSnapshot(t, { stdout: cmd.stdout.split(":")[0], stderr: cmd.stderr, exitCode: cmd.code });
123147
});
124148

149+
async function* walkFiles(dir: string): AsyncGenerator<string> {
150+
for await (const entry of Deno.readDir(dir)) {
151+
const path = `${dir}/${entry.name}`;
152+
if (entry.isDirectory) {
153+
yield* walkFiles(path);
154+
} else {
155+
yield path;
156+
}
157+
}
158+
}
159+
160+
async function getAllFiles(dir: string): Promise<string[]> {
161+
const files = [];
162+
for await (const file of walkFiles(dir)) {
163+
files.push(file);
164+
}
165+
return files;
166+
}
167+

tests/fixtures/prompters/echoer

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
3+
echo '{"server": "main.xargs.io", "arg": 1, "anArray": ["a", "b"]}'
4+
# cat /dev/stdin

0 commit comments

Comments
 (0)