Skip to content

Commit 3d6c4ff

Browse files
fix: read the comment-length repository without changing directory
`runtimes (bun)` failed all seven repository tests with `process.chdir() is not supported in workers` — jest runs them in a worker there, and the newest Bun refuses the call. Take the repository as an argument through `report` and `main` instead, which also stops the checker depending on whatever directory it is called from.
1 parent 79c2b13 commit 3d6c4ff

2 files changed

Lines changed: 25 additions & 25 deletions

File tree

‎test/CheckCommentLength.unittest.js‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const {
1111
untrackedDiff
1212
} = require("../tooling/check-comment-length");
1313

14+
const SCRIPT = path.join(__dirname, "..", "tooling", "check-comment-length.js");
15+
1416
/**
1517
* @param {string[]} lines added lines, without the `+`
1618
* @returns {string[]} what the checker reports for them
@@ -141,8 +143,6 @@ describe("check-comment-length", () => {
141143
describe("over a real repository", () => {
142144
/** @type {string} */
143145
let dir;
144-
/** @type {string} */
145-
let cwd;
146146

147147
const git = (/** @type {string[]} */ args) =>
148148
execFileSync("git", args, { cwd: dir, encoding: "utf8" });
@@ -161,12 +161,9 @@ describe("check-comment-length", () => {
161161
"-qm",
162162
"base"
163163
]);
164-
cwd = process.cwd();
165-
process.chdir(dir);
166164
});
167165

168166
afterEach(() => {
169-
process.chdir(cwd);
170167
fs.rmSync(dir, { recursive: true, force: true });
171168
});
172169

@@ -175,36 +172,36 @@ describe("check-comment-length", () => {
175172
path.join(dir, "kept.js"),
176173
"// one\n// two\n// three\n"
177174
);
178-
expect(report("HEAD")).toEqual(["kept.js:2"]);
175+
expect(report("HEAD", dir)).toEqual(["kept.js:2"]);
179176
});
180177

181178
it("reads a file git does not track yet", () => {
182179
fs.writeFileSync(
183180
path.join(dir, "new.js"),
184181
"/* one\n two\n three */\n"
185182
);
186-
expect(report("HEAD")).toEqual(["new.js:1"]);
183+
expect(report("HEAD", dir)).toEqual(["new.js:1"]);
187184
});
188185

189186
it("reports nothing when every comment is short enough", () => {
190187
fs.appendFileSync(path.join(dir, "kept.js"), "// one\n// two\n");
191188
fs.writeFileSync(path.join(dir, "new.js"), "/* just the one */\n");
192-
expect(report("HEAD")).toEqual([]);
189+
expect(report("HEAD", dir)).toEqual([]);
193190
});
194191

195192
it("returns 1 and names each offender", () => {
196193
fs.writeFileSync(path.join(dir, "new.js"), "// one\n// two\n// three\n");
197194
/** @type {string[]} */
198195
const written = [];
199-
expect(main((text) => written.push(text), "HEAD")).toBe(1);
196+
expect(main((text) => written.push(text), "HEAD", dir)).toBe(1);
200197
expect(written).toEqual(["new.js:1: comment over 2 lines\n"]);
201198
});
202199

203200
it("returns 0 and writes nothing when the diff is clean", () => {
204201
fs.appendFileSync(path.join(dir, "kept.js"), "// one\n");
205202
/** @type {string[]} */
206203
const written = [];
207-
expect(main((text) => written.push(text), "HEAD")).toBe(0);
204+
expect(main((text) => written.push(text), "HEAD", dir)).toBe(0);
208205
expect(written).toEqual([]);
209206
});
210207

@@ -213,11 +210,11 @@ describe("check-comment-length", () => {
213210
let status = 0;
214211
let stderr = "";
215212
try {
216-
execFileSync(
217-
process.execPath,
218-
[path.join(cwd, "tooling/check-comment-length.js"), "HEAD"],
219-
{ cwd: dir, encoding: "utf8", stdio: "pipe" }
220-
);
213+
execFileSync(process.execPath, [SCRIPT, "HEAD"], {
214+
cwd: dir,
215+
encoding: "utf8",
216+
stdio: "pipe"
217+
});
221218
} catch (err) {
222219
status = /** @type {EXPECTED_ANY} */ (err).status;
223220
stderr = /** @type {EXPECTED_ANY} */ (err).stderr;
@@ -228,11 +225,11 @@ describe("check-comment-length", () => {
228225

229226
it("exits 0 and says nothing when the diff is clean", () => {
230227
fs.appendFileSync(path.join(dir, "kept.js"), "// one\n");
231-
const out = execFileSync(
232-
process.execPath,
233-
[path.join(cwd, "tooling/check-comment-length.js"), "HEAD"],
234-
{ cwd: dir, encoding: "utf8", stdio: "pipe" }
235-
);
228+
const out = execFileSync(process.execPath, [SCRIPT, "HEAD"], {
229+
cwd: dir,
230+
encoding: "utf8",
231+
stdio: "pipe"
232+
});
236233
expect(out).toBe("");
237234
});
238235
});

‎tooling/check-comment-length.js‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
const { execFileSync } = require("child_process");
1212
const fs = require("fs");
13+
const path = require("path");
1314

1415
const LIMIT = 2;
1516
const MAX_BUFFER = 1024 * 1024 * 256;
@@ -92,11 +93,12 @@ const untrackedDiff = (files, read) =>
9293

9394
/**
9495
* @param {string=} from base to diff against, defaulting to the merge base
96+
* @param {string=} cwd repository to read, defaulting to the current directory
9597
* @returns {string[]} one `<file>:<line>` per comment over the limit
9698
*/
97-
const report = (from) => {
99+
const report = (from, cwd) => {
98100
const run = (/** @type {string[]} */ args) =>
99-
execFileSync("git", args, { encoding: "utf8", maxBuffer: MAX_BUFFER });
101+
execFileSync("git", args, { encoding: "utf8", maxBuffer: MAX_BUFFER, cwd });
100102
const base = from || run(["merge-base", "HEAD", "origin/main"]).trim();
101103
const globs = ["*.js", "*.mjs", "*.cjs"];
102104
const untracked = run([
@@ -110,18 +112,19 @@ const report = (from) => {
110112
return overLimit(
111113
`${run(["diff", "-U0", base, "--", ...globs])}\n${untrackedDiff(
112114
untracked,
113-
(one) => fs.readFileSync(one, "utf8")
115+
(one) => fs.readFileSync(cwd ? path.join(cwd, one) : one, "utf8")
114116
)}`
115117
);
116118
};
117119

118120
/**
119121
* @param {(text: string) => void} write receives one line per offender
120122
* @param {string=} from base to diff against, defaulting to the merge base
123+
* @param {string=} cwd repository to read, defaulting to the current directory
121124
* @returns {number} the exit code, non-zero where a comment is over the limit
122125
*/
123-
const main = (write, from) => {
124-
const found = report(from);
126+
const main = (write, from, cwd) => {
127+
const found = report(from, cwd);
125128
for (const one of found) write(`${one}: comment over ${LIMIT} lines\n`);
126129
return found.length > 0 ? 1 : 0;
127130
};

0 commit comments

Comments
 (0)