Skip to content

Commit 4253bef

Browse files
Merge pull request #207 from eerovaher/rm-tmpdir
Avoid using `py.path`
2 parents 68cbbab + 07e12f4 commit 4253bef

File tree

3 files changed

+60
-63
lines changed

3 files changed

+60
-63
lines changed

Diff for: pytest_mpl/plugin.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363
ALL_IMAGE_FORMATS = RASTER_IMAGE_FORMATS + VECTOR_IMAGE_FORMATS
6464

6565

66+
def _get_item_dir(item):
67+
# .path is available starting from pytest 7, .fspath is for older versions.
68+
return getattr(item, "path", Path(item.fspath)).parent
69+
70+
6671
def _hash_file(in_stream):
6772
"""
6873
Hashes an already opened file.
@@ -445,19 +450,19 @@ def get_baseline_directory(self, item):
445450
baseline_dir = compare.kwargs.get('baseline_dir', None)
446451
if baseline_dir is None:
447452
if self.baseline_dir is None:
448-
baseline_dir = Path(item.fspath).parent / 'baseline'
453+
baseline_dir = _get_item_dir(item) / 'baseline'
449454
else:
450455
if self.baseline_relative_dir:
451456
# baseline dir is relative to the current test
452-
baseline_dir = Path(item.fspath).parent / self.baseline_relative_dir
457+
baseline_dir = _get_item_dir(item) / self.baseline_relative_dir
453458
else:
454459
# baseline dir is relative to where pytest was run
455460
baseline_dir = self.baseline_dir
456461

457462
baseline_remote = (isinstance(baseline_dir, str) and # noqa
458463
baseline_dir.startswith(('http://', 'https://')))
459464
if not baseline_remote:
460-
return Path(item.fspath).parent / baseline_dir
465+
return _get_item_dir(item) / baseline_dir
461466

462467
return baseline_dir
463468

@@ -686,7 +691,7 @@ def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
686691
hash_library_filename = compare.kwargs.get("hash_library", None) or self.hash_library
687692
if self._hash_library_from_cli: # for backwards compatibility
688693
hash_library_filename = self.hash_library
689-
hash_library_filename = (Path(item.fspath).parent / hash_library_filename).absolute()
694+
hash_library_filename = _get_item_dir(item) / hash_library_filename
690695

691696
if not Path(hash_library_filename).exists():
692697
pytest.fail(f"Can't find hash library at path {hash_library_filename}")

Diff for: tests/test_default_tolerance.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88

99
@pytest.fixture(scope="module")
10-
def baseline_image(tmpdir_factory):
10+
def baseline_image(tmp_path_factory):
1111
path = Path(__file__).parent / "baseline" / "2.0.x" / f"{TEST_NAME}.png"
1212
image = Image.open(path)
1313
draw = ImageDraw.Draw(image)
1414
draw.rectangle(((0, 0), (100, 100)), fill="red")
15-
output = Path(tmpdir_factory.mktemp("data").join(f"{TEST_NAME}.png"))
15+
output = tmp_path_factory.mktemp("data") / f"{TEST_NAME}.png"
1616
image.save(output)
1717
return output
1818

Diff for: tests/test_pytest_mpl.py

+49-57
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,10 @@ def test_fail():
120120
"""
121121

122122

123-
def test_fails(tmpdir):
124-
125-
test_file = tmpdir.join('test.py').strpath
126-
with open(test_file, 'w') as f:
127-
f.write(TEST_FAILING)
123+
def test_fails(tmp_path):
124+
test_file = tmp_path / "test.py"
125+
test_file.write_text(TEST_FAILING)
126+
test_file = str(test_file)
128127

129128
# If we use --mpl, it should detect that the figure is wrong
130129
code = call_pytest(['--mpl', test_file])
@@ -147,16 +146,15 @@ def test_output_dir():
147146
"""
148147

149148

150-
def test_output_dir(tmpdir):
151-
test_file = tmpdir.join('test.py').strpath
152-
with open(test_file, 'w') as f:
153-
f.write(TEST_OUTPUT_DIR)
149+
def test_output_dir(tmp_path):
150+
test_file = tmp_path / "test.py"
151+
test_file.write_text(TEST_OUTPUT_DIR)
154152

155-
output_dir = tmpdir.join('test_output_dir')
153+
output_dir = tmp_path / "test_output_dir"
156154

157155
# When we run the test, we should get output images where we specify
158156
code = call_pytest([f'--mpl-results-path={output_dir}',
159-
'--mpl', test_file])
157+
'--mpl', str(test_file)])
160158

161159
assert code != 0
162160
assert output_dir.exists()
@@ -175,13 +173,14 @@ def test_gen():
175173
"""
176174

177175

178-
def test_generate(tmpdir):
176+
def test_generate(tmp_path):
179177

180-
test_file = tmpdir.join('test.py').strpath
181-
with open(test_file, 'w') as f:
182-
f.write(TEST_GENERATE)
178+
test_file = tmp_path / "test.py"
179+
test_file.write_text(TEST_GENERATE)
180+
test_file = str(test_file)
183181

184-
gen_dir = tmpdir.mkdir('spam').mkdir('egg').strpath
182+
gen_dir = tmp_path / "spam" / "egg"
183+
gen_dir.mkdir(parents=True)
185184

186185
# If we don't generate, the test will fail
187186
assert_pytest_fails_with(['--mpl', test_file], 'Image file not found for comparison test')
@@ -301,11 +300,10 @@ def test_hash_fails():
301300
"""
302301

303302

304-
def test_hash_fails(tmpdir):
305-
306-
test_file = tmpdir.join('test.py').strpath
307-
with open(test_file, 'w', encoding='ascii') as f:
308-
f.write(TEST_FAILING_HASH)
303+
def test_hash_fails(tmp_path):
304+
test_file = tmp_path / "test.py"
305+
test_file.write_text(TEST_FAILING_HASH, encoding="ascii")
306+
test_file = str(test_file)
309307

310308
# If we use --mpl, it should detect that the figure is wrong
311309
output = assert_pytest_fails_with(['--mpl', test_file], "doesn't match hash FAIL in library")
@@ -340,11 +338,11 @@ def test_hash_fail_hybrid():
340338

341339

342340
@pytest.mark.skipif(ftv != '261', reason="Incorrect freetype version for hash check")
343-
def test_hash_fail_hybrid(tmpdir):
341+
def test_hash_fail_hybrid(tmp_path):
344342

345-
test_file = tmpdir.join('test.py').strpath
346-
with open(test_file, 'w', encoding='ascii') as f:
347-
f.write(TEST_FAILING_HYBRID)
343+
test_file = tmp_path / "test.py"
344+
test_file.write_text(TEST_FAILING_HYBRID, encoding="ascii")
345+
test_file = str(test_file)
348346

349347
# Assert that image comparison runs and fails
350348
output = assert_pytest_fails_with(['--mpl', test_file,
@@ -382,18 +380,18 @@ def test_hash_fails():
382380

383381

384382
@pytest.mark.skipif(ftv != '261', reason="Incorrect freetype version for hash check")
385-
def test_hash_fail_new_hashes(tmpdir):
383+
def test_hash_fail_new_hashes(tmp_path):
386384
# Check that the hash comparison fails even if a new hash file is requested
387-
test_file = tmpdir.join('test.py').strpath
388-
with open(test_file, 'w', encoding='ascii') as f:
389-
f.write(TEST_FAILING_NEW_HASH)
385+
test_file = tmp_path / "test.py"
386+
test_file.write_text(TEST_FAILING_NEW_HASH, encoding="ascii")
387+
test_file = str(test_file)
390388

391389
# Assert that image comparison runs and fails
392390
assert_pytest_fails_with(['--mpl', test_file,
393391
f'--mpl-hash-library={fail_hash_library}'],
394392
"doesn't match hash FAIL in library")
395393

396-
hash_file = tmpdir.join('new_hashes.json').strpath
394+
hash_file = tmp_path / "new_hashes.json"
397395
# Assert that image comparison runs and fails
398396
assert_pytest_fails_with(['--mpl', test_file,
399397
f'--mpl-hash-library={fail_hash_library}',
@@ -413,11 +411,10 @@ def test_hash_missing():
413411
"""
414412

