Repository navigation
feat(scripts): validate-urls takes a file list #951
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
+199
−43
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,92 @@ | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| // The file-selection half of the script is what is under test, and nothing | ||
| // here names a URL. The guard is stubbed anyway, so that a regression that | ||
| // reached the network would fail here instead of making a request. | ||
| vi.mock("../lib/url-guard.js", () => ({ | ||
| fetchPublic: vi.fn(() => Promise.reject(new Error("network disabled in tests"))), | ||
| isPrivateDestinationError: () => false, | ||
| })); | ||
|
|
||
| import { parseArgs, resolveListedFiles } from "../validate-urls.js"; | ||
|
|
||
| describe("parseArgs", () => { | ||
| it("reads --files and leaves the git lane off", () => { | ||
| expect(parseArgs(["--files", "changed.txt"])).toMatchObject({ | ||
| changedOnly: false, | ||
| filesList: "changed.txt", | ||
| }); | ||
| expect(parseArgs(["--files", "-"]).filesList).toBe("-"); | ||
| }); | ||
|
|
||
| it("refuses --files together with --changed-only", () => { | ||
| expect(() => parseArgs(["--changed-only", "--base", "abc1234", "--files", "x"])).toThrow( | ||
| /mutually exclusive/ | ||
| ); | ||
| }); | ||
|
|
||
| it("still requires --base with --changed-only", () => { | ||
| expect(() => parseArgs(["--changed-only"])).toThrow(/--base/); | ||
| }); | ||
| }); | ||
|
|
||
| describe("resolveListedFiles", () => { | ||
| const dirs: string[] = []; | ||
| afterEach(() => { | ||
| for (const dir of dirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true }); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
| const tmpRoot = (): string => { | ||
| const dir = fs.mkdtempSync(path.join(os.tmpdir(), "catalog-validate-urls-")); | ||
| dirs.push(dir); | ||
| const root = path.join(dir, "data"); | ||
| fs.mkdirSync(path.join(root, "software"), { recursive: true }); | ||
| return root; | ||
| }; | ||
|
|
||
| it("resolves only the listed files, skipping blank lines", () => { | ||
| const root = tmpRoot(); | ||
| const listed = path.join(root, "software", "a.yaml"); | ||
| const unlisted = path.join(root, "software", "b.yaml"); | ||
| fs.writeFileSync(listed, "name: A\n"); | ||
| fs.writeFileSync(unlisted, "name: B\n"); | ||
|
|
||
| expect(resolveListedFiles(`${listed}\n\n`, root)).toEqual([fs.realpathSync(listed)]); | ||
| }); | ||
|
|
||
| it("refuses a path that leaves the root and never reads it", () => { | ||
| const root = tmpRoot(); | ||
| const outside = path.join(root, "..", "outside.yaml"); | ||
| fs.writeFileSync(outside, "url: https://example.com\n"); | ||
| const stderr = vi.spyOn(console, "error").mockImplementation(() => {}); | ||
|
|
||
| expect( | ||
| resolveListedFiles(`${path.join(root, "software", "..", "..", "outside.yaml")}\n`, root) | ||
| ).toEqual([]); | ||
| expect(stderr).toHaveBeenCalledWith(expect.stringMatching(/outside\.yaml.*is outside/)); | ||
| }); | ||
|
|
||
| it("refuses a symlink inside the root", () => { | ||
| const root = tmpRoot(); | ||
| const link = path.join(root, "software", "x.yaml"); | ||
| fs.symlinkSync("/dev/null", link); | ||
| const stderr = vi.spyOn(console, "error").mockImplementation(() => {}); | ||
|
|
||
| expect(resolveListedFiles(link, root)).toEqual([]); | ||
| expect(stderr).toHaveBeenCalledWith(expect.stringMatching(/is a symlink/)); | ||
| }); | ||
|
|
||
| it("reports a listed file that does not exist unless told not to", () => { | ||
| const root = tmpRoot(); | ||
| const missing = path.join(root, "software", "gone.yaml"); | ||
| const stderr = vi.spyOn(console, "error").mockImplementation(() => {}); | ||
|
|
||
| expect(resolveListedFiles(missing, root)).toEqual([]); | ||
| expect(stderr).toHaveBeenCalledTimes(1); | ||
| expect(resolveListedFiles(missing, root, { ignoreMissing: true })).toEqual([]); | ||
| expect(stderr).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.