-
Notifications
You must be signed in to change notification settings - Fork 7.2k
[deps] raydepsets: lockfile parser #60839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| from typing import List | ||
|
|
||
| from pip_requirements_parser import Requirement, RequirementsFile | ||
|
|
||
|
|
||
| def parse_lock_file(lock_file_path: str): | ||
| rf = RequirementsFile.from_file(lock_file_path) | ||
| return rf.requirements | ||
|
|
||
|
|
||
| def write_lock_file(requirements: List[Requirement], lock_file_path: str): | ||
| with open(lock_file_path, "w") as f: | ||
| for req in requirements: | ||
| f.write(req.requirement_line.line + "\n") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import sys | ||
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from ci.raydepsets.parser import parse_lock_file, write_lock_file | ||
|
|
||
|
|
||
| def test_parse_lock_file(): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| lock_file = Path(tmpdir) / "requirements.txt" | ||
| lock_file.write_text( | ||
| "emoji==2.9.0 \\\n" | ||
| " --hash=sha256:abc123\n" | ||
| "pyperclip==1.6.0 \\\n" | ||
| " --hash=sha256:def456\n" | ||
| ) | ||
| reqs = parse_lock_file(str(lock_file)) | ||
| names = [req.name for req in reqs] | ||
| assert "emoji" in names | ||
| assert "pyperclip" in names | ||
| assert len(reqs) == 2 | ||
|
|
||
|
|
||
| def test_parse_lock_file_with_index_url(): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| lock_file = Path(tmpdir) / "requirements.txt" | ||
| lock_file.write_text( | ||
| "--index-url https://pypi.org/simple\n" | ||
| "\n" | ||
| "emoji==2.9.0 \\\n" | ||
| " --hash=sha256:abc123\n" | ||
| ) | ||
| reqs = parse_lock_file(str(lock_file)) | ||
| assert len(reqs) == 1 | ||
| assert reqs[0].name == "emoji" | ||
|
|
||
|
|
||
| def test_parse_lock_file_empty(): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| lock_file = Path(tmpdir) / "requirements.txt" | ||
| lock_file.write_text("") | ||
| reqs = parse_lock_file(str(lock_file)) | ||
| assert len(reqs) == 0 | ||
|
|
||
|
|
||
| def test_parse_lock_file_comments_only(): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| lock_file = Path(tmpdir) / "requirements.txt" | ||
| lock_file.write_text("# This is a comment\n" "# Another comment\n") | ||
| reqs = parse_lock_file(str(lock_file)) | ||
| assert len(reqs) == 0 | ||
|
|
||
|
|
||
| def test_write_requirements_file(): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| lock_file = Path(tmpdir) / "requirements.txt" | ||
| lock_file.write_text( | ||
| "emoji==2.9.0 \\\n" | ||
| " --hash=sha256:abc123\n" | ||
| "pyperclip==1.6.0 \\\n" | ||
| " --hash=sha256:def456\n" | ||
| ) | ||
| reqs = parse_lock_file(str(lock_file)) | ||
|
|
||
| output_file = Path(tmpdir) / "output.txt" | ||
| write_lock_file(reqs, str(output_file)) | ||
|
|
||
| output_text = output_file.read_text() | ||
| assert "emoji==2.9.0" in output_text | ||
| assert "pyperclip==1.6.0" in output_text | ||
|
|
||
|
|
||
| def test_write_requirements_file_empty(): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| output_file = Path(tmpdir) / "output.txt" | ||
| write_lock_file([], str(output_file)) | ||
| assert output_file.read_text() == "" | ||
|
|
||
|
|
||
| def test_roundtrip_preserves_packages(): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| lock_file = Path(tmpdir) / "requirements.txt" | ||
| lock_file.write_text( | ||
| "emoji==2.9.0 \\\n" | ||
| " --hash=sha256:abc123\n" | ||
| "pyperclip==1.6.0 \\\n" | ||
| " --hash=sha256:def456\n" | ||
| ) | ||
| reqs = parse_lock_file(str(lock_file)) | ||
|
|
||
| output_file = Path(tmpdir) / "output.txt" | ||
| write_lock_file(reqs, str(output_file)) | ||
|
|
||
| reqs2 = parse_lock_file(str(output_file)) | ||
| assert len(reqs) == len(reqs2) | ||
| assert [r.name for r in reqs] == [r.name for r in reqs2] | ||
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(pytest.main(["-v", __file__])) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ aws_requests_auth | |
| tzdata | ||
| tqdm | ||
| networkx | ||
| pip_requirements_parser | ||
| -r requirements-doc.txt | ||
|
|
||
| # Upgrades | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.