From 9ae689f79533ec40908db8d804059f2fbb38e751 Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Tue, 12 Sep 2023 15:57:22 +0200 Subject: [PATCH 01/12] Fix: Use absolute path for the report Fixes #732 --- src/pytest_html/basereport.py | 4 +++- testing/test_integration.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index 1cccc939..ad771766 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -18,7 +18,9 @@ class BaseReport: def __init__(self, report_path, config, report_data, template, css): - self._report_path = Path(os.path.expandvars(report_path)).expanduser() + self._report_path = ( + Path(os.path.expandvars(report_path)).expanduser().absolute() + ) self._report_path.parent.mkdir(parents=True, exist_ok=True) self._config = config self._template = template diff --git a/testing/test_integration.py b/testing/test_integration.py index 39b46bf0..c1094569 100644 --- a/testing/test_integration.py +++ b/testing/test_integration.py @@ -377,6 +377,21 @@ def test_function(arg): assert_that(col_name).contains("::setup") assert_that(get_log(page)).contains("ValueError") + def test_chdir(self, pytester): + pytester.makepyfile( + """ + import pytest + @pytest.fixture + def changing_dir(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + yield tmp_path + def test_function(changing_dir): + pass + """ + ) + page = run(pytester) + assert_results(page, passed=1) + @pytest.mark.parametrize("title", ["", "Special Report"]) def test_report_title(self, pytester, title): pytester.makepyfile("def test_pass(): pass") From 58e1cb79980174ca07b9f1c239c68dfba5e487df Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Tue, 12 Sep 2023 18:46:06 +0200 Subject: [PATCH 02/12] Move and fix test --- testing/test_integration.py | 15 --------------- testing/test_unit.py | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/testing/test_integration.py b/testing/test_integration.py index c1094569..39b46bf0 100644 --- a/testing/test_integration.py +++ b/testing/test_integration.py @@ -377,21 +377,6 @@ def test_function(arg): assert_that(col_name).contains("::setup") assert_that(get_log(page)).contains("ValueError") - def test_chdir(self, pytester): - pytester.makepyfile( - """ - import pytest - @pytest.fixture - def changing_dir(tmp_path, monkeypatch): - monkeypatch.chdir(tmp_path) - yield tmp_path - def test_function(changing_dir): - pass - """ - ) - page = run(pytester) - assert_results(page, passed=1) - @pytest.mark.parametrize("title", ["", "Special Report"]) def test_report_title(self, pytester, title): pytester.makepyfile("def test_pass(): pass") diff --git a/testing/test_unit.py b/testing/test_unit.py index be8ef387..6089d55a 100644 --- a/testing/test_unit.py +++ b/testing/test_unit.py @@ -70,6 +70,26 @@ def pytest_html_results_summary(prefix, summary, postfix, session): result.assert_outcomes(passed=1) +def test_chdir(tmp_path, pytester): + pytester.makepyfile( + """ + import pytest + + @pytest.fixture + def changing_dir(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + + def test_function(changing_dir): + pass + """ + ) + page = pytester.runpytest("--html", "reports/report.html") + assert page.ret == 0 + assert ( + f"Generated html report: file://{tmp_path / 'reports' / 'report.html'}" + ).replace("test_chdir0", "test_chdir1") in page.outlines[-2] + + @pytest.fixture def css_file_path(pytester): css_one = """ From 49b78e0ba0f7a9e64e154581a6b43d5f17246eb4 Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Tue, 12 Sep 2023 22:11:41 +0200 Subject: [PATCH 03/12] Simpler test --- testing/test_unit.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/testing/test_unit.py b/testing/test_unit.py index 6089d55a..11c307e2 100644 --- a/testing/test_unit.py +++ b/testing/test_unit.py @@ -70,7 +70,7 @@ def pytest_html_results_summary(prefix, summary, postfix, session): result.assert_outcomes(passed=1) -def test_chdir(tmp_path, pytester): +def test_chdir(pytester): pytester.makepyfile( """ import pytest @@ -86,8 +86,8 @@ def test_function(changing_dir): page = pytester.runpytest("--html", "reports/report.html") assert page.ret == 0 assert ( - f"Generated html report: file://{tmp_path / 'reports' / 'report.html'}" - ).replace("test_chdir0", "test_chdir1") in page.outlines[-2] + f"Generated html report: file://{pytester.path / 'reports' / 'report.html'}" + ) in page.outlines[-2] @pytest.fixture From 3e0a5b8034131f95bfbdea22964726d07db2aecb Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Tue, 12 Sep 2023 22:30:08 +0200 Subject: [PATCH 04/12] Add Changelog entry --- docs/changelog.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 9b2c5f2a..bc3d800b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,13 @@ Versions follow `Semantic Versioning`_ (``..``). Version History --------------- +4.0.2 (2023-09-12) +~~~~~~~~~~~~~~~~~~ + +* Use absolute path to the report file. + + * Thanks to `@adrien-berchet `_ for reporting and for the PR. + 4.0.1 (2023-09-10) ~~~~~~~~~~~~~~~~~~ From d0ca2d80fc6ff03f13a3053d9233f11328dc133a Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Wed, 13 Sep 2023 09:12:13 +0200 Subject: [PATCH 05/12] Use resolve() instead of absolute() --- src/pytest_html/basereport.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index ad771766..35443334 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -18,9 +18,7 @@ class BaseReport: def __init__(self, report_path, config, report_data, template, css): - self._report_path = ( - Path(os.path.expandvars(report_path)).expanduser().absolute() - ) + self._report_path = Path(os.path.expandvars(report_path)).expanduser().resolve() self._report_path.parent.mkdir(parents=True, exist_ok=True) self._config = config self._template = template @@ -188,7 +186,7 @@ def pytest_sessionfinish(self, session): def pytest_terminal_summary(self, terminalreporter): terminalreporter.write_sep( "-", - f"Generated html report: file://{self._report_path.resolve().as_posix()}", + f"Generated html report: file://{self._report_path.as_posix()}", ) @pytest.hookimpl(trylast=True) From 2f56409714e5d0114a44dd59157c67ecea6e062d Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Wed, 13 Sep 2023 09:21:37 +0200 Subject: [PATCH 06/12] Use as_uri() --- src/pytest_html/basereport.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index 35443334..9aa8599d 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -186,7 +186,7 @@ def pytest_sessionfinish(self, session): def pytest_terminal_summary(self, terminalreporter): terminalreporter.write_sep( "-", - f"Generated html report: file://{self._report_path.as_posix()}", + f"Generated html report: {self._report_path.as_uri()}", ) @pytest.hookimpl(trylast=True) From 00a6419b4be93f6f5d79254fa37fda2e3656f403 Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Wed, 13 Sep 2023 09:50:57 +0200 Subject: [PATCH 07/12] Use Path.cwd() to build the absolute path because resolve() fails on Windows --- src/pytest_html/basereport.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index 9aa8599d..b160e910 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -18,7 +18,9 @@ class BaseReport: def __init__(self, report_path, config, report_data, template, css): - self._report_path = Path(os.path.expandvars(report_path)).expanduser().resolve() + self._report_path = ( + Path.cwd() / Path(os.path.expandvars(report_path)).expanduser() + ) self._report_path.parent.mkdir(parents=True, exist_ok=True) self._config = config self._template = template From fd4d86c68bd01372a6687b15e4dcb192cd543e93 Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Wed, 13 Sep 2023 13:07:00 +0200 Subject: [PATCH 08/12] Fix test --- testing/test_unit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_unit.py b/testing/test_unit.py index 11c307e2..3515d375 100644 --- a/testing/test_unit.py +++ b/testing/test_unit.py @@ -86,7 +86,7 @@ def test_function(changing_dir): page = pytester.runpytest("--html", "reports/report.html") assert page.ret == 0 assert ( - f"Generated html report: file://{pytester.path / 'reports' / 'report.html'}" + f"Generated html report: {(pytester.path / 'reports' / 'report.html').as_uri()}" ) in page.outlines[-2] From 6bf1be989c54b18c13af06cd3bcf50fc3f465062 Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Wed, 13 Sep 2023 13:20:20 +0200 Subject: [PATCH 09/12] Try again with resolve() --- src/pytest_html/basereport.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index b160e910..9aa8599d 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -18,9 +18,7 @@ class BaseReport: def __init__(self, report_path, config, report_data, template, css): - self._report_path = ( - Path.cwd() / Path(os.path.expandvars(report_path)).expanduser() - ) + self._report_path = Path(os.path.expandvars(report_path)).expanduser().resolve() self._report_path.parent.mkdir(parents=True, exist_ok=True) self._config = config self._template = template From c5c077ac48bdf59afeb4bbc5e890ac2712d60bdd Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Wed, 13 Sep 2023 14:02:08 +0200 Subject: [PATCH 10/12] Revert to Path.cwd() --- src/pytest_html/basereport.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index 9aa8599d..b160e910 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -18,7 +18,9 @@ class BaseReport: def __init__(self, report_path, config, report_data, template, css): - self._report_path = Path(os.path.expandvars(report_path)).expanduser().resolve() + self._report_path = ( + Path.cwd() / Path(os.path.expandvars(report_path)).expanduser() + ) self._report_path.parent.mkdir(parents=True, exist_ok=True) self._config = config self._template = template From d5b68ce19e0fbb90ae846cb617ad27f5e23e0636 Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Wed, 13 Sep 2023 22:57:51 +0200 Subject: [PATCH 11/12] Another try with resolve --- src/pytest_html/basereport.py | 4 +--- testing/test_unit.py | 6 ++++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index b160e910..9aa8599d 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -18,9 +18,7 @@ class BaseReport: def __init__(self, report_path, config, report_data, template, css): - self._report_path = ( - Path.cwd() / Path(os.path.expandvars(report_path)).expanduser() - ) + self._report_path = Path(os.path.expandvars(report_path)).expanduser().resolve() self._report_path.parent.mkdir(parents=True, exist_ok=True) self._config = config self._template = template diff --git a/testing/test_unit.py b/testing/test_unit.py index 3515d375..f82825ce 100644 --- a/testing/test_unit.py +++ b/testing/test_unit.py @@ -1,6 +1,7 @@ import importlib.resources import os import sys +from pathlib import Path import pkg_resources import pytest @@ -83,10 +84,11 @@ def test_function(changing_dir): pass """ ) - page = pytester.runpytest("--html", "reports/report.html") + report_path = Path("reports") / "report.html" + page = pytester.runpytest("--html", str(report_path)) assert page.ret == 0 assert ( - f"Generated html report: {(pytester.path / 'reports' / 'report.html').as_uri()}" + f"Generated html report: {(pytester.path / report_path).as_uri()}" ) in page.outlines[-2] From 28d01291a922bc50d89e16ec11436dcfd8ff6a0a Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Wed, 13 Sep 2023 23:56:20 +0200 Subject: [PATCH 12/12] Revert to Path.cwd() --- src/pytest_html/basereport.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index 9aa8599d..b160e910 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -18,7 +18,9 @@ class BaseReport: def __init__(self, report_path, config, report_data, template, css): - self._report_path = Path(os.path.expandvars(report_path)).expanduser().resolve() + self._report_path = ( + Path.cwd() / Path(os.path.expandvars(report_path)).expanduser() + ) self._report_path.parent.mkdir(parents=True, exist_ok=True) self._config = config self._template = template