Skip to content

Commit 2c0c0d6

Browse files
committed
Add tests
1 parent a19aadc commit 2c0c0d6

File tree

9 files changed

+27
-3
lines changed

9 files changed

+27
-3
lines changed

pylint/lint/expand_modules.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@ def discover_package_path(
3737
for source_root in source_roots:
3838
source_root = os.path.realpath(os.path.expanduser(source_root))
3939
if os.path.commonpath([source_root, dirname]) == source_root:
40-
#return cast(Sequence[str], [source_root])
4140
return [source_root]
4241

4342
if len(source_roots) != 0:
4443
return source_roots
4544

4645
# Fall back to legacy discovery by looking for __init__.py upwards as
47-
# it's the only way given that source root was not found or was not provided
46+
# source_roots was not provided
4847
while True:
4948
if not os.path.exists(os.path.join(dirname, "__init__.py")):
5049
return [dirname]

tests/lint/unittest_expand_modules.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import pytest
1515

1616
from pylint.checkers import BaseChecker
17-
from pylint.lint.expand_modules import _is_in_ignore_list_re, expand_modules
17+
from pylint.lint.expand_modules import _is_in_ignore_list_re, discover_package_path, expand_modules
1818
from pylint.testutils import CheckerTestCase, set_config
1919
from pylint.typing import MessageDefinitionTuple, ModuleDescriptionDict
2020

@@ -306,3 +306,13 @@ def test_expand_modules_with_ignore(
306306
)
307307
assert modules == expected
308308
assert not errors
309+
310+
def test_discover_package_path_no_source_root_overlap(tmp_path):
311+
"""Test whether source_roots is returned even if module doesn't overlap
312+
with source_roots"""
313+
source_roots = [tmp_path]
314+
package_paths = discover_package_path(__file__, source_roots)
315+
316+
expected = source_roots
317+
assert(package_paths == expected)
318+

tests/regrtest_data/source_roots_implicit_namespace_pkg/src/namespacepkg/pkg/__init__.py

Whitespace-only changes.

tests/regrtest_data/source_roots_implicit_namespace_pkg/src/namespacepkg/pkg/app.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
""" Test for source-roots import for implicit namespace package. The following
2+
should succeed."""
3+
import namespacepkg.pkg.app

tests/regrtest_data/source_roots_src_layout/src/mypkg/__init__.py

Whitespace-only changes.

tests/regrtest_data/source_roots_src_layout/src/mypkg/app.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
""" Test for import. The following import should work if source-roots is setup correctly """
2+
import mypkg.app

tests/test_self.py

+10
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ def test_exit_zero(self) -> None:
208208
["--exit-zero", join(HERE, "regrtest_data", "syntax_error.py")], code=0
209209
)
210210

211+
@pytest.mark.parametrize(
212+
"repo", ["source_roots_src_layout", "source_roots_implicit_namespace_pkg"]
213+
)
214+
def test_source_roots_src_layout(self, repo: str) -> None:
215+
repo = join(HERE, "regrtest_data",repo)
216+
src_path = join(repo, "src")
217+
self._runtest(
218+
["-d", "unused-import", f"--source-roots={src_path}", repo], code=0
219+
)
220+
211221
def test_nonexistent_config_file(self) -> None:
212222
self._runtest(["--rcfile=/tmp/this_file_does_not_exist"], code=32)
213223

0 commit comments

Comments
 (0)