Skip to content

Commit

Permalink
fix: handle process interruption gracefully (#73)
Browse files Browse the repository at this point in the history
* wip

* chore: capture interrupt

* chore: formatting

* chore: use utils

* test: handle process interrupt
  • Loading branch information
iamogbz committed Apr 7, 2020
1 parent 2521ce0 commit 3237c96
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/nvshim/core/shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
import sys

import nvshim.core.__main__ as core
from nvshim.utils.constants import ErrorCode
from nvshim.utils.message import print_process_interrupted


def main():
sys.argv.insert(1, os.path.basename(sys.argv[0]))
core.main()
try:
sys.argv.insert(1, os.path.basename(sys.argv[0]))
core.main()
except KeyboardInterrupt as e:
print_process_interrupted(e)
sys.exit(ErrorCode.KEYBOARD_INTERRUPT)


if __name__ == "__main__":
Expand Down
3 changes: 3 additions & 0 deletions src/nvshim/core/tests/__snapshots__/test_shim.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# name: test_shim_handles_process_interrupt
'Interrupted. Ctrl+C'
---
18 changes: 17 additions & 1 deletion src/nvshim/core/tests/test_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import pytest

from nvshim.core.shim import main
from nvshim.utils.constants import ErrorCode
from nvshim.utils.process import clean_output


@pytest.fixture
Expand All @@ -15,6 +17,20 @@ def test_shim_args():


def test_shim_executes_with_args(mocker, test_shim_args):
mocked_core_main = mocker.patch("nvshim.core.shim.core.main")
mocked_core_main = mocker.patch("nvshim.core.shim.core.main", autospec=True)
main()
mocked_core_main.assert_called_once_with()


def test_shim_handles_process_interrupt(mocker, capsys, test_shim_args, snapshot):
mocked_core_main = mocker.patch(
"nvshim.core.shim.core.main",
autospec=True,
side_effect=KeyboardInterrupt("Ctrl+C"),
)
mocked_sys_exit = mocker.patch("sys.exit")
main()
mocked_core_main.assert_called_once_with()
mocked_sys_exit.assert_called_once_with(ErrorCode.KEYBOARD_INTERRUPT)
captured = capsys.readouterr()
snapshot.assert_match(clean_output(captured.out))
1 change: 1 addition & 0 deletions src/nvshim/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class ErrorCode(IntEnum):
ENV_NVM_DIR_MISSING = 1003
EXECUTABLE_NOT_FOUND = 1002
KEYBOARD_INTERRUPT = 130
VERSION_NOT_INSTALLED = 1001


Expand Down
4 changes: 4 additions & 0 deletions src/nvshim/utils/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ def print_running_version(version_number: str):

def print_unable_to_get_stable_version():
_print_error("Unable to retrieve stable version from nvm")


def print_process_interrupted(exc: KeyboardInterrupt):
_print(f"\nInterrupted. {exc}")

0 comments on commit 3237c96

Please sign in to comment.