|
| 1 | +from unittest.mock import patch, MagicMock |
| 2 | + |
| 3 | +from metaflow.plugins.pypi.pip import Pip |
| 4 | + |
| 5 | + |
| 6 | +def _make_pip(): |
| 7 | + pip = object.__new__(Pip) |
| 8 | + return pip |
| 9 | + |
| 10 | + |
| 11 | +class TestPipIndices: |
| 12 | + def test_single_index_url(self): |
| 13 | + pip = _make_pip() |
| 14 | + config_output = "global.index-url='https://pypi.org/simple'" |
| 15 | + with patch.object(pip, "_call", return_value=config_output): |
| 16 | + index, extras = pip.indices("dummy") |
| 17 | + assert index == "https://pypi.org/simple" |
| 18 | + assert extras == [] |
| 19 | + |
| 20 | + def test_single_extra_index_url(self): |
| 21 | + pip = _make_pip() |
| 22 | + config_output = ( |
| 23 | + "global.index-url='https://pypi.org/simple'\n" |
| 24 | + "global.extra-index-url='https://extra.example.com/simple'" |
| 25 | + ) |
| 26 | + with patch.object(pip, "_call", return_value=config_output): |
| 27 | + index, extras = pip.indices("dummy") |
| 28 | + assert index == "https://pypi.org/simple" |
| 29 | + assert extras == ["https://extra.example.com/simple"] |
| 30 | + |
| 31 | + def test_multiple_extra_index_urls_literal_newline(self): |
| 32 | + """Regression test: pip config list separates multiple URLs with literal \\n.""" |
| 33 | + pip = _make_pip() |
| 34 | + config_output = ( |
| 35 | + "global.index-url='https://pypi.org/simple'\n" |
| 36 | + r"global.extra-index-url='https://extra1.example.com/simple'\n'https://extra2.example.com/simple'" |
| 37 | + ) |
| 38 | + with patch.object(pip, "_call", return_value=config_output): |
| 39 | + index, extras = pip.indices("dummy") |
| 40 | + assert index == "https://pypi.org/simple" |
| 41 | + assert extras == [ |
| 42 | + "https://extra1.example.com/simple", |
| 43 | + "https://extra2.example.com/simple", |
| 44 | + ] |
| 45 | + |
| 46 | + def test_multiple_index_urls_first_wins(self): |
| 47 | + pip = _make_pip() |
| 48 | + config_output = r"global.index-url='https://first.example.com'\n'https://second.example.com'" |
| 49 | + with patch.object(pip, "_call", return_value=config_output): |
| 50 | + index, extras = pip.indices("dummy") |
| 51 | + assert index == "https://first.example.com" |
| 52 | + assert extras == ["https://second.example.com"] |
| 53 | + |
| 54 | + def test_empty_config(self): |
| 55 | + pip = _make_pip() |
| 56 | + with patch.object(pip, "_call", return_value=""): |
| 57 | + index, extras = pip.indices("dummy") |
| 58 | + assert index is None |
| 59 | + assert extras == [] |
| 60 | + |
| 61 | + def test_config_call_fails(self): |
| 62 | + pip = _make_pip() |
| 63 | + with patch.object(pip, "_call", side_effect=Exception("pip not found")): |
| 64 | + index, extras = pip.indices("dummy") |
| 65 | + assert index is None |
| 66 | + assert extras == [] |
0 commit comments