Coverage for codexa/commands/compare.py: 55%

29 statements  

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

1import logging 

2import os 

3from pathlib import Path 

4from typing import Optional 

5 

6import click 

7 

8from codexa.client.accessor import RepoAnalyzer 

9from codexa.client.versioning import compare_git_diff 

10from codexa.core.env import load_api_key 

11from codexa.core.errors import CodexaInputError, CodexaOutputError 

12 

13logger = logging.getLogger(__name__) 

14 

15 

16@click.command("compare") 

17@click.option( 

18 "--directory", 

19 "-d", 

20 type=click.Path(exists=True, dir_okay=True, resolve_path=True, path_type=str), 

21 required=False, 

22 default=os.getcwd(), 

23 help="Output file with generated code", 

24) 

25@click.option( 

26 "--ref-branch", 

27 "ref_branch", 

28 "-r", 

29 type=str, 

30 required=False, 

31 default="os.getcwd()", 

32 help="Remote branch to compare against", 

33) 

34@click.option( 

35 "--quiet", 

36 "-q", 

37 is_flag=True, 

38 help="Suppress test execution shell output", 

39) 

40@click.option( 

41 "--output", 

42 "output", 

43 "-o", 

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

45 required=False, 

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

47 help="Output file with generated code", 

48) 

49def compare_command( 

50 directory: str, ref_branch: str, quiet: bool, output: Optional[Path] 

51) -> None: 

52 """Generate smart analysis from diff comparison..""" 

53 if output is not None and output.suffix != ".md": 

54 raise CodexaInputError( 

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

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

57 ) 

58 key = load_api_key() 

59 diff = compare_git_diff(ref_branch, directory) 

60 logger.info("Forwarding git diff to LLM") 

61 analyzer = RepoAnalyzer(key) 

62 assessment = analyzer.compare_diff(diff) 

63 

64 click.secho( 

65 f"[!] Recommendations for tests: {directory}", 

66 fg="green", 

67 bold=True, 

68 ) 

69 if not quiet: 

70 click.echo(assessment) 

71 

72 if output is not None: 

73 output.write_text(assessment) 

74 logger.info(f"Diff report generated: {output.absolute()}")