|
| 1 | +import json |
| 2 | +from pathlib import Path |
1 | 3 | from codeanalyzer.__main__ import app |
2 | 4 | from codeanalyzer.utils import logger |
3 | 5 |
|
4 | 6 |
|
5 | 7 | def test_cli_help(cli_runner): |
6 | 8 | """Must be able to run the CLI and see help output.""" |
7 | | - result = cli_runner.invoke(app, ["--help"]) |
| 9 | + result = cli_runner.invoke(app, ["--help"], env={"NO_COLOR": "1", "TERM": "dumb"}) |
8 | 10 | assert result.exit_code == 0 |
9 | | - assert "Usage: codeanalyzer [OPTIONS] COMMAND [ARGS]..." in result.output |
10 | 11 |
|
11 | | - |
12 | | -def test_cli_call_symbol_table(cli_runner, project_root): |
| 12 | +def test_cli_call_symbol_table_with_json(cli_runner, project_root): |
13 | 13 | """Must be able to run the CLI with symbol table analysis.""" |
14 | | - |
15 | | - output_dir = project_root / "src" / "test" / ".output" |
| 14 | + output_dir = project_root.joinpath("test", ".output") |
16 | 15 | output_dir.mkdir(parents=True, exist_ok=True) |
17 | | - |
18 | 16 | result = cli_runner.invoke( |
19 | 17 | app, |
20 | 18 | [ |
21 | 19 | "--input", |
22 | 20 | str(project_root), |
23 | 21 | "--output", |
24 | | - str(project_root / "src" / "test" / ".output"), |
| 22 | + str(output_dir), |
25 | 23 | "--analysis-level", |
26 | 24 | "1", |
27 | 25 | "--no-codeql", |
28 | 26 | "--cache-dir", |
29 | | - str(project_root / "src" / "test" / ".cache"), |
| 27 | + str(project_root.joinpath("test", ".cache")), |
30 | 28 | "--keep-cache", |
31 | | - "-v", |
| 29 | + "--format=json", |
32 | 30 | ], |
| 31 | + env={"NO_COLOR": "1", "TERM": "dumb"}, |
33 | 32 | ) |
34 | | - logger.debug(f"CLI result: {result.output}") |
35 | | - # assert result.exit_code == 0 |
36 | | - # assert json.load(Path(output_dir) / ".output" / "analysis.json") is not None |
37 | | - # assert "symbol_table" in json.load(Path(output_dir) / ".output" / "analysis.json") |
| 33 | + assert result.exit_code == 0, "CLI command should succeed" |
| 34 | + assert Path(output_dir).joinpath("analysis.json").exists(), "Output JSON file should be created" |
| 35 | + json_obj = json.loads(Path(output_dir).joinpath("analysis.json").read_text()) |
| 36 | + assert json_obj is not None, "JSON output should not be None" |
| 37 | + assert isinstance(json_obj, dict), "JSON output should be a dictionary" |
| 38 | + assert "symbol_table" in json_obj.keys(), "Symbol table should be present in the output" |
| 39 | + assert len(json_obj["symbol_table"]) > 0, "Symbol table should not be empty" |
0 commit comments