Skip to content

Commit

Permalink
Merge branch 'next' into compare_modules_test
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-barzilay authored Oct 10, 2023
2 parents ed46d27 + 12cc1e5 commit 2ebfc46
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Installation
------------

::
.. code-block:: sh
pip install pipreqs
Expand Down
4 changes: 4 additions & 0 deletions tests/_data/imports_any_version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
numpy
pandas==2.0.0
tensorflow
torch<4.0.0
3 changes: 3 additions & 0 deletions tests/_data/imports_no_version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pandas
tensorflow
torch
129 changes: 105 additions & 24 deletions tests/test_pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
Tests for `pipreqs` module.
"""

import io
import sys
import unittest
import os
import requests
Expand Down Expand Up @@ -37,6 +39,31 @@ def setUp(self):
self.modules2 = ["beautifulsoup4"]
self.local = ["docopt", "requests", "nose", "pyflakes"]
self.project = os.path.join(os.path.dirname(__file__), "_data")
self.empty_filepath = os.path.join(self.project, "empty.txt")
self.imports_filepath = os.path.join(self.project, "imports.txt")
self.imports_no_version_filepath = os.path.join(self.project, "imports_no_version.txt")
self.imports_any_version_filepath = os.path.join(self.project, "imports_any_version.txt")
self.non_existent_filepath = os.path.join(self.project, "non_existent_file.txt")

self.parsed_packages = [
{"name": "pandas", "version": "2.0.0"},
{"name": "numpy", "version": "1.2.3"},
{"name": "torch", "version": "4.0.0"},
]

self.parsed_packages_no_version = [
{"name": "pandas", "version": None},
{"name": "tensorflow", "version": None},
{"name": "torch", "version": None},
]

self.parsed_packages_any_version = [
{"name": "numpy", "version": None},
{"name": "pandas", "version": "2.0.0"},
{"name": "tensorflow", "version": None},
{"name": "torch", "version": "4.0.0"},
]

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 = [
Expand All @@ -46,12 +73,10 @@ def setUp(self):
]
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_duplicated_deps = os.path.join(
os.path.dirname(__file__), "_data_duplicated_deps"
)

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")

Check warning on line 79 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 blank line contains whitespace Raw Output: ./tests/test_pipreqs.py:79:1: W293 blank line contains whitespace
self.requirements_path = os.path.join(self.project, "requirements.txt")
self.alt_requirement_path = os.path.join(self.project, "requirements2.txt")

Expand Down Expand Up @@ -228,12 +253,8 @@ def test_get_import_name_without_alias(self):
"""
import_name_with_alias = "requests as R"
expected_import_name_without_alias = "requests"
import_name_without_aliases = pipreqs.get_name_without_alias(
import_name_with_alias
)
self.assertEqual(
import_name_without_aliases, expected_import_name_without_alias
)
import_name_without_aliases = pipreqs.get_name_without_alias(import_name_with_alias)
self.assertEqual(import_name_without_aliases, expected_import_name_without_alias)

def test_custom_pypi_server(self):
"""
Expand Down Expand Up @@ -272,9 +293,7 @@ def test_ignored_directory(self):
"--mode": None,
}
)
with open(
os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r"
) as f:
with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f:
data = f.read().lower()
for item in ["click", "getpass"]:
self.assertFalse(item.lower() in data)
Expand All @@ -297,9 +316,7 @@ def test_dynamic_version_no_pin_scheme(self):
"--mode": "no-pin",
}
)
with open(
os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r"
) as f:
with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f:
data = f.read().lower()
for item in ["beautifulsoup4", "boto"]:
self.assertTrue(item.lower() in data)
Expand All @@ -322,9 +339,7 @@ def test_dynamic_version_gt_scheme(self):
"--mode": "gt",
}
)
with open(
os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r"
) as f:
with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f:
data = f.readlines()
for item in data:
symbol = ">="
Expand All @@ -349,9 +364,7 @@ def test_dynamic_version_compat_scheme(self):
"--mode": "compat",
}
)
with open(
os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r"
) as f:
with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f:
data = f.readlines()
for item in data:
symbol = "~="
Expand Down Expand Up @@ -458,6 +471,74 @@ def test_compare_modules(self):
modules_not_imported = pipreqs.compare_modules(filename, imports)

self.assertSetEqual(modules_not_imported, expected_modules_not_imported)

Check warning on line 474 in tests/test_pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 blank line contains whitespace Raw Output: ./tests/test_pipreqs.py:474:1: W293 blank line contains whitespace
def test_output_requirements(self):
"""
Test --print parameter
It should print to stdout the same content as requeriments.txt
"""

capturedOutput = io.StringIO()
sys.stdout = capturedOutput

pipreqs.init(
{
"<path>": self.project,
"--savepath": None,
"--print": True,
"--use-local": None,
"--force": None,
"--proxy": None,
"--pypi-server": None,
"--diff": None,
"--clean": None,
"--mode": None,
}
)
pipreqs.init(
{
"<path>": self.project,
"--savepath": None,
"--print": False,
"--use-local": None,
"--force": True,
"--proxy": None,
"--pypi-server": None,
"--diff": None,
"--clean": None,
"--mode": None,
}
)

with open(self.requirements_path, "r") as f:
file_content = f.read().lower()
stdout_content = capturedOutput.getvalue().lower()
self.assertTrue(file_content == stdout_content)

def test_parse_requirements(self):
"""
Test parse_requirements function
"""
test_cases = [
(self.empty_filepath, []), # empty file
(self.imports_filepath, self.parsed_packages), # imports with versions
(
self.imports_no_version_filepath,
self.parsed_packages_no_version,
), # imports without versions
(
self.imports_any_version_filepath,
self.parsed_packages_any_version,
), # imports with and without versions
]

for test in test_cases:
with self.subTest(test):
filename, expected_parsed_requirements = test

parsed_requirements = pipreqs.parse_requirements(filename)

self.assertListEqual(parsed_requirements, expected_parsed_requirements)

def tearDown(self):
"""
Expand Down

0 comments on commit 2ebfc46

Please sign in to comment.