Skip to content

Commit

Permalink
Update max-line-length to 79 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Younis-Ahmed committed Feb 24, 2024
1 parent 16ffec3 commit d968055
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 46 deletions.
2 changes: 1 addition & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ indent-after-paren=4
indent-string=' '

# Maximum number of characters on a single line.
max-line-length=100
max-line-length=79

# Maximum number of lines in a module.
max-module-lines=1000
Expand Down
27 changes: 17 additions & 10 deletions tests/test_autoprototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def setup_method(self):

with open("test.c", "w", encoding="utf-8") as f:
f.write(
'int main(int argc, char **argv){\nprintf("Hello World"\nreturn 0; \n}')
'int main(int argc, char **argv)\
{\nprintf("Hello World"\nreturn 0; \n}')
yield

os.remove("test.c")
Expand All @@ -53,11 +54,12 @@ def test_betty_check_installed(self, mocker):
"subprocess.run"), mocker.patch("glob.glob")

mock_glob.return_value = ["test.c"]
mock_run.return_value = CompletedProcess(args=['betty'] + mock_glob.return_value,
returncode=0,
stdout='ERROR: ',
stderr='WARNING:'
)
mock_run.return_value = CompletedProcess(
args=['betty'] + mock_glob.return_value,
returncode=0,
stdout='ERROR: ',
stderr='WARNING:'
)
betty_check_result = betty_check()
assert betty_check_result is not None
assert betty_check_result is False
Expand Down Expand Up @@ -122,14 +124,16 @@ def test_check_ctags_not_installed(self, mocker):
assert ctag[0] is False and isinstance(ctag[1], str)

def test_generate_tags_failure(self, mocker):
""" Test the generate_tags function not working from autoprototype.py"""
""" Test the generate_tags function not
working from autoprototype.py"""
mock_run = mocker.patch("subprocess.run")
mock_run.side_effect = subprocess.CalledProcessError(
returncode=1, cmd=['ctags', '-R', '.'], stderr="Error")
assert not generate_tags('.')

def test_generate_tags_success(self, mocker):
"""Test the generate_tags function when the subprocess command succeeds."""
"""Test the generate_tags function when the subprocess command
succeeds."""
mock_run = mocker.patch("subprocess.run")
mock_run.return_value = CompletedProcess(args=[
'ctags', '-R', '--c-kinds=+p',
Expand All @@ -152,7 +156,8 @@ def test_generate_tags_success(self, mocker):

@pytest.mark.usefixtures("setup_tear_down_temp_files")
def test_filter_tags_success(self):
"""Test the filter_tags function when the subprocess command succeeds."""
"""Test the filter_tags function when the
subprocess command succeeds."""
generate_tags(".")
result = filter_tags('.', "tags")

Expand Down Expand Up @@ -226,7 +231,9 @@ def test_check_header_file(self):
os.remove("test.h")
result = check_header_file("test")
assert result == (
False, "Error: Invalid header file. It should have a '.h' extension.")
False,
"Error: Invalid header file. It should have a '.h' extension."
)

@pytest.mark.usefixtures("setup_tear_down_temp_files")
def test_autoproto(self, mocker):
Expand Down
7 changes: 5 additions & 2 deletions tests/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ class TestBackup:

@pytest.fixture(autouse=True)
def setup_method(self):
"""Create a test file.""" # pylint: disable=attribute-defined-outside-init
"""Create a test file."""
# pylint: disable=attribute-defined-outside-init
self.test_file = 'test.c'
with open(self.test_file, 'w', encoding='utf-8') as f:
self.test_file = f.name
f.write(
'int main(int argc, char **argv){\nprintf("Hello World"\nreturn 0; \n}')
'int main(int argc, char **argv){\nprintf(\
"Hello World"\nreturn 0; \n}'
)
yield
os.remove(self.test_file)
if os.path.exists(self.test_file + '.bak'):
Expand Down
82 changes: 57 additions & 25 deletions tests/test_betty_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def setup_teardown(self):

