mirror of
https://github.com/bndr/pipreqs.git
synced 2025-06-06 03:25:21 +00:00
Merge pull request #389 from mateuslatrova/compare_modules_test
Add unit test for "compare_modules" function
This commit is contained in:
commit
6f232bd080
@ -149,10 +149,6 @@ def get_all_imports(
|
|||||||
return list(packages - data)
|
return list(packages - data)
|
||||||
|
|
||||||
|
|
||||||
def filter_line(line):
|
|
||||||
return len(line) > 0 and line[0] != "#"
|
|
||||||
|
|
||||||
|
|
||||||
def generate_requirements_file(path, imports, symbol):
|
def generate_requirements_file(path, imports, symbol):
|
||||||
with _open(path, "w") as out_file:
|
with _open(path, "w") as out_file:
|
||||||
logging.debug('Writing {num} requirements: {imports} to {file}'.format(
|
logging.debug('Writing {num} requirements: {imports} to {file}'.format(
|
||||||
@ -319,7 +315,7 @@ def parse_requirements(file_):
|
|||||||
OSerror: If there's any issues accessing the file.
|
OSerror: If there's any issues accessing the file.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
tuple: The contents of the file, excluding comments.
|
list: The contents of the file, excluding comments.
|
||||||
"""
|
"""
|
||||||
modules = []
|
modules = []
|
||||||
# For the dependency identifier specification, see
|
# For the dependency identifier specification, see
|
||||||
@ -357,7 +353,6 @@ def parse_requirements(file_):
|
|||||||
|
|
||||||
return modules
|
return modules
|
||||||
|
|
||||||
|
|
||||||
def compare_modules(file_, imports):
|
def compare_modules(file_, imports):
|
||||||
"""Compare modules in a file to imported modules in a project.
|
"""Compare modules in a file to imported modules in a project.
|
||||||
|
|
||||||
@ -366,8 +361,8 @@ def compare_modules(file_, imports):
|
|||||||
imports (tuple): Modules being imported in the project.
|
imports (tuple): Modules being imported in the project.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
tuple: The modules not imported in the project, but do exist in the
|
set: The modules not imported in the project, but do exist in the
|
||||||
specified file.
|
specified file.
|
||||||
"""
|
"""
|
||||||
modules = parse_requirements(file_)
|
modules = parse_requirements(file_)
|
||||||
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
pandas==2.0.0
|
pandas==2.0.0
|
||||||
numpy>=1.2.3
|
numpy>=1.2.3
|
||||||
torch<4.0.0
|
torch<4.0.0
|
@ -66,8 +66,17 @@ class TestPipreqs(unittest.TestCase):
|
|||||||
|
|
||||||
self.project_clean = os.path.join(os.path.dirname(__file__), "_data_clean")
|
self.project_clean = os.path.join(os.path.dirname(__file__), "_data_clean")
|
||||||
self.project_invalid = os.path.join(os.path.dirname(__file__), "_invalid_data")
|
self.project_invalid = os.path.join(os.path.dirname(__file__), "_invalid_data")
|
||||||
|
self.parsed_packages = [
|
||||||
|
{"name": "pandas", "version": "2.0.0"},
|
||||||
|
{"name": "numpy", "version": "1.2.3"},
|
||||||
|
{"name": "torch", "version": "4.0.0"},
|
||||||
|
]
|
||||||
|
self.empty_filepath = os.path.join(self.project, "empty.txt")
|
||||||
|
self.imports_filepath = os.path.join(self.project, "imports.txt")
|
||||||
|
|
||||||
self.project_with_ignore_directory = os.path.join(os.path.dirname(__file__), "_data_ignore")
|
self.project_with_ignore_directory = os.path.join(os.path.dirname(__file__), "_data_ignore")
|
||||||
self.project_with_duplicated_deps = os.path.join(os.path.dirname(__file__), "_data_duplicated_deps")
|
self.project_with_duplicated_deps = os.path.join(os.path.dirname(__file__), "_data_duplicated_deps")
|
||||||
|
|
||||||
self.requirements_path = os.path.join(self.project, "requirements.txt")
|
self.requirements_path = os.path.join(self.project, "requirements.txt")
|
||||||
self.alt_requirement_path = os.path.join(self.project, "requirements2.txt")
|
self.alt_requirement_path = os.path.join(self.project, "requirements2.txt")
|
||||||
|
|
||||||
@ -438,6 +447,31 @@ class TestPipreqs(unittest.TestCase):
|
|||||||
data = f.read().lower()
|
data = f.read().lower()
|
||||||
self.assertTrue(cleaned_module not in data)
|
self.assertTrue(cleaned_module not in data)
|
||||||
|
|
||||||
|
def test_compare_modules(self):
|
||||||
|
test_cases = [
|
||||||
|
(self.empty_filepath, [], set()), # both empty
|
||||||
|
(self.empty_filepath, self.parsed_packages, set()), # only file empty
|
||||||
|
(
|
||||||
|
self.imports_filepath,
|
||||||
|
[],
|
||||||
|
set(package["name"] for package in self.parsed_packages),
|
||||||
|
), # only imports empty
|
||||||
|
(self.imports_filepath, self.parsed_packages, set()), # no difference
|
||||||
|
(
|
||||||
|
self.imports_filepath,
|
||||||
|
self.parsed_packages[1:],
|
||||||
|
set([self.parsed_packages[0]["name"]]),
|
||||||
|
), # common case
|
||||||
|
]
|
||||||
|
|
||||||
|
for test_case in test_cases:
|
||||||
|
with self.subTest(test_case):
|
||||||
|
filename, imports, expected_modules_not_imported = test_case
|
||||||
|
|
||||||
|
modules_not_imported = pipreqs.compare_modules(filename, imports)
|
||||||
|
|
||||||
|
self.assertSetEqual(modules_not_imported, expected_modules_not_imported)
|
||||||
|
|
||||||
def test_output_requirements(self):
|
def test_output_requirements(self):
|
||||||
"""
|
"""
|
||||||
Test --print parameter
|
Test --print parameter
|
||||||
|
Loading…
x
Reference in New Issue
Block a user