Passed
Push — develop ( 6195a6...399285 )
by Jace
01:47
created

tests.test_cli.describe_cli()   B

Complexity

Conditions 1

Size

Total Lines 51
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 37
nop 0
dl 0
loc 51
rs 8.9919
c 0
b 0
f 0

How to fix   Long Method   

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:

1
# -*- coding: utf-8 -*-
2
3
# pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned
4
5
from __future__ import unicode_literals
6
7
import logging
8
import os
9
import sys
10
11
import pytest
0 ignored issues
show
introduced by
Unable to import 'pytest'
Loading history...
12
import scripttest
0 ignored issues
show
introduced by
Unable to import 'scripttest'
Loading history...
13
from expecter import expect
0 ignored issues
show
introduced by
Unable to import 'expecter'
Loading history...
14
15
16
TESTS_DIR = os.path.abspath(os.path.dirname(__file__))
17
ROOT_DIR = os.path.join(TESTS_DIR, "..")
18
EXAMPLES_DIR = os.path.join(ROOT_DIR, "examples")
19
BIN_DIR = os.path.join(EXAMPLES_DIR, "bin")
20
21
STYLED_OUTPUT = """
22
Checking for Working Program...
23
24
$ working-program --version
25
1.2.3
26
✔ MATCHED: 1.2.
27
28
Checking for Newer Working Program...
29
30
$ working-program --version
31
1.2.3
32
✘ EXPECTED: 4.1. | 4.2.
33
⭑ MESSAGE: Version 4.x is required to get the special features.
34
35
Checking for Broken Program...
36
37
$ broken-program --version
38
An error occurred.
39
✘ EXPECTED: 1.2.3
40
41
Checking for Optional Missing Program...
42
43
$ missing-program --version
44
sh: command not found: missing-program
45
⚠ EXPECTED: 1.2.3
46
47
Checking for Missing Program...
48
49
$ missing-program --version
50
sh: command not found: missing-program
51
✘ EXPECTED: 1.2.3
52
53
Results: ✔ ✘ ✘ ⚠ ✘
54
55
"""
56
57
UNSTYLED_OUTPUT = STYLED_OUTPUT \
58
    .replace('✔', '~').replace('⭑', '*').replace('⚠', '?').replace('✘', 'x')
59
60
UNSTYLED_OUTPUT_WINDOWS = """
61
Checking for Working Program...
62
63
$ working-program --version
64
sh: command not found: working-program
65
x EXPECTED: 1.2.
66
67
Checking for Newer Working Program...
68
69
$ working-program --version
70
sh: command not found: working-program
71
x EXPECTED: 4.1. | 4.2.
72
* MESSAGE: Version 4.x is required to get the special features.
73
74
Checking for Broken Program...
75
76
$ broken-program --version
77
sh: command not found: broken-program
78
x EXPECTED: 1.2.3
79
80
Checking for Optional Missing Program...
81
82
$ missing-program --version
83
sh: command not found: missing-program
84
? EXPECTED: 1.2.3
85
86
Checking for Missing Program...
87
88
$ missing-program --version
89
sh: command not found: missing-program
90
x EXPECTED: 1.2.3
91
92
Results: x x x ? x
93
94
"""
95
96
log = logging.getLogger(__name__)
97
98
99
@pytest.fixture
100
def env(tmpdir):
101
    path = str(tmpdir.join("test"))
102
    env = scripttest.TestFileEnvironment(path)
103
    os.chdir(path)
104
    env.environ['PATH'] = BIN_DIR
105
    log.debug("ENV: %s", env.environ)
106
    return env
107
108
109
@pytest.fixture
110
def cli(env):
111
    path = os.path.join(os.path.dirname(sys.executable), "verchew")
112
    return lambda *args: call(env, path, *args)
113
114
115
def call(env, path, *args):
116
    log.info("$ %s %s", path, ' '.join(args))
117
    return env.run(path, *args, expect_error=True)
118
119
120
def describe_cli():
121
122
    def it_displays_help_information(cli):
123
        cmd = cli('--help')
124
125
        expect(cmd.returncode) == 0
126
        expect(cmd.stdout).contains("usage: verchew")
127
128
    def it_displays_version_information(cli):
129
        cmd = cli('--version')
130
131
        expect(cmd.returncode) == 0
132
        expect(cmd.stdout or cmd.stderr).contains("verchew v1.")
133
134
    def it_generates_a_sample_config(cli):
135
        cmd = cli('--init')
136
137
        expect(cmd.returncode) == 0
138
        expect(cmd.stderr) == ""
139
        expect(cmd.stdout).contains("Checking for Make")
140
141
    @pytest.mark.skipif(sys.platform == 'win32', reason="unix only")
142
    @pytest.mark.skipif(sys.version_info[0] == 2, reason="python3 only")
143
    def it_displays_results_on_unix_python_3(cli):
144
        cmd = cli('--root', EXAMPLES_DIR)
145
146
        expect(cmd.returncode) == 0
147
        expect(cmd.stderr) == ""
148
        expect(cmd.stdout) == STYLED_OUTPUT
149
150
    @pytest.mark.skipif(sys.platform == 'win32', reason="unix only")
151
    @pytest.mark.skipif(sys.version_info[0] == 3, reason="python2 only")
152
    def it_displays_results_on_unix_python_2(cli):
153
        cmd = cli('--root', EXAMPLES_DIR)
154
155
        expect(cmd.returncode) == 0
156
        expect(cmd.stderr) == ""
157
        expect(cmd.stdout) == UNSTYLED_OUTPUT
158
159
    @pytest.mark.skipif(sys.platform != 'win32', reason="windows only")
160
    def it_displays_results_on_windows(cli):
161
        cmd = cli('--root', EXAMPLES_DIR)
162
163
        expect(cmd.returncode) == 0
164
        expect(cmd.stderr) == ""
165
        expect(cmd.stdout) == UNSTYLED_OUTPUT_WINDOWS
166
167
    def it_exits_with_an_error_code_if_enabled(cli):
168
        cmd = cli('--root', EXAMPLES_DIR, '--exit-code')
169
170
        expect(cmd.returncode) == 1
171