1
|
|
|
# pylint: disable=misplaced-comparison-constant,no-self-use |
2
|
|
|
|
3
|
|
|
import os |
4
|
|
|
from unittest.mock import Mock, patch |
5
|
|
|
import logging |
6
|
|
|
|
7
|
|
|
import pytest |
8
|
|
|
|
9
|
|
|
from mine import cli |
10
|
|
|
from mine import common |
11
|
|
|
from mine.models import Application |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class TestMain: |
15
|
|
|
"""Unit tests for the `main` function.""" |
16
|
|
|
|
17
|
|
|
@patch('mine.cli.run', Mock(return_value=True)) |
18
|
|
|
def test_run(self): |
19
|
|
|
"""Verify the CLI can be run.""" |
20
|
|
|
cli.main([]) |
21
|
|
|
|
22
|
|
|
@patch('mine.cli.run', Mock(return_value=False)) |
23
|
|
|
def test_run_fail(self): |
24
|
|
|
"""Verify the CLI can detect errors.""" |
25
|
|
|
with pytest.raises(SystemExit): |
26
|
|
|
cli.main([]) |
27
|
|
|
|
28
|
|
|
def test_help(self): |
29
|
|
|
"""Verify the CLI help text can be displayed.""" |
30
|
|
|
with pytest.raises(SystemExit): |
31
|
|
|
cli.main(['--help']) |
32
|
|
|
|
33
|
|
|
@patch('mine.cli.run', Mock(side_effect=KeyboardInterrupt)) |
34
|
|
|
def test_interrupt(self): |
35
|
|
|
"""Verify the CLI can be interrupted.""" |
36
|
|
|
with pytest.raises(SystemExit): |
37
|
|
|
cli.main([]) |
38
|
|
|
|
39
|
|
|
@patch('mine.cli.log') |
40
|
|
|
@patch('mine.cli.run', Mock(side_effect=KeyboardInterrupt)) |
41
|
|
|
def test_interrupt_verbose(self, mock_log): |
42
|
|
|
"""Verify the CLI can be interrupted (verbose output).""" |
43
|
|
|
with pytest.raises(SystemExit): |
44
|
|
|
cli.main(['-vvvv']) |
45
|
|
|
assert mock_log.exception.call_count == 1 |
46
|
|
|
|
47
|
|
|
@patch('mine.cli.daemon', None) |
48
|
|
|
def test_path(self, path): |
49
|
|
|
"""Verify a custom setting file path can be used.""" |
50
|
|
|
cli.main(['--file', path]) |
51
|
|
|
|
52
|
|
|
assert os.path.isfile(path) |
53
|
|
|
|
54
|
|
|
@patch('mine.cli.run') |
55
|
|
|
def test_daemon(self, mock_run): |
56
|
|
|
cli.main(['--daemon']) |
57
|
|
|
mock_run.assert_called_once_with(path=None, delay=300) |
58
|
|
|
|
59
|
|
|
@patch('mine.cli.run') |
60
|
|
|
def test_daemon_with_specific_delay(self, mock_run): |
61
|
|
|
cli.main(['--daemon', '42']) |
62
|
|
|
mock_run.assert_called_once_with(path=None, delay=42) |
63
|
|
|
|
64
|
|
|
@patch('mine.cli.daemon', Application(None)) |
65
|
|
|
def test_warning_when_daemon_is_not_running(self, path): |
66
|
|
|
with pytest.raises(SystemExit): |
67
|
|
|
cli.main(['--file', path]) |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
class TestSwitch: |
71
|
|
|
"""Unit tests for the `switch` function.""" |
72
|
|
|
|
73
|
|
|
@patch('mine.cli.run') |
74
|
|
|
def test_switch(self, mock_run): |
75
|
|
|
"""Verify a the current computer can be queued.""" |
76
|
|
|
cli.main(['switch']) |
77
|
|
|
mock_run.assert_called_once_with(path=None, delay=None, |
78
|
|
|
switch=True) |
79
|
|
|
|
80
|
|
|
@patch('mine.cli.run') |
81
|
|
|
def test_switch_specific(self, mock_run): |
82
|
|
|
"""Verify a specific computer can be queued.""" |
83
|
|
|
cli.main(['switch', 'foobar']) |
84
|
|
|
mock_run.assert_called_once_with(path=None, delay=None, |
85
|
|
|
switch='foobar') |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
class TestClean: |
89
|
|
|
|
90
|
|
|
@patch('mine.cli.run') |
91
|
|
|
def test_clean(self, mock_run): |
92
|
|
|
cli.main(['clean']) |
93
|
|
|
mock_run.assert_called_once_with(path=None, delay=None, |
94
|
|
|
delete=True, force=False) |
95
|
|
|
|
96
|
|
|
@patch('mine.cli.run') |
97
|
|
|
def test_clean_with_force(self, mock_run): |
98
|
|
|
cli.main(['clean', '--force']) |
99
|
|
|
mock_run.assert_called_once_with(path=None, delay=None, |
100
|
|
|
delete=True, force=True) |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
class TestEdit: |
104
|
|
|
|
105
|
|
|
@patch('mine.cli.run') |
106
|
|
|
def test_edit(self, mock_run): |
107
|
|
|
cli.main(['edit']) |
108
|
|
|
mock_run.assert_called_once_with(path=None, delay=None, |
109
|
|
|
edit=True,) |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
def _mock_run(*args, **kwargs): |
113
|
|
|
"""Placeholder logic for logging tests.""" |
114
|
|
|
logging.debug(args) |
115
|
|
|
logging.debug(kwargs) |
116
|
|
|
logging.warning("warning") |
117
|
|
|
logging.error("error") |
118
|
|
|
return True |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
@patch('mine.cli.run', _mock_run) |
122
|
|
|
class TestLogging: |
123
|
|
|
"""Integration tests for the Doorstop CLI logging.""" |
124
|
|
|
|
125
|
|
|
arg_verbosity = [ |
126
|
|
|
('', 0), |
127
|
|
|
('-v', 1), |
128
|
|
|
('-vv', 2), |
129
|
|
|
('-vvv', 3), |
130
|
|
|
('-vvvv', 4), |
131
|
|
|
('-vvvvv', 4), |
132
|
|
|
('-q', -1), |
133
|
|
|
] |
134
|
|
|
|
135
|
|
|
@pytest.mark.parametrize("arg,verbosity", arg_verbosity) |
136
|
|
|
def test_verbose(self, arg, verbosity): |
137
|
|
|
"""Verify verbose level can be set.""" |
138
|
|
|
cli.main([arg] if arg else []) |
139
|
|
|
assert verbosity == common.verbosity |
140
|
|
|
|