|
| 1 | +"""Tests for PEP 658 metadata support.""" |
| 2 | + |
| 3 | +from unittest.mock import Mock, patch |
| 4 | + |
| 5 | +from packaging.version import Version |
| 6 | + |
| 7 | +from fromager.candidate import Candidate, get_metadata_for_wheel |
| 8 | + |
| 9 | + |
| 10 | +class TestPEP658Support: |
| 11 | + """Test PEP 658 metadata support in fromager.""" |
| 12 | + |
| 13 | + def test_candidate_with_metadata_url(self): |
| 14 | + """Test that Candidate can be created with a metadata URL.""" |
| 15 | + candidate = Candidate( |
| 16 | + name="test-package", |
| 17 | + version=Version("1.0.0"), |
| 18 | + url="https://example.com/test-package-1.0.0-py3-none-any.whl", |
| 19 | + metadata_url="https://example.com/test-package-1.0.0-py3-none-any.whl.metadata", |
| 20 | + ) |
| 21 | + |
| 22 | + assert ( |
| 23 | + candidate.metadata_url |
| 24 | + == "https://example.com/test-package-1.0.0-py3-none-any.whl.metadata" |
| 25 | + ) |
| 26 | + |
| 27 | + def test_candidate_without_metadata_url(self): |
| 28 | + """Test that Candidate works without metadata URL (legacy behavior).""" |
| 29 | + candidate = Candidate( |
| 30 | + name="test-package", |
| 31 | + version=Version("1.0.0"), |
| 32 | + url="https://example.com/test-package-1.0.0-py3-none-any.whl", |
| 33 | + ) |
| 34 | + |
| 35 | + assert candidate.metadata_url is None |
| 36 | + |
| 37 | + @patch("fromager.candidate.session") |
| 38 | + def test_get_metadata_with_pep658_success(self, mock_session): |
| 39 | + """Test successful metadata retrieval via PEP 658 endpoint.""" |
| 40 | + # Mock the metadata response |
| 41 | + mock_response = Mock() |
| 42 | + mock_response.content = b"""Metadata-Version: 2.1 |
| 43 | +Name: test-package |
| 44 | +Version: 1.0.0 |
| 45 | +Summary: A test package |
| 46 | +Requires-Dist: requests >= 2.0.0 |
| 47 | +""" |
| 48 | + mock_response.raise_for_status.return_value = None |
| 49 | + mock_session.get.return_value = mock_response |
| 50 | + |
| 51 | + wheel_url = "https://example.com/test-package-1.0.0-py3-none-any.whl" |
| 52 | + metadata_url = ( |
| 53 | + "https://example.com/test-package-1.0.0-py3-none-any.whl.metadata" |
| 54 | + ) |
| 55 | + |
| 56 | + metadata = get_metadata_for_wheel(wheel_url, metadata_url) |
| 57 | + |
| 58 | + # Verify the metadata was parsed correctly |
| 59 | + assert metadata["Name"] == "test-package" |
| 60 | + assert metadata["Version"] == "1.0.0" |
| 61 | + assert metadata["Summary"] == "A test package" |
| 62 | + assert "requests >= 2.0.0" in metadata.get_all("Requires-Dist", []) |
| 63 | + |
| 64 | + # Verify only the metadata URL was called, not the wheel URL |
| 65 | + mock_session.get.assert_called_once_with(metadata_url) |
| 66 | + |
| 67 | + @patch("fromager.candidate.session") |
| 68 | + def test_get_metadata_pep658_fallback_behavior(self, mock_session): |
| 69 | + """Test that PEP 658 is tried first, then falls back to wheel download.""" |
| 70 | + # Mock that metadata URL fails, then wheel URL succeeds |
| 71 | + responses = [] |
| 72 | + |
| 73 | + def side_effect(url): |
| 74 | + if url.endswith(".metadata"): |
| 75 | + # First call - metadata request fails |
| 76 | + mock_response = Mock() |
| 77 | + mock_response.raise_for_status.side_effect = Exception("404 Not Found") |
| 78 | + responses.append(("metadata", url)) |
| 79 | + return mock_response |
| 80 | + else: |
| 81 | + # Second call - wheel request |
| 82 | + responses.append(("wheel", url)) |
| 83 | + raise Exception("Wheel parsing intentionally mocked to fail") |
| 84 | + |
| 85 | + mock_session.get.side_effect = side_effect |
| 86 | + |
| 87 | + wheel_url = "https://example.com/test-package-1.0.0-py3-none-any.whl" |
| 88 | + metadata_url = ( |
| 89 | + "https://example.com/test-package-1.0.0-py3-none-any.whl.metadata" |
| 90 | + ) |
| 91 | + |
| 92 | + # This should raise an exception during wheel parsing, but we can verify the order |
| 93 | + try: |
| 94 | + get_metadata_for_wheel(wheel_url, metadata_url) |
| 95 | + except Exception: |
| 96 | + pass # Expected to fail during wheel parsing mock |
| 97 | + |
| 98 | + # Verify that both URLs were called in the correct order |
| 99 | + assert len(responses) == 2 |
| 100 | + assert responses[0] == ("metadata", metadata_url) |
| 101 | + assert responses[1] == ("wheel", wheel_url) |
| 102 | + assert mock_session.get.call_count == 2 |
| 103 | + |
| 104 | + @patch("fromager.candidate.session") |
| 105 | + def test_get_metadata_without_pep658_behavior(self, mock_session): |
| 106 | + """Test that without PEP 658 metadata URL, only wheel URL is called.""" |
| 107 | + # Mock wheel request |
| 108 | + responses = [] |
| 109 | + |
| 110 | + def side_effect(url): |
| 111 | + responses.append(("wheel", url)) |
| 112 | + raise Exception("Wheel parsing intentionally mocked to fail") |
| 113 | + |
| 114 | + mock_session.get.side_effect = side_effect |
| 115 | + |
| 116 | + wheel_url = "https://example.com/test-package-1.0.0-py3-none-any.whl" |
| 117 | + |
| 118 | + # This should raise an exception during wheel parsing, but we can verify the behavior |
| 119 | + try: |
| 120 | + get_metadata_for_wheel(wheel_url, metadata_url=None) |
| 121 | + except Exception: |
| 122 | + pass # Expected to fail during wheel parsing mock |
| 123 | + |
| 124 | + # Verify that only the wheel URL was called |
| 125 | + assert len(responses) == 1 |
| 126 | + assert responses[0] == ("wheel", wheel_url) |
| 127 | + mock_session.get.assert_called_once_with(wheel_url) |
| 128 | + |
| 129 | + def test_candidate_repr_with_metadata_url(self): |
| 130 | + """Test that Candidate representation includes metadata URL info.""" |
| 131 | + candidate = Candidate( |
| 132 | + name="test-package", |
| 133 | + version=Version("1.0.0"), |
| 134 | + url="https://example.com/test-package-1.0.0-py3-none-any.whl", |
| 135 | + metadata_url="https://example.com/test-package-1.0.0-py3-none-any.whl.metadata", |
| 136 | + ) |
| 137 | + |
| 138 | + # The candidate should have the metadata URL attribute |
| 139 | + assert hasattr(candidate, "metadata_url") |
| 140 | + assert candidate.metadata_url is not None |
| 141 | + |
| 142 | + def test_metadata_url_construction(self): |
| 143 | + """Test that metadata URLs are constructed correctly.""" |
| 144 | + base_url = ( |
| 145 | + "https://pypi.org/simple/test-package/test-package-1.0.0-py3-none-any.whl" |
| 146 | + ) |
| 147 | + expected_metadata_url = base_url + ".metadata" |
| 148 | + |
| 149 | + # This tests the expected pattern for PEP 658 metadata URLs |
| 150 | + assert expected_metadata_url.endswith(".whl.metadata") |
| 151 | + assert expected_metadata_url.startswith("https://") |
| 152 | + |
| 153 | + def test_pep658_integration_with_resolver(self): |
| 154 | + """Test that PEP 658 metadata URLs are properly handled by the candidate system.""" |
| 155 | + # Test the basic integration of metadata URLs with candidates |
| 156 | + candidate_with_metadata = Candidate( |
| 157 | + name="test-package", |
| 158 | + version=Version("1.0.0"), |
| 159 | + url="https://example.com/test.whl", |
| 160 | + metadata_url="https://example.com/test.whl.metadata", |
| 161 | + ) |
| 162 | + |
| 163 | + candidate_without_metadata = Candidate( |
| 164 | + name="test-package", |
| 165 | + version=Version("1.0.0"), |
| 166 | + url="https://example.com/test.whl", |
| 167 | + ) |
| 168 | + |
| 169 | + # Verify PEP 658 metadata URL handling |
| 170 | + assert ( |
| 171 | + candidate_with_metadata.metadata_url |
| 172 | + == "https://example.com/test.whl.metadata" |
| 173 | + ) |
| 174 | + assert candidate_without_metadata.metadata_url is None |
| 175 | + |
| 176 | + # Both should have the same basic properties |
| 177 | + assert candidate_with_metadata.name == candidate_without_metadata.name |
| 178 | + assert candidate_with_metadata.version == candidate_without_metadata.version |
| 179 | + assert candidate_with_metadata.url == candidate_without_metadata.url |
0 commit comments