Test project-specific commands in security hook.
()
| 400 | |
| 401 | |
| 402 | def test_project_commands(): |
| 403 | """Test project-specific commands in security hook.""" |
| 404 | print("\nTesting project-specific commands:\n") |
| 405 | passed = 0 |
| 406 | failed = 0 |
| 407 | |
| 408 | with tempfile.TemporaryDirectory() as tmpdir: |
| 409 | project_dir = Path(tmpdir) |
| 410 | autoforge_dir = project_dir / ".autoforge" |
| 411 | autoforge_dir.mkdir() |
| 412 | |
| 413 | # Create a config with Swift commands |
| 414 | config_path = autoforge_dir / "allowed_commands.yaml" |
| 415 | config_path.write_text("""version: 1 |
| 416 | commands: |
| 417 | - name: swift |
| 418 | description: Swift compiler |
| 419 | - name: xcodebuild |
| 420 | description: Xcode build |
| 421 | - name: swift* |
| 422 | description: All Swift tools |
| 423 | """) |
| 424 | |
| 425 | # Test 1: Project command should be allowed |
| 426 | input_data = {"tool_name": "Bash", "tool_input": {"command": "swift --version"}} |
| 427 | context = {"project_dir": str(project_dir)} |
| 428 | result = asyncio.run(bash_security_hook(input_data, context=context)) |
| 429 | if result.get("decision") != "block": |
| 430 | print(" PASS: Project command 'swift' allowed") |
| 431 | passed += 1 |
| 432 | else: |
| 433 | print(" FAIL: Project command 'swift' should be allowed") |
| 434 | print(f" Reason: {result.get('reason')}") |
| 435 | failed += 1 |
| 436 | |
| 437 | # Test 2: Pattern match should work |
| 438 | input_data = {"tool_name": "Bash", "tool_input": {"command": "swiftlint"}} |
| 439 | result = asyncio.run(bash_security_hook(input_data, context=context)) |
| 440 | if result.get("decision") != "block": |
| 441 | print(" PASS: Pattern 'swift*' matches 'swiftlint'") |
| 442 | passed += 1 |
| 443 | else: |
| 444 | print(" FAIL: Pattern 'swift*' should match 'swiftlint'") |
| 445 | print(f" Reason: {result.get('reason')}") |
| 446 | failed += 1 |
| 447 | |
| 448 | # Test 3: Non-allowed command should be blocked |
| 449 | input_data = {"tool_name": "Bash", "tool_input": {"command": "rustc"}} |
| 450 | result = asyncio.run(bash_security_hook(input_data, context=context)) |
| 451 | if result.get("decision") == "block": |
| 452 | print(" PASS: Non-allowed command 'rustc' blocked") |
| 453 | passed += 1 |
| 454 | else: |
| 455 | print(" FAIL: Non-allowed command 'rustc' should be blocked") |
| 456 | failed += 1 |
| 457 | |
| 458 | # Test 4: Empty command name is rejected |
| 459 | config_path.write_text("""version: 1 |
no test coverage detected