Coverage for codexa/commands/run.py: 46%

35 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-08-10 07:53 +0000

1import logging 

2from pathlib import Path 

3from typing import List 

4 

5import click 

6 

7from codexa.client.accessor import ReportScanner 

8from codexa.client.executor import TestExecutor 

9from codexa.core.env import load_api_key 

10from codexa.core.errors import CodexaAccessorError, CodexaInputError 

11 

12logger = logging.getLogger(__name__) 

13 

14 

15@click.command("run") 

16@click.argument("test_ids", nargs=-1) 

17@click.option( 

18 "--output", 

19 "output", 

20 "-o", 

21 type=click.Path(exists=False, dir_okay=False, resolve_path=True, path_type=Path), 

22 required=False, 

23 default=Path(Path.cwd(), "report.md"), 

24 help="Output file with generated code", 

25) 

26@click.option( 

27 "--quiet", 

28 "quiet", 

29 "-q", 

30 is_flag=True, 

31 help="Suppress test execution shell output", 

32) 

33def run_command(test_ids: List[str], output: Path, quiet: bool) -> None: 

34 """Analyze the contents of a file for testing.""" 

35 if output.suffix != ".md": 

36 raise CodexaInputError( 

37 message=f"Output file must be a Markdown file, got {output.suffix}", 

38 help_text=f"Rename the output file to a {output.stem}.md", 

39 ) 

40 key = load_api_key() 

41 client = ReportScanner(key) 

42 if not test_ids: 

43 logger.info("No test IDs provided, running all tests") 

44 

45 executor = TestExecutor(test_ids) 

46 exit_code, shell_output, error_output = executor.run(verbose=not quiet) 

47 if not quiet: 

48 click.echo(shell_output) 

49 click.echo(error_output) 

50 if exit_code != 0: 

51 raise CodexaAccessorError( 

52 message=f"Failed to generate tests: {error_output}", 

53 help_text=f"Please check the output for more information", 

54 ) 

55 

56 logger.debug(f"Test execution complete, proceeding to results analysis") 

57 response = client.analyze_tests(shell_output) 

58 try: 

59 with open(output, "w") as f: 

60 f.write(response) 

61 except IOError as e: 

62 raise CodexaAccessorError( 

63 message=f"Failed to generate test file: {e}", 

64 ) 

65 

66 click.secho( 

67 f"\n[!] Test summary generated! Report file: {output}", 

68 fg="green", 

69 bold=True, 

70 )