From ff6668f2955ac8b3e5a6f02ca3e86c4cc3130b8e Mon Sep 17 00:00:00 2001 From: Philipp Albrecht Date: Mon, 28 Aug 2023 09:49:08 +0200 Subject: Generalize (testdata) substitution in end2end tests We replace the `(testdata)` placeholder with `testutils.abs_datapath()` in a few end2end tests. So far we only needed to replace `(testdata)` with an OS path (e.g. in `tests/end2end/features/spawn.feature`). By introducing `(testdata)` to an end2end test in `tests/end2end/features/hints.feature`, we required a new use case: replacing `(testdata)` as part of a valid file:// URI. ``` When I open file://(testdata)/some/file.txt ``` Replacing `(testdata)` in above BDD step with a plain OS path resulted in invalid URIs, e.g. for the path "C:\\Users" above BDD step results in this invalid URI: ``` When I open file://C:\Users/some/file.txt ``` We deal with this by first isolating the `(testdata)` substitution in a single place. Having `(testdata)` substitutions in a single place, we simply special-case the substitution of file:// paths, which will be part of a URI. Successful substitution for above BDD step looks like the following: ``` When I open file:///C:/Users/some/file.txt ``` --- tests/end2end/features/conftest.py | 6 +++--- tests/helpers/testutils.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/end2end/features/conftest.py b/tests/end2end/features/conftest.py index 62a81c5fc..089f0c42c 100644 --- a/tests/end2end/features/conftest.py +++ b/tests/end2end/features/conftest.py @@ -198,7 +198,7 @@ def open_path(quteproc, server, path): - With "... as a URL", it's opened according to new_instance_open_target. """ path = path.replace('(port)', str(server.port)) - path = path.replace('(testdata)', os.fspath(testutils.abs_datapath())) + path = testutils.substitute_testdata(path) new_tab = False new_bg_tab = False @@ -270,7 +270,7 @@ def run_command(quteproc, server, tmpdir, command): invalid = False command = command.replace('(port)', str(server.port)) - command = command.replace('(testdata)', str(testutils.abs_datapath())) + command = testutils.substitute_testdata(command) command = command.replace('(tmpdir)', str(tmpdir)) command = command.replace('(dirsep)', os.sep) command = command.replace('(echo-exe)', _get_echo_exe_path()) @@ -364,7 +364,7 @@ def hint(quteproc, args): @bdd.when(bdd.parsers.parse('I hint with args "{args}" and follow {letter}')) def hint_and_follow(quteproc, args, letter): - args = args.replace('(testdata)', str(testutils.abs_datapath())) + args = testutils.substitute_testdata(args) args = args.replace('(python-executable)', sys.executable) quteproc.send_cmd(':hint {}'.format(args)) quteproc.wait_for(message='hints: *') diff --git a/tests/helpers/testutils.py b/tests/helpers/testutils.py index a6022a6f9..d74c46fcb 100644 --- a/tests/helpers/testutils.py +++ b/tests/helpers/testutils.py @@ -196,6 +196,24 @@ def abs_datapath(): return path.resolve() +def substitute_testdata(path): + r"""Replace the (testdata) placeholder in path with `abs_datapath()`. + + If path is starting with file://, return path as an URI with file:// removed. This + is useful if path is going to be inserted into an URI: + + >>> path = substitute_testdata("C:\Users\qute") + >>> f"file://{path}/slug # results in valid URI + 'file:///C:/Users/qute/slug' + """ + if path.startswith('file://'): + testdata_path = abs_datapath().as_uri().replace('file://', '') + else: + testdata_path = str(abs_datapath()) + + return path.replace('(testdata)', testdata_path) + + @contextlib.contextmanager def nop_contextmanager(): yield -- cgit v1.2.3-54-g00ecf