11import assert from "node:assert/strict" ;
2+ import * as fs from "node:fs" ;
3+ import * as path from "node:path" ;
24import { describe , it } from "node:test" ;
35
4- import { withTmpFile } from "../../src/util" ;
6+ import { withTmpDir , withTmpFile } from "../../src/util" ;
57
68import {
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} ) ;
0 commit comments