-
Notifications
You must be signed in to change notification settings - Fork 7
feat: becwright init #6
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
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,108 @@ | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| from becwright import cli | ||
| from becwright.rules import load_rules | ||
|
|
||
| _SRC = Path(__file__).resolve().parents[1] / "src" | ||
|
|
||
|
|
||
| def _git(root, *args): | ||
| subprocess.run(["git", *args], cwd=root, check=True, capture_output=True, text=True) | ||
|
|
||
|
|
||
| def _init_repo(path): | ||
| _git(path, "init") | ||
| _git(path, "config", "user.email", "t@t.t") | ||
| _git(path, "config", "user.name", "t") | ||
| return path | ||
|
|
||
|
|
||
| # --- detection --- | ||
|
|
||
| def test_detect_languages_skips_noise_dirs(tmp_path): | ||
| (tmp_path / "a.py").write_text("x = 1\n", encoding="utf-8") | ||
| (tmp_path / "b.ts").write_text("const x = 1\n", encoding="utf-8") | ||
| (tmp_path / "node_modules").mkdir() | ||
| (tmp_path / "node_modules" / "c.js").write_text("//\n", encoding="utf-8") | ||
| assert cli._detect_languages(tmp_path) == ["python", "ts"] | ||
|
|
||
|
|
||
| def test_detect_languages_none(tmp_path): | ||
| (tmp_path / "README.md").write_text("# hi\n", encoding="utf-8") | ||
| assert cli._detect_languages(tmp_path) == [] | ||
|
|
||
|
|
||
| # --- starter rules --- | ||
|
|
||
| def test_starter_rules_python(): | ||
| ids = [r["id"] for r in cli._starter_rules(["python"])] | ||
| assert ids == ["no-hardcoded-secrets", "no-debug-remnants", "no-dangerous-eval"] | ||
|
|
||
|
|
||
| def test_starter_rules_js(): | ||
| ids = [r["id"] for r in cli._starter_rules(["js"])] | ||
| assert ids == ["no-hardcoded-secrets", "no-debugger-js", "no-console-log-js"] | ||
|
|
||
|
|
||
| def test_starter_rules_empty(): | ||
| assert cli._starter_rules([]) == [] | ||
|
|
||
|
|
||
| # --- rendering --- | ||
|
|
||
| def test_render_yaml_parses_and_keeps_forbid(tmp_path): | ||
| p = tmp_path / "rules.yaml" | ||
| p.write_text(cli._render_rules_yaml(cli._starter_rules(["js"])), encoding="utf-8") | ||
| rules = load_rules(p) | ||
| assert {r.id for r in rules} == {"no-hardcoded-secrets", "no-debugger-js", "no-console-log-js"} | ||
| dbg = next(r for r in rules if r.id == "no-debugger-js") | ||
| assert r"--pattern '\bdebugger\b'" in dbg.check | ||
|
|
||
|
|
||
| def test_render_empty_is_valid(tmp_path): | ||
| p = tmp_path / "rules.yaml" | ||
| p.write_text(cli._render_rules_yaml([]), encoding="utf-8") | ||
| assert load_rules(p) == [] | ||
|
|
||
|
|
||
| # --- the init command --- | ||
|
|
||
| def test_init_creates_rules_and_hook(tmp_path, monkeypatch): | ||
| _init_repo(tmp_path) | ||
| (tmp_path / "a.py").write_text("x = 1\n", encoding="utf-8") | ||
| monkeypatch.chdir(tmp_path) | ||
| assert cli.main(["init"]) == 0 | ||
| rules_path = tmp_path / ".bec" / "rules.yaml" | ||
| assert "no-debug-remnants" in {r.id for r in load_rules(rules_path)} | ||
| assert (tmp_path / ".git" / "hooks" / "pre-commit").exists() | ||
|
|
||
|
|
||
| def test_init_refuses_existing(tmp_path, monkeypatch): | ||
| _init_repo(tmp_path) | ||
| (tmp_path / ".bec").mkdir() | ||
| rules_path = tmp_path / ".bec" / "rules.yaml" | ||
| rules_path.write_text("rules: []\n", encoding="utf-8") | ||
| monkeypatch.chdir(tmp_path) | ||
| assert cli.main(["init"]) == 1 | ||
| assert rules_path.read_text(encoding="utf-8") == "rules: []\n" | ||
|
|
||
|
|
||
| def test_init_force_overwrites(tmp_path, monkeypatch): | ||
| _init_repo(tmp_path) | ||
| (tmp_path / "a.py").write_text("x = 1\n", encoding="utf-8") | ||
| (tmp_path / ".bec").mkdir() | ||
| (tmp_path / ".bec" / "rules.yaml").write_text("rules: []\n", encoding="utf-8") | ||
| monkeypatch.chdir(tmp_path) | ||
| assert cli.main(["init", "--force"]) == 0 | ||
| assert load_rules(tmp_path / ".bec" / "rules.yaml") | ||
|
|
||
|
|
||
| def test_init_generated_rule_blocks_via_check(tmp_path, monkeypatch): | ||
| monkeypatch.setenv("PYTHONPATH", str(_SRC)) | ||
| _init_repo(tmp_path) | ||
| (tmp_path / "app.js").write_text("function f(){ debugger; }\n", encoding="utf-8") | ||
| monkeypatch.chdir(tmp_path) | ||
| assert cli.main(["init"]) == 0 | ||
| _git(tmp_path, "add", "app.js") | ||
| assert cli.main(["check", "--all"]) == 1 |
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.
🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win
Prune skipped directories before walking into them.
Line 79 still descends through
node_modules,.venv,.git,dist, and similar directories; the Line 82 filter only ignores files after traversal. On large repos,becwright initcan become unexpectedly slow.⚙️ Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents