Skip to content

Commit

Permalink
add unit test for "get_locally_installed_packages" function
Browse files Browse the repository at this point in the history
  • Loading branch information
willianrocha committed Sep 20, 2023
1 parent a1f83d2 commit ce27276
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid_module
1 change: 1 addition & 0 deletions tests/_local_packages/invalid_package/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
valid_package
tests
24 changes: 20 additions & 4 deletions tests/test_pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"""

import unittest
from unittest.mock import patch
import sys
import os
import requests

Expand Down Expand Up @@ -48,6 +50,10 @@ def setUp(self):
self.project,
"requirements2.txt"
)
self.local_packages_path = os.path.join(
os.path.dirname(__file__),
"_local_packages"
)

def test_get_all_imports(self):
imports = pipreqs.get_all_imports(self.project)
Expand Down Expand Up @@ -109,6 +115,16 @@ def test_get_use_local_only(self):
for item in imports_with_info:
self.assertTrue(item['name'].lower() in self.local)

def test_get_locally_installed_packages(self):
with patch.object(sys, 'path', new=[self.local_packages_path]):
local_installed_packages = pipreqs.get_locally_installed_packages()
expected_output = [
{'exports': [], 'name': 'invalid_package', 'version': None},
{'exports': ['valid_package'], 'name': 'valid_package', 'version': '1.0.0'},

Check failure on line 123 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 line too long (92 > 79 characters) Raw Output: ./tests/test_pipreqs.py:123:80: E501 line too long (92 > 79 characters)
{'exports': [], 'name': 'EGG', 'version': 'INFO'}
]
self.assertEqual(local_installed_packages, expected_output)

def test_init(self):
"""
Test that all modules we will test upon are in requirements file
Expand Down Expand Up @@ -145,7 +161,7 @@ def test_init_savepath(self):
Test that we can save requirements.txt correctly
to a different path
"""
pipreqs.init({'<path>': self.project, '--savepath': self.alt_requirement_path,
pipreqs.init({'<path>': self.project, '--savepath': self.alt_requirement_path,

Check failure on line 164 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 line too long (86 > 79 characters) Raw Output: ./tests/test_pipreqs.py:164:80: E501 line too long (86 > 79 characters)
'--use-local': None, '--proxy':None, '--pypi-server':None, '--print': False,
'--diff': None, '--clean': None, '--mode': None})
assert os.path.exists(self.alt_requirement_path) == 1
Expand All @@ -163,7 +179,7 @@ def test_init_overwrite(self):
"""
with open(self.requirements_path, "w") as f:
f.write("should_not_be_overwritten")
pipreqs.init({'<path>': self.project, '--savepath': None, '--use-local': None,
pipreqs.init({'<path>': self.project, '--savepath': None, '--use-local': None,

Check failure on line 182 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 line too long (86 > 79 characters) Raw Output: ./tests/test_pipreqs.py:182:80: E501 line too long (86 > 79 characters)
'--force': None, '--proxy':None, '--pypi-server':None, '--print': False,
'--diff': None, '--clean': None, '--mode': None})
assert os.path.exists(self.requirements_path) == 1
Expand Down Expand Up @@ -203,7 +219,7 @@ def test_ignored_directory(self):
Test --ignore parameter
"""
pipreqs.init(
{'<path>': self.project_with_ignore_directory, '--savepath': None,
{'<path>': self.project_with_ignore_directory, '--savepath': None,
'--print': False, '--use-local': None, '--force': True,
'--proxy':None, '--pypi-server':None,
'--ignore':'.ignored_dir,.ignore_second',
Expand All @@ -222,7 +238,7 @@ def test_dynamic_version_no_pin_scheme(self):
Test --mode=no-pin
"""
pipreqs.init(
{'<path>': self.project_with_ignore_directory, '--savepath': None,
{'<path>': self.project_with_ignore_directory, '--savepath': None,
'--print': False, '--use-local': None, '--force': True,
'--proxy': None, '--pypi-server': None,
'--diff': None,
Expand Down

0 comments on commit ce27276

Please sign in to comment.