ploughshares/tests/test_code_quality.py

81 lines
3.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Test script for code quality checks.
This script runs basic code quality checks on the Python files in the project.
"""
import os
import unittest
import subprocess
import glob
class CodeQualityTests(unittest.TestCase):
"""Tests to verify code quality standards."""
def setUp(self):
"""Set up the test environment."""
self.project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
self.app_dir = os.path.join(self.project_root, 'docker', 'ploughshares')
self.python_files = glob.glob(os.path.join(self.app_dir, '*.py'))
# Add test files
self.python_files.extend(glob.glob(os.path.join(self.project_root, 'tests', '*.py')))
def test_python_syntax(self):
"""Test that all Python files have valid syntax."""
for py_file in self.python_files:
with self.subTest(file=py_file):
try:
with open(py_file, 'r') as f:
compile(f.read(), py_file, 'exec')
except SyntaxError as e:
self.fail(f"Syntax error in {py_file}: {str(e)}")
def test_no_print_statements(self):
"""Test that Python files don't contain print statements (except in app.py and test files)."""
for py_file in self.python_files:
# Skip app.py as it needs print statements for logging
if os.path.basename(py_file) == 'app.py':
continue
# Skip test files
if '/tests/' in py_file or py_file.endswith('_test.py') or py_file.endswith('test_.py'):
continue
# Skip init_db.py as it's a setup script
if os.path.basename(py_file) == 'init_db.py':
continue
with self.subTest(file=py_file):
with open(py_file, 'r') as f:
content = f.read()
# Check for print statements (simple check, could have false positives)
self.assertNotIn('print(', content,
f"{py_file} contains print statements, use logging instead")
def test_flake8_installed(self):
"""Test if flake8 is installed and can be run."""
try:
# Check if flake8 is installed
result = subprocess.run(
["flake8", "--version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=False
)
if result.returncode != 0:
self.skipTest("flake8 not installed, skipping flake8 checks")
# Note: In a real test, you might run flake8 on your files
# This is just checking if flake8 is available
self.assertEqual(0, result.returncode, "flake8 should be installed")
except FileNotFoundError:
self.skipTest("flake8 not installed, skipping flake8 checks")
if __name__ == '__main__':
unittest.main()