mirror of
https://github.com/bndr/pipreqs.git
synced 2025-06-06 03:25:21 +00:00
add test for parse_requirements function
This commit is contained in:
parent
3c786e3537
commit
12cc1e5b74
0
tests/_data/empty.txt
Normal file
0
tests/_data/empty.txt
Normal file
3
tests/_data/imports.txt
Normal file
3
tests/_data/imports.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pandas==2.0.0
|
||||||
|
numpy>=1.2.3
|
||||||
|
torch<4.0.0
|
4
tests/_data/imports_any_version.txt
Normal file
4
tests/_data/imports_any_version.txt
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
numpy
|
||||||
|
pandas==2.0.0
|
||||||
|
tensorflow
|
||||||
|
torch<4.0.0
|
3
tests/_data/imports_no_version.txt
Normal file
3
tests/_data/imports_no_version.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pandas
|
||||||
|
tensorflow
|
||||||
|
torch
|
@ -39,14 +39,35 @@ class TestPipreqs(unittest.TestCase):
|
|||||||
self.modules2 = ["beautifulsoup4"]
|
self.modules2 = ["beautifulsoup4"]
|
||||||
self.local = ["docopt", "requests", "nose", "pyflakes"]
|
self.local = ["docopt", "requests", "nose", "pyflakes"]
|
||||||
self.project = os.path.join(os.path.dirname(__file__), "_data")
|
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_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.project_with_ignore_directory = os.path.join(
|
self.project_with_ignore_directory = os.path.join(os.path.dirname(__file__), "_data_ignore")
|
||||||
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")
|
||||||
|
|
||||||
@ -223,12 +244,8 @@ class TestPipreqs(unittest.TestCase):
|
|||||||
"""
|
"""
|
||||||
import_name_with_alias = "requests as R"
|
import_name_with_alias = "requests as R"
|
||||||
expected_import_name_without_alias = "requests"
|
expected_import_name_without_alias = "requests"
|
||||||
import_name_without_aliases = pipreqs.get_name_without_alias(
|
import_name_without_aliases = pipreqs.get_name_without_alias(import_name_with_alias)
|
||||||
import_name_with_alias
|
self.assertEqual(import_name_without_aliases, expected_import_name_without_alias)
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
import_name_without_aliases, expected_import_name_without_alias
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_custom_pypi_server(self):
|
def test_custom_pypi_server(self):
|
||||||
"""
|
"""
|
||||||
@ -267,9 +284,7 @@ class TestPipreqs(unittest.TestCase):
|
|||||||
"--mode": None,
|
"--mode": None,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
with open(
|
with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f:
|
||||||
os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r"
|
|
||||||
) as f:
|
|
||||||
data = f.read().lower()
|
data = f.read().lower()
|
||||||
for item in ["click", "getpass"]:
|
for item in ["click", "getpass"]:
|
||||||
self.assertFalse(item.lower() in data)
|
self.assertFalse(item.lower() in data)
|
||||||
@ -292,9 +307,7 @@ class TestPipreqs(unittest.TestCase):
|
|||||||
"--mode": "no-pin",
|
"--mode": "no-pin",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
with open(
|
with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f:
|
||||||
os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r"
|
|
||||||
) as f:
|
|
||||||
data = f.read().lower()
|
data = f.read().lower()
|
||||||
for item in ["beautifulsoup4", "boto"]:
|
for item in ["beautifulsoup4", "boto"]:
|
||||||
self.assertTrue(item.lower() in data)
|
self.assertTrue(item.lower() in data)
|
||||||
@ -317,9 +330,7 @@ class TestPipreqs(unittest.TestCase):
|
|||||||
"--mode": "gt",
|
"--mode": "gt",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
with open(
|
with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f:
|
||||||
os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r"
|
|
||||||
) as f:
|
|
||||||
data = f.readlines()
|
data = f.readlines()
|
||||||
for item in data:
|
for item in data:
|
||||||
symbol = ">="
|
symbol = ">="
|
||||||
@ -344,9 +355,7 @@ class TestPipreqs(unittest.TestCase):
|
|||||||
"--mode": "compat",
|
"--mode": "compat",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
with open(
|
with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f:
|
||||||
os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r"
|
|
||||||
) as f:
|
|
||||||
data = f.readlines()
|
data = f.readlines()
|
||||||
for item in data:
|
for item in data:
|
||||||
symbol = "~="
|
symbol = "~="
|
||||||
@ -472,6 +481,31 @@ class TestPipreqs(unittest.TestCase):
|
|||||||
stdout_content = capturedOutput.getvalue().lower()
|
stdout_content = capturedOutput.getvalue().lower()
|
||||||
self.assertTrue(file_content == stdout_content)
|
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):
|
def tearDown(self):
|
||||||
"""
|
"""
|
||||||
Remove requiremnts.txt files that were written
|
Remove requiremnts.txt files that were written
|
||||||
|
Loading…
x
Reference in New Issue
Block a user