Skip to content

Commit

Permalink
Cleaned up code and formatted #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
EmbeddedDevops1 committed May 20, 2024
1 parent 5359c22 commit e70e59e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
14 changes: 6 additions & 8 deletions cover_agent/FilePreprocessor.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import os
import ast
import textwrap


class FilePreprocessor:
def __init__(self, path_to_file):
self.path_to_file = path_to_file

# List of rules/action key pair.
# Add your new rule and how to process the text (function) here
self.rules = [
(self._is_python_file, self._process_if_python)
]
self.rules = [(self._is_python_file, self._process_if_python)]

def process_file(self, text: str) -> str:
"""
Expand All @@ -25,27 +23,27 @@ def _is_python_file(self) -> bool:
"""
Rule to check if the file is a Python file.
"""
return self.path_to_file.endswith('.py')
return self.path_to_file.endswith(".py")

def _process_if_python(self, text: str) -> str:
"""
Action to process Python files by checking for class definitions and indenting if found.
"""
if self._contains_class_definition():
return textwrap.indent(text, ' ')
return textwrap.indent(text, " ")
return text

def _contains_class_definition(self) -> bool:
"""
Check if the file contains a Python class definition using the ast module.
"""
try:
with open(self.path_to_file, 'r') as file:
with open(self.path_to_file, "r") as file:
content = file.read()
parsed_ast = ast.parse(content)
for node in ast.walk(parsed_ast):
if isinstance(node, ast.ClassDef):
return True
except SyntaxError as e:
print(f"Syntax error when parsing the file: {e}")
return False
return False
20 changes: 14 additions & 6 deletions tests/test_FilePreprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import textwrap
from cover_agent.FilePreprocessor import FilePreprocessor


class TestFilePreprocessor:
# Test for a C file
def test_c_file(self):
with tempfile.NamedTemporaryFile(delete=False, suffix=".c") as tmp:
preprocessor = FilePreprocessor(tmp.name)
input_text = "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt."
processed_text = preprocessor.process_file(input_text)
assert processed_text == input_text, "C file processing should not alter the text."
assert (
processed_text == input_text
), "C file processing should not alter the text."

# Test for a Python file with only a function
def test_py_file_with_function_only(self):
Expand All @@ -20,7 +23,9 @@ def test_py_file_with_function_only(self):
preprocessor = FilePreprocessor(tmp.name)
input_text = "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt."
processed_text = preprocessor.process_file(input_text)
assert processed_text == input_text, "Python file without class should not alter the text."
assert (
processed_text == input_text
), "Python file without class should not alter the text."

# Test for a Python file with a comment that looks like a class definition
def test_py_file_with_commented_class(self):
Expand All @@ -30,7 +35,9 @@ def test_py_file_with_commented_class(self):
preprocessor = FilePreprocessor(tmp.name)
input_text = "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt."
processed_text = preprocessor.process_file(input_text)
assert processed_text == input_text, "Commented class definition should not trigger processing."
assert (
processed_text == input_text
), "Commented class definition should not trigger processing."

# Test for a Python file with an actual class definition
def test_py_file_with_class(self):
Expand All @@ -40,6 +47,7 @@ def test_py_file_with_class(self):
preprocessor = FilePreprocessor(tmp.name)
input_text = "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt."
processed_text = preprocessor.process_file(input_text)
expected_output = textwrap.indent(input_text, ' ')
assert processed_text == expected_output, "Python file with class should indent the text."

expected_output = textwrap.indent(input_text, " ")
assert (
processed_text == expected_output
), "Python file with class should indent the text."

0 comments on commit e70e59e

Please sign in to comment.