Skip to content

Commit 196cfbe

Browse files
committed
Clean up test_util, reorganizing for readability
- Slightly improve import sorting, grouping, and formatting. - Move the cygpath pairs parameters into the test class, so they can be immediately above the tests that use them. This was almost the case in the past, but stopped being the case when helpers for some new tests above those were introduced (and those helpers can't be moved inside the class without extra complexity). - Rename TestIterableMember to _Member, so it is no longer named as a test class. The unittest framework wouldn't consider it one, since it doesn't derive from unittest.TestCase, but the pytest runner, which we're actually using, does. More importanly (since it has no test methods anyway), this makes clear to humans that it is a helper class for tests, rather than a class of tests. - Improve the style of _Member, and have its __repr__ show the actual class of the instance, so if future tests ever use a derived class of it--or if its name ever changes again--the type name in the repr will be correct. - Remove the setup method (of TestUtils). It looks like this may at one time have been intended as a setUp method (note the case difference), but it is unused and there doesn't seem to be any attempt to use the instance attribute it was setting. - Use R"" instead of r"" for raw strings representing Windows paths, so that some editors (at least VS Code) refrain from highlighting their contents as regular expressions. - Other very minor reformatting and slight comment rewording.
1 parent 7dd5904 commit 196cfbe

File tree

1 file changed

+46
-55
lines changed

1 file changed

+46
-55
lines changed

test/test_util.py

+46-55
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# the BSD License: https://opensource.org/license/bsd-3-clause/
66

77
import contextlib
8+
from datetime import datetime
89
import os
910
import pathlib
1011
import pickle
@@ -13,7 +14,6 @@
1314
import tempfile
1415
import time
1516
from unittest import SkipTest, mock, skipIf, skipUnless
16-
from datetime import datetime
1717

1818
import ddt
1919
import pytest
@@ -28,10 +28,6 @@
2828
utctz_to_altz,
2929
verify_utctz,
3030
)
31-
from test.lib import (
32-
TestBase,
33-
with_rw_repo,
34-
)
3531
from git.util import (
3632
Actor,
3733
BlockingLockFile,
@@ -43,41 +39,19 @@
4339
remove_password_if_present,
4440
rmtree,
4541
)
42+
from test.lib import TestBase, with_rw_repo
4643

4744

48-
_norm_cygpath_pairs = (
49-
(r"foo\bar", "foo/bar"),
50-
(r"foo/bar", "foo/bar"),
51-
(r"C:\Users", "/cygdrive/c/Users"),
52-
(r"C:\d/e", "/cygdrive/c/d/e"),
53-
("C:\\", "/cygdrive/c/"),
54-
(r"\\server\C$\Users", "//server/C$/Users"),
55-
(r"\\server\C$", "//server/C$"),
56-
("\\\\server\\c$\\", "//server/c$/"),
57-
(r"\\server\BAR/", "//server/BAR/"),
58-
(r"D:/Apps", "/cygdrive/d/Apps"),
59-
(r"D:/Apps\fOO", "/cygdrive/d/Apps/fOO"),
60-
(r"D:\Apps/123", "/cygdrive/d/Apps/123"),
61-
)
62-
63-
_unc_cygpath_pairs = (
64-
(r"\\?\a:\com", "/cygdrive/a/com"),
65-
(r"\\?\a:/com", "/cygdrive/a/com"),
66-
(r"\\?\UNC\server\D$\Apps", "//server/D$/Apps"),
67-
)
68-
69-
70-
class TestIterableMember(object):
71-
72-
"""A member of an iterable list"""
45+
class _Member:
46+
"""A member of an IterableList."""
7347

74-
__slots__ = "name"
48+
__slots__ = ("name",)
7549

7650
def __init__(self, name):
7751
self.name = name
7852

7953
def __repr__(self):
80-
return "TestIterableMember(%r)" % self.name
54+
return f"{type(self).__name__}({self.name!r})"
8155

8256

8357
@contextlib.contextmanager
@@ -106,13 +80,6 @@ def _tmpdir_for_file_not_found():
10680

10781
@ddt.ddt
10882
class TestUtils(TestBase):
109-
def setup(self):
110-
self.testdict = {
111-
"string": "42",
112-
"int": 42,
113-
"array": [42],
114-
}
115-
11683
def test_rmtree_deletes_nested_dir_with_files(self):
11784
with tempfile.TemporaryDirectory() as parent:
11885
td = pathlib.Path(parent, "testdir")
@@ -131,7 +98,7 @@ def test_rmtree_deletes_nested_dir_with_files(self):
13198
@skipIf(sys.platform == "cygwin", "Cygwin can't set the permissions that make the test meaningful.")
13299
def test_rmtree_deletes_dir_with_readonly_files(self):
133100
# Automatically works on Unix, but requires special handling on Windows.
134-
# Not to be confused with _tmpdir_to_force_permission_error (which is used below).
101+
# Not to be confused with what _tmpdir_to_force_permission_error sets up (see below).
135102
with tempfile.TemporaryDirectory() as parent:
136103
td = pathlib.Path(parent, "testdir")
137104
for d in td, td / "sub":
@@ -179,6 +146,27 @@ def test_rmtree_does_not_wrap_unless_called_for(self, case):
179146
except SkipTest as ex:
180147
self.fail(f"rmtree unexpectedly attempts skip: {ex!r}")
181148

