diff --git a/pr-checks/changelog/validate.test.mts b/pr-checks/changelog/validate.test.mts index ea07d93cc..dc2ab87d6 100644 --- a/pr-checks/changelog/validate.test.mts +++ b/pr-checks/changelog/validate.test.mts @@ -1,34 +1,16 @@ import assert from "node:assert/strict"; -import * as fs from "node:fs"; -import * as os from "node:os"; -import * as path from "node:path"; import { describe, it } from "node:test"; +import { withTmpFile } from "../../src/util.js"; + import { + hasValidChangenoteCategory, isValidChangenoteContent, isValidChangenoteFile, isValidChangenoteFilename, - hasValidChangenoteCategory, VALID_CHANGE_NOTE_CATEGORIES, } from "./validate.mjs"; -async function withTmpFile( - baseFileName: string, - contents: string, - body: (filePath: string) => Promise | T, -): Promise { - const tmpDir = fs.mkdtempSync( - path.join(os.tmpdir(), "changetool-validate-test-"), - ); - try { - const filePath = path.join(tmpDir, baseFileName); - fs.writeFileSync(filePath, contents); - return await body(filePath); - } finally { - fs.rmSync(tmpDir, { recursive: true, force: true }); - } -} - await describe("isValidChangenoteContent", async () => { await it("recognizes an unordered Markdown list", () => { const inputs = [ diff --git a/src/util.ts b/src/util.ts index f6258b285..574741b83 100644 --- a/src/util.ts +++ b/src/util.ts @@ -89,6 +89,24 @@ export async function withTmpDir( return result; } +// Creates a random temporary file, runs the given body, and then deletes the file. +export async function withTmpFile( + baseFileName: string, + contents: string, + body: (filePath: string) => Promise | T, +): Promise { + const tmpDir = fs.mkdtempSync( + path.join(os.tmpdir(), "changetool-validate-test-"), + ); + try { + const filePath = path.join(tmpDir, baseFileName); + fs.writeFileSync(filePath, contents); + return await body(filePath); + } finally { + fs.rmSync(tmpDir, { recursive: true, force: true }); + } +} + /** * Gets an OS-specific amount of memory (in MB) to reserve for OS processes * when the user doesn't explicitly specify a memory setting.