diff --git a/babi/screen.py b/babi/screen.py index a18dc14..146eb3e 100644 --- a/babi/screen.py +++ b/babi/screen.py @@ -494,17 +494,17 @@ class Screen: else: self.file.filename = filename - if os.path.isfile(self.file.filename): + if not os.path.isfile(self.file.filename): + sha256: Optional[str] = None + else: with open(self.file.filename, encoding='UTF-8', newline='') as f: *_, sha256 = get_lines(f) - else: - sha256 = hashlib.sha256(b'').hexdigest() contents = self.file.nl.join(self.file.buf) sha256_to_save = hashlib.sha256(contents.encode()).hexdigest() # the file on disk is the same as when we opened it - if sha256 not in (self.file.sha256, sha256_to_save): + if sha256 not in (None, self.file.sha256, sha256_to_save): self.status.update('(file changed on disk, not implemented)') return PromptResult.CANCELLED diff --git a/tests/features/save_test.py b/tests/features/save_test.py index fbf8245..c74e079 100644 --- a/tests/features/save_test.py +++ b/tests/features/save_test.py @@ -162,6 +162,18 @@ def test_save_via_ctrl_o_set_filename(run, tmpdir): assert f.read() == 'hello world\n' +def test_save_via_ctrl_o_new_filename(run, tmpdir): + f = tmpdir.join('f') + f.write('wat\n') + with run(str(f)) as h, and_exit(h): + h.press('^O') + h.await_text('enter filename: ') + h.press_and_enter('new') + h.await_text('saved! (1 line written)') + assert f.read() == 'wat\n' + assert tmpdir.join('fnew').read() == 'wat\n' + + @pytest.mark.parametrize('key', ('^C', 'Enter')) def test_save_via_ctrl_o_cancelled(run, key): with run() as h, and_exit(h):