Repository navigation
Expand file tree
/
Copy pathgraphify.test.js
More file actions
163 lines (144 loc) · 7.06 KB
/
Copy pathgraphify.test.js
File metadata and controls
163 lines (144 loc) · 7.06 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const fs = require("fs");
const os = require("os");
const path = require("path");
const { GraphifyPlugin } = require("../graphify.js");
// The plugin only ever appends advisory text to the agent's output; it never
// runs the command. These tests therefore assert on the reminder the agent would
// copy, which is where a mis-quoted graph path does its damage. Expectations are
// built from os.tmpdir() rather than a literal /tmp, and mirror the separator
// handling the plugin applies, so the suite holds on Windows and macOS too.
// shellQuote normalises Windows separators to forward slashes, so the expected
// value has to be normalised the same way or the comparison fails on a native
// Windows runner.
const expectedGraph = (root) => {
const joined = path.join(root, "graphify-out", "graph.json");
return process.platform === "win32" ? joined.replace(/\\/g, "/") : joined;
};
async function reminderFor(prefix) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), `graphify-${prefix}-`));
fs.mkdirSync(path.join(root, "graphify-out"));
fs.writeFileSync(path.join(root, "graphify-out", "graph.json"), "{}");
const plugin = await GraphifyPlugin({ directory: root, worktree: root });
const output = { output: "bash done" };
await plugin["tool.execute.after"](
{ tool: "bash", sessionID: `s-${prefix}` },
output,
);
return { root, output: output.output };
}
// The quoted value may itself contain spaces, so capture up to the clause that
// follows rather than to the next whitespace.
const graphArg = (reminder) => reminder.match(/--graph (.+?) \(scoped subgraph/)?.[1];
describe("graphify reminder quotes the graph path safely", () => {
test("leaves a plain path unquoted, so no shell has to be guessed", async () => {
const { root, output } = await reminderFor("plain");
expect(graphArg(output)).toBe(expectedGraph(root));
});
test("double-quotes a path with a space, which every shell groups alike", async () => {
const { root, output } = await reminderFor("with space");
expect(graphArg(output)).toBe(`"${expectedGraph(root)}"`);
});
test("double-quotes a path with an apostrophe rather than guessing a shell", async () => {
// PowerShell escapes an apostrophe as '' and POSIX as '\'' , and cmd.exe does
// not treat single quotes as quoting at all, so picking by platform corrupted
// the argument on the other shell. Double quotes group in all three.
const { root, output } = await reminderFor("owner's repo");
expect(root).toContain("'");
expect(graphArg(output)).toBe(`"${expectedGraph(root)}"`);
});
test("normalises Windows separators so a native path needs no quoting at all", async () => {
// A native Windows path is full of backslashes, which are double-quote
// unsafe, so without normalisation every Windows path would drop out of the
// portable tiers. A backslash is a legal filename character on Linux, so the
// same input can be built here and the platform mocked.
const real = process.platform;
Object.defineProperty(process, "platform", {
value: "win32",
configurable: true,
});
try {
const outer = fs.mkdtempSync(path.join(os.tmpdir(), "graphify-win-"));
const root = path.join(outer, "win\\style");
fs.mkdirSync(path.join(root, "graphify-out"), { recursive: true });
fs.writeFileSync(path.join(root, "graphify-out", "graph.json"), "{}");
const plugin = await GraphifyPlugin({ directory: root, worktree: root });
const output = { output: "bash done" };
await plugin["tool.execute.after"](
{ tool: "bash", sessionID: "win" },
output,
);
// Forward slashes and no quoting: correct in cmd, PowerShell and Bash.
const arg = graphArg(output.output);
expect(root).toContain("\\");
expect(arg).not.toContain("\\");
expect(arg).toBe(expectedGraph(root));
} finally {
Object.defineProperty(process, "platform", {
value: real,
configurable: true,
});
}
});
test("offers no runnable example for a percent sign, which cmd.exe expands", async () => {
// cmd.exe expands %VAR% even inside double quotes, so a literal
// %USERNAME% directory would be rewritten before graphify ever saw it. On
// POSIX a percent sign is an ordinary filename character, so the path is
// still safe there, but it has no form that is right in both.
const { root, output } = await reminderFor("%USERNAME%");
expect(root).toContain("%USERNAME%");
expect(graphArg(output)).toBeUndefined();
expect(output).not.toContain("--graph");
expect(output).toContain("no runnable scoped example");
});
test("offers no runnable example for a bang, which two shells expand", async () => {
// cmd.exe expands !VAR! inside double quotes when delayed expansion is on,
// and interactive bash performs history expansion on !. Neither is POSIX
// script behaviour, but the reminder is aimed at whatever shell a human
// typed, so a path containing one has no form that is right in all of them.
const { root, output } = await reminderFor("!USERNAME!");
expect(root).toContain("!USERNAME!");
expect(graphArg(output)).toBeUndefined();
expect(output).not.toContain("--graph");
expect(output).toContain("no runnable scoped example");
});
test("offers no runnable example for a path no shell quotes the same way", async () => {
// A dollar sign, double quote, backtick or backslash has no portable form:
// it needs POSIX '\'' or PowerShell '' for an apostrophe, and cmd.exe cannot
// group it at all. Emitting a command that is wrong in some shell is worse
// than saying so, so the scoped example is dropped.
const { root, output } = await reminderFor("cash$");
expect(root).toContain("$");
expect(graphArg(output)).toBeUndefined();
expect(output).not.toContain("--graph");
expect(output).toContain("no runnable scoped example");
expect(output).toContain("GRAPH_REPORT.md");
});
test("does not re-add the reminder for a second bash call in the same session", async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "graphify-once-"));
fs.mkdirSync(path.join(root, "graphify-out"));
fs.writeFileSync(path.join(root, "graphify-out", "graph.json"), "{}");
const plugin = await GraphifyPlugin({ directory: root, worktree: root });
const first = { output: "one" };
await plugin["tool.execute.after"](
{ tool: "bash", sessionID: "same" },
first,
);
const second = { output: "two" };
await plugin["tool.execute.after"](
{ tool: "bash", sessionID: "same" },
second,
);
expect(first.output).toContain("graphify");
expect(second.output).toBe("two");
});
test("stays silent when the repository has no graph", async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "graphify-none-"));
const plugin = await GraphifyPlugin({ directory: root, worktree: root });
const output = { output: "bash done" };
await plugin["tool.execute.after"](
{ tool: "bash", sessionID: "none" },
output,
);
expect(output.output).toBe("bash done");
});
});