|
| 1 | +from unittest.mock import Mock, PropertyMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from hapless.cli_utils import get_or_exit |
| 6 | + |
| 7 | + |
| 8 | +@patch("hapless.cli_utils.hapless") |
| 9 | +def test_get_or_exit_non_existing(hapless_mock, capsys): |
| 10 | + with patch.object(hapless_mock, "get_hap", return_value=None) as get_hap_mock: |
| 11 | + with pytest.raises(SystemExit) as e: |
| 12 | + get_or_exit("hap-me") |
| 13 | + |
| 14 | + assert e.value.code == 1 |
| 15 | + get_hap_mock.assert_called_once_with("hap-me") |
| 16 | + |
| 17 | + captured = capsys.readouterr() |
| 18 | + assert "No such hap: hap-me" in captured.out |
| 19 | + |
| 20 | + |
| 21 | +@patch("hapless.cli_utils.hapless") |
| 22 | +def test_get_or_exit_not_accessible(hapless_mock, capsys): |
| 23 | + hap_mock = Mock(owner="someone-else") |
| 24 | + prop_mock = PropertyMock(return_value=False) |
| 25 | + type(hap_mock).accessible = prop_mock |
| 26 | + with patch.object(hapless_mock, "get_hap", return_value=hap_mock) as get_hap_mock: |
| 27 | + with pytest.raises(SystemExit) as e: |
| 28 | + get_or_exit("hap-not-mine") |
| 29 | + |
| 30 | + assert e.value.code == 1 |
| 31 | + get_hap_mock.assert_called_once_with("hap-not-mine") |
| 32 | + prop_mock.assert_called_once_with() |
| 33 | + |
| 34 | + captured = capsys.readouterr() |
| 35 | + assert ( |
| 36 | + "Cannot manage hap launched by another user. Owner: someone-else" |
| 37 | + in captured.out |
| 38 | + ) |
0 commit comments