-
Notifications
You must be signed in to change notification settings - Fork 0
Add RSS feed XML validation and fix undeclared itunes namespace #49
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
Merged
Merged
Changes from all commits
Commits
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
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
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,70 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /** | ||
| * Validates the generated RSS feed (dist/rss.xml) for XML well-formedness and | ||
| * namespace correctness using xmllint. | ||
| * | ||
| * Why wrap xmllint instead of calling it directly? | ||
| * xmllint treats an undeclared namespace prefix — e.g. <itunes:image> with no | ||
| * matching xmlns:itunes on the <rss> root — as a *recoverable* error: it prints | ||
| * a "namespace error" to stderr but still exits 0. A bare `xmllint --noout` | ||
| * would therefore happily pass the exact bug this check exists to catch. So we | ||
| * treat ANY stderr output (or a non-zero exit) as a validation failure. | ||
| * | ||
| * Usage: | ||
| * node scripts/validate-feed.js [path-to-xml] # defaults to dist/rss.xml | ||
| */ | ||
|
|
||
| import { existsSync } from "fs"; | ||
| import { spawnSync } from "child_process"; | ||
| import path from "path"; | ||
| import { fileURLToPath } from "url"; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
|
|
||
| // Allow an explicit path argument (handy for tests); default to the built feed. | ||
| const feedPath = process.argv[2] | ||
| ? path.resolve(process.argv[2]) | ||
| : path.join(__dirname, "..", "dist", "rss.xml"); | ||
|
|
||
| const relFeed = path.relative(process.cwd(), feedPath); | ||
|
|
||
| console.log("🔍 Validating RSS feed XML...\n"); | ||
|
|
||
| // 1. The feed has to have been built first. | ||
| if (!existsSync(feedPath)) { | ||
| console.error(`❌ Feed not found: ${relFeed}`); | ||
| console.error(" Run \"npm run build\" first (or pass a path to an XML file)."); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| // 2. xmllint has to be available. | ||
| const probe = spawnSync("xmllint", ["--version"], { stdio: "ignore" }); | ||
| if (probe.error || probe.status !== 0) { | ||
| console.error("❌ xmllint not found on PATH."); | ||
| console.error(" macOS ships it by default; on Debian/Ubuntu install it with"); | ||
| console.error(" \"sudo apt-get install -y libxml2-utils\"."); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| // 3. Validate. --nonet keeps the check hermetic (never fetch over the network). | ||
| const result = spawnSync("xmllint", ["--noout", "--nonet", feedPath], { | ||
| encoding: "utf8", | ||
| }); | ||
|
|
||
| const stderr = (result.stderr || "").trim(); | ||
|
|
||
| // xmllint exits 0 on recoverable namespace errors, so fail on ANY stderr too. | ||
| if (result.status !== 0 || stderr) { | ||
| console.error(`❌ Feed validation failed for ${relFeed}:\n`); | ||
| if (stderr) { | ||
| console.error(stderr); | ||
| } else { | ||
| console.error(`xmllint exited with status ${result.status}.`); | ||
| } | ||
| console.error("\nThe feed is malformed or uses an undeclared XML namespace."); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| console.log(`✅ ${relFeed} is well-formed and all XML namespaces are declared.`); |
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,68 @@ | ||
| import { spawnSync } from "child_process"; | ||
| import { afterEach, describe, expect, it } from "vitest"; | ||
| import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "fs"; | ||
| import { tmpdir } from "os"; | ||
| import path from "path"; | ||
| import { fileURLToPath } from "url"; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
| const validatorPath = path.join(__dirname, "validate-feed.js"); | ||
| const tempDirs: string[] = []; | ||
|
|
||
| function makeTempDir(): string { | ||
| const tempDir = mkdtempSync(path.join(tmpdir(), "validate-feed-")); | ||
| tempDirs.push(tempDir); | ||
| return tempDir; | ||
| } | ||
|
|
||
| function writeExecutable(filePath: string, contents: string): void { | ||
| writeFileSync(filePath, contents); | ||
| chmodSync(filePath, 0o755); | ||
| } | ||
|
|
||
| function runValidator(feedPath: string, binDir: string) { | ||
| return spawnSync(process.execPath, [validatorPath, feedPath], { | ||
| encoding: "utf8", | ||
| env: { | ||
| ...process.env, | ||
| PATH: `${binDir}${path.delimiter}${process.env.PATH ?? ""}`, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| describe("validate-feed", () => { | ||
| afterEach(() => { | ||
| for (const tempDir of tempDirs) { | ||
| rmSync(tempDir, { recursive: true, force: true }); | ||
| } | ||
| tempDirs.length = 0; | ||
| }); | ||
|
|
||
| it("fails when xmllint exits 0 but reports a namespace error on stderr", () => { | ||
| const tempDir = makeTempDir(); | ||
| const binDir = path.join(tempDir, "bin"); | ||
| const feedPath = path.join(tempDir, "rss.xml"); | ||
| const xmllintPath = path.join(binDir, "xmllint"); | ||
|
|
||
| mkdirSync(binDir); | ||
| writeFileSync(feedPath, "<rss><channel><itunes:image /></channel></rss>"); | ||
| writeExecutable( | ||
| xmllintPath, | ||
| `#!/usr/bin/env node | ||
| if (process.argv.includes("--version")) { | ||
| process.exit(0); | ||
| } | ||
|
|
||
| console.error("namespace error : Namespace prefix itunes on image is not defined"); | ||
| process.exit(0); | ||
| `, | ||
| ); | ||
|
|
||
| const result = runValidator(feedPath, binDir); | ||
|
|
||
| expect(result.status).toBe(1); | ||
| expect(result.stderr).toContain("namespace error"); | ||
| expect(result.stderr).toContain("undeclared XML namespace"); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| // Unit tests live next to the code they cover as `*.test.ts` under `src/`. | ||
| // Unit tests live next to the code they cover as `*.test.ts` under `src/` | ||
| // or `scripts/`. | ||
| // The `tests/` directory is reserved for Playwright (`*.spec.ts`) QA suites, | ||
| // so we scope Vitest to `src/` to avoid the two runners fighting over files. | ||
| export default defineConfig({ | ||
| test: { | ||
| include: ["src/**/*.test.ts"], | ||
| include: ["src/**/*.test.ts", "scripts/**/*.test.ts"], | ||
| }, | ||
| }); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
ubuntu-latestjob used by both deploy and PR validation, this new step invokesscripts/validate-feed.jswithout first installingxmllint; the validator exits 1 whenxmllintis missing, and the current GitHub-hosted Ubuntu 24.04 runner software list does not includelibxml2-utils/xmllint(also reproduced in this clean Ubuntu environment). This makes every build fail at RSS validation unless the workflow installslibxml2-utilsbefore running this command.Useful? React with 👍 / 👎.