From d2de8c1a80dabe18052ddbaa9b63b3dcc39f2844 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Sat, 22 Jul 2023 10:15:14 +0300 Subject: [PATCH] tests: fix tests calling themselves --- tests/commands/scripts.py | 36 ++++++++++++++++++++++++++++++++++++ tests/commands/test_edit.py | 21 +++------------------ tests/commands/test_open.py | 14 ++++---------- 3 files changed, 43 insertions(+), 28 deletions(-) create mode 100644 tests/commands/scripts.py diff --git a/tests/commands/scripts.py b/tests/commands/scripts.py new file mode 100644 index 000000000..4514c5530 --- /dev/null +++ b/tests/commands/scripts.py @@ -0,0 +1,36 @@ +import sys + + +def sed_replace(filename: str) -> None: + # NOTE: this function is used by 'test_edit_run' to provide a cross-platform + # way to edit a file that the test can later recognize and see it was called + with open(filename) as fd: + contents = "\n".join([ + line.replace("title: ", "title: test_edit") for line in fd + ]) + + with open(filename, "w") as fd: + fd.write(contents) + + +def echo(filename: str) -> None: + # NOTE: this function is used by 'test_open_run' to provide a cross-platform + # custom command that shows it tried to open a file + print("Attempted to open '{}'".format(filename)) + + +if __name__ == "__main__": + cmd, filename = sys.argv[-2:] + + try: + ret = 0 + if cmd == "sed": + sed_replace(filename) + elif cmd == "echo": + echo(filename) + else: + ret = 1 + except Exception: + ret = 1 + + raise SystemExit(ret) diff --git a/tests/commands/test_edit.py b/tests/commands/test_edit.py index b29e52718..c969766b3 100644 --- a/tests/commands/test_edit.py +++ b/tests/commands/test_edit.py @@ -5,9 +5,11 @@ from tests.testlib import TemporaryLibrary, PapisRunner +script = os.path.join(os.path.dirname(__file__), "scripts.py") + @pytest.mark.library_setup(settings={ - "editor": "python {}".format(__file__), + "editor": "python {} sed".format(script) }) def test_edit_run(tmp_library: TemporaryLibrary) -> None: import papis.config @@ -70,20 +72,3 @@ def test_edit_cli(tmp_library: TemporaryLibrary) -> None: expected_notes_path = os.path.join(folder, notes_name) assert os.path.exists(expected_notes_path) - - -def sed_replace(filename: str) -> None: - # NOTE: this function is used by 'test_edit_run' to provide a cross-platform - # way to edit a file that the test can later recognize and see it was called - with open(filename) as fd: - contents = "\n".join([ - line.replace("title: ", "title: test_edit") for line in fd - ]) - - with open(filename, "w") as fd: - fd.write(contents) - - -if __name__ == "__main__": - import sys - sed_replace(sys.argv[-1]) diff --git a/tests/commands/test_open.py b/tests/commands/test_open.py index 3a97ff65c..59e877c14 100644 --- a/tests/commands/test_open.py +++ b/tests/commands/test_open.py @@ -1,7 +1,10 @@ +import os import pytest from tests.testlib import TemporaryLibrary, PapisRunner +script = os.path.join(os.path.dirname(__file__), "scripts.py") + @pytest.mark.library_setup(settings={ "file-browser": "echo" @@ -30,14 +33,5 @@ def test_open_cli(tmp_library: TemporaryLibrary) -> None: result = cli_runner.invoke( cli, - ["--tool", "python {}".format(__file__), "--mark", "--all", "Krishnamurti"]) + ["--tool", "python {} echo".format(script), "--mark", "--all", "Krishnamurti"]) assert result.exit_code == 0 - - -def echo(filename: str) -> None: - print("Attempted to open '{}'".format(filename)) - - -if __name__ == "__main__": - import sys - echo(sys.argv[-1])