Skip to content

Commit

Permalink
add test for "compare_modules" function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateus Latrova committed Sep 28, 2023
1 parent 6ac4357 commit 98fb469
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
Empty file added tests/_data/empty.txt
Empty file.
3 changes: 3 additions & 0 deletions tests/_data/imports.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pandas==2.0.0
numpy>=1.2.3
torch<4.0.0
20 changes: 20 additions & 0 deletions tests/test_pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def setUp(self):
self.project = os.path.join(os.path.dirname(__file__), "_data")
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.parsed_packages = [{'name': 'pandas', 'version': '2.0.0'}, {'name': 'numpy', 'version': '1.2.3'}, {'name': 'torch', 'version': '4.0.0'}]

Check failure on line 42 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 line too long (149 > 120 characters) Raw Output: ./tests/test_pipreqs.py:42:121: E501 line too long (149 > 120 characters)
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"
)
Expand Down Expand Up @@ -427,6 +430,23 @@ def test_clean_with_imports_to_clean(self):
data = f.read().lower()
self.assertTrue(cleaned_module not in data)

def test_compare_modules(self):
test_cases = [
( self.empty_filepath, [], set() ), # both empty

Check failure on line 435 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 whitespace after '(' Raw Output: ./tests/test_pipreqs.py:435:14: E201 whitespace after '('

Check failure on line 435 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 whitespace before ')' Raw Output: ./tests/test_pipreqs.py:435:45: E202 whitespace before ')'

Check failure on line 435 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 at least two spaces before inline comment Raw Output: ./tests/test_pipreqs.py:435:48: E261 at least two spaces before inline comment
( self.empty_filepath, self.parsed_packages, set() ), # only file empty

Check failure on line 436 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 whitespace after '(' Raw Output: ./tests/test_pipreqs.py:436:14: E201 whitespace after '('

Check failure on line 436 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 whitespace before ')' Raw Output: ./tests/test_pipreqs.py:436:63: E202 whitespace before ')'

Check failure on line 436 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 at least two spaces before inline comment Raw Output: ./tests/test_pipreqs.py:436:66: E261 at least two spaces before inline comment
( self.imports_filepath, [], set(package["name"] for package in self.parsed_packages) ), # only imports empty

Check failure on line 437 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 whitespace after '(' Raw Output: ./tests/test_pipreqs.py:437:14: E201 whitespace after '('

Check failure on line 437 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 whitespace before ')' Raw Output: ./tests/test_pipreqs.py:437:98: E202 whitespace before ')'
( 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 tearDown(self):
"""
Remove requiremnts.txt files that were written
Expand Down

0 comments on commit 98fb469

Please sign in to comment.