SXXXXXXX_PyUCC/tests/test_cache_consistency.py

97 lines
2.7 KiB
Python

"""Test cache consistency: same file content in different locations should give same result."""
import os
import shutil
import tempfile
from pathlib import Path
# Create temporary directories
temp_dir1 = tempfile.mkdtemp(prefix="test_cache_1_")
temp_dir2 = tempfile.mkdtemp(prefix="test_cache_2_")
try:
# Create identical file content in two different locations
test_content = '''"""Test module with some code."""
def function_one():
"""This is a function."""
x = 1
y = 2
return x + y
def function_two():
"""Another function."""
# This is a comment
result = function_one()
return result * 2
# Main code
if __name__ == "__main__":
print("Hello World")
'''
file1 = Path(temp_dir1) / "test.py"
file2 = Path(temp_dir2) / "test.py"
file1.write_text(test_content, encoding="utf-8")
file2.write_text(test_content, encoding="utf-8")
print(f"File 1: {file1}")
print(f"File 2: {file2}")
print()
# Import after creating files to ensure clean cache
from pyucc.core.countings_impl import analyze_file_counts, _COUNTING_CACHE
# Analyze both files
print("Analyzing file 1 (should compute)...")
result1 = analyze_file_counts(file1)
print(
f"Result 1: code={result1['code_lines']}, comment={result1['comment_lines']}, blank={result1['blank_lines']}"
)
print(f"Cache size: {len(_COUNTING_CACHE)}")
print()
print("Analyzing file 2 (should use cache)...")
result2 = analyze_file_counts(file2)
print(
f"Result 2: code={result2['code_lines']}, comment={result2['comment_lines']}, blank={result2['blank_lines']}"
)
print(f"Cache size: {len(_COUNTING_CACHE)}")
print()
# Compare results (ignoring 'file' field which contains path)
keys_to_compare = [
"physical_lines",
"code_lines",
"comment_lines",
"blank_lines",
"language",
]
results_match = all(result1[k] == result2[k] for k in keys_to_compare)
print(f"Results match: {results_match}")
if results_match:
print(
"✅ SUCCESS: Same content in different locations produces identical results!"
)
else:
print("❌ FAIL: Results differ!")
for k in keys_to_compare:
if result1[k] != result2[k]:
print(f" {k}: {result1[k]} != {result2[k]}")
# Verify cache was used
print()
if len(_COUNTING_CACHE) == 1:
print(
"✅ Cache working: Only one entry for identical content in different locations"
)
else:
print(f"❌ Cache issue: Expected 1 cache entry, got {len(_COUNTING_CACHE)}")
finally:
# Cleanup
shutil.rmtree(temp_dir1, ignore_errors=True)
shutil.rmtree(temp_dir2, ignore_errors=True)