Passed
Pull Request — master (#21)
by
unknown
01:34
created

test_command_flag_on_off()   B

Complexity

Conditions 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
c 1
b 0
f 1
dl 0
loc 19
rs 8.5454
1
"""
2
For ideas how to test commands based on clicks, see http://click.pocoo.org/5/testing/#basic-testing
3
"""
4
import click
0 ignored issues
show
introduced by
Unable to import 'click'
Loading history...
5
import pytest
0 ignored issues
show
introduced by
Unable to import 'pytest'
Loading history...
6
from click import Argument, Option
0 ignored issues
show
introduced by
Unable to import 'click'
Loading history...
introduced by
Imports from package click are not grouped
Loading history...
7
from click.testing import CliRunner
0 ignored issues
show
introduced by
Unable to import 'click.testing'
Loading history...
8
9
from groundwork.patterns.gw_commands_pattern import CommandExistException
10
11
12
def test_command_plugin_activation(basicApp):
0 ignored issues
show
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
13
    plugin = basicApp.plugins.get("CommandPlugin")
14
    assert plugin is not None
15
    assert plugin.active is True
16
17
18
# def test_command_cli_activation(basicApp):
19
#     test = basicApp.commands.start_cli(standalone_mode=True, args=[])
20
#     print(test)
21
22
23
def test_command_plugin_execution(basicApp):
0 ignored issues
show
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
24
    runner = CliRunner()
25
    result = runner.invoke(basicApp.commands._commands["test"].click_command, ["--invalid"])
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _commands was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
26
    assert "no such option" in result.output
27
    assert result.exit_code == 2
28
29
    result = runner.invoke(basicApp.commands._commands["test"].click_command, ["--arg", "12345"])
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _commands was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
30
    assert "12345" in result.output
31
    assert result.exit_code == 0
32
33
    command = basicApp.commands.get("test")
34
    assert command is not None
35
36
37
def test_command_multi_registration(basicApp):
0 ignored issues
show
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
38
    def _test_command(arg):
39
        print(arg)
40
41
    plugin = basicApp.plugins.get("CommandPlugin")
42
    with pytest.raises(CommandExistException):
43
        plugin.commands.register("test", "my test command", _test_command, params=[Option(("--arg", "-a"))])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (108/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
44
45
    plugin.commands.unregister("test")
46
    plugin.commands.register("test", "my test command", _test_command, params=[Option(("--arg", "-a"))])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (104/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
47
    assert len(basicApp.commands.get()) == 1
48
49
    plugin.commands.register("test2", "my test2 command", _test_command, params=[Option(("--arg", "-a"))])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (106/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
50
    assert len(basicApp.commands.get()) == 2
51
52
    basicApp.plugins.deactivate(["CommandPlugin"])
53
    print(basicApp.commands.get().keys())
54
    assert len(basicApp.commands.get().keys()) == 0
0 ignored issues
show
Unused Code introduced by
Do not use len(SEQUENCE) as condition value
Loading history...
55
56
57
def test_command_multi_plugin_registration(basicApp, EmptyCommandPlugin):
0 ignored issues
show
Coding Style Naming introduced by
The name test_command_multi_plugin_registration does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name EmptyCommandPlugin does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
58
    def _test_command(arg):
59
        print(arg)
60
61
    plugin = basicApp.plugins.get("CommandPlugin")
62
    plugin2 = EmptyCommandPlugin(app=basicApp, name="CommandPlugin2")
63
    plugin2.activate()
64
    plugin2.commands.register("test2", "my test2 command", _test_command, params=[Option(("--arg", "-a"))])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (107/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
65
    assert len(basicApp.commands.get()) == 2
66
    assert len(plugin.commands.get()) == 1
67
    assert len(plugin2.commands.get()) == 1
68
69
    basicApp.plugins.deactivate(["CommandPlugin2"])
70
    assert len(basicApp.commands.get()) == 1
71
    assert len(plugin.commands.get()) == 1
72
    assert len(plugin2.commands.get()) == 0
0 ignored issues
show
Unused Code introduced by
Do not use len(SEQUENCE) as condition value
Loading history...
73
74
75 View Code Duplication
def test_command_mandatory_argument(basicApp):
0 ignored issues
show
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
76
    def _test_command(*args, **kwargs):
77
        print(args)
78
        print(kwargs)
79
80
    plugin = basicApp.plugins.get("CommandPlugin")
81
    plugin.commands.unregister("test")
82
    # register a command with a mandatory argument
83
    plugin.commands.register("test", "my test command", _test_command, params=[Argument(("man_arg",), required=True)])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (118/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
84
    # call command with argument -> expect ok
85
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, ["arg"])
86
    assert result.exit_code == 0
87
88
    # call command with no argument -> expect error
89
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, [])
90
    assert result.exit_code == 2
91
92
93 View Code Duplication
def test_command_optional_argument(basicApp):
0 ignored issues
show
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
94
    def _test_command(*args, **kwargs):
95
        print(args)
96
        print(kwargs)
97
98
    plugin = basicApp.plugins.get("CommandPlugin")
99
    plugin.commands.unregister("test")
100
    # register a command with an optional argument
101
    plugin.commands.register("test", "my test command", _test_command, params=[Argument(("opt_arg",), required=False)])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (119/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
102
    # call command with argument -> expect ok
103
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, ["arg"])
104
    assert result.exit_code == 0
105
106
    # call command with no argument -> expect ok
107
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, [])
108
    assert result.exit_code == 0
109
110
111 View Code Duplication
def test_command_mandatory_option(basicApp):
0 ignored issues
show
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
112
    def _test_command(*args, **kwargs):
113
        print(args)
114
        print(kwargs)
115
116
    plugin = basicApp.plugins.get("CommandPlugin")
117
    plugin.commands.unregister("test")
118
    # register a command with a mandatory option
119
    plugin.commands.register("test", "my test command", _test_command, params=[Option(["--man-opt"], required=True)])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (117/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
120
    # call command with option --man-opt -> expect ok
121
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, ["--man-opt", 123])
122
    assert result.exit_code == 0
123
124
    # call command with no option -> expect error
125
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, [])
126
    assert result.exit_code == 2
127
128
129 View Code Duplication
def test_command_optional_option(basicApp):
0 ignored issues
show
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
130
    def _test_command(*args, **kwargs):
131
        print(args)
132
        print(kwargs)
133
134
    plugin = basicApp.plugins.get("CommandPlugin")
135
    plugin.commands.unregister("test")
136
    # register a command with a mandatory option
137
    plugin.commands.register("test", "my test command", _test_command, params=[Option(["--opt-opt"], required=False)])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (118/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
138
    # call command with option --opt-opt -> expect ok
139
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, ["--opt-opt", 123])
140
    assert result.exit_code == 0
141
142
    # call command with no option -> expect ok
143
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, [])
144
    assert result.exit_code == 0
145
146
147 View Code Duplication
def test_command_path_argument(basicApp):
0 ignored issues
show
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
148
    def _test_command(*args, **kwargs):
149
        print(args)
150
        print(kwargs)
151
152
    plugin = basicApp.plugins.get("CommandPlugin")
153
    plugin.commands.unregister("test")
154
    # register a command with a path argument, that must exist
155
    plugin.commands.register("test", "my test command", _test_command, params=[Argument(("man_arg",),
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
156
                                                                                        type=click.Path(exists=True))])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (119/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
157
    # call command with existing path as argument -> expect ok
158
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, [__file__])
159
    assert result.exit_code == 0
160
161
    # call command with non-existing path as argument -> expect error
162
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, ['no such path'])
163
    assert result.exit_code == 2