with open("test.c", "w", encoding="utf-8") as f:
f.write(
'int main(int argc, char **argv){\nprintf("Hello World"\nreturn 0; \n}')
'int main(int argc, char **argv)\
{\nprintf("Hello World"\nreturn 0; \n}')
yield

os.remove("test.c")
Expand All @@ -38,7 +39,8 @@ def setup_teardown(self):
def test_read_file(self):
"""Test read_file function."""
assert read_file(
"test.c") == 'int main(int argc, char **argv){\nprintf("Hello World"\nreturn 0; \n}'
"test.c") == 'int main(int argc, char **argv)\
{\nprintf("Hello World"\nreturn 0; \n}'

def test_write_file(self):
"""Test write_file function."""
Expand All @@ -50,7 +52,8 @@ def test_add_line_without_newline(self):
"""Test add_line_without_newline function."""
add_line_without_newline("test.c", "\n")
assert read_file(
"test.c") == 'int main(int argc, char **argv){\nprintf("Hello World"\nreturn 0; \n}\n'
"test.c") == 'int main(int argc, char **argv)\
{\nprintf("Hello World"\nreturn 0; \n}\n'

def test_add_line_without_newline_fail(self):
"""Test add_line_without_newline function."""
Expand Down Expand Up @@ -99,13 +102,18 @@ def test_remove_trailing_whitespaces(self):
"Hello World\t", "Hello World\t\t"]
# ❗ "Hello World\n" and "Hello World\n " is failing
assert all(remove_trailing_whitespaces(line)
== re.search(r"Hello World\S*", line).group() for line in lines)
== re.search(
r"Hello World\S*", line).group() for line in lines
)

# ❗ @pytest.mark.skip(reason="Needs to be refactored with Dependency Injection in mind")
# ❗ @pytest.mark.skip(reason=
# "Needs to be refactored with Dependency Injection in mind")
def test_fix_betty_warnings(self, mocker):
"""Test fix_betty_warnings function."""
mock_remove_consecutive_blank_lines = mocker.patch(
"bettyfixer.betty_fixer.remove_consecutive_blank_lines", return_value="some_content")
"bettyfixer.betty_fixer.remove_consecutive_blank_lines",
return_value="some_content"
)
mock_fix_comments = mocker.patch(
"bettyfixer.betty_fixer.fix_comments", return_value="some_content")
mock_remove_trailing_whitespaces = mocker.patch(
Expand All @@ -132,29 +140,42 @@ def test_remove_blank_lines_inside_comments(self, mocker):
mock_open.assert_any_call("some_file", 'w', encoding='utf-8')
mock_open().writelines.assert_called_once_with(["/**\n", "*/"])

# ❗@pytest.mark.skip(reason="in serious need of refactoring.Decoupling and Dependency Injection")
# ❗@pytest.mark.skip(reason=
# "in serious need of refactoring.Decoupling and Dependency Injection")
def test_fix_betty_style(self, mocker):
"""Test fix_betty_style function."""
file_paths = ["file1", "file2", "file3"]
mock_create_backup = mocker.patch(
"bettyfixer.betty_fixer.create_backup", return_value="file content")
"bettyfixer.betty_fixer.create_backup",
return_value="file content"
)
mock_run_vi_script = mocker.patch(
"bettyfixer.betty_fixer.run_vi_script", return_value="file content")
"bettyfixer.betty_fixer.run_vi_script",
return_value="file content"
)
mock_read_file = mocker.patch(
"bettyfixer.betty_fixer.read_file", return_value="file content")
"bettyfixer.betty_fixer.read_file",
return_value="file content"
)
mock_write_file = mocker.patch(
"bettyfixer.betty_fixer.write_file", return_value="file content")
"bettyfixer.betty_fixer.write_file",
return_value="file content"
)
mock_add_line_without_newline = mocker.patch(
"bettyfixer.betty_fixer.add_line_without_newline", return_value="file content")
"bettyfixer.betty_fixer.add_line_without_newline",
return_value="file content"
)
mock_process_errors = mocker.patch(
"bettyfixer.betty_fixer.process_errors", return_value="file content")
"bettyfixer.betty_fixer.process_errors",
return_value="file content"
)
mock_open = mocker.patch(
"builtins.open", mocker.mock_open(read_data="file content"))

