-
Notifications
You must be signed in to change notification settings - Fork 86
fix: validate invalid blog post dates #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sameerchore
wants to merge
5
commits into
webpack:main
Choose a base branch
from
sameerchore:fix/validate-blog-post-dates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+222
−4
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8ca6967
changed to show proper error /error falback to locate the exact error…
sameerchore 5756658
Added some test over blog post invalid pattern datess to handle error…
sameerchore 907bf47
Added some test over diffrent date pattern causing error
sameerchore 10e985d
fix: allow valid ISO end-of-day blog dates
sameerchore 7da7888
fix: normalize offsetless blog timestamps as UTC
sameerchore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import { strict as assert } from 'node:assert'; | ||
| import { execFile } from 'node:child_process'; | ||
| import { | ||
| copyFile, | ||
| mkdtemp, | ||
| mkdir, | ||
| readFile, | ||
| rm, | ||
| writeFile, | ||
| } from 'node:fs/promises'; | ||
| import { promisify } from 'node:util'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { join } from 'node:path'; | ||
| import { test } from 'node:test'; | ||
|
|
||
| const execFileAsync = promisify(execFile); | ||
| const blogScript = fileURLToPath( | ||
| new URL('../../scripts/data/blog.mjs', import.meta.url) | ||
| ); | ||
|
|
||
| const createFixture = async content => { | ||
| const root = await mkdtemp(join('tests', 'data', '.blog-')); | ||
| await mkdir(join(root, 'scripts', 'data'), { recursive: true }); | ||
| await mkdir(join(root, 'pages', 'blog', 'posts'), { recursive: true }); | ||
| await mkdir(join(root, 'generated'), { recursive: true }); | ||
| await copyFile(blogScript, join(root, 'scripts', 'data', 'blog.mjs')); | ||
| await writeFile(join(root, 'pages', 'blog', 'posts', 'example.md'), content); | ||
| return root; | ||
| }; | ||
|
|
||
| const runFixture = (root, timeZone = 'UTC') => | ||
| execFileAsync(process.execPath, ['scripts/data/blog.mjs'], { | ||
| cwd: root, | ||
| env: { ...process.env, TZ: timeZone }, | ||
| }); | ||
|
|
||
| test('generates blog data for valid dates', async t => { | ||
| const root = await createFixture("---\ndate: '2026-09-22'\n---\n# Test\n"); | ||
| t.after(() => rm(root, { recursive: true, force: true })); | ||
|
|
||
| await runFixture(root); | ||
| const posts = JSON.parse( | ||
| await readFile(join(root, 'generated', 'blog.json'), 'utf8') | ||
| ); | ||
|
|
||
| assert.equal(posts[0].date, '2026-09-22T00:00:00.000Z'); | ||
| }); | ||
|
|
||
| test('normalizes offset-less quoted date-times as UTC', async t => { | ||
| const root = await createFixture( | ||
| "---\ndate: '2026-09-22 10:00'\n---\n# Test\n" | ||
| ); | ||
| t.after(() => rm(root, { recursive: true, force: true })); | ||
|
|
||
| const dates = []; | ||
| for (const timeZone of ['UTC', 'America/Los_Angeles']) { | ||
| await runFixture(root, timeZone); | ||
| const posts = JSON.parse( | ||
| await readFile(join(root, 'generated', 'blog.json'), 'utf8') | ||
| ); | ||
| dates.push(posts[0].date); | ||
| } | ||
|
|
||
| assert.deepEqual(dates, [ | ||
| '2026-09-22T10:00:00.000Z', | ||
| '2026-09-22T10:00:00.000Z', | ||
| ]); | ||
| }); | ||
|
|
||
| test('reports the file for an invalid blog date', async t => { | ||
| const root = await createFixture('---\ndate: not-a-date\n---\n# Test\n'); | ||
| t.after(() => rm(root, { recursive: true, force: true })); | ||
|
|
||
| await assert.rejects(runFixture(root), error => { | ||
| assert.match(error.stderr, /InvalidBlogDateError: Invalid blog date/); | ||
| assert.match(error.stderr, /pages[\\/]blog[\\/]posts[\\/]example\.md/); | ||
| assert.match(error.stderr, /not-a-date/); | ||
| return true; | ||
| }); | ||
| }); | ||
|
|
||
| test('rejects nonexistent calendar dates', async t => { | ||
| const root = await createFixture('---\ndate: 2026-02-30\n---\n# Test\n'); | ||
| t.after(() => rm(root, { recursive: true, force: true })); | ||
|
|
||
| await assert.rejects(runFixture(root), error => { | ||
| assert.match(error.stderr, /Invalid blog date/); | ||
| assert.match(error.stderr, /2026-02-30/); | ||
| return true; | ||
| }); | ||
| }); | ||
|
|
||
| test('rejects one-digit dates that roll over', async t => { | ||
| const root = await createFixture( | ||
| '---\ndate: 2024-2-30 10:00:00\n---\n# Test\n' | ||
| ); | ||
| t.after(() => rm(root, { recursive: true, force: true })); | ||
|
|
||
| await assert.rejects(runFixture(root), error => { | ||
| assert.match(error.stderr, /Invalid blog date/); | ||
| assert.match(error.stderr, /2024-2-30/); | ||
| return true; | ||
| }); | ||
| }); | ||
|
|
||
| test('rejects out-of-range time components', async t => { | ||
| const root = await createFixture( | ||
| '---\ndate: 2024-01-01 25:60:60\n---\n# Test\n' | ||
| ); | ||
| t.after(() => rm(root, { recursive: true, force: true })); | ||
|
|
||
| await assert.rejects(runFixture(root), error => { | ||
| assert.match(error.stderr, /Invalid blog date/); | ||
| assert.match(error.stderr, /25:60:60/); | ||
| return true; | ||
| }); | ||
| }); | ||
|
|
||
| test('accepts ISO end-of-day timestamps', async t => { | ||
| for (const value of ['2026-09-22T24:00:00Z', '2026-09-22 24:00']) { | ||
| const root = await createFixture(`---\ndate: '${value}'\n---\n# Test\n`); | ||
| t.after(() => rm(root, { recursive: true, force: true })); | ||
|
|
||
| await runFixture(root); | ||
| } | ||
| }); | ||
|
|
||
| test('rejects nonzero end-of-day time components', async t => { | ||
| const root = await createFixture( | ||
| "---\ndate: '2026-09-22T24:00:01Z'\n---\n# Test\n" | ||
| ); | ||
| t.after(() => rm(root, { recursive: true, force: true })); | ||
|
|
||
| await assert.rejects(runFixture(root), error => { | ||
| assert.match(error.stderr, /Invalid blog date/); | ||
| assert.match(error.stderr, /24:00:01/); | ||
| return true; | ||
| }); | ||
| }); | ||
|
|
||
| test('reports the file for a missing blog date', async t => { | ||
| const root = await createFixture('---\ntitle: Test\n---\n# Test\n'); | ||
| t.after(() => rm(root, { recursive: true, force: true })); | ||
|
|
||
| await assert.rejects(runFixture(root), error => { | ||
| assert.match(error.stderr, /Invalid blog date/); | ||
| assert.match(error.stderr, /example\.md/); | ||
| assert.match(error.stderr, /<missing>/); | ||
| return true; | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.