From 1c392823f0bdb01a649eb58b0f84cbcb68b15147 Mon Sep 17 00:00:00 2001 From: okwn Date: Thu, 21 May 2026 01:17:59 +0000 Subject: [PATCH 1/2] test: add Jest tests for validate-filepaths --- .../__tests__/validate.test.js | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 .github/script/validate-filepaths/__tests__/validate.test.js diff --git a/.github/script/validate-filepaths/__tests__/validate.test.js b/.github/script/validate-filepaths/__tests__/validate.test.js new file mode 100644 index 00000000000..7907f243270 --- /dev/null +++ b/.github/script/validate-filepaths/__tests__/validate.test.js @@ -0,0 +1,92 @@ +/** + * Tests for validate-filepaths main script logic + */ + +describe("isFileInsideAYearFolder", () => { + // This function is defined in index.js - we test the logic it's testing + + function isFileInsideAYearFolder(filepath) { + return filepath.match(/^\d{4}/) !== null; + } + + test("returns true for file in year folder", () => { + expect(isFileInsideAYearFolder("2024/2024-01-01-brand.markdown")).toBe(true); + }); + + test("returns true for file in year/month subfolder", () => { + expect(isFileInsideAYearFolder("2024/01/2024-01-01-brand.markdown")).toBe(true); + }); + + test("returns false for file not in year folder", () => { + expect(isFileInsideAYearFolder("README.md")).toBe(false); + }); + + test("returns false for file with non-year folder", () => { + expect(isFileInsideAYearFolder("foo/2024-01-01-brand.markdown")).toBe(false); + }); +}); + +describe("filepath date validation pattern", () => { + // Tests for the YYYY-MM-DD brand.markdown naming pattern + + function isFilepathDateValid(filepath) { + const filename = filepath.split("/").pop(); + const dateStringInFilename = filename.match(/\d{4}-\d{2}-\d{2}/); + return dateStringInFilename !== null; + } + + test("validates correct YYYY-MM-DD format in filename", () => { + expect(isFilepathDateValid("2024/01/2024-01-15-brand.markdown")).toBe(true); + expect(isFilepathDateValid("2023/12/2023-12-31-company.markdown")).toBe(true); + }); + + test("invalidates incorrect date formats", () => { + expect(isFilepathDateValid("2024/1-01-15-brand.markdown")).toBe(false); + expect(isFilepathDateValid("2024/01/24-01-15-brand.markdown")).toBe(false); + expect(isFilepathDateValid("2024/01/2024-1-15-brand.markdown")).toBe(false); + expect(isFilepathDateValid("2024/01/2024-01-5-brand.markdown")).toBe(false); + }); + + test("invalidates filenames without date", () => { + expect(isFilepathDateValid("README.md")).toBe(false); + expect(isFilepathDateValid("2024/01/brand.markdown")).toBe(false); + }); +}); + +describe("filepath structure validation", () => { + // Tests for the overall filepath structure (year folder, optional month folder, filename) + + function isFileInsideAYearFolder(filepath) { + return filepath.match(/^\d{4}/) !== null; + } + + function isFilepathDateValid(filepath) { + const filename = filepath.split("/").pop(); + const dateStringInFilename = filename.match(/\d{4}-\d{2}-\d{2}/); + return dateStringInFilename !== null; + } + + test("correct structure with year-only folder", () => { + const validPath = "2024/2024-06-15-brand.markdown"; + expect(isFileInsideAYearFolder(validPath)).toBe(true); + expect(isFilepathDateValid(validPath)).toBe(true); + }); + + test("correct structure with year/month folders", () => { + const validPath = "2024/06/2024-06-15-brand.markdown"; + expect(isFileInsideAYearFolder(validPath)).toBe(true); + expect(isFilepathDateValid(validPath)).toBe(true); + }); + + test("incorrect structure with missing year folder", () => { + const invalidPath = "brand/2024-06-15-brand.markdown"; + expect(isFileInsideAYearFolder(invalidPath)).toBe(false); + expect(isFilepathDateValid(invalidPath)).toBe(true); + }); + + test("incorrect structure with non-matching date in filename", () => { + const invalidPath = "2024/06/2023-06-15-brand.markdown"; + expect(isFileInsideAYearFolder(invalidPath)).toBe(true); + expect(isFilepathDateValid(invalidPath)).toBe(true); + }); +}); \ No newline at end of file From 6f1b4489589dcf84a94493f4a01893b6a3dc5768 Mon Sep 17 00:00:00 2001 From: OKWN Date: Thu, 21 May 2026 09:50:06 +0300 Subject: [PATCH 2/2] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../__tests__/validate.test.js | 99 +++++++++++++------ 1 file changed, 67 insertions(+), 32 deletions(-) diff --git a/.github/script/validate-filepaths/__tests__/validate.test.js b/.github/script/validate-filepaths/__tests__/validate.test.js index 7907f243270..89764eb19e0 100644 --- a/.github/script/validate-filepaths/__tests__/validate.test.js +++ b/.github/script/validate-filepaths/__tests__/validate.test.js @@ -2,54 +2,89 @@ * Tests for validate-filepaths main script logic */ -describe("isFileInsideAYearFolder", () => { - // This function is defined in index.js - we test the logic it's testing +function runValidateFilepaths(changedFiles) { + jest.resetModules(); + + process.env.CHANGED_FILES = changedFiles.join("\n"); + + const core = { + getInput: jest.fn((name) => (name === "changed-files" ? process.env.CHANGED_FILES : "")), + setFailed: jest.fn(), + info: jest.fn(), + warning: jest.fn(), + error: jest.fn(), + setOutput: jest.fn(), + }; + + jest.doMock("@actions/core", () => core); + jest.doMock("fs", () => ({ + existsSync: jest.fn(() => true), + statSync: jest.fn(() => ({ isDirectory: () => false })), + readdirSync: jest.fn(() => []), + readFileSync: jest.fn(() => ""), + })); + + jest.isolateModules(() => { + require("../index.js"); + }); - function isFileInsideAYearFolder(filepath) { - return filepath.match(/^\d{4}/) !== null; - } + return core; +} - test("returns true for file in year folder", () => { - expect(isFileInsideAYearFolder("2024/2024-01-01-brand.markdown")).toBe(true); +afterEach(() => { + delete process.env.CHANGED_FILES; + jest.resetModules(); + jest.clearAllMocks(); + jest.unmock("@actions/core"); + jest.unmock("fs"); +}); + +describe("isFileInsideAYearFolder", () => { + test("accepts a file in a year folder", () => { + const core = runValidateFilepaths(["2024/2024-01-01-brand.markdown"]); + + expect(core.setFailed).not.toHaveBeenCalled(); }); - test("returns true for file in year/month subfolder", () => { - expect(isFileInsideAYearFolder("2024/01/2024-01-01-brand.markdown")).toBe(true); + test("accepts a file in a year/month subfolder", () => { + const core = runValidateFilepaths(["2024/01/2024-01-01-brand.markdown"]); + + expect(core.setFailed).not.toHaveBeenCalled(); }); - test("returns false for file not in year folder", () => { - expect(isFileInsideAYearFolder("README.md")).toBe(false); + test("rejects a file not in a year folder", () => { + const core = runValidateFilepaths(["README.md"]); + + expect(core.setFailed).toHaveBeenCalled(); }); - test("returns false for file with non-year folder", () => { - expect(isFileInsideAYearFolder("foo/2024-01-01-brand.markdown")).toBe(false); + test("rejects a file with a non-year top-level folder", () => { + const core = runValidateFilepaths(["foo/2024-01-01-brand.markdown"]); + + expect(core.setFailed).toHaveBeenCalled(); }); }); describe("filepath date validation pattern", () => { - // Tests for the YYYY-MM-DD brand.markdown naming pattern - - function isFilepathDateValid(filepath) { - const filename = filepath.split("/").pop(); - const dateStringInFilename = filename.match(/\d{4}-\d{2}-\d{2}/); - return dateStringInFilename !== null; - } - - test("validates correct YYYY-MM-DD format in filename", () => { - expect(isFilepathDateValid("2024/01/2024-01-15-brand.markdown")).toBe(true); - expect(isFilepathDateValid("2023/12/2023-12-31-company.markdown")).toBe(true); + test("accepts correct YYYY-MM-DD format in filename", () => { + expect( + runValidateFilepaths(["2024/01/2024-01-15-brand.markdown"]).setFailed + ).not.toHaveBeenCalled(); + expect( + runValidateFilepaths(["2023/12/2023-12-31-company.markdown"]).setFailed + ).not.toHaveBeenCalled(); }); - test("invalidates incorrect date formats", () => { - expect(isFilepathDateValid("2024/1-01-15-brand.markdown")).toBe(false); - expect(isFilepathDateValid("2024/01/24-01-15-brand.markdown")).toBe(false); - expect(isFilepathDateValid("2024/01/2024-1-15-brand.markdown")).toBe(false); - expect(isFilepathDateValid("2024/01/2024-01-5-brand.markdown")).toBe(false); + test("rejects incorrect date formats", () => { + expect(runValidateFilepaths(["2024/1-01-15-brand.markdown"]).setFailed).toHaveBeenCalled(); + expect(runValidateFilepaths(["2024/01/24-01-15-brand.markdown"]).setFailed).toHaveBeenCalled(); + expect(runValidateFilepaths(["2024/01/2024-1-15-brand.markdown"]).setFailed).toHaveBeenCalled(); + expect(runValidateFilepaths(["2024/01/2024-01-5-brand.markdown"]).setFailed).toHaveBeenCalled(); }); - test("invalidates filenames without date", () => { - expect(isFilepathDateValid("README.md")).toBe(false); - expect(isFilepathDateValid("2024/01/brand.markdown")).toBe(false); + test("rejects filenames without date", () => { + expect(runValidateFilepaths(["README.md"]).setFailed).toHaveBeenCalled(); + expect(runValidateFilepaths(["2024/01/brand.markdown"]).setFailed).toHaveBeenCalled(); }); });