Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ def analyze(
except Exception as e:
if json_output:
import json
print(json.dumps({"error": str(e)}))
# Replace newlines with spaces or escape them properly
error_msg = str(e).replace('\n', ' ')
print(json.dumps({"error": error_msg}))
else:
print(f"[red]{messages['error']}[/] {e}")

Expand Down
Binary file modified requirements.txt
Binary file not shown.
18 changes: 13 additions & 5 deletions spice/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def analyze_file(file_path: str, selected_stats=None):

# process comment line count if requested
if "comment_line_count" in selected_stats:
results["comment_line_count"] = count_comment_lines(tokens)
results["comment_line_count"] = count_comment_lines(code)

# only put the code through the parser and proceed with parsing if we need function count (UPDATE THIS WHEN NEEDED PLEASE !!!!!!!!)
if "function_count" in selected_stats:
Expand Down Expand Up @@ -137,11 +137,19 @@ def search_node(node):


# this will count comment lines, since our AST/Parser doesn't include comment lines, this needs to be done in the tokenized output of the lexer
def count_comment_lines(tokens):
# COMMENT LINE IS A LINE THAT EXCLUSIVELY HAS A COMMENT
# so like: y = 5 #sets y to 5 IS NOT A COMMENT LINE!!!!!!!!
def count_comment_lines(code):
"""Count lines that are exclusively comments (no code on the same line)"""
# split the code into lines
lines = code.splitlines()
comment_count = 0

for token in tokens:
if token.type == TokenType.COMMENT:

for line in lines:
# Remove leading whitespace
stripped = line.strip()
# Check if this line consists only of a comment
if stripped and stripped.startswith('#'):
comment_count += 1

return comment_count
61 changes: 61 additions & 0 deletions tests/analyze/test_analyze_json_python-example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file removed tests/unit/__init__.py
Empty file.
49 changes: 0 additions & 49 deletions tests/unit/test_analyze.py

This file was deleted.