|
13 | 13 | from io import StringIO |
14 | 14 | from os import chdir, getcwd |
15 | 15 | from os.path import abspath, basename, dirname, isdir, join, sep |
| 16 | +from pathlib import Path |
16 | 17 | from shutil import rmtree |
17 | 18 | from typing import Iterable, Iterator, List, Optional, Tuple |
18 | 19 |
|
|
29 | 30 | OLD_DEFAULT_PYLINT_HOME, |
30 | 31 | ) |
31 | 32 | from pylint.exceptions import InvalidMessageError |
32 | | -from pylint.lint import ArgumentPreprocessingError, PyLinter, Run, preprocess_options |
| 33 | +from pylint.lint import ( |
| 34 | + ArgumentPreprocessingError, |
| 35 | + PyLinter, |
| 36 | + Run, |
| 37 | + fix_import_path, |
| 38 | + preprocess_options, |
| 39 | +) |
33 | 40 | from pylint.message import Message |
34 | 41 | from pylint.reporters import text |
35 | 42 | from pylint.typing import MessageLocationTuple |
@@ -868,3 +875,67 @@ def test_by_module_statement_value(initialized_linter: PyLinter) -> None: |
868 | 875 | # Check that the by_module "statement" is equal to the global "statement" |
869 | 876 | # computed for that module |
870 | 877 | assert module_stats["statement"] == linter2.stats.statement |
| 878 | + |
| 879 | + |
| 880 | +@pytest.mark.parametrize( |
| 881 | + "ignore_parameter,ignore_parameter_value", |
| 882 | + [ |
| 883 | + ("--ignore", "failing.py"), |
| 884 | + ("--ignore", "ignored_subdirectory"), |
| 885 | + ("--ignore-patterns", "failing.*"), |
| 886 | + ("--ignore-patterns", "ignored_*"), |
| 887 | + ("--ignore-paths", ".*directory/ignored.*"), |
| 888 | + ("--ignore-paths", ".*ignored.*/failing.*"), |
| 889 | + ], |
| 890 | +) |
| 891 | +def test_recursive_ignore(ignore_parameter, ignore_parameter_value) -> None: |
| 892 | + run = Run( |
| 893 | + [ |
| 894 | + "--recursive", |
| 895 | + "y", |
| 896 | + ignore_parameter, |
| 897 | + ignore_parameter_value, |
| 898 | + join(REGRTEST_DATA_DIR, "directory"), |
| 899 | + ], |
| 900 | + exit=False, |
| 901 | + ) |
| 902 | + |
| 903 | + linted_files = run.linter._iterate_file_descrs( |
| 904 | + tuple(run.linter._discover_files([join(REGRTEST_DATA_DIR, "directory")])) |
| 905 | + ) |
| 906 | + linted_file_paths = [file_item.filepath for file_item in linted_files] |
| 907 | + |
| 908 | + ignored_file = os.path.abspath( |
| 909 | + join(REGRTEST_DATA_DIR, "directory", "ignored_subdirectory", "failing.py") |
| 910 | + ) |
| 911 | + assert ignored_file not in linted_file_paths |
| 912 | + |
| 913 | + for regrtest_data_module in ( |
| 914 | + ("directory", "subdirectory", "subsubdirectory", "module.py"), |
| 915 | + ("directory", "subdirectory", "module.py"), |
| 916 | + ("directory", "package", "module.py"), |
| 917 | + ("directory", "package", "subpackage", "module.py"), |
| 918 | + ): |
| 919 | + module = os.path.abspath(join(REGRTEST_DATA_DIR, *regrtest_data_module)) |
| 920 | + assert module in linted_file_paths |
| 921 | + |
| 922 | + |
| 923 | +def test_import_sibling_module_from_namespace(initialized_linter: PyLinter) -> None: |
| 924 | + """If the parent directory above `namespace` is on sys.path, ensure that |
| 925 | + modules under `namespace` can import each other without raising `import-error`.""" |
| 926 | + linter = initialized_linter |
| 927 | + with tempdir() as tmpdir: |
| 928 | + create_files(["namespace/submodule1.py", "namespace/submodule2.py"]) |
| 929 | + second_path = Path("namespace/submodule2.py") |
| 930 | + with open(second_path, "w", encoding="utf-8") as f: |
| 931 | + f.write( |
| 932 | + """\"\"\"This module imports submodule1.\"\"\" |
| 933 | +import submodule1 |
| 934 | +print(submodule1) |
| 935 | +""" |
| 936 | + ) |
| 937 | + os.chdir("namespace") |
| 938 | + # Add the parent directory to sys.path |
| 939 | + with fix_import_path([tmpdir]): |
| 940 | + linter.check(["submodule2.py"]) |
| 941 | + assert not linter.stats.by_msg |
0 commit comments