From fcdc1d867e25b212ca7681b70cbf785b2645ad06 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Wed, 1 Aug 2018 08:25:37 +0200 Subject: [PATCH] fix #3745 - display absolute cache_dir if necessary --- changelog/3745.bugfix | 1 + src/_pytest/cacheprovider.py | 11 +++++++++-- testing/test_cacheprovider.py | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 changelog/3745.bugfix diff --git a/changelog/3745.bugfix b/changelog/3745.bugfix new file mode 100644 index 000000000..0225be77a --- /dev/null +++ b/changelog/3745.bugfix @@ -0,0 +1 @@ +Display the absolute path if ``cache_dir`` is not relative to the ``rootdir`` instead of failing. diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index f4ab3bfdc..dc72512b8 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -312,8 +312,15 @@ def cache(request): def pytest_report_header(config): if config.option.verbose: - relpath = config.cache._cachedir.relative_to(config.rootdir) - return "cachedir: {}".format(relpath) + cachedir = config.cache._cachedir + # TODO: evaluate generating upward relative paths + # starting with .., ../.. if sensible + + try: + displaypath = cachedir.relative_to(config.rootdir) + except ValueError: + displaypath = cachedir + return "cachedir: {}".format(displaypath) def cacheshow(config, session): diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py index 7c51b770d..7ec73ec63 100644 --- a/testing/test_cacheprovider.py +++ b/testing/test_cacheprovider.py @@ -150,6 +150,32 @@ def test_cache_reportheader(testdir): result.stdout.fnmatch_lines(["cachedir: .pytest_cache"]) +def test_cache_reportheader_external_abspath(testdir, tmpdir_factory): + external_cache = tmpdir_factory.mktemp( + "test_cache_reportheader_external_abspath_abs" + ) + + testdir.makepyfile( + """ + def test_hello(): + pass + """ + ) + testdir.makeini( + """ + [pytest] + cache_dir = {abscache} + """.format( + abscache=external_cache + ) + ) + + result = testdir.runpytest("-v") + result.stdout.fnmatch_lines( + ["cachedir: {abscache}".format(abscache=external_cache)] + ) + + def test_cache_show(testdir): result = testdir.runpytest("--cache-show") assert result.ret == 0