From d178456ede909903ea711f462955728e4f12803f Mon Sep 17 00:00:00 2001 From: Krzysztof Laskowski Date: Mon, 19 Feb 2024 02:47:23 +0100 Subject: [PATCH] deps: removed dependency on "py" package - no need to import Source from py library (it's enough to dedent) - no need to use obsolete py.path.local, using pathlib.Path instead --- docs/requirements.txt | 1 - pytest_logger/plugin.py | 32 ++++++++++++++++---------------- setup.py | 2 +- tests/test_pytest_logger.py | 11 +++++------ 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/docs/requirements.txt b/docs/requirements.txt index 91e8e80..db6029f 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,4 +1,3 @@ -py==1.11.0 pytest==7.4.4 sphinx==7.2.6 sphinx_rtd_theme==2.0.0 diff --git a/pytest_logger/plugin.py b/pytest_logger/plugin.py index 3402479..a67b364 100644 --- a/pytest_logger/plugin.py +++ b/pytest_logger/plugin.py @@ -1,13 +1,14 @@ import os import sys import re -import py import pytest import logging import time import datetime import argparse +import shutil from builtins import object, int +from pathlib import Path def pytest_addhooks(pluginmanager): @@ -107,13 +108,13 @@ def pytest_runtest_makereport(self, item, call): logger = getattr(item, '_logger', None) if logger: if self._logsdir and self._split_by_outcome_subdir and tr.outcome in self._split_by_outcome_outcomes: - split_by_outcome_logdir = self._logsdir.join(self._split_by_outcome_subdir, tr.outcome) + split_by_outcome_logdir = self._logsdir / self._split_by_outcome_subdir / tr.outcome nodeid = _sanitize_nodeid(item.nodeid) nodepath = os.path.dirname(nodeid) - split_by_outcome_logdir.join(nodepath).ensure(dir=1) - destdir_relpath = os.path.relpath(str(self._logsdir.join(nodeid)), - str(split_by_outcome_logdir.join(nodepath))) - _refresh_link(destdir_relpath, str(split_by_outcome_logdir.join(nodeid))) + outcomedir = split_by_outcome_logdir / nodepath + outcomedir.mkdir(parents=True, exist_ok=True) + destdir_relpath = os.path.relpath(self._logsdir / nodeid, outcomedir) + _refresh_link(destdir_relpath, split_by_outcome_logdir / nodeid) if call.when == 'teardown': logger.on_makereport() @@ -356,21 +357,20 @@ def _make_logsdir_tmpdir(tmpdirhandler): if logsdir.basename.startswith('popen-gw'): logsdir = logsdir.join('..') logsdir = logsdir.join('logs').ensure(dir=1) - return logsdir + return Path(logsdir) -def _make_logsdir_dir(dstname, cleandir=True): - logsdir = py.path.local(dstname) - if cleandir: - if logsdir.check(): - logsdir.remove() - logsdir.mkdir() - return logsdir +def _make_logsdir_dir(dstname): + shutil.rmtree(dstname, ignore_errors=True) + os.mkdir(dstname) + return Path(dstname) def _make_logdir(item): plugin = item.config.pluginmanager.getplugin('_logger') - return plugin.logsdir().join(_sanitize_nodeid(item.nodeid)).ensure(dir=1) + logdir = plugin.logsdir() / _sanitize_nodeid(item.nodeid) + logdir.mkdir(parents=True) + return logdir def _enable(handlers): @@ -485,7 +485,7 @@ def make_handler(logdir, logger_and_level, fmt): name, level = logger_and_level logger = logging.getLogger(name) name = name or 'logs' - logfile = str(logdir.join(name)) + logfile = str(logdir / name) handler = logging.FileHandler(filename=logfile, mode='w', delay=True) handler.setFormatter(fmt) handler.setLevel(level) diff --git a/setup.py b/setup.py index 1b8a016..4df26ef 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ def read(fname): long_description=read('README.rst'), long_description_content_type='text/x-rst', packages=['pytest_logger'], - install_requires=['pytest>=3.2', 'py'], + install_requires=['pytest>=3.2'], keywords='py.test pytest logging', classifiers=[ 'Development Status :: 5 - Production/Stable', diff --git a/tests/test_pytest_logger.py b/tests/test_pytest_logger.py index f14aac3..74f93ca 100644 --- a/tests/test_pytest_logger.py +++ b/tests/test_pytest_logger.py @@ -2,8 +2,7 @@ import sys import pytest import platform -from py.code import Source -from _pytest.pytester import LineMatcher +import textwrap win32 = sys.platform == 'win32' win32py2 = win32 and sys.version_info[0] == 2 @@ -11,7 +10,7 @@ def makefile(testdir, path, content): - return testdir.tmpdir.ensure(*path).write('\n'.join(Source(content))) + return testdir.tmpdir.ensure(*path).write(textwrap.dedent(content)) def ls(dir, filepath=''): @@ -64,10 +63,10 @@ def test_case(): return filename -class FileLineMatcher(LineMatcher): +class FileLineMatcher(pytest.LineMatcher): def __init__(self, dir, filepath): lines = dir.join(filepath).read().splitlines() - LineMatcher.__init__(self, lines) + pytest.LineMatcher.__init__(self, lines) def test_logdir_fixture(testdir): @@ -566,7 +565,7 @@ def test_logsdir_cleanup(testdir, conftest_py, test_case_py): logger_logsdir={0} """.format(logsdir)) - logsdir.ensure('tmpfile').write('\n'.join(Source('this shall be removed'))) + logsdir.ensure('tmpfile').write(textwrap.dedent('this shall be removed')) logsdir.join('tmpdir') result = testdir.runpytest('-s')