From 61229eb0d57ccbb647da390cb474b77c7786cf22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionel=20Cristian=20M=C4=83rie=C8=99?= Date: Mon, 28 Oct 2024 18:00:36 +0200 Subject: [PATCH] Replace all uses of py.path with pathlib. --- src/pytest_benchmark/csv.py | 13 +++++++------ src/pytest_benchmark/histogram.py | 6 +++--- tests/test_cli.py | 6 +++--- tests/test_elasticsearch_storage.py | 6 +++--- tests/test_storage.py | 6 +++--- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/pytest_benchmark/csv.py b/src/pytest_benchmark/csv.py index e1e0af9..2bcac52 100644 --- a/src/pytest_benchmark/csv.py +++ b/src/pytest_benchmark/csv.py @@ -1,7 +1,6 @@ import csv import operator - -import py +from pathlib import Path class CSVResults: @@ -11,10 +10,12 @@ def __init__(self, columns, sort, logger): self.logger = logger def render(self, output_file, groups): - output_file = py.path.local(output_file) - if not output_file.ext: - output_file = output_file.new(ext='csv') - with output_file.open('w', ensure=True) as stream: + output_file = Path(output_file) + output_file.parent.mkdir(exist_ok=True, parents=True) + + if not output_file.suffix: + output_file = output_file.with_suffix('.csv') + with output_file.open('w') as stream: writer = csv.writer(stream) params = sorted( {param for group, benchmarks in groups for benchmark in benchmarks for param in benchmark.get('params', {}) or ()} diff --git a/src/pytest_benchmark/histogram.py b/src/pytest_benchmark/histogram.py index 15aa791..ec49d20 100644 --- a/src/pytest_benchmark/histogram.py +++ b/src/pytest_benchmark/histogram.py @@ -1,6 +1,5 @@ from collections.abc import Iterable - -import py +from pathlib import Path from .utils import TIME_UNITS from .utils import slugify @@ -100,7 +99,8 @@ def make_histogram(output_prefix, name, benchmarks, unit, adjustment): path = f'{output_prefix}.svg' title = f'Speed in {TIME_UNITS[unit]}' - output_file = py.path.local(path).ensure() + output_file = Path(path) + output_file.parent.mkdir(exist_ok=True, parents=True) plot = make_plot( benchmarks=benchmarks, diff --git a/tests/test_cli.py b/tests/test_cli.py index 7126b82..bef7248 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,14 +1,14 @@ import sys from collections import namedtuple +from pathlib import Path -import py import pytest from _pytest.pytester import LineMatcher pytest_plugins = ('pytester',) -THIS = py.path.local(__file__) -STORAGE = THIS.dirpath('test_storage') +THIS = Path(__file__) +STORAGE = THIS.with_name('test_storage') @pytest.fixture diff --git a/tests/test_elasticsearch_storage.py b/tests/test_elasticsearch_storage.py index 25499f0..c8f9a8b 100644 --- a/tests/test_elasticsearch_storage.py +++ b/tests/test_elasticsearch_storage.py @@ -3,9 +3,9 @@ import os from io import BytesIO from io import StringIO +from pathlib import Path import elasticsearch -import py import pytest from freezegun import freeze_time @@ -25,8 +25,8 @@ logger = logging.getLogger(__name__) -THIS = py.path.local(__file__) -BENCHFILE = THIS.dirpath('test_storage/0030_5b78858eb718649a31fb93d8dc96ca2cee41a4cd_20150815_030419_uncommitted-changes.json') +THIS = Path(__file__) +BENCHFILE = THIS.with_name('test_storage') / '0030_5b78858eb718649a31fb93d8dc96ca2cee41a4cd_20150815_030419_uncommitted-changes.json' SAVE_DATA = json.loads(BENCHFILE.read_text(encoding='utf8')) SAVE_DATA['machine_info'] = {'foo': 'bar'} SAVE_DATA['commit_info'] = {'foo': 'bar'} diff --git a/tests/test_storage.py b/tests/test_storage.py index 73ee794..5d1e2fd 100644 --- a/tests/test_storage.py +++ b/tests/test_storage.py @@ -28,10 +28,10 @@ pytest_plugins = 'pytester' -THIS = py.path.local(__file__) -STORAGE = THIS.dirpath(THIS.purebasename) +THIS = Path(__file__) +STORAGE = THIS.with_suffix('') -JSON_DATA = json.loads(STORAGE.listdir('0030_*.json')[0].read_text(encoding='utf8')) +JSON_DATA = json.loads(next(STORAGE.glob('0030_*.json')).read_text(encoding='utf8')) JSON_DATA['machine_info'] = {'foo': 'bar'} JSON_DATA['commit_info'] = {'foo': 'bar'} list(normalize_stats(bench['stats']) for bench in JSON_DATA['benchmarks'])