149+
_norm_cygpath_pairs = (
150+
(R"foo\bar", "foo/bar"),
151+
(R"foo/bar", "foo/bar"),
152+
(R"C:\Users", "/cygdrive/c/Users"),
153+
(R"C:\d/e", "/cygdrive/c/d/e"),
154+
("C:\\", "/cygdrive/c/"),
155+
(R"\\server\C$\Users", "//server/C$/Users"),
156+
(R"\\server\C$", "//server/C$"),
157+
("\\\\server\\c$\\", "//server/c$/"),
158+
(R"\\server\BAR/", "//server/BAR/"),
159+
(R"D:/Apps", "/cygdrive/d/Apps"),
160+
(R"D:/Apps\fOO", "/cygdrive/d/Apps/fOO"),
161+
(R"D:\Apps/123", "/cygdrive/d/Apps/123"),
162+
)
163+
164+
_unc_cygpath_pairs = (
165+
(R"\\?\a:\com", "/cygdrive/a/com"),
166+
(R"\\?\a:/com", "/cygdrive/a/com"),
167+
(R"\\?\UNC\server\D$\Apps", "//server/D$/Apps"),
168+
)
169+
182170
# FIXME: Mark only the /proc-prefixing cases xfail, somehow (or fix them).
183171
@pytest.mark.xfail(
184172
reason="Many return paths prefixed /proc/cygdrive instead.",
@@ -192,16 +180,16 @@ def test_cygpath_ok(self, case):
192180
self.assertEqual(cwpath, cpath, wpath)
193181

194182
@pytest.mark.xfail(
195-
reason=r'2nd example r".\bar" -> "bar" fails, returns "./bar"',
183+
reason=R'2nd example r".\bar" -> "bar" fails, returns "./bar"',
196184
raises=AssertionError,
197185
)
198186
@skipUnless(sys.platform == "cygwin", "Paths specifically for Cygwin.")
199187
@ddt.data(
200-
(r"./bar", "bar"),
201-
(r".\bar", "bar"), # FIXME: Mark only this one xfail, somehow (or fix it).
202-
(r"../bar", "../bar"),
203-
(r"..\bar", "../bar"),
204-
(r"../bar/.\foo/../chu", "../bar/chu"),
188+
(R"./bar", "bar"),
189+
(R".\bar", "bar"), # FIXME: Mark only this one xfail, somehow (or fix it).
190+
(R"../bar", "../bar"),
191+
(R"..\bar", "../bar"),
192+
(R"../bar/.\foo/../chu", "../bar/chu"),
205193
)
206194
def test_cygpath_norm_ok(self, case):
207195
wpath, cpath = case
@@ -210,12 +198,12 @@ def test_cygpath_norm_ok(self, case):
210198

211199
@skipUnless(sys.platform == "cygwin", "Paths specifically for Cygwin.")
212200
@ddt.data(
213-
r"C:",
214-
r"C:Relative",
215-
r"D:Apps\123",
216-
r"D:Apps/123",
217-
r"\\?\a:rel",
218-
r"\\share\a:rel",
201+
R"C:",
202+
R"C:Relative",
203+
R"D:Apps\123",
204+
R"D:Apps/123",
205+
R"\\?\a:rel",
206+
R"\\share\a:rel",
219207
)
220208
def test_cygpath_invalids(self, wpath):
221209
cwpath = cygpath(wpath)
@@ -380,15 +368,18 @@ def test_actor_from_string(self):
380368
Actor("name last another", "some-very-long-email@example.com"),
381369
)
382370

383-
@ddt.data(("name", ""), ("name", "prefix_"))
371+
@ddt.data(
372+
("name", ""),
373+
("name", "prefix_"),
374+
)
384375
def test_iterable_list(self, case):
385376
name, prefix = case
386377
ilist = IterableList(name, prefix)
387378

388379
name1 = "one"
389380
name2 = "two"
390-
m1 = TestIterableMember(prefix + name1)
391-
m2 = TestIterableMember(prefix + name2)
381+
m1 = _Member(prefix + name1)
382+
m2 = _Member(prefix + name2)
392383

393384
ilist.extend((m1, m2))
394385

0 commit comments

Comments
 (0)