test: add Jest tests for validate-filepaths#20615
Open
okwn wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a Jest test suite for the .github/script/validate-filepaths tooling intended to validate DMCA notice filepath conventions.
Changes:
- Added a new Jest test file covering year-folder detection and
YYYY-MM-DDdate substring matching in filenames. - Added basic “structure” scenarios combining the above checks.
Comments suppressed due to low confidence (3)
.github/script/validate-filepaths/tests/validate.test.js:36
isFilepathDateValidis redefined here rather than importing the real implementation fromutils/validators.js. That means changes to the actual validator won’t be caught by these tests. Importing the exported function will make this test suite a true regression check for the repo’s validation behavior.
function isFilepathDateValid(filepath) {
const filename = filepath.split("/").pop();
const dateStringInFilename = filename.match(/\d{4}-\d{2}-\d{2}/);
return dateStringInFilename !== null;
}
.github/script/validate-filepaths/tests/validate.test.js:67
- The helper functions are duplicated across describe blocks (
isFileInsideAYearFolderandisFilepathDateValidare declared twice). Once you switch to importing production code, this duplication will go away; otherwise, consider defining these helpers once at the top-level to avoid divergence between blocks.
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;
}
.github/script/validate-filepaths/tests/validate.test.js:91
- The last two tests are labeled as “structure validation”, but they only assert
isFilepathDateValidis true (even when the year/month folders don’t match the filename date). If the intent is to validate structure, these should assert against the realisFileInCorrectFoldervalidator (fromutils/validators.js) so the tests actually catch mis-foldered notices.
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);
});
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+5
to
+52
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds Jest tests for the validate-filepaths script, covering file path validation logic for the YYYY-MM-DD-brand.markdown naming pattern.