415413

416-
def test_hash_missing(tmpdir):
417-
418-
test_file = tmpdir.join('test.py').strpath
419-
with open(test_file, 'w') as f:
420-
f.write(TEST_MISSING_HASH)
414+
def test_hash_missing(tmp_path):
415+
test_file = tmp_path / "test.py"
416+
test_file.write_text(TEST_MISSING_HASH)
417+
test_file = str(test_file)
421418

422419
# Assert fails if hash library missing
423420
assert_pytest_fails_with(['--mpl', test_file, '--mpl-hash-library=/not/a/path'],
@@ -450,31 +447,26 @@ def test_unmodified(): return plot()
450447

451448

452449
@pytest.mark.skipif(not hash_library.exists(), reason="No hash library for this mpl version")
453-
def test_results_always(tmpdir):
450+
def test_results_always(tmp_path):
451+
test_file = tmp_path / "test.py"
452+
test_file.write_text(TEST_RESULTS_ALWAYS)
453+
results_path = tmp_path / "results"
454+
results_path.mkdir()
454455

455-
test_file = tmpdir.join('test.py').strpath
456-
with open(test_file, 'w') as f:
457-
f.write(TEST_RESULTS_ALWAYS)
458-
results_path = tmpdir.mkdir('results')
459-
460-
code = call_pytest(['--mpl', test_file, '--mpl-results-always',
456+
code = call_pytest(['--mpl', str(test_file), '--mpl-results-always',
461457
rf'--mpl-hash-library={hash_library}',
462458
rf'--mpl-baseline-path={baseline_dir_abs}',
463459
'--mpl-generate-summary=html,json,basic-html',
464-
rf'--mpl-results-path={results_path.strpath}'])
460+
rf'--mpl-results-path={results_path}'])
465461
assert code == 0 # hashes correct, so all should pass
466462

467463
# assert files for interactive HTML exist
468-
assert results_path.join('fig_comparison.html').exists()
469-
assert results_path.join('styles.css').exists()
470-
assert results_path.join('extra.js').exists()
471-
472-
comparison_file = results_path.join('fig_comparison_basic.html')
473-
with open(comparison_file, 'r') as f:
474-
html = f.read()
464+
assert (results_path / "fig_comparison.html").exists()
465+
assert (results_path / "styles.css").exists()
466+
assert (results_path / "extra.js").exists()
475467

476-
json_file = results_path.join('results.json')
477-
with open(json_file, 'r') as f:
468+
html = (results_path / "fig_comparison_basic.html").read_text()
469+
with (results_path / "results.json").open("r") as f:
478470
json_results = json.load(f)
479471

480472
# each test, and which images should exist
@@ -495,7 +487,7 @@ def test_results_always(tmpdir):
495487

496488
for image_type in ['baseline', 'result-failed-diff', 'result']:
497489
image = f'{test_name}/{image_type}.png'
498-
image_exists = results_path.join(*image.split('/')).exists()
490+
image_exists = (results_path / image).exists()
499491
json_image_key = f"{image_type.split('-')[-1]}_image"
500492
if image_type in exists: # assert image so pytest prints it on error
501493
assert image and image_exists
@@ -554,11 +546,11 @@ def test_fails(self):
554546
TEST_FAILING_CLASS_SETUP_METHOD,
555547
TEST_FAILING_UNITTEST_TESTCASE,
556548
])
557-
def test_class_fail(code, tmpdir):
549+
def test_class_fail(code, tmp_path):
558550

559-
test_file = tmpdir.join('test.py').strpath
560-
with open(test_file, 'w') as f:
561-
f.write(code)
551+
test_file = tmp_path / "test.py"
552+
test_file.write_text(code)
553+
test_file = str(test_file)
562554

563555
# Assert fails if hash library missing
564556
assert_pytest_fails_with(['--mpl', test_file, '--mpl-hash-library=/not/a/path'],

0 commit comments

Comments
 (0)