Skip to content

Commit aed8543

Browse files
committed
fixup! refactor: add editor module
1 parent 777eb55 commit aed8543

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/git_draft/editor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ def open_editor(placeholder="") -> str:
3333
with open(temp.name, "w") as writer:
3434
writer.write(placeholder)
3535

36-
stdout = open(_get_tty_filename(), "wb")
37-
proc = subprocess.Popen(
38-
[binpath, temp.name], close_fds=True, stdout=stdout
39-
)
40-
proc.communicate()
36+
with open(_get_tty_filename(), "wb") as stdout:
37+
proc = subprocess.Popen(
38+
[binpath, temp.name], close_fds=True, stdout=stdout
39+
)
40+
proc.communicate()
4141

4242
with open(temp.name, mode="r") as reader:
4343
return reader.read()

tests/git_draft/editor_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import pytest
12
import shutil
3+
import subprocess
24

35
import git_draft.editor as sut
46

@@ -40,3 +42,30 @@ def which(_editor):
4042
monkeypatch.setenv("EDITOR", "")
4143

4244
assert sut._guess_editor_binpath() == ""
45+
46+
47+
class TestOpenEditor:
48+
def test_no_binpath(self, monkeypatch) -> None:
49+
def which(_editor):
50+
return ""
51+
52+
monkeypatch.setattr(shutil, "which", which)
53+
54+
with pytest.raises(ValueError):
55+
sut.open_editor()
56+
57+
def test_ok(self, monkeypatch) -> None:
58+
def which(editor):
59+
return f"/bin/{editor}"
60+
61+
class Popen:
62+
def __init__(self, *_args, **_kwargs):
63+
pass
64+
65+
def communicate(self):
66+
pass
67+
68+
monkeypatch.setattr(shutil, "which", which)
69+
monkeypatch.setattr(subprocess, "Popen", Popen)
70+
71+
assert sut.open_editor(placeholder="hello") == "hello"

0 commit comments

Comments
 (0)