From e059dc294b8376a400f4240006b5cd0899d45e14 Mon Sep 17 00:00:00 2001 From: theendlessriver13 Date: Sat, 30 Jan 2021 02:17:40 +0100 Subject: [PATCH] fix crashing on permission denied --- babi/screen.py | 10 ++++++++-- tests/features/save_test.py | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/babi/screen.py b/babi/screen.py index cb58161..a18dc14 100644 --- a/babi/screen.py +++ b/babi/screen.py @@ -508,8 +508,14 @@ class Screen: self.status.update('(file changed on disk, not implemented)') return PromptResult.CANCELLED - with open(self.file.filename, 'w', encoding='UTF-8', newline='') as f: - f.write(contents) + try: + with open( + self.file.filename, 'w', encoding='UTF-8', newline='', + ) as f: + f.write(contents) + except OSError as e: + self.status.update(f'cannot save file: {e}') + return PromptResult.CANCELLED self.file.modified = False self.file.sha256 = sha256_to_save diff --git a/tests/features/save_test.py b/tests/features/save_test.py index cb268dc..fbf8245 100644 --- a/tests/features/save_test.py +++ b/tests/features/save_test.py @@ -128,6 +128,18 @@ def test_save_file_when_it_did_not_exist(run, tmpdir): assert f.read() == 'hello world\n' +def test_saving_file_permission_denied(run, tmpdir): + f = tmpdir.join('f').ensure() + f.chmod(0o400) + + with run(str(f)) as h, and_exit(h): + h.press('hello world') + h.press('^S') + # the filename message is missing as it is too long to be captured + h.await_text('cannot save file: [Errno 13] Permission denied:') + h.await_text(' *') + + def test_save_via_ctrl_o(run, tmpdir): f = tmpdir.join('f') with run(str(f)) as h, and_exit(h):