Passed
Push — develop ( e76d2d...088138 )
by Jace
01:20
created

describe_cli()   F

Complexity

Conditions 13

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
cc 13
c 7
b 0
f 0
dl 0
loc 74
rs 2.2665

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like describe_cli() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned
2
3
from __future__ import unicode_literals
4
5
import os
6
import sys
7
8
import pytest
9
import scripttest
10
import six
11
from expecter import expect
12
13
14
SLUG = "jacebrowning/coverage-space-cli-demo"
15
16
17
@pytest.fixture
18
def env(tmpdir):
19
    path = str(tmpdir.join('test'))
20
    env = scripttest.TestFileEnvironment(path)
21
    env.environ.pop('APPVEYOR', None)
22
    env.environ.pop('CI', None)
23
    env.environ.pop('CONTINUOUS_INTEGRATION', None)
24
    env.environ.pop('DISABLE_COVERAGE', None)
25
    env.environ.pop('TRAVIS', None)
26
    return env
27
28
29
def cli(env, *args):
30
    prog = os.path.join(os.path.dirname(sys.executable), 'coveragespace')
31
    cmd = env.run(prog, *args, expect_error=True)
32
    six.print_(cmd)
33
    return cmd
34
35
36
def describe_cli():
37
38
    def it_fails_when_missing_arguments(env):
39
        cmd = cli(env)
40
41
        expect(cmd.returncode) == 1
42
        expect(cmd.stderr).contains("Usage:")
43
44
    def describe_update():
45
46
        @pytest.fixture
47
        def slug():
48
            return SLUG + "/update"
49
50
        def it_can_update_metrics(env, slug):
51
            cmd = cli(env, slug, 'unit', '100')
52
53
            expect(cmd.returncode) == 0
54
            expect(cmd.stderr) == ""
55
            expect(cmd.stdout) == ""
56
57
        def it_indicates_when_metrics_decrease(env, slug):
58
            cmd = cli(env, slug, 'unit', '0')
59
60
            expect(cmd.returncode) == 0
61
            expect(cmd.stderr) == ""
62
            expect(cmd.stdout).contains("coverage decreased")
63
            expect(cmd.stdout).contains(
64
                "To reset metrics, run: coveragespace " + slug + " --reset"
65
            )
66
67
        def it_fails_when_metrics_decrease_if_requested(env, slug):
68
            cmd = cli(env, slug, 'unit', '0', '--exit-code')
69
70
            expect(cmd.returncode) == 1
71
            expect(cmd.stderr) == ""
72
            expect(cmd.stdout).contains("coverage decreased")
73
74
        def it_always_display_metrics_when_verbose(env, slug):
75
            cmd = cli(env, slug, 'unit', '100', '--verbose')
76
77
            expect(cmd.returncode) == 0
78
            expect(cmd.stderr) != ""  # expect lots of logging
79
            expect(cmd.stdout).contains("coverage increased")
80
81
        def it_skips_when_running_on_ci(env, slug):
82
            env.environ['CI'] = 'true'
83
84
            cmd = cli(env, slug, 'unit', '0', '--exit-code', '--verbose')
85
86
            expect(cmd.returncode) == 0
87
            expect(cmd.stderr).contains("Coverage check skipped")
88
            expect(cmd.stdout) == ""
89
90
        def it_fails_on_slugs_missing_a_slash(env):
91
            cmd = cli(env, 'foobar', 'unit', '100')
92
93
            expect(cmd.returncode) == 1
94
            expect(cmd.stderr).contains(
95
                "<owner/repo> slug must contain a slash")
96
            expect(cmd.stdout) == ""
97
98
    def describe_reset():
99
100
        @pytest.fixture
101
        def slug():
102
            return SLUG + "/reset"
103
104
        def it_can_reset_metrics(env, slug):
105
            cmd = cli(env, slug, '--reset')
106
107
            expect(cmd.returncode) == 0
108
            expect(cmd.stderr) == ""
109
            expect(cmd.stdout).contains("coverage reset")
110