def test_pylintrc_parentdir() -> None: with tempdir() as chroot: create_files([ "a/pylintrc", "a/b/__init__.py", "a/b/pylintrc", "a/b/c/__init__.py", "a/b/c/d/__init__.py", "a/b/c/d/e/.pylintrc", ]) with fake_home(): with pytest.warns(DeprecationWarning): assert config.find_pylintrc() is None results = { "a": join(chroot, "a", "pylintrc"), "a/b": join(chroot, "a", "b", "pylintrc"), "a/b/c": join(chroot, "a", "b", "pylintrc"), "a/b/c/d": join(chroot, "a", "b", "pylintrc"), "a/b/c/d/e": join(chroot, "a", "b", "c", "d", "e", ".pylintrc"), } for basedir, expected in results.items(): os.chdir(join(chroot, basedir)) with pytest.warns(DeprecationWarning): assert config.find_pylintrc() == expected
def test_pylintrc_parentdir() -> None: """Test that the first pylintrc we find is the first parent directory.""" with tempdir() as chroot: chroot_path = Path(chroot) testutils.create_files( [ "a/pylintrc", "a/b/__init__.py", "a/b/pylintrc", "a/b/c/__init__.py", "a/b/c/d/__init__.py", "a/b/c/d/e/.pylintrc", ] ) with fake_home(): assert not list(config.find_default_config_files()) results = { "a": chroot_path / "a" / "pylintrc", "a/b": chroot_path / "a" / "b" / "pylintrc", "a/b/c": chroot_path / "a" / "b" / "pylintrc", "a/b/c/d": chroot_path / "a" / "b" / "pylintrc", "a/b/c/d/e": chroot_path / "a" / "b" / "c" / "d" / "e" / ".pylintrc", } for basedir, expected in results.items(): os.chdir(chroot_path / basedir) assert next(config.find_default_config_files()) == expected
def test_two_similar_args(fake_path, case): with tempdir() as chroot: create_files(["a/b/__init__.py", "a/c/__init__.py"]) expected = [join(chroot, "a")] + fake_path assert sys.path == fake_path with lint.fix_import_path(case): assert sys.path == expected assert sys.path == fake_path
def test_verbose_output_no_config(capsys: CaptureFixture) -> None: """Test that we print a log message in verbose mode with no file.""" with tempdir() as chroot: with fake_home(): chroot_path = Path(chroot) testutils.create_files(["a/b/c/d/__init__.py"]) os.chdir(chroot_path / "a/b/c") with pytest.raises(SystemExit): Run(["--verbose"]) out = capsys.readouterr() assert "No config file found, using default configuration" in out.err
def test_more_args(fake_path, case): with tempdir() as chroot: create_files(["a/b/c/__init__.py", "a/d/__init__.py", "a/e/f.py"]) expected = [ join(chroot, suffix) for suffix in (sep.join(("a", "b")), "a", sep.join(("a", "e"))) ] + fake_path assert sys.path == fake_path with lint.fix_import_path(case): assert sys.path == expected assert sys.path == fake_path
def test_verbose_abbreviation(capsys: CaptureFixture) -> None: """Test that we correctly handle an abbreviated pre-processable option.""" with tempdir() as chroot: with fake_home(): chroot_path = Path(chroot) testutils.create_files(["a/b/c/d/__init__.py"]) os.chdir(chroot_path / "a/b/c") with pytest.raises(SystemExit): Run(["--ve"]) out = capsys.readouterr() # This output only exists when launched in verbose mode assert "No config file found, using default configuration" in out.err
def test_import_sibling_module_from_namespace( initialized_linter: PyLinter) -> None: """If the parent directory above `namespace` is on sys.path, ensure that modules under `namespace` can import each other without raising `import-error`.""" linter = initialized_linter with tempdir() as tmpdir: create_files(["namespace/submodule1.py", "namespace/submodule2.py"]) second_path = Path("namespace/submodule2.py") with open(second_path, "w", encoding="utf-8") as f: f.write("""\"\"\"This module imports submodule1.\"\"\" import submodule1 print(submodule1) """) os.chdir("namespace") # Add the parent directory to sys.path with fix_import_path([tmpdir]): linter.check(["submodule2.py"]) assert not linter.stats.by_msg
def test_pylintrc_parentdir_no_package() -> None: """Test that we don't find a pylintrc in sub-packages.""" with tempdir() as chroot: with fake_home(): chroot_path = Path(chroot) testutils.create_files( ["a/pylintrc", "a/b/pylintrc", "a/b/c/d/__init__.py"] ) assert config.find_pylintrc() is None results = { "a": chroot_path / "a" / "pylintrc", "a/b": chroot_path / "a" / "b" / "pylintrc", "a/b/c": None, "a/b/c/d": None, } for basedir, expected in results.items(): os.chdir(chroot_path / basedir) assert next(config.find_default_config_files(), None) == expected
sys.path[:] = fake yield fake sys.path[:] = orig def test_no_args(fake_path: list[int]) -> None: with lint.fix_import_path([]): assert sys.path == fake_path assert sys.path == fake_path @pytest.mark.parametrize( "case", [["a/b/"], ["a/b"], ["a/b/__init__.py"], ["a/"], ["a"]]) def test_one_arg(fake_path: list[str], case: list[str]) -> None: with tempdir() as chroot: create_files(["a/b/__init__.py"]) expected = [join(chroot, "a")] + fake_path assert sys.path == fake_path with lint.fix_import_path(case): assert sys.path == expected assert sys.path == fake_path @pytest.mark.parametrize( "case", [ ["a/b", "a/c"], ["a/c/", "a/b/"], ["a/b/__init__.py", "a/c/__init__.py"], ["a", "a/c/__init__.py"],