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