|
| 1 | +""" |
| 2 | +License GPLv3 or higher. |
| 3 | +
|
| 4 | +(C) 2026 Created by Maikel Mardjan - https://nocomplexity.com/ |
| 5 | +
|
| 6 | +This program is free software: you can redistribute it and/or modify it under the terms |
| 7 | +of the GNU General Public License as published by the Free Software Foundation, either |
| 8 | +version 3 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | +This program is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 12 | +PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU General Public License along with this |
| 15 | +program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +
|
| 17 | +Unit tests for the filehelpfunctions module. |
| 18 | +""" |
| 19 | + |
| 20 | +from unittest.mock import patch |
| 21 | + |
| 22 | +from pytest import mark |
| 23 | + |
| 24 | +from codeaudit.filehelpfunctions import collect_python_source_files |
| 25 | + |
| 26 | +_EXCLUDE_DIRS = {"docs", "docker", "dist", "tests"} |
| 27 | +_EXAMPLE_DIR = "example_dir" |
| 28 | + |
| 29 | + |
| 30 | +@mark.xfail(reason="'dirs' in the 'collect_python_source_files' is not used.") |
| 31 | +@mark.parametrize("directory", _EXCLUDE_DIRS) |
| 32 | +def test_excluded_dirs(directory): |
| 33 | + """Test that excluded directories are not included in the result. |
| 34 | +
|
| 35 | + Test vector: |
| 36 | + - Directory that is excluded from the search. |
| 37 | +
|
| 38 | + Expected behavior: |
| 39 | + - Walk through a directory that is excluded from the search. |
| 40 | + - Verify that the result is an empty list. |
| 41 | + """ |
| 42 | + with patch("codeaudit.filehelpfunctions.os", autospec=True) as mocked_os: |
| 43 | + mocked_os.walk.return_value = [(".", [directory], ["example.py"])] |
| 44 | + result = collect_python_source_files(directory=directory) |
| 45 | + assert result == [] |
| 46 | + |
| 47 | + |
| 48 | +def test_not_file(): |
| 49 | + """Test that non-file entries are not included in the result. |
| 50 | +
|
| 51 | + Test vector: |
| 52 | + - Directory is not on the excluded list. |
| 53 | + - Tested file is a non-file entry. |
| 54 | +
|
| 55 | + Expected behavior: |
| 56 | + - Walk through a directory with a non-file entry. |
| 57 | + - Verify that the result is an empty list. |
| 58 | + """ |
| 59 | + with patch("codeaudit.filehelpfunctions.os", autospec=True) as mocked_os: |
| 60 | + mocked_os.walk.return_value = [(".", [_EXAMPLE_DIR], ["example.py"])] |
| 61 | + mocked_os.path.isfile.return_value = False |
| 62 | + result = collect_python_source_files(directory=_EXAMPLE_DIR) |
| 63 | + assert result == [] |
| 64 | + |
| 65 | + |
| 66 | +def test_not_ast_parsable(): |
| 67 | + """Test that non-AST-parsable files are not included in the result. |
| 68 | +
|
| 69 | + Test vector: |
| 70 | + - Directory is not on the excluded list. |
| 71 | + - Tested file exists. |
| 72 | + - Tested file is a non-AST-parsable file. |
| 73 | +
|
| 74 | + Expected behavior: |
| 75 | + - Walk through a directory with a non-AST-parsable file. |
| 76 | + - Verify that the result is an empty list. |
| 77 | + """ |
| 78 | + with ( |
| 79 | + patch("codeaudit.filehelpfunctions.os", autospec=True) as mocked_os, |
| 80 | + patch( |
| 81 | + "codeaudit.filehelpfunctions.is_ast_parsable", autospec=True |
| 82 | + ) as mocked_is_ast, |
| 83 | + ): |
| 84 | + mocked_os.walk.return_value = [(".", [_EXAMPLE_DIR], ["example.py"])] |
| 85 | + mocked_os.path.isfile.return_value = True |
| 86 | + mocked_is_ast.return_value = False |
| 87 | + result = collect_python_source_files(directory=_EXAMPLE_DIR) |
| 88 | + assert result == [] |
| 89 | + |
| 90 | + |
| 91 | +def test_proper_python_file(): |
| 92 | + """Test that proper Python files are included in the result. |
| 93 | +
|
| 94 | + Test vector: |
| 95 | + - Directory is not on the excluded list. |
| 96 | + - Tested file exists. |
| 97 | + - Tested file is a proper Python file. |
| 98 | +
|
| 99 | + Expected behavior: |
| 100 | + - Walk through a directory with a proper Python file. |
| 101 | + - Verify that the result contains the file path. |
| 102 | + """ |
| 103 | + with ( |
| 104 | + patch("codeaudit.filehelpfunctions.os", autospec=True) as mocked_os, |
| 105 | + patch( |
| 106 | + "codeaudit.filehelpfunctions.is_ast_parsable", autospec=True |
| 107 | + ) as mocked_is_ast, |
| 108 | + ): |
| 109 | + mocked_os.walk.return_value = [(".", [_EXAMPLE_DIR], ["example.py"])] |
| 110 | + mocked_os.path.isfile.return_value = True |
| 111 | + mocked_os.path.abspath.return_value = "./example.py" |
| 112 | + mocked_is_ast.return_value = True |
| 113 | + result = collect_python_source_files(directory=_EXAMPLE_DIR) |
| 114 | + assert result == ["./example.py"] |
| 115 | + |
| 116 | + |
| 117 | +def test_file_starts_with_dot(): |
| 118 | + """Test that files starting with a dot are not included in the result. |
| 119 | +
|
| 120 | + Test vector: |
| 121 | + - Directory is not on the excluded list. |
| 122 | + - Tested file exists. |
| 123 | + - Tested file starts with a dot. |
| 124 | +
|
| 125 | + Expected behavior: |
| 126 | + - Walk through a directory with a file starting with a dot. |
| 127 | + - Verify that the result is an empty list. |
| 128 | + """ |
| 129 | + with ( |
| 130 | + patch("codeaudit.filehelpfunctions.os", autospec=True) as mocked_os, |
| 131 | + patch( |
| 132 | + "codeaudit.filehelpfunctions.is_ast_parsable", autospec=True |
| 133 | + ) as mocked_is_ast, |
| 134 | + ): |
| 135 | + mocked_os.walk.return_value = [(".", [_EXAMPLE_DIR], [".example.py"])] |
| 136 | + mocked_os.path.isfile.return_value = True |
| 137 | + mocked_is_ast.return_value = False |
| 138 | + result = collect_python_source_files(directory=_EXAMPLE_DIR) |
| 139 | + assert result == [] |
0 commit comments