fix_betty_style(file_paths)

mock_process_errors.call_count = len(file_paths) * 2
# Check that the mocked functions were called with the correct arguments
# Check that the mocked functions were called with correct args
for file_path in file_paths:
mock_create_backup.assert_any_call(file_path)
mock_run_vi_script.assert_any_call(file_path)
Expand All @@ -168,7 +189,9 @@ def test_fix_betty_style(self, mocker):
mock_open().writelines.assert_called_with(["file content"])

# ❗
@pytest.mark.skip(reason="Needs refactoring [DI, pure functions, decoupling]")
@pytest.mark.skip(
reason="Needs refactoring [DI, pure functions, decoupling]"
)
def test_more_than_5_functions_in_the_file(self, mocker):
"""Test more_than_5_functions_in_the_file function."""
pass # pylint: disable=unnecessary-pass
Expand All @@ -183,15 +206,15 @@ def test_find_available_file_name_with_existing_files(self, mocker):
"""Test find_available_file_name when
there are existing files with the same base name."""
exists_side_effect = [
True, True, False] # Simulate that "test1.txt" and "test2.txt" exist
True, True, False]
mocker.patch('os.path.exists', side_effect=exists_side_effect)
assert find_available_file_name("test.txt") == "test3.txt"

def test_find_available_file_name_with_many_existing_files(self, mocker):
"""Test find_available_file_name when there are many existing
files with the same base name."""
exists_side_effect = [
True] * 100 + [False] # Simulate that "test1.txt" to "test100.txt" exist
True] * 100 + [False]
mocker.patch('os.path.exists', side_effect=exists_side_effect)
assert find_available_file_name("test.txt") == "test101.txt"

Expand All @@ -217,13 +240,16 @@ def test_betty_handler_no_errors(self, mocker):

def test_betty_handler_with_errors(self, mocker):
"""Test betty_handler function when there are Betty errors."""
error_lines = "More than 40 lines in a function\nline over 80 characters\n"
error_lines = "More than 40 lines in a function\nline over\
80 characters\n"
mock_open = mocker.patch(
"builtins.open", mocker.mock_open(read_data=error_lines))
mock_other_handlers = mocker.patch(
"bettyfixer.betty_fixer.other_handlers")
mock_extract_and_print_variables = mocker.patch(
"bettyfixer.betty_fixer.extract_and_print_variables", return_value=("file_path",))
"bettyfixer.betty_fixer.extract_and_print_variables",
return_value=("file_path",)
)
betty_handler("errors.txt")
mock_open.assert_called_once_with("errors.txt", 'r', encoding='utf-8')
mock_extract_and_print_variables.assert_called()
Expand Down Expand Up @@ -264,7 +290,8 @@ def test_copy_files_to_tasks(self, mocker):
"""Test copy_files_to_tasks function."""
mock_exists = mocker.patch("os.path.exists", return_value=False)
mock_open = mocker.patch("builtins.open", mocker.mock_open(
read_data="#include <stdio.h>\nint main() {}\n#include \"file.h\""))
read_data="#include <stdio.h>\nint main(\
) {}\n#include \"file.h\""))
files = ["file1.c", "file2.c"]
copy_files_to_tasks(files)
assert mock_exists.call_count == len(files)
Expand All @@ -275,7 +302,8 @@ def test_copy_files_to_tasks(self, mocker):
def test_modify_main_files(self, mocker):
"""Test modify_main_files function."""
mock_open = mocker.patch("builtins.open", mocker.mock_open(
read_data="#include <stdio.h>\nint main() {}\n#include \"file.h\""))
read_data="#include <stdio.h>\nint main(\
) {}\n#include \"file.h\""))
files = ["file1.c", "file2.c"]
modify_main_files(files)
assert mock_open.call_count == len(files) * 2
Expand Down Expand Up @@ -307,7 +335,8 @@ def test_is_file_processed_not_exists(self, mocker):
assert is_file_processed("test_file.c") is False

