-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-github-cli.test.mjs
More file actions
102 lines (91 loc) · 2.69 KB
/
Copy pathrun-github-cli.test.mjs
File metadata and controls
102 lines (91 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { chmodSync, mkdtempSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
const scriptPath = new URL("./run-github-cli.mjs", import.meta.url).pathname;
function createFakeGh() {
const dir = mkdtempSync(join(tmpdir(), "github-cli-action-"));
const path = join(dir, "gh");
writeFileSync(path, `#!/bin/sh
if [ "$1" = "--version" ]; then
printf 'gh version 2.92.0 (test)\\n'
exit 0
fi
if [ "$1" = "fail" ]; then
printf 'intentional failure\\n' >&2
exit 7
fi
if [ "$1" = "stdin" ]; then
cat
exit 0
fi
printf 'repo=%s\\n' "$GH_REPO"
printf 'host=%s\\n' "$GH_HOST"
printf 'prompt=%s\\n' "$GH_PROMPT_DISABLED" >&2
printf 'args='
for arg do
printf '[%s]' "$arg"
done
printf '\\n'
`);
chmodSync(path, 0o755);
return dir;
}
function runAction(params) {
const fakeGhDir = createFakeGh();
const result = spawnSync(process.execPath, [scriptPath], {
env: {
...process.env,
PATH: `${fakeGhDir}:${process.env.PATH}`,
DAG_PARAMS_JSON: JSON.stringify(params),
},
encoding: "utf8",
});
assert.equal(result.stderr, "");
const payload = JSON.parse(result.stdout);
return { status: result.status, payload };
}
test("runs gh with array args and repo/host environment", () => {
const { status, payload } = runAction({
args: ["repo", "view", "--json", "name"],
repo: "dagucloud/dagu",
host: "github.com",
});
assert.equal(status, 0);
assert.equal(payload.ok, true);
assert.equal(payload.exitCode, 0);
assert.equal(payload.ghVersion, "gh version 2.92.0 (test)");
assert.match(payload.stdout, /repo=dagucloud\/dagu/);
assert.match(payload.stdout, /host=github.com/);
assert.match(payload.stdout, /args=\[repo\]\[view\]\[--json\]\[name\]/);
assert.match(payload.stderr, /prompt=1/);
});
test("passes stdin to gh", () => {
const { status, payload } = runAction({
args: ["stdin"],
stdin: "hello from stdin\n",
});
assert.equal(status, 0);
assert.equal(payload.ok, true);
assert.equal(payload.stdout, "hello from stdin\n");
});
test("fails when gh exits non-zero", () => {
const { status, payload } = runAction({
args: ["fail"],
});
assert.equal(status, 7);
assert.equal(payload.ok, false);
assert.equal(payload.exitCode, 7);
assert.match(payload.stderr, /intentional failure/);
});
test("rejects shell-string args", () => {
const { status, payload } = runAction({
args: "repo view dagucloud/dagu",
});
assert.equal(status, 1);
assert.equal(payload.ok, false);
assert.equal(payload.exitCode, -1);
assert.equal(payload.error.name, "ValidationError");
});