-
Notifications
You must be signed in to change notification settings - Fork 0
Delegate configure.sh's Description Check to Python Too #918
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
4 commits
Select commit
Hold shift + click to select a range
5efeb4e
Delegate configure.sh's declared-description validation to Python too…
ptr727 ce82780
Fail loud on a malformed registry, resolve python3 across platforms (…
ptr727 cc7bd94
Make the Python interpreter resolution lazy (PR #918 round 2)
ptr727 8de62f9
Fix a real syntax-error gap, close a padded-name near-miss, type reso…
ptr727 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,63 @@ | ||
| #!/usr/bin/env python3 | ||
| """Exercise resolve_description()'s registry-shape and fail-loud guards directly.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "spec")) | ||
| import resolve_description | ||
|
|
||
|
|
||
| class ResolveDescriptionCase(unittest.TestCase): | ||
| """repo-config/configure.sh's declared-description resolution (spec/resolve_description.py).""" | ||
|
|
||
| def test_a_repo_with_no_declared_description_resolves_to_none(self) -> None: | ||
| registry = {"repos": [{"name": "Fixture"}]} | ||
| self.assertIsNone(resolve_description.resolve_description(registry, "Fixture")) | ||
|
|
||
| def test_a_repo_absent_from_the_registry_resolves_to_none(self) -> None: | ||
| registry = {"repos": [{"name": "Other"}]} | ||
| self.assertIsNone(resolve_description.resolve_description(registry, "Fixture")) | ||
|
|
||
| def test_a_valid_declared_description_is_returned(self) -> None: | ||
| registry = {"repos": [{"name": "Fixture", "description": "A short tagline."}]} | ||
| self.assertEqual( | ||
| resolve_description.resolve_description(registry, "Fixture"), "A short tagline." | ||
| ) | ||
|
|
||
| def test_a_duplicate_name_raises_rather_than_picking_one(self) -> None: | ||
| registry = { | ||
| "repos": [ | ||
| {"name": "Fixture", "description": "First."}, | ||
| {"name": "Fixture", "description": "Second."}, | ||
| ] | ||
| } | ||
| with self.assertRaises(resolve_description.ResolveError): | ||
| resolve_description.resolve_description(registry, "Fixture") | ||
|
|
||
| def test_an_invalid_declared_description_raises(self) -> None: | ||
| registry = {"repos": [{"name": "Fixture", "description": None}]} | ||
| with self.assertRaises(resolve_description.ResolveError): | ||
| resolve_description.resolve_description(registry, "Fixture") | ||
|
|
||
| def test_a_registry_with_no_repos_array_raises_rather_than_reading_as_no_match(self) -> None: | ||
| with self.assertRaises(resolve_description.ResolveError): | ||
| resolve_description.resolve_description({}, "Fixture") | ||
|
|
||
| def test_a_repos_value_that_is_not_a_list_raises(self) -> None: | ||
| with self.assertRaises(resolve_description.ResolveError): | ||
| resolve_description.resolve_description({"repos": "not-a-list"}, "Fixture") | ||
|
|
||
| def test_a_padded_name_that_would_otherwise_match_raises_rather_than_reading_as_absent( | ||
| self, | ||
| ) -> None: | ||
| registry = {"repos": [{"name": " Fixture ", "description": "A short tagline."}]} | ||
| with self.assertRaises(resolve_description.ResolveError): | ||
| resolve_description.resolve_description(registry, "Fixture") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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 @@ | ||
| #!/usr/bin/env python3 | ||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
| """Resolve one repo's declared registry/repos.json description, for repo-config/configure.sh. | ||
|
|
||
| Delegates to description_errors() (validate.py) so configure.sh validates a declared description | ||
| against the exact same contract spec/audit.py's description_findings() does, rather than a third | ||
| hand-rolled copy of the same rules. | ||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
|
|
||
| Prints the declared description to stdout and exits 0 when the repo has no declared description | ||
| (nothing printed) or exactly one valid one. Exits 1 with a message on stderr for anything | ||
| configure.sh should fail loud on: a malformed registry, more than one entry named NAME, or a | ||
| declared description that description_errors() rejects. | ||
|
|
||
| Usage: resolve_description.py REGISTRY_PATH NAME | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
|
|
||
| import json | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| import validate # sibling, import-safe (its main is guarded) | ||
|
|
||
|
|
||
| class ResolveError(Exception): | ||
| """A condition resolve_description() must fail loud on.""" | ||
|
|
||
|
|
||
| def resolve_description(registry: dict, name: str) -> str | None: | ||
|
ptr727 marked this conversation as resolved.
|
||
| """The declared description for NAME in REGISTRY, or None if the repo has none declared. | ||
|
|
||
| Raises ResolveError for anything the caller should fail loud on rather than silently read as | ||
| absent: a registry that is not an object carrying a `repos` array, an entry whose own name | ||
| would match NAME but for leading/trailing whitespace (spec/validate.py rejects that shape too, | ||
| so it is never the intended way to spell a mismatch), more than one entry named NAME, or a | ||
| declared description description_errors() rejects. | ||
| """ | ||
| if not isinstance(registry, dict) or not isinstance(registry.get("repos"), list): | ||
| raise ResolveError("registry is not an object with a 'repos' array") | ||
| repos = registry["repos"] | ||
| near_miss = next( | ||
| ( | ||
| r["name"] | ||
| for r in repos | ||
| if isinstance(r, dict) | ||
| and isinstance(r.get("name"), str) | ||
| and r["name"] != name | ||
| and r["name"].strip() == name | ||
| ), | ||
| None, | ||
| ) | ||
| if near_miss is not None: | ||
| raise ResolveError( | ||
| f"a registry entry's name {near_miss!r} carries leading/trailing whitespace" | ||
| ) | ||
| matches = [r for r in repos if isinstance(r, dict) and r.get("name") == name] | ||
| if len(matches) > 1: | ||
| raise ResolveError( | ||
| f"{len(matches)} registry entries named {name}. " | ||
| "Resolve the duplicate before its description can be read" | ||
| ) | ||
| if not matches or "description" not in matches[0]: | ||
| return None | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| desc = matches[0]["description"] | ||
| errors = validate.description_errors(name, desc) | ||
| if errors: | ||
| raise ResolveError("; ".join(errors)) | ||
| return desc | ||
|
|
||
|
|
||
| def main() -> int: | ||
| if len(sys.argv) != 3: | ||
| print("usage: resolve_description.py REGISTRY_PATH NAME", file=sys.stderr) | ||
| return 1 | ||
| registry_path, name = sys.argv[1], sys.argv[2] | ||
|
ptr727 marked this conversation as resolved.
|
||
| try: | ||
| registry = json.loads(Path(registry_path).read_text(encoding="utf-8")) | ||
| except (OSError, json.JSONDecodeError) as e: | ||
| print(f"Failed to read {registry_path}: {e}", file=sys.stderr) | ||
| return 1 | ||
| try: | ||
| desc = resolve_description(registry, name) | ||
| except ResolveError as e: | ||
| print(f"{e} (spec/validate.py rejects this once run).", file=sys.stderr) | ||
| return 1 | ||
| if desc is not None: | ||
| sys.stdout.write(desc) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
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
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.