Move fzf error handling into its own class and add appropriate tests
This commit is contained in:
parent
037f6dd119
commit
bdc3ecae3b
@ -72,7 +72,7 @@ if __name__ == "__main__":
|
|||||||
exit(error.E_INTERRUPT)
|
exit(error.E_INTERRUPT)
|
||||||
except fzf.FZFError as f:
|
except fzf.FZFError as f:
|
||||||
print(f)
|
print(f)
|
||||||
exit(error.E_INTERRUPT)
|
exit(f.exit_code)
|
||||||
|
|
||||||
dest_file = Path(args.dest)
|
dest_file = Path(args.dest)
|
||||||
|
|
||||||
|
@ -97,8 +97,9 @@ if __name__ == "__main__":
|
|||||||
selected_file = fzf.select_file_with_fzf(files)
|
selected_file = fzf.select_file_with_fzf(files)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
exit(error.E_INTERRUPT)
|
exit(error.E_INTERRUPT)
|
||||||
except fzf.FZFError:
|
except fzf.FZFError as e:
|
||||||
exit(error.E_NOFILESELECTED)
|
print(e)
|
||||||
|
exit(e.exit_code)
|
||||||
|
|
||||||
if selected_file != "":
|
if selected_file != "":
|
||||||
cmd = editor.gen_editor_cmd(selected_editor, selected_file)
|
cmd = editor.gen_editor_cmd(selected_editor, selected_file)
|
||||||
|
@ -22,7 +22,28 @@ LOCALE = "utf-8"
|
|||||||
|
|
||||||
|
|
||||||
class FZFError(Exception):
|
class FZFError(Exception):
|
||||||
pass
|
"""Custom error class for any errors that occur when fzf is run.
|
||||||
|
|
||||||
|
**Attributes**
|
||||||
|
|
||||||
|
* exit_code: exit code when fzf exited
|
||||||
|
* message: stderr of fzf's process when the error occured
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, exit_code, message):
|
||||||
|
"""
|
||||||
|
:type exit_code: int
|
||||||
|
:type message: str
|
||||||
|
"""
|
||||||
|
self.exit_code = exit_code
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"FZFError({self.exit_code}, {self.message})"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.message
|
||||||
|
|
||||||
|
|
||||||
# ========== Functions ==========
|
# ========== Functions ==========
|
||||||
@ -40,6 +61,6 @@ def select_file_with_fzf(files):
|
|||||||
try:
|
try:
|
||||||
output.check_returncode()
|
output.check_returncode()
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
raise FZFError(error.NO_FILE_SELECTED_MESSAGE) from e
|
raise FZFError(e.returncode, e.stderr) from e
|
||||||
else:
|
else:
|
||||||
return Path(output.stdout.decode(LOCALE).strip("\x00"))
|
return Path(output.stdout.decode(LOCALE).strip("\x00"))
|
||||||
|
@ -25,3 +25,21 @@ class TestRunFZF(unittest.TestCase):
|
|||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
patch.stopall()
|
patch.stopall()
|
||||||
|
|
||||||
|
|
||||||
|
class TestFZFErrors(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.patched_subprocess = patch(f"{TESTING_MODULE}.subprocess.run")
|
||||||
|
|
||||||
|
self.mocked_run = self.patched_subprocess.start()
|
||||||
|
|
||||||
|
def test_raises_error(self):
|
||||||
|
self.mocked_run.return_value.check_returncode.side_effect = (
|
||||||
|
subprocess.CalledProcessError(1, "hi")
|
||||||
|
)
|
||||||
|
|
||||||
|
with self.assertRaises(fzf.FZFError):
|
||||||
|
fzf.select_file_with_fzf(b"test")
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
patch.stopall()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user