|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import datetime |
3 | 4 | import os |
| 5 | +from optparse import Option, OptionParser, Values |
4 | 6 | from pathlib import Path |
5 | 7 | from venv import EnvBuilder |
6 | 8 |
|
7 | 9 | import pytest |
8 | 10 |
|
9 | | -from pip._internal.cli.cmdoptions import _convert_python_version |
| 11 | +from pip._internal.cli.cmdoptions import ( |
| 12 | + _convert_python_version, |
| 13 | + _handle_uploaded_prior_to, |
| 14 | +) |
10 | 15 | from pip._internal.cli.main_parser import identify_python_interpreter |
11 | 16 |
|
12 | 17 |
|
@@ -51,3 +56,122 @@ def test_identify_python_interpreter_venv(tmpdir: Path) -> None: |
51 | 56 |
|
52 | 57 | # Passing a non-existent file returns None |
53 | 58 | assert identify_python_interpreter(str(tmpdir / "nonexistent")) is None |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize( |
| 62 | + "value, expected_datetime", |
| 63 | + [ |
| 64 | + ( |
| 65 | + "2023-01-01T00:00:00+00:00", |
| 66 | + datetime.datetime(2023, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), |
| 67 | + ), |
| 68 | + ( |
| 69 | + "2023-01-01T12:00:00-05:00", |
| 70 | + datetime.datetime( |
| 71 | + *(2023, 1, 1, 12, 0, 0), |
| 72 | + tzinfo=datetime.timezone(datetime.timedelta(hours=-5)), |
| 73 | + ), |
| 74 | + ), |
| 75 | + ], |
| 76 | +) |
| 77 | +def test_handle_uploaded_prior_to_with_timezone( |
| 78 | + value: str, expected_datetime: datetime.datetime |
| 79 | +) -> None: |
| 80 | + """Test that timezone-aware ISO 8601 date strings are parsed correctly.""" |
| 81 | + option = Option("--uploaded-prior-to", dest="uploaded_prior_to") |
| 82 | + opt = "--uploaded-prior-to" |
| 83 | + parser = OptionParser() |
| 84 | + parser.values = Values() |
| 85 | + |
| 86 | + _handle_uploaded_prior_to(option, opt, value, parser) |
| 87 | + |
| 88 | + result = parser.values.uploaded_prior_to |
| 89 | + assert isinstance(result, datetime.datetime) |
| 90 | + assert result == expected_datetime |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.parametrize( |
| 94 | + "value, expected_date_time", |
| 95 | + [ |
| 96 | + # Test basic ISO 8601 formats (timezone-naive, will get local timezone) |
| 97 | + ("2023-01-01T00:00:00", (2023, 1, 1, 0, 0, 0)), |
| 98 | + ("2023-12-31T23:59:59", (2023, 12, 31, 23, 59, 59)), |
| 99 | + # Test date only (will be extended to midnight) |
| 100 | + ("2023-01-01", (2023, 1, 1, 0, 0, 0)), |
| 101 | + ], |
| 102 | +) |
| 103 | +def test_handle_uploaded_prior_to_naive_dates( |
| 104 | + value: str, expected_date_time: tuple[int, int, int, int, int, int] |
| 105 | +) -> None: |
| 106 | + """Test that timezone-naive ISO 8601 date strings get local timezone applied.""" |
| 107 | + option = Option("--uploaded-prior-to", dest="uploaded_prior_to") |
| 108 | + opt = "--uploaded-prior-to" |
| 109 | + parser = OptionParser() |
| 110 | + parser.values = Values() |
| 111 | + |
| 112 | + _handle_uploaded_prior_to(option, opt, value, parser) |
| 113 | + |
| 114 | + result = parser.values.uploaded_prior_to |
| 115 | + assert isinstance(result, datetime.datetime) |
| 116 | + |
| 117 | + # Check that the date/time components match |
| 118 | + assert result.timetuple()[:6] == expected_date_time |
| 119 | + |
| 120 | + # Check that local timezone was applied (result should not be timezone-naive) |
| 121 | + assert result.tzinfo is not None |
| 122 | + |
| 123 | + # Verify it's equivalent to what .astimezone() produces on a naive datetime |
| 124 | + naive_dt = datetime.datetime(*expected_date_time) |
| 125 | + expected_with_local_tz = naive_dt.astimezone() |
| 126 | + assert result == expected_with_local_tz |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.parametrize( |
| 130 | + "invalid_value", |
| 131 | + [ |
| 132 | + "not-a-date", |
| 133 | + "2023-13-01", # Invalid month |
| 134 | + "2023-01-32", # Invalid day |
| 135 | + "2023-01-01T25:00:00", # Invalid hour |
| 136 | + "", # Empty string |
| 137 | + ], |
| 138 | +) |
| 139 | +def test_handle_uploaded_prior_to_invalid_dates(invalid_value: str) -> None: |
| 140 | + """Test that invalid date strings raise SystemExit via raise_option_error.""" |
| 141 | + option = Option("--uploaded-prior-to", dest="uploaded_prior_to") |
| 142 | + opt = "--uploaded-prior-to" |
| 143 | + parser = OptionParser() |
| 144 | + parser.values = Values() |
| 145 | + |
| 146 | + with pytest.raises(SystemExit): |
| 147 | + _handle_uploaded_prior_to(option, opt, invalid_value, parser) |
| 148 | + |
| 149 | + |
| 150 | +def test_handle_uploaded_prior_to_naive() -> None: |
| 151 | + """ |
| 152 | + Test that a naive datetime is interpreted as local time. |
| 153 | + """ |
| 154 | + option = Option("--uploaded-prior-to", dest="uploaded_prior_to") |
| 155 | + opt = "--uploaded-prior-to" |
| 156 | + parser = OptionParser() |
| 157 | + parser.values = Values() |
| 158 | + |
| 159 | + # Parse a naive datetime |
| 160 | + naive_input = "2023-06-15T14:30:00" |
| 161 | + _handle_uploaded_prior_to(option, opt, naive_input, parser) |
| 162 | + result = parser.values.uploaded_prior_to |
| 163 | + |
| 164 | + assert result.hour == 14, ( |
| 165 | + f"Expected hour=14 (from input), got hour={result.hour}. " |
| 166 | + "This suggests the naive datetime was incorrectly interpreted as UTC " |
| 167 | + "and converted to local timezone." |
| 168 | + ) |
| 169 | + assert result.minute == 30 |
| 170 | + assert result.year == 2023 |
| 171 | + assert result.month == 6 |
| 172 | + assert result.day == 15 |
| 173 | + |
| 174 | + # Verify by creating the same datetime with explicit local timezone |
| 175 | + local_tz = datetime.datetime.now().astimezone().tzinfo |
| 176 | + expected = datetime.datetime(2023, 6, 15, 14, 30, 0, tzinfo=local_tz) |
| 177 | + assert result == expected |
0 commit comments