Skip to content

Commit 4b20b2c

Browse files
committed
Extend changetool to support directories
1 parent b1c1bbb commit 4b20b2c

3 files changed

Lines changed: 86 additions & 10 deletions

File tree

‎pr-checks/changelog/validate.mts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function hasValidChangenoteCategory(
8484
* @param filename The name of the change-note file to validate.
8585
* @returns True if the file is a valid change-note, false otherwise.
8686
*/
87-
export function isValidChangenoteFile(filename: string): boolean {
87+
function isValidChangenoteFile(filename: string): boolean {
8888
let isValid: boolean = true;
8989

9090
let fileData: string | undefined;
@@ -119,3 +119,31 @@ export function isValidChangenoteFile(filename: string): boolean {
119119

120120
return isValid;
121121
}
122+
123+
/**
124+
* Validates that the given path is a valid change-note file or directory of change-note files.
125+
* @param filepath The path to the change-note file or directory to validate.
126+
* @returns True if the path is valid, false otherwise.
127+
*/
128+
export function isValidChangenoteFileOrDir(filepath: string): boolean {
129+
try {
130+
const stats = fs.statSync(filepath);
131+
if (stats.isFile()) {
132+
return isValidChangenoteFile(filepath);
133+
}
134+
if (stats.isDirectory()) {
135+
return fs
136+
.readdirSync(filepath)
137+
.filter((f) => f !== ".gitkeep")
138+
.reduce(
139+
(r, f) => r && isValidChangenoteContent(path.join(filepath, f)),
140+
true,
141+
);
142+
}
143+
console.error(`${filepath}: not a file or directory`);
144+
return false;
145+
} catch (error) {
146+
console.error(`${filepath}: failed to read file or directory`, error);
147+
return false;
148+
}
149+
}

‎pr-checks/changelog/validate.test.mts‎

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import assert from "node:assert/strict";
2+
import * as fs from "node:fs";
3+
import * as path from "node:path";
24
import { describe, it } from "node:test";
35

4-
import { withTmpFile } from "../../src/util";
6+
import { withTmpDir, withTmpFile } from "../../src/util";
57

68
import {
79
hasValidChangenoteCategory,
810
isValidChangenoteContent,
9-
isValidChangenoteFile,
11+
isValidChangenoteFileOrDir,
1012
isValidChangenoteFilename,
1113
VALID_CHANGE_NOTE_CATEGORIES,
1214
} from "./validate.mjs";
@@ -139,23 +141,55 @@ await describe("hasValidChangenoteCategory", async () => {
139141
});
140142
});
141143

142-
await describe("isValidChangenoteFile", async () => {
144+
await describe("isValidChangenoteFileOrDir", async () => {
143145
await it("accepts a valid change-note file", async () => {
144146
await withTmpFile(
145147
"2026-01-01-fix-bug.md",
146148
"---\ncategory: fix\n---\n- Fixed a bug\n",
147149
(filePath) => {
148-
assert.equal(isValidChangenoteFile(filePath), true);
150+
assert.equal(isValidChangenoteFileOrDir(filePath), true);
149151
},
150152
);
151153
});
152154

155+
await it("accepts a directory of valid change-note files", async () => {
156+
await withTmpDir(async (tmpDir) => {
157+
fs.writeFileSync(
158+
path.join(tmpDir, "2026-01-01-fix-bug.md"),
159+
"---\ncategory: fix\n---\n- Fixed a bug\n",
160+
);
161+
fs.writeFileSync(
162+
path.join(tmpDir, "2026-01-02-add-feature.md"),
163+
"---\ncategory: feature\n---\n- Added a feature\n",
164+
);
165+
assert.equal(isValidChangenoteFileOrDir(tmpDir), true);
166+
});
167+
});
168+
169+
await it("accepts an empty directory", async () => {
170+
await withTmpDir(async (tmpDir) => {
171+
assert.equal(isValidChangenoteFileOrDir(tmpDir), true);
172+
});
173+
});
174+
175+
await it("accepts a directory of .gitkeep only", async () => {
176+
await withTmpFile(".gitkeep", "", async (filePath) => {
177+
const dirPath = path.dirname(filePath);
178+
assert.equal(isValidChangenoteFileOrDir(dirPath), true);
179+
});
180+
});
181+
182+
await it("rejects a non-existent path", async () => {
183+
assert.equal(isValidChangenoteFileOrDir("non-existent-file.md"), false);
184+
assert.equal(isValidChangenoteFileOrDir("this-dir-does-not-exist"), false);
185+
});
186+
153187
await it("rejects invalid filename", async () => {
154188
await withTmpFile(
155189
"fix-bug.md",
156190
"---\ncategory: fix\n---\n- Fixed a bug\n",
157191
(filePath) => {
158-
assert.equal(isValidChangenoteFile(filePath), false);
192+
assert.equal(isValidChangenoteFileOrDir(filePath), false);
159193
},
160194
);
161195
});
@@ -165,7 +199,7 @@ await describe("isValidChangenoteFile", async () => {
165199
"2026-01-01-fix-bug.md",
166200
"- Fixed a bug\n",
167201
(filePath) => {
168-
assert.equal(isValidChangenoteFile(filePath), false);
202+
assert.equal(isValidChangenoteFileOrDir(filePath), false);
169203
},
170204
);
171205
});
@@ -175,8 +209,22 @@ await describe("isValidChangenoteFile", async () => {
175209
"2026-01-01-fix-bug.md",
176210
"---\ncategory: fix\n---\n* Fixed a bug\n",
177211
(filePath) => {
178-
assert.equal(isValidChangenoteFile(filePath), false);
212+
assert.equal(isValidChangenoteFileOrDir(filePath), false);
179213
},
180214
);
181215
});
216+
217+
await it("rejects directory with one invalid change-note file", async () => {
218+
await withTmpDir(async (tmpDir) => {
219+
fs.writeFileSync(
220+
path.join(tmpDir, "2026-01-01-fix-bug.md"),
221+
"---\ncategory: fix\n---\n- Fixed a bug\n",
222+
);
223+
fs.writeFileSync(
224+
path.join(tmpDir, "2026-01-02-wrong-category.md"),
225+
"---\ncategory: foobar\n---\n- Added a feature\n",
226+
);
227+
assert.equal(isValidChangenoteFileOrDir(tmpDir), false);
228+
});
229+
});
182230
});

‎pr-checks/changenotes.mts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { pathToFileURL } from "node:url";
44
import { parseArgs } from "node:util";
55

6-
import { isValidChangenoteFile } from "./changelog/validate.mjs";
6+
import { isValidChangenoteFileOrDir } from "./changelog/validate.mjs";
77

88
const entryPoint = process.argv[1];
99
if (entryPoint && import.meta.url === pathToFileURL(entryPoint).href) {
@@ -45,7 +45,7 @@ function validate(paths: string[]): number {
4545
return 1;
4646
}
4747
for (const path of paths) {
48-
if (!isValidChangenoteFile(path)) {
48+
if (!isValidChangenoteFileOrDir(path)) {
4949
valid = false;
5050
}
5151
}

0 commit comments

Comments
 (0)