11"""Tests for autoimport utility functions, written in pytest"""
22
3+ import venv
34from pathlib import Path
45
56from rope .contrib .autoimport import utils
@@ -22,6 +23,29 @@ def test_get_package_source_pytest(example_external_package_path):
2223 assert source == Source .SITE_PACKAGE
2324
2425
26+ def test_get_package_source_venv_in_project_dir (example_external_package_path , project ):
27+ # Many Python dev tools create .venv folders inside the project directory.
28+ # Modules in such folders should count as SITE_PACKAGE files despite
29+ # technically being inside the project folder.
30+
31+ # Set up actual venv in project folder:
32+ project_venv_path = project .root .pathlib / ".venv"
33+ venv .create (project_venv_path )
34+
35+ # Crude approximation of a package installed into this venv:
36+ project_venv_site_packages_path = next (project_venv_path .glob ("lib/python*/site-packages" ))
37+ project .prefs ["python_path" ].append (project_venv_site_packages_path )
38+ module_path = project_venv_site_packages_path / "foo.py"
39+ module_path .touch ()
40+
41+ # Such directories are normally part of `ignored_resources` (e.g. `.venv`
42+ # is in there by default):
43+ project .prefs ["ignored_resources" ] += [".venv" ]
44+
45+ source = utils .get_package_source (module_path , project , "foo" )
46+ assert source == Source .SITE_PACKAGE
47+
48+
2549def test_get_package_source_typing (typing_path ):
2650 assert utils .get_package_source (typing_path , None , "typing" ) == Source .STANDARD
2751
0 commit comments