|
6 | 6 | import importlib.metadata |
7 | 7 | import io |
8 | 8 | import json |
| 9 | +import ntpath |
9 | 10 | import os |
10 | 11 | import pathlib |
11 | 12 | import pickle |
|
14 | 15 | import struct |
15 | 16 | import sys |
16 | 17 | import tempfile |
| 18 | +import types |
17 | 19 | import unittest |
18 | 20 | from datetime import date, datetime, time, timedelta, timezone |
19 | 21 | from functools import cached_property |
| 22 | +from unittest import mock |
20 | 23 |
|
21 | 24 | from test.support import MISSING_C_DOCSTRINGS |
22 | 25 | from test.support.os_helper import EnvironmentVarGuard, FakePath |
@@ -2017,6 +2020,38 @@ def test_tzpath_type_error(self): |
2017 | 2020 | with self.assertRaises(TypeError): |
2018 | 2021 | self.module.reset_tzpath(bad_value) |
2019 | 2022 |
|
| 2023 | + def test_validate_key_windows_paths(self): |
| 2024 | + # Check the key validation with Windows path rules on every platform. |
| 2025 | + tzpath = self.module._tzpath |
| 2026 | + base = ntpath.normpath(ntpath.join("_", "_"))[:-1] |
| 2027 | + fake_os = types.SimpleNamespace(path=ntpath) |
| 2028 | + |
| 2029 | + good_keys = [ |
| 2030 | + "UTC", |
| 2031 | + "America/New_York", |
| 2032 | + "America/Argentina/Buenos_Aires", |
| 2033 | + "Etc/GMT+1", |
| 2034 | + ] |
| 2035 | + bad_keys = [ |
| 2036 | + "C:\\Windows\\win.ini", |
| 2037 | + "America/C:\\Windows\\win.ini", # Drive in a component |
| 2038 | + "America/C:/Windows/win.ini", |
| 2039 | + "America/D:secret", # Drive-relative component |
| 2040 | + "America/Argentina/D:secret", |
| 2041 | + "America/\\Windows\\win.ini", # Rooted component |
| 2042 | + "America/\\\\server\\share\\file", # UNC component |
| 2043 | + ] |
| 2044 | + |
| 2045 | + with mock.patch.object(tzpath, "os", fake_os): |
| 2046 | + for key in good_keys: |
| 2047 | + with self.subTest(key=key): |
| 2048 | + tzpath._validate_tzfile_path(key, _base=base) |
| 2049 | + |
| 2050 | + for key in bad_keys: |
| 2051 | + with self.subTest(key=key): |
| 2052 | + with self.assertRaises(ValueError): |
| 2053 | + tzpath._validate_tzfile_path(key, _base=base) |
| 2054 | + |
2020 | 2055 | def test_tzpath_attribute(self): |
2021 | 2056 | tzpath_0 = [f"{DRIVE}/one", f"{DRIVE}/two"] |
2022 | 2057 | tzpath_1 = [f"{DRIVE}/three"] |
|
0 commit comments