52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Test script for line_counter.py utility."""
|
|
|
|
import subprocess
|
|
import sys
|
|
import os
|
|
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
def run_test(cmd, expected, description):
|
|
"""Run a test command and verify output."""
|
|
result = subprocess.run(cmd, capture_output=True, text=True)
|
|
actual = result.stdout.strip()
|
|
|
|
if actual == str(expected) and result.returncode == 0:
|
|
print(f"PASS: {description}")
|
|
return True
|
|
else:
|
|
print(f"FAIL: {description}")
|
|
print(f" Expected: {expected}, Got: {actual}")
|
|
return False
|
|
|
|
# Test CSV mode with known data
|
|
test_csv_pass = run_test(
|
|
['python3', f'{BASE_DIR}/line_counter.py', f'{BASE_DIR}/test_data.csv', 'csv'],
|
|
5,
|
|
"CSV count mode (5 items)"
|
|
)
|
|
|
|
# Test count mode with known lines
|
|
test_count_pass = run_test(
|
|
['python3', f'{BASE_DIR}/line_counter.py', f'{BASE_DIR}/test_log.txt', 'count'],
|
|
10,
|
|
"Line count mode (10 lines)"
|
|
)
|
|
|
|
# Test error handling for non-existent file
|
|
result = subprocess.run(
|
|
['python3', f'{BASE_DIR}/line_counter.py', f'{BASE_DIR}/nonexistent.txt', 'count'],
|
|
capture_output=True,
|
|
text=True
|
|
)
|
|
if result.returncode == 2:
|
|
print("PASS: Error handling (exit code 2 for missing file)")
|
|
error_pass = True
|
|
else:
|
|
print(f"FAIL: Error handling (expected exit code 2, got {result.returncode})")
|
|
error_pass = False
|
|
|
|
all_pass = test_csv_pass and test_count_pass and error_pass
|
|
print(f"\nAll tests: {'PASSED' if all_pass else 'FAILED'}")
|
|
sys.exit(0 if all_pass else 1) |