def test_is_file_processed_not_processed(self, mocker):
"""Test is_file_processed function when the file has not been processed."""
"""Test is_file_processed function when
the file has not been processed."""
# Mock os.path.exists to return True
mocker.patch("os.path.exists", return_value=True)
# Mock builtins.open to return a file without the filename in it
Expand All @@ -324,7 +353,8 @@ def test_main_processed_files(self, mocker, capsys):
with pytest.raises(SystemExit):
main()
captured = capsys.readouterr()
assert "The files have already been processed. Skipping.\n" == captured.out
assert "The files have already been processed.\
Skipping.\n" == captured.out

def test_main_no_args(self, mocker, capsys):
"""Test main function with no arguments."""
Expand All @@ -335,7 +365,8 @@ def test_main_no_args(self, mocker, capsys):
main()

captured = capsys.readouterr()
assert "Usage: python -m betty_fixer_package.betty_fixer " in captured.out
assert "Usage: python -m \
betty_fixer_package.betty_fixer " in captured.out

def test_main_header_option(self, mocker):
"""Test main function with -H option."""
Expand All @@ -358,7 +389,8 @@ def test_main_header_option_no_header(self, mocker, capsys):
mocker.patch("sys.argv", ["betty_fixer", "-H"])
main()
capured = capsys.readouterr()
assert "\x1b[31mUsage : bettyfixer -H <heahdername>.h\x1b[39m" in capured.out
assert "\x1b[31mUsage : bettyfixer -H \
<heahdername>.h\x1b[39m" in capured.out

def test_main_header_print_assertion(self, mocker):
"""Test main function with -H option but no header.
Expand Down
24 changes: 16 additions & 8 deletions tests/test_errors_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ def test_exctract_errors_no_errors(mocker):
"""
Test the exctract_errors function when Betty does not return any errors.
"""
mock_subprocess = mocker.patch('subprocess.run', return_value=CompletedProcess(
['betty', 'file1.c'], 0, 'output', ''))
mock_subprocess = mocker.patch(
'subprocess.run', return_value=CompletedProcess(
['betty', 'file1.c'], 0, 'output', '')
)
mock_open = mocker.patch('builtins.open', mocker.mock_open())

exctract_errors('file1.c', 'errors.txt')

mock_subprocess.assert_called_once_with(['betty', 'file1.c'],
capture_output=True, text=True, check=True)
mock_subprocess.assert_called_once_with(
['betty', 'file1.c'],
capture_output=True, text=True, check=True
)
mock_open.assert_called_once_with('errors.txt', 'a', encoding='utf-8')
mock_open().write.assert_called_once_with('output')

Expand All @@ -25,12 +29,16 @@ def test_exctract_errors_with_errors(mocker):
"""
Test the exctract_errors function when Betty returns errors.
"""
mock_subprocess = mocker.patch('subprocess.run', side_effect=CalledProcessError(
['betty', 'file1.c'], 1, 'output', 'error'))
mock_subprocess = mocker.patch(
'subprocess.run', side_effect=CalledProcessError(
['betty', 'file1.c'], 1, 'output', 'error')
)
mock_open = mocker.patch('builtins.open', mocker.mock_open())

exctract_errors('file1.c', 'errors.txt')

mock_subprocess.assert_called_once_with(['betty', 'file1.c'],
capture_output=True, text=True, check=True)
mock_subprocess.assert_called_once_with(
['betty', 'file1.c'],
capture_output=True, text=True, check=True
)
mock_open.assert_called_once_with('errors.txt', 'a', encoding='utf-8')

0 comments on commit d968055

Please sign in to comment.