-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_analyze_json_python-example.py
More file actions
61 lines (47 loc) · 2.05 KB
/
test_analyze_json_python-example.py
File metadata and controls
61 lines (47 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import json
import os
import pytest
from typer.testing import CliRunner
from cli.main import app
# Setup test runner
runner = CliRunner()
# Get the absolute path to the sample file
SAMPLE_FILE_PATH = os.path.join(os.path.dirname(__file__), "..", "sample-code", "example.py")
def test_analyze_command_with_json_flag():
"""Test the analyze command with the --json flag"""
# Run the command with --json flag
result = runner.invoke(app, ["analyze", SAMPLE_FILE_PATH, "--json"])
# Check if the command executed successfully
assert result.exit_code == 0
# Parse the JSON output
output = json.loads(result.stdout)
# Check if all expected stats are in the output
assert "file_name" in output
assert "line_count" in output
assert "comment_line_count" in output
assert "function_count" in output
# Verify the values match expected results
assert output["file_name"] == os.path.basename(SAMPLE_FILE_PATH)
assert output["line_count"] == 161
assert output["comment_line_count"] == 25
assert output["function_count"] == 17
def test_analyze_command_with_all_and_json_flags():
"""Test the analyze command with both --all and --json flags"""
# Run the command with both flags
result = runner.invoke(app, ["analyze", SAMPLE_FILE_PATH, "--all", "--json"])
# Check if the command executed successfully
assert result.exit_code == 0
# Parse the JSON output
output = json.loads(result.stdout)
# Verify the values match expected results
assert output["line_count"] == 161
assert output["comment_line_count"] == 25
assert output["function_count"] == 17
def test_analyze_command_with_nonexistent_file():
"""Test the analyze command with a nonexistent file"""
# Run the command with a file that doesn't exist
result = runner.invoke(app, ["analyze", "nonexistent_file.py", "--json"])
# Parse the JSON output (should contain an error)
output = json.loads(result.stdout)
# Check if the output contains an error message
assert "error" in output