Completed
Pull Request — master (#58)
by Jace
02:38
created

TestMain   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 55
rs 10
wmc 16

9 Methods

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