From ce272765f46eac0f722c34fdc96bef185977dde6 Mon Sep 17 00:00:00 2001 From: Willian Rocha Date: Wed, 20 Sep 2023 16:36:46 -0300 Subject: [PATCH] add unit test for "get_locally_installed_packages" function --- .../invalid_module/EGG-INFO/top_level.txt | 1 + .../invalid_package/top_level.txt | 1 + .../top_level.txt | 2 ++ tests/test_pipreqs.py | 24 +++++++++++++++---- 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 tests/_local_packages/invalid_module/EGG-INFO/top_level.txt create mode 100644 tests/_local_packages/invalid_package/top_level.txt create mode 100644 tests/_local_packages/valid_package-1.0.0.dist-info/top_level.txt diff --git a/tests/_local_packages/invalid_module/EGG-INFO/top_level.txt b/tests/_local_packages/invalid_module/EGG-INFO/top_level.txt new file mode 100644 index 0000000..36f50e2 --- /dev/null +++ b/tests/_local_packages/invalid_module/EGG-INFO/top_level.txt @@ -0,0 +1 @@ +invalid_module diff --git a/tests/_local_packages/invalid_package/top_level.txt b/tests/_local_packages/invalid_package/top_level.txt new file mode 100644 index 0000000..2b29f27 --- /dev/null +++ b/tests/_local_packages/invalid_package/top_level.txt @@ -0,0 +1 @@ +tests diff --git a/tests/_local_packages/valid_package-1.0.0.dist-info/top_level.txt b/tests/_local_packages/valid_package-1.0.0.dist-info/top_level.txt new file mode 100644 index 0000000..6dbce5c --- /dev/null +++ b/tests/_local_packages/valid_package-1.0.0.dist-info/top_level.txt @@ -0,0 +1,2 @@ +valid_package +tests diff --git a/tests/test_pipreqs.py b/tests/test_pipreqs.py index f82d3db..8f7cc25 100644 --- a/tests/test_pipreqs.py +++ b/tests/test_pipreqs.py @@ -9,6 +9,8 @@ """ import unittest +from unittest.mock import patch +import sys import os import requests @@ -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) @@ -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'}, + {'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 @@ -145,7 +161,7 @@ def test_init_savepath(self): Test that we can save requirements.txt correctly to a different path """ - pipreqs.init({'': self.project, '--savepath': self.alt_requirement_path, + pipreqs.init({'': self.project, '--savepath': self.alt_requirement_path, '--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 @@ -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({'': self.project, '--savepath': None, '--use-local': None, + pipreqs.init({'': self.project, '--savepath': None, '--use-local': None, '--force': None, '--proxy':None, '--pypi-server':None, '--print': False, '--diff': None, '--clean': None, '--mode': None}) assert os.path.exists(self.requirements_path) == 1 @@ -203,7 +219,7 @@ def test_ignored_directory(self): Test --ignore parameter """ pipreqs.init( - {'': self.project_with_ignore_directory, '--savepath': None, + {'': self.project_with_ignore_directory, '--savepath': None, '--print': False, '--use-local': None, '--force': True, '--proxy':None, '--pypi-server':None, '--ignore':'.ignored_dir,.ignore_second', @@ -222,7 +238,7 @@ def test_dynamic_version_no_pin_scheme(self): Test --mode=no-pin """ pipreqs.init( - {'': self.project_with_ignore_directory, '--savepath': None, + {'': self.project_with_ignore_directory, '--savepath': None, '--print': False, '--use-local': None, '--force': True, '--proxy': None, '--pypi-server': None, '--diff': None,