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