Skip to content

Commit

Permalink
add test for parse_requirements function
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando-crz authored and alan-barzilay committed Oct 10, 2023
1 parent 3c786e3 commit 12cc1e5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 24 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
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
82 changes: 58 additions & 24 deletions tests/test_pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,35 @@ 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.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")
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 @@ -223,12 +244,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 @@ -267,9 +284,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 @@ -292,9 +307,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 @@ -317,9 +330,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 @@ -344,9 +355,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 @@ -472,6 +481,31 @@ def test_output_requirements(self):
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):
"""
Remove requiremnts.txt files that were written
Expand Down

0 comments on commit 12cc1e5

Please sign in to comment.