Coverage for codexa/commands/list.py: 56%

25 statements  

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

1import json 

2import logging 

3import os 

4from pathlib import Path 

5 

6import click 

7 

8from codexa.client.accessor import ReportScanner 

9from codexa.client.executor import TestExecutor 

10from codexa.core.env import load_api_key 

11from codexa.core.errors import CodexaAccessorError, CodexaInputError 

12 

13logger = logging.getLogger(__name__) 

14 

15 

16@click.command("list") 

17@click.option( 

18 "--base-dir", 

19 "base_dir", 

20 "-b", 

21 type=click.Path(exists=False, file_okay=False, path_type=Path), 

22 required=False, 

23 default=Path(Path.cwd(), "tests"), 

24 help="Base directory of tests", 

25) 

26@click.option( 

27 "--json", 

28 "as_json", 

29 is_flag=True, 

30 help="Print the test list as JSON map", 

31) 

32def list_command(base_dir: Path, as_json: bool) -> None: 

33 """List all available tests.""" 

34 rendered_output = "" 

35 base_path = [str(base_dir)] 

36 executor = TestExecutor(base_path) 

37 if as_json: 

38 logger.debug("Generating JSON map of all tests") 

39 test_map = executor.get_structured_test_tree() 

40 rendered_output = json.dumps( 

41 test_map, indent=2, sort_keys=True, ensure_ascii=False 

42 ) 

43 else: 

44 logger.debug("Generating list of all tests") 

45 numbered_list = [ 

46 f"{index}. {entry}" 

47 for index, entry in enumerate(executor.collect_all_tests(), start=1) 

48 ] 

49 rendered_output = "\n".join(numbered_list) 

50 click.echo(rendered_output)