Skip to content

Commit 9e7650e

Browse files
authored
Merge pull request #13058 from anntzer/pathlib-triage_tests
pathlibify/cleanup triage_tests.py.
2 parents b94a312 + daf2e66 commit 9e7650e

File tree

1 file changed

+31
-43
lines changed

1 file changed

+31
-43
lines changed

tools/triage_tests.py

+31-43
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
# these two places.
3939

4040
BASELINE_IMAGES = [
41-
os.path.join('lib', 'matplotlib', 'tests', 'baseline_images'),
42-
os.path.join('lib', 'mpl_toolkits', 'tests', 'baseline_images')
43-
]
41+
Path('lib/matplotlib/tests/baseline_images'),
42+
Path('lib/mpl_toolkits/tests/baseline_images'),
43+
]
4444

4545

4646
# Non-png image extensions
@@ -172,7 +172,7 @@ def set_entry(self, index):
172172

173173
self.pixmaps = []
174174
for fname, thumbnail in zip(entry.thumbnails, self.thumbnails):
175-
pixmap = QtGui.QPixmap(fname)
175+
pixmap = QtGui.QPixmap(os.fspath(fname))
176176
scaled_pixmap = pixmap.scaled(
177177
thumbnail.size(), QtCore.Qt.KeepAspectRatio,
178178
QtCore.Qt.SmoothTransformation)
@@ -185,8 +185,9 @@ def set_entry(self, index):
185185
def set_large_image(self, index):
186186
self.thumbnails[self.current_thumbnail].setFrameShape(0)
187187
self.current_thumbnail = index
188-
pixmap = QtGui.QPixmap(self.entries[self.current_entry]
189-
.thumbnails[self.current_thumbnail])
188+
pixmap = QtGui.QPixmap(os.fspath(
189+
self.entries[self.current_entry]
190+
.thumbnails[self.current_thumbnail]))
190191
self.image_display.setPixmap(pixmap)
191192
self.thumbnails[self.current_thumbnail].setFrameShape(1)
192193

@@ -236,11 +237,11 @@ class Entry(object):
236237
def __init__(self, path, root, source):
237238
self.source = source
238239
self.root = root
239-
self.dir, fname = os.path.split(path)
240-
self.reldir = os.path.relpath(self.dir, self.root)
241-
self.diff = fname
240+
self.dir = path.parent
241+
self.diff = path.name
242+
self.reldir = self.dir.relative_to(self.root)
242243

243-
basename = fname[:-len('-failed-diff.png')]
244+
basename = self.diff[:-len('-failed-diff.png')]
244245
for ext in exts:
245246
if basename.endswith('_' + ext):
246247
display_extension = '_' + ext
@@ -258,46 +259,33 @@ def __init__(self, path, root, source):
258259
self.expected_display = (basename + '-expected' + display_extension +
259260
'.png')
260261
self.generated_display = basename + display_extension + '.png'
261-
self.name = os.path.join(self.reldir, self.basename)
262+
self.name = self.reldir / self.basename
262263
self.destdir = self.get_dest_dir(self.reldir)
263264

264265
self.thumbnails = [
265266
self.generated_display,
266267
self.expected_display,
267268
self.diff
268269
]
269-
self.thumbnails = [os.path.join(self.dir, x) for x in self.thumbnails]
270+
self.thumbnails = [self.dir / x for x in self.thumbnails]
270271

271272
if not Path(self.destdir, self.generated).exists():
272273
# This case arises from a check_figures_equal test.
273274
self.status = 'autogen'
274-
elif self.same(os.path.join(self.dir, self.generated),
275-
os.path.join(self.destdir, self.generated)):
275+
elif ((self.dir / self.generated).read_bytes()
276+
== (self.destdir / self.generated).read_bytes()):
276277
self.status = 'accept'
277278
else:
278279
self.status = 'unknown'
279280

280-
def same(self, a, b):
281-
"""
282-
Returns True if two files have the same content.
283-
"""
284-
return Path(a).read_bytes() == Path(b).read_bytes()
285-
286-
def copy_file(self, a, b):
287-
"""
288-
Copy file from a to b.
289-
"""
290-
print("copying: {} to {}".format(a, b))
291-
shutil.copyfile(a, b)
292-
293281
def get_dest_dir(self, reldir):
294282
"""
295283
Find the source tree directory corresponding to the given
296284
result_images subdirectory.
297285
"""
298286
for baseline_dir in BASELINE_IMAGES:
299-
path = os.path.join(self.source, baseline_dir, reldir)
300-
if os.path.isdir(path):
287+
path = self.source / baseline_dir / reldir
288+
if path.is_dir():
301289
return path
302290
raise ValueError("Can't find baseline dir for {}".format(reldir))
303291

@@ -320,30 +308,30 @@ def accept(self):
320308
"""
321309
Accept this test by copying the generated result to the source tree.
322310
"""
323-
a = os.path.join(self.dir, self.generated)
324-
b = os.path.join(self.destdir, self.generated)
325-
self.copy_file(a, b)
311+
copy_file(self.dir / self.generated, self.destdir / self.generated)
326312
self.status = 'accept'
327313

328314
def reject(self):
329315
"""
330316
Reject this test by copying the expected result to the source tree.
331317
"""
332-
a = os.path.join(self.dir, self.expected)
333-
b = os.path.join(self.destdir, self.generated)
334-
self.copy_file(a, b)
318+
copy_file(self.dir / self.expected, self.destdir / self.generated)
335319
self.status = 'reject'
336320

337321

322+
def copy_file(a, b):
323+
"""Copy file from *a* to *b*."""
324+
print(f'copying: {a} to {b}')
325+
shutil.copyfile(a, b)
326+
327+
338328
def find_failing_tests(result_images, source):
339329
"""
340330
Find all of the failing tests by looking for files with
341331
`-failed-diff` at the end of the basename.
342332
"""
343-
return sorted(
344-
(Entry(path, result_images, source)
345-
for path in Path(result_images).glob("**/*-failed-diff.*")),
346-
key=lambda x: x.name)
333+
return [Entry(path, result_images, source)
334+
for path in sorted(Path(result_images).glob("**/*-failed-diff.*"))]
347335

348336

349337
def launch(result_images, source):
@@ -367,7 +355,7 @@ def launch(result_images, source):
367355
if __name__ == '__main__':
368356
import argparse
369357

370-
source_dir = os.path.join(os.path.dirname(__file__), '..')
358+
source_dir = Path(__file__).parent.parent
371359

372360
parser = argparse.ArgumentParser(
373361
formatter_class=argparse.RawDescriptionHelpFormatter,
@@ -383,10 +371,10 @@ def launch(result_images, source):
383371
A: Accept test. Copy the test result to the source tree.
384372
R: Reject test. Copy the expected result to the source tree.
385373
""")
386-
parser.add_argument("result_images", type=str, nargs='?',
387-
default=os.path.join(source_dir, 'result_images'),
374+
parser.add_argument("result_images", type=Path, nargs='?',
375+
default=source_dir / 'result_images',
388376
help="The location of the result_images directory")
389-
parser.add_argument("source", type=str, nargs='?', default=source_dir,
377+
parser.add_argument("source", type=Path, nargs='?', default=source_dir,
390378
help="The location of the matplotlib source tree")
391379
args = parser.parse_args()
392380

0 commit comments

Comments
 (0)