164
165
166 View Code Duplication
def test_command_path_option(basicApp):
0 ignored issues
show
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
167
    def _test_command(*args, **kwargs):
168
        print(args)
169
        print(kwargs)
170
171
    plugin = basicApp.plugins.get("CommandPlugin")
172
    plugin.commands.unregister("test")
173
    # register a command with a path option, that must exist
174
    plugin.commands.register("test", "my test command", _test_command, params=[Option(["--path"],
175
                                                                                      type=click.Path(exists=True))])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (117/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
176
    # call command with existing path as option -> expect ok
177
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, ['--path', __file__])
178
    assert result.exit_code == 0
179
180
    # call command with non-existing path as option -> expect error
181
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, ['--path', 'no such path'])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (104/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
182
    assert result.exit_code == 2
183
184
185
def test_command_flag_on_off(basicApp):
0 ignored issues
show
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
186
    def _test_command(*args, **kwargs):
187
        print(args)
188
        print(kwargs)
189
        if not kwargs['flag_on']:
190
            raise Exception
191
192
    plugin = basicApp.plugins.get("CommandPlugin")
193
    plugin.commands.unregister("test")
194
    # register a command with an on/off flag; command throws exception if flag is off
195
    plugin.commands.register("test", "my test command", _test_command, params=[Option(["--flag-on/--flag-off"])])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (113/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
196
197
    # call command with --flag-on (True) -> expect ok
198
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, ['--flag-on'])
199
    assert result.exit_code == 0
200
201
    # call command with --flag-off (False)  -> expect error
202
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, ['--flag-off'])
203
    assert result.exit_code == -1
204
205
206
def test_command_flag_count(basicApp):
0 ignored issues
show
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
207
    def _test_command(*args, **kwargs):
208
        print(args)
209
        print(kwargs)
210
        assert kwargs['flag'] == 3
211
212
    plugin = basicApp.plugins.get("CommandPlugin")
213
    plugin.commands.unregister("test")
214
    # register a command with a countable flag
215
    plugin.commands.register("test", "my test command", _test_command, params=[Option(["-f", "--flag"], count=True)])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (117/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
216
217
    # call command with --fff -> count == 3 is asserted in the command handler
218
    result = CliRunner().invoke(basicApp.commands.get("test").click_command, ['-fff'])
219
    assert result.exit_code == 0
220