|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import datetime |
3 | 4 | import os
|
| 5 | +from collections.abc import Callable |
| 6 | +from optparse import Option, OptionParser, Values |
4 | 7 | from pathlib import Path
|
5 | 8 | from venv import EnvBuilder
|
6 | 9 |
|
7 | 10 | import pytest
|
8 | 11 |
|
9 |
| -from pip._internal.cli.cmdoptions import _convert_python_version |
| 12 | +from pip._internal.cli.cmdoptions import ( |
| 13 | + _convert_python_version, |
| 14 | + _handle_exclude_newer_than, |
| 15 | +) |
10 | 16 | from pip._internal.cli.main_parser import identify_python_interpreter
|
11 | 17 |
|
12 | 18 |
|
@@ -51,3 +57,110 @@ def test_identify_python_interpreter_venv(tmpdir: Path) -> None:
|
51 | 57 |
|
52 | 58 | # Passing a non-existent file returns None
|
53 | 59 | assert identify_python_interpreter(str(tmpdir / "nonexistent")) is None
|
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize( |
| 63 | + "value, expected_check", |
| 64 | + [ |
| 65 | + # Test with timezone info (should be preserved exactly) |
| 66 | + ( |
| 67 | + "2023-01-01T00:00:00+00:00", |
| 68 | + lambda dt: dt |
| 69 | + == datetime.datetime(2023, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), |
| 70 | + ), |
| 71 | + ( |
| 72 | + "2023-01-01T12:00:00-05:00", |
| 73 | + lambda dt: ( |
| 74 | + dt |
| 75 | + == datetime.datetime( |
| 76 | + *(2023, 1, 1, 12, 0, 0), |
| 77 | + tzinfo=datetime.timezone(datetime.timedelta(hours=-5)), |
| 78 | + ) |
| 79 | + ), |
| 80 | + ), |
| 81 | + ], |
| 82 | +) |
| 83 | +def test_handle_exclude_newer_than_with_timezone( |
| 84 | + value: str, expected_check: Callable[[datetime.datetime], bool] |
| 85 | +) -> None: |
| 86 | + """Test that timezone-aware ISO 8601 date strings are parsed correctly.""" |
| 87 | + option = Option("--exclude-newer-than", dest="exclude_newer_than") |
| 88 | + opt = "--exclude-newer-than" |
| 89 | + parser = OptionParser() |
| 90 | + parser.values = Values() |
| 91 | + |
| 92 | + _handle_exclude_newer_than(option, opt, value, parser) |
| 93 | + |
| 94 | + result = parser.values.exclude_newer_than |
| 95 | + assert isinstance(result, datetime.datetime) |
| 96 | + assert expected_check(result) |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.parametrize( |
| 100 | + "value, expected_date_time", |
| 101 | + [ |
| 102 | + # Test basic ISO 8601 formats (timezone-naive, will get local timezone) |
| 103 | + ("2023-01-01T00:00:00", (2023, 1, 1, 0, 0, 0)), |
| 104 | + ("2023-12-31T23:59:59", (2023, 12, 31, 23, 59, 59)), |
| 105 | + # Test date only (will be extended to midnight) |
| 106 | + ("2023-01-01", (2023, 1, 1, 0, 0, 0)), |
| 107 | + ], |
| 108 | +) |
| 109 | +def test_handle_exclude_newer_than_naive_dates( |
| 110 | + value: str, expected_date_time: tuple[int, int, int, int, int, int] |
| 111 | +) -> None: |
| 112 | + """Test that timezone-naive ISO 8601 date strings get local timezone applied.""" |
| 113 | + option = Option("--exclude-newer-than", dest="exclude_newer_than") |
| 114 | + opt = "--exclude-newer-than" |
| 115 | + parser = OptionParser() |
| 116 | + parser.values = Values() |
| 117 | + |
| 118 | + _handle_exclude_newer_than(option, opt, value, parser) |
| 119 | + |
| 120 | + result = parser.values.exclude_newer_than |
| 121 | + assert isinstance(result, datetime.datetime) |
| 122 | + |
| 123 | + # Check that the date/time components match |
| 124 | + ( |
| 125 | + expected_year, |
| 126 | + expected_month, |
| 127 | + expected_day, |
| 128 | + expected_hour, |
| 129 | + expected_minute, |
| 130 | + expected_second, |
| 131 | + ) = expected_date_time |
| 132 | + assert result.year == expected_year |
| 133 | + assert result.month == expected_month |
| 134 | + assert result.day == expected_day |
| 135 | + assert result.hour == expected_hour |
| 136 | + assert result.minute == expected_minute |
| 137 | + assert result.second == expected_second |
| 138 | + |
| 139 | + # Check that local timezone was applied (result should not be timezone-naive) |
| 140 | + assert result.tzinfo is not None |
| 141 | + |
| 142 | + # Verify it's equivalent to creating the same datetime and applying local timezone |
| 143 | + naive_dt = datetime.datetime(*expected_date_time) |
| 144 | + expected_with_local_tz = naive_dt.astimezone() |
| 145 | + assert result == expected_with_local_tz |
| 146 | + |
| 147 | + |
| 148 | +@pytest.mark.parametrize( |
| 149 | + "invalid_value", |
| 150 | + [ |
| 151 | + "not-a-date", |
| 152 | + "2023-13-01", # Invalid month |
| 153 | + "2023-01-32", # Invalid day |
| 154 | + "2023-01-01T25:00:00", # Invalid hour |
| 155 | + "", # Empty string |
| 156 | + ], |
| 157 | +) |
| 158 | +def test_handle_exclude_newer_than_invalid_dates(invalid_value: str) -> None: |
| 159 | + """Test that invalid date strings raise ValueError.""" |
| 160 | + option = Option("--exclude-newer-than", dest="exclude_newer_than") |
| 161 | + opt = "--exclude-newer-than" |
| 162 | + parser = OptionParser() |
| 163 | + parser.values = Values() |
| 164 | + |
| 165 | + with pytest.raises(ValueError): |
| 166 | + _handle_exclude_newer_than(option, opt, invalid_value, parser) |
0 commit comments