Adding only venv use case test

This commit is contained in:
José Alexandre 2025-04-10 18:51:54 -03:00
parent 3b4c4750b3
commit 8083f856db
4 changed files with 32 additions and 3 deletions

View File

@ -49,6 +49,7 @@ from docopt import docopt
import requests import requests
from yarg import json2package from yarg import json2package
from yarg.exceptions import HTTPError from yarg.exceptions import HTTPError
from typing import Optional
from pipreqs import __version__ from pipreqs import __version__
@ -317,9 +318,8 @@ def get_locally_installed_packages(use_venv_packages: bool, encoding="utf-8"):
return packages return packages
def get_import_local(imports, use_venv_packages: bool, encoding="utf-8"): def get_import_local(imports, use_venv_packages: Optional[bool]=False, encoding="utf-8"):
local = get_locally_installed_packages(use_venv_packages=use_venv_packages) local = get_locally_installed_packages(use_venv_packages=use_venv_packages)
print('LOCAL: ', local)
result = [] result = []
for item in imports: for item in imports:
# search through local packages # search through local packages
@ -330,7 +330,6 @@ def get_import_local(imports, use_venv_packages: bool, encoding="utf-8"):
if item in package["exports"] or item == package["name"]: if item in package["exports"] or item == package["name"]:
result.append(package) result.append(package)
print(result)
# removing duplicates of package/version # removing duplicates of package/version
# had to use second method instead of the previous one, # had to use second method instead of the previous one,
# because we have a list in the 'exports' field # because we have a list in the 'exports' field

View File

@ -0,0 +1,12 @@
asposestorage==1.0.2
beautifulsoup4==4.13.3
boto==2.49.0
docopt==0.6.2
Flask==3.1.0
ipython==8.12.3
nose==1.3.7
peewee==3.17.9
pyflakes==3.1.0
Requests==2.32.3
SQLAlchemy==2.0.40
ujson==5.10.0

View File

@ -0,0 +1 @@

View File

@ -149,6 +149,22 @@ class TestPipreqs(unittest.TestCase):
for item in imports_with_info: for item in imports_with_info:
self.assertTrue(item["name"].lower() in self.local) self.assertTrue(item["name"].lower() in self.local)
@patch("pipreqs.pipreqs.get_locally_installed_packages")
def test_get_import_local_only_from_venv(self, mock_get_local_packages):
mock_get_local_packages.return_value = [
{"name": "requests", "version": "2.31.0", "exports": ["requests"]},
{"name": "docopt", "version": "0.6.2", "exports": ["docopt"]},
]
imports = {"requests": "2.31.0", "docopt": "0.6.2", "flask": "3.0.2"}
result = pipreqs.get_import_local(imports, use_venv_packages=True)
self.assertEqual(result, [
{"name": "requests", "version": "2.31.0", "exports": ["requests"]},
{"name": "docopt", "version": "0.6.2", "exports": ["docopt"]},
])
def test_init(self): def test_init(self):
""" """
Test that all modules we will test upon are in requirements file Test that all modules we will test upon are in requirements file
@ -187,6 +203,7 @@ class TestPipreqs(unittest.TestCase):
"--savepath": None, "--savepath": None,
"--print": False, "--print": False,
"--use-local": True, "--use-local": True,
"--only-venv": False,
"--force": True, "--force": True,
"--proxy": None, "--proxy": None,
"--pypi-server": None, "--pypi-server": None,