-
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: refuse to deploy a dist that names files it never built #57
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
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
Binary file not shown.
Binary file not shown.
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,116 @@ | ||
| #!/usr/bin/env python3 | ||
| """Checks the deploy guard that keeps a half-built dist/ off production. | ||
|
|
||
| The FTP mirror prunes any remote file with no local counterpart, so deploying a | ||
| dist/index.html that names a bundle nobody built uploads the broken HTML *and* | ||
| deletes the bundle currently serving the site. dist/index.html is tracked while | ||
| dist/assets/ is gitignored, so a fresh clone reaches that state just by | ||
| skipping `npm run build`. | ||
|
|
||
| Run: python3 scripts/test-deploy-guard.py | ||
| """ | ||
| import importlib.util | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parent.parent | ||
|
|
||
|
|
||
| def load_deploy_module(): | ||
| spec = importlib.util.spec_from_file_location( | ||
| "deploy_ftp", REPO_ROOT / "scripts" / "deploy_ftp.py", | ||
| ) | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| return module | ||
|
|
||
|
|
||
| def main(): | ||
| deploy = load_deploy_module() | ||
| built_html = (deploy.DIST / "index.html").read_text(encoding="utf-8") | ||
|
|
||
| cases = [ | ||
| ( | ||
| "the committed dist references only files that exist", | ||
| built_html, | ||
| [], | ||
| ), | ||
| ( | ||
| "a bundle hash with no built file is caught", | ||
| built_html.replace("assets/index-", "assets/index-absent-"), | ||
| None, # asserted below - the real hash is not known here | ||
| ), | ||
| ( | ||
| "external, protocol-relative, data and anchor refs are ignored", | ||
| '<a href="#top"></a>' | ||
| '<script src="https://cdn.tailwindcss.com"></script>' | ||
| '<link href="//fonts.gstatic.com/x.css">' | ||
| '<img src="data:image/png;base64,AA">' | ||
| '<a href="mailto:someone@example.com">mail</a>', | ||
| [], | ||
| ), | ||
| ( | ||
| "a query string does not hide a file that exists", | ||
| '<script src="/assets/PLACEHOLDER?v=2"></script>', | ||
| [], | ||
| ), | ||
| ( | ||
| "a relative reference is checked too", | ||
| '<script src="assets/never-built.js"></script>', | ||
| ["assets/never-built.js"], | ||
| ), | ||
| ( | ||
| "single-quoted attributes are checked", | ||
| "<script src='/assets/never-built-2.js'></script>", | ||
| ["assets/never-built-2.js"], | ||
| ), | ||
| # The stylesheet is inline, so the built font is reachable only through | ||
| # CSS url() - a colon, not an `=`. Missing it would let the mirror prune | ||
| # the production font while the guard stayed silent. | ||
| ( | ||
| "a missing font in CSS url() is caught", | ||
| "<style>@font-face{font-family:D;src:url('/assets/never-built.ttf');}</style>", | ||
| ["assets/never-built.ttf"], | ||
| ), | ||
| ( | ||
| "an unquoted CSS url() is checked", | ||
| "<style>@font-face{src:url(/assets/never-built-3.ttf);}</style>", | ||
| ["assets/never-built-3.ttf"], | ||
| ), | ||
| ( | ||
| "SVG fragment and data: url() are ignored", | ||
| "<style>.a{fill:url(#gem-grad)}" | ||
| ".b{background:url(\"data:image/svg+xml,%3Csvg%3E%3C/svg%3E\")}</style>", | ||
| [], | ||
| ), | ||
| ] | ||
|
|
||
| # Resolve the real bundle name for the query-string case. | ||
| real_bundle = next( | ||
| (p.name for p in (deploy.DIST / "assets").glob("index-*.js")), None, | ||
| ) | ||
| if real_bundle is None: | ||
| sys.exit("No built bundle in dist/assets/. Run `npm run build` first.") | ||
|
|
||
| failures = [] | ||
| for name, document, expected in cases: | ||
| document = document.replace("PLACEHOLDER", real_bundle) | ||
| actual = deploy.missing_referenced_assets(document) | ||
|
|
||
| if expected is None: | ||
| ok = len(actual) == 1 and actual[0].startswith("assets/index-absent-") | ||
| else: | ||
| ok = actual == expected | ||
|
|
||
| print(f"{'PASS' if ok else 'FAIL'} {name}") | ||
| if not ok: | ||
| failures.append(f"{name}: expected {expected}, got {actual}") | ||
|
|
||
| if failures: | ||
| print("\n" + "\n".join(failures)) | ||
| sys.exit(1) | ||
| print(f"\n{len(cases)}/{len(cases)} passed") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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.
When
--allow-dirtydeploys a dist containing its JavaScript bundle but missing the generated font, the guard ignores the existingsrc: url('/assets/Delight-VF-CnVpVuQk.ttf')reference because it scans onlysrc=andhref=attributes, allowing FTP pruning to remove the production font and break the site's intended typography.Prompt To Fix With AI