Skip to content

Commit fb1a9f8

Browse files
authored
Merge pull request #105 from SpiceCodeCLI/fix-tests
Fix tests
2 parents 388bae4 + f7e207e commit fb1a9f8

File tree

10 files changed

+77
-55
lines changed

10 files changed

+77
-55
lines changed

β€Žcli/main.pyβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ def analyze(
224224
except Exception as e:
225225
if json_output:
226226
import json
227-
print(json.dumps({"error": str(e)}))
227+
# Replace newlines with spaces or escape them properly
228+
error_msg = str(e).replace('\n', ' ')
229+
print(json.dumps({"error": error_msg}))
228230
else:
229231
print(f"[red]{messages['error']}[/] {e}")
230232

β€Žrequirements.txtβ€Ž

30 Bytes
Binary file not shown.

β€Žspice/analyze.pyβ€Ž

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def analyze_file(file_path: str, selected_stats=None):
6666

6767
# process comment line count if requested
6868
if "comment_line_count" in selected_stats:
69-
results["comment_line_count"] = count_comment_lines(tokens)
69+
results["comment_line_count"] = count_comment_lines(code)
7070

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

138138

139139
# 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
140-
def count_comment_lines(tokens):
140+
# COMMENT LINE IS A LINE THAT EXCLUSIVELY HAS A COMMENT
141+
# so like: y = 5 #sets y to 5 IS NOT A COMMENT LINE!!!!!!!!
142+
def count_comment_lines(code):
143+
"""Count lines that are exclusively comments (no code on the same line)"""
144+
# split the code into lines
145+
lines = code.splitlines()
141146
comment_count = 0
142-
143-
for token in tokens:
144-
if token.type == TokenType.COMMENT:
147+
148+
for line in lines:
149+
# Remove leading whitespace
150+
stripped = line.strip()
151+
# Check if this line consists only of a comment
152+
if stripped and stripped.startswith('#'):
145153
comment_count += 1
146154

147155
return comment_count
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

β€Žtests/unit/__init__.pyβ€Ž

Whitespace-only changes.

β€Žtests/unit/test_analyze.pyβ€Ž

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
Β (0)