Repository: metov/openeditor

Issue:
FileNotFoundError when editing a file without contents
The following succeeds:
```
python -c "import openeditor; print(openeditor.edit_temp(contents='hello'))"
```
but this gives FileNotFoundError:
```
python -c "import openeditor; print(openeditor.edit_temp())"
```

Comments:

Patch:
diff --git a/src/lib.py b/src/lib.py
index 9d0eb86..9a0e2f0 100644
--- a/src/lib.py
+++ b/src/lib.py
@@ -37,3 +37,10 @@ def editor() -> str:
             "Both $VISUAL and $EDITOR are unset, could not pick "
             "an appropriate editor."
         )
+
+
+def write_file(path, contents=""):
+    """Create a file. If contents are given, they will be written to it."""
+    with open(path, 'w') as f:
+        if contents:
+            f.write(contents)
diff --git a/src/openeditor.py b/src/openeditor.py
index 51efe9a..ea94349 100644
--- a/src/openeditor.py
+++ b/src/openeditor.py
@@ -3,7 +3,7 @@
 import tempfile
 from pathlib import Path
 
-from lib import edit
+from lib import edit, write_file
 
 DEFAULT_TEMPFILE = "tempfile"
 
@@ -31,14 +31,10 @@ def edit_temp(contents="", name=""):
     :return: Contents of the file when the editor is closed.
     """
 
-    # Create a temp file with requested name, if any
+    # Create a temp file, ensure it has requested name and contents
     td = tempfile.TemporaryDirectory()
     tfpath = Path(td.name) / (name or DEFAULT_TEMPFILE)
-
-    # Populate contents if needed
-    if contents:
-        with tfpath.open("w") as f:
-            f.write(contents)
+    write_file(tfpath, contents)
 
     # Edit interactively
     return edit(tfpath)


Test patch:
diff --git a/tests/lib/test_write_file.py b/tests/lib/test_write_file.py
new file mode 100644
index 0000000..45acbb1
--- /dev/null
+++ b/tests/lib/test_write_file.py
@@ -0,0 +1,10 @@
+from unittest.mock import mock_open, patch
+
+from lib import write_file
+
+
+def test_empty_file_created(monkeypatch):
+    fake_open = mock_open()
+    with patch('lib.open', fake_open):
+        write_file('/tmp/nosuchfile.txt', contents="")
+        fake_open.assert_called_once()
diff --git a/tests/openeditor/test_edit_temp.py b/tests/openeditor/test_edit_temp.py
index e5ccd8d..e6e7233 100644
--- a/tests/openeditor/test_edit_temp.py
+++ b/tests/openeditor/test_edit_temp.py
@@ -1,6 +1,5 @@
 import tempfile
 from pathlib import Path
-from unittest.mock import mock_open
 
 import openeditor
 
@@ -9,23 +8,17 @@ class MockDir:
     name = "/fake/temp/dir"
 
 
-class Recorder:
-    """A class for recording calls."""
-
-    last_arg = None
-
-    def record(self, arg):
-        self.last_arg = arg
+class MockWrite:
+    def record(self, path, contents):
+        self.path = path
 
 
 def test_default_name_on_empty_name(monkeypatch):
-    # Don't actually create a temp dir
     monkeypatch.setattr(tempfile, "TemporaryDirectory", MockDir)
-    mock_open()
-    fake_edit = Recorder()
-    monkeypatch.setattr(openeditor, "edit", fake_edit.record)
+    fake_write = MockWrite()
+    monkeypatch.setattr(openeditor, "write_file", fake_write.record)
+    monkeypatch.setattr(openeditor, "edit", lambda fn: None)
 
     openeditor.edit_temp(name="")
-    exp_args = str(Path(MockDir.name) / openeditor.DEFAULT_TEMPFILE)
-    obs_args = str(fake_edit.last_arg)
-    assert exp_args == obs_args
+    exp = str(Path(MockDir.name) / openeditor.DEFAULT_TEMPFILE)
+    assert exp == str(fake_write.path)