|
3 | 3 | import tempfile
|
4 | 4 | import unittest
|
5 | 5 | from pathlib import Path
|
| 6 | +from textwrap import dedent |
| 7 | + |
| 8 | +from pip._internal.req.req_install import InstallRequirement |
6 | 9 |
|
7 | 10 | from python.pip_install.parse_requirements_to_bzl import (
|
8 | 11 | generate_parsed_requirements_contents,
|
| 12 | + parse_install_requirements, |
9 | 13 | parse_whl_library_args,
|
10 | 14 | )
|
11 | 15 |
|
12 | 16 |
|
13 | 17 | class TestParseRequirementsToBzl(unittest.TestCase):
|
| 18 | + maxDiff = None |
| 19 | + |
14 | 20 | def test_generated_requirements_bzl(self) -> None:
|
15 | 21 | with tempfile.TemporaryDirectory() as temp_dir:
|
16 | 22 | requirements_lock = Path(temp_dir) / "requirements.txt"
|
@@ -64,6 +70,55 @@ def test_generated_requirements_bzl(self) -> None:
|
64 | 70 | # Assert it gets set to an empty dict by default.
|
65 | 71 | self.assertIn("'environment': {}", contents, contents)
|
66 | 72 |
|
| 73 | + def test_parse_install_requirements_with_args(self): |
| 74 | + # Test requirements files with varying arguments |
| 75 | + for requirement_args in ("", "--index-url https://index.python.com"): |
| 76 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 77 | + requirements_lock = Path(temp_dir) / "requirements.txt" |
| 78 | + requirements_lock.write_text( |
| 79 | + dedent( |
| 80 | + """\ |
| 81 | + {} |
| 82 | +
|
| 83 | + wheel==0.37.1 \\ |
| 84 | + --hash=sha256:4bdcd7d840138086126cd09254dc6195fb4fc6f01c050a1d7236f2630db1d22a \\ |
| 85 | + --hash=sha256:e9a504e793efbca1b8e0e9cb979a249cf4a0a7b5b8c9e8b65a5e39d49529c1c4 |
| 86 | + # via -r requirements.in |
| 87 | + setuptools==58.2.0 \\ |
| 88 | + --hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11 \ |
| 89 | + --hash=sha256:2c55bdb85d5bb460bd2e3b12052b677879cffcf46c0c688f2e5bf51d36001145 |
| 90 | + # via -r requirements.in |
| 91 | + """.format( |
| 92 | + requirement_args |
| 93 | + ) |
| 94 | + ) |
| 95 | + ) |
| 96 | + |
| 97 | + install_req_and_lines = parse_install_requirements( |
| 98 | + str(requirements_lock), ["-v"] |
| 99 | + ) |
| 100 | + |
| 101 | + # There should only be two entries for the two requirements |
| 102 | + self.assertEqual(len(install_req_and_lines), 2) |
| 103 | + |
| 104 | + # The first index in each tuple is expected to be an `InstallRequirement` object |
| 105 | + self.assertIsInstance(install_req_and_lines[0][0], InstallRequirement) |
| 106 | + self.assertIsInstance(install_req_and_lines[1][0], InstallRequirement) |
| 107 | + |
| 108 | + # Ensure the requirements text is correctly parsed with the trailing arguments |
| 109 | + self.assertTupleEqual( |
| 110 | + install_req_and_lines[0][1:], |
| 111 | + ( |
| 112 | + "wheel==0.37.1 --hash=sha256:4bdcd7d840138086126cd09254dc6195fb4fc6f01c050a1d7236f2630db1d22a --hash=sha256:e9a504e793efbca1b8e0e9cb979a249cf4a0a7b5b8c9e8b65a5e39d49529c1c4", |
| 113 | + ), |
| 114 | + ) |
| 115 | + self.assertTupleEqual( |
| 116 | + install_req_and_lines[1][1:], |
| 117 | + ( |
| 118 | + "setuptools==58.2.0 --hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11 --hash=sha256:2c55bdb85d5bb460bd2e3b12052b677879cffcf46c0c688f2e5bf51d36001145", |
| 119 | + ), |
| 120 | + ) |
| 121 | + |
67 | 122 |
|
68 | 123 | if __name__ == "__main__":
|
69 | 124 | unittest.main()
|
0 commit comments