Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akamat10 committed Sep 29, 2024
1 parent a19aadc commit 2c0c0d6
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 3 deletions.
3 changes: 1 addition & 2 deletions pylint/lint/expand_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ def discover_package_path(
for source_root in source_roots:
source_root = os.path.realpath(os.path.expanduser(source_root))
if os.path.commonpath([source_root, dirname]) == source_root:
#return cast(Sequence[str], [source_root])
return [source_root]

if len(source_roots) != 0:
return source_roots

# Fall back to legacy discovery by looking for __init__.py upwards as
# it's the only way given that source root was not found or was not provided
# source_roots was not provided
while True:
if not os.path.exists(os.path.join(dirname, "__init__.py")):
return [dirname]
Expand Down
12 changes: 11 additions & 1 deletion tests/lint/unittest_expand_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import pytest

from pylint.checkers import BaseChecker
from pylint.lint.expand_modules import _is_in_ignore_list_re, expand_modules
from pylint.lint.expand_modules import _is_in_ignore_list_re, discover_package_path, expand_modules
from pylint.testutils import CheckerTestCase, set_config
from pylint.typing import MessageDefinitionTuple, ModuleDescriptionDict

Expand Down Expand Up @@ -306,3 +306,13 @@ def test_expand_modules_with_ignore(
)
assert modules == expected
assert not errors

def test_discover_package_path_no_source_root_overlap(tmp_path):
"""Test whether source_roots is returned even if module doesn't overlap
with source_roots"""
source_roots = [tmp_path]
package_paths = discover_package_path(__file__, source_roots)

expected = source_roots
assert(package_paths == expected)

Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
""" Test for source-roots import for implicit namespace package. The following
should succeed."""
import namespacepkg.pkg.app
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions tests/regrtest_data/source_roots_src_layout/tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
""" Test for import. The following import should work if source-roots is setup correctly """
import mypkg.app
10 changes: 10 additions & 0 deletions tests/test_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ def test_exit_zero(self) -> None:
["--exit-zero", join(HERE, "regrtest_data", "syntax_error.py")], code=0
)

@pytest.mark.parametrize(
"repo", ["source_roots_src_layout", "source_roots_implicit_namespace_pkg"]
)
def test_source_roots_src_layout(self, repo: str) -> None:
repo = join(HERE, "regrtest_data",repo)
src_path = join(repo, "src")
self._runtest(
["-d", "unused-import", f"--source-roots={src_path}", repo], code=0
)

def test_nonexistent_config_file(self) -> None:
self._runtest(["--rcfile=/tmp/this_file_does_not_exist"], code=32)

Expand Down

0 comments on commit 2c0c0d6

Please sign in to comment.