|
1 | 1 | import pytest |
2 | 2 |
|
3 | 3 | from unittest.mock import Mock, ANY, patch |
| 4 | +from serial import SerialException |
4 | 5 | from nrfcredstore.cli import main |
5 | 6 |
|
6 | 7 | from nrfcredstore.credstore import CredType |
7 | | -from nrfcredstore.exceptions import NoATClientException |
| 8 | +from nrfcredstore.exceptions import NoATClientException, ATCommandError |
8 | 9 |
|
9 | 10 | # pylint: disable=no-self-use |
10 | 11 | class TestCli(): |
@@ -47,7 +48,8 @@ def test_at_client_verify(self, credstore, at_client, offline, empty_cred_list): |
47 | 48 | @patch('builtins.print') |
48 | 49 | def test_at_client_verify_fail(self, mock_print, credstore, at_client): |
49 | 50 | at_client.verify.side_effect = NoATClientException() |
50 | | - main(['fakedev', 'list'], credstore) |
| 51 | + with pytest.raises(SystemExit) as e: |
| 52 | + main(['fakedev', 'list'], credstore) |
51 | 53 | assert 'does not respond to AT commands' in mock_print.call_args[0][0] |
52 | 54 |
|
53 | 55 | def test_at_client_enable_error_codes(self, credstore, at_client, offline, empty_cred_list): |
@@ -103,3 +105,38 @@ def test_generate_file(self, mock_file, credstore, offline): |
103 | 105 | credstore.keygen.return_value = True |
104 | 106 | main(['fakedev', 'generate', '123', 'foo.der'], credstore) |
105 | 107 | mock_file.assert_called_with('foo.der', 'wb', ANY, ANY, ANY) |
| 108 | + |
| 109 | + def test_no_at_client_exit_code(self, credstore, at_client): |
| 110 | + at_client.verify.side_effect = NoATClientException() |
| 111 | + with pytest.raises(SystemExit) as e: |
| 112 | + main(['fakedev', 'list'], credstore) |
| 113 | + assert e.type == SystemExit |
| 114 | + assert e.value.code == 10 |
| 115 | + |
| 116 | + def test_at_command_error_exit_code(self, credstore, at_client): |
| 117 | + at_client.verify.side_effect = ATCommandError() |
| 118 | + with pytest.raises(SystemExit) as e: |
| 119 | + main(['fakedev', 'list'], credstore) |
| 120 | + assert e.type == SystemExit |
| 121 | + assert e.value.code == 11 |
| 122 | + |
| 123 | + def test_timeout_error_exit_code(self, credstore, at_client): |
| 124 | + at_client.verify.side_effect = TimeoutError() |
| 125 | + with pytest.raises(SystemExit) as e: |
| 126 | + main(['fakedev', 'list'], credstore) |
| 127 | + assert e.type == SystemExit |
| 128 | + assert e.value.code == 12 |
| 129 | + |
| 130 | + def test_serial_exception_exit_code(self, credstore, at_client): |
| 131 | + at_client.connect.side_effect = SerialException() |
| 132 | + with pytest.raises(SystemExit) as e: |
| 133 | + main(['fakedev', 'list'], credstore) |
| 134 | + assert e.type == SystemExit |
| 135 | + assert e.value.code == 13 |
| 136 | + |
| 137 | + def test_unhandled_exception_exit_code(self, credstore, at_client): |
| 138 | + at_client.verify.side_effect = Exception() |
| 139 | + with pytest.raises(SystemExit) as e: |
| 140 | + main(['fakedev', 'list'], credstore) |
| 141 | + assert e.type == SystemExit |
| 142 | + assert e.value.code == 1 |
0 commit comments