From 22d2c98fc3db837467fc344a44f786e7ee7544e0 Mon Sep 17 00:00:00 2001 From: Tom Ritchford Date: Sat, 6 Jan 2024 12:29:31 +0100 Subject: [PATCH] Teach editor to be monkeypatchable (see #6) --- editor/__init__.py | 2 +- test_editor.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/editor/__init__.py b/editor/__init__.py index 04d2619..084685a 100644 --- a/editor/__init__.py +++ b/editor/__init__.py @@ -59,7 +59,7 @@ EDITORS = {'Windows': 'notepad'} -@xmod.xmod +@xmod.xmod(mutable=True) def editor( text: t.Optional[str] = None, filename: t.Union[None, Path, str] = None, diff --git a/test_editor.py b/test_editor.py index 6869183..4736408 100644 --- a/test_editor.py +++ b/test_editor.py @@ -8,6 +8,7 @@ FILENAME = 'a_file.txt' EDITOR = editor.default_editor() +TEST_CONTENT = 'roses are red,\nwater is blue.\n' @mock.patch('editor.runs.call', autospec=True) @@ -46,3 +47,15 @@ def test_temp2(self, call): expected = 'some contents' assert actual == expected call.assert_called_once() + + +def main(): + print(editor.editor()) + + +def test_main(monkeypatch, capsys): + monkeypatch.setattr('editor.editor', lambda: TEST_CONTENT) + + main() + + assert TEST_CONTENT + '\n' == capsys.readouterr().out