Completed
Push — develop ( 666233...7113ee )
by Jace
21s queued 11s
created

tests.test_cli   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 245
Duplicated Lines 14.29 %

Importance

Changes 0
Metric Value
wmc 9
eloc 110
dl 35
loc 245
rs 10
c 0
b 0
f 0

8 Functions

Rating   Name   Duplication   Size   Complexity  
A call() 0 3 1
A describe_meta() 0 12 1
A env() 0 8 1
A cli() 0 4 2
A describe_init() 0 7 1
A describe_vendor() 0 18 1
A describe_main() 0 35 1
A describe_quiet() 35 41 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
12
import scripttest
13
from expecter import expect
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 = (
58
    STYLED_OUTPUT.replace('✔', '~')
59
    .replace('⭑', '*')
60
    .replace('⚠', '?')
61
    .replace('✘', 'x')
62
)
63
64
UNSTYLED_OUTPUT_WINDOWS = """
65
Checking for Working Program...
66
67
$ working-program --version
68
sh: command not found: working-program
69
x EXPECTED: 1.2
70
71
Checking for Newer Working Program...
72
73
$ working-program --version
74
sh: command not found: working-program
75
x EXPECTED: 4.1 || 4.2
76
* MESSAGE: Version 4.x is required to get the special features.
77
78
Checking for Broken Program...
79
80
$ broken-program --version
81
sh: command not found: broken-program
82
x EXPECTED: 1.2.3
83
84
Checking for Optional Missing Program...
85
86
$ missing-program --version
87
sh: command not found: missing-program
88
? EXPECTED: 1.2.3
89
90
Checking for Missing Program...
91
92
$ missing-program --version
93
sh: command not found: missing-program
94
x EXPECTED: 1.2.3
95
96
Results: x x x ? x
97
98
"""
99
100
log = logging.getLogger(__name__)
101
102
103
@pytest.fixture
104
def env(tmpdir):
105
    path = str(tmpdir.join("test"))
106
    env = scripttest.TestFileEnvironment(path)
107
    os.chdir(path)
108
    env.environ['PATH'] = BIN_DIR
109
    log.debug("ENV: %s", env.environ)
110
    return env
111
112
113
@pytest.fixture
114
def cli(env):
115
    path = os.path.join(os.path.dirname(sys.executable), "verchew")
116
    return lambda *args: call(env, path, *args)
117
118
119
def call(env, path, *args):
120
    log.info("$ %s %s", path, ' '.join(args))
121
    return env.run(path, *args, expect_error=True)
122
123
124
def describe_meta():
125
    def it_displays_help_information(cli):
126
        cmd = cli('--help')
127
128
        expect(cmd.stdout).contains("usage: verchew")
129
        expect(cmd.returncode) == 0
130
131
    def it_displays_version_information(cli):
132
        cmd = cli('--version')
133
134
        expect(cmd.stdout).startswith("verchew v")
135
        expect(cmd.returncode) == 0
136
137
138
def describe_init():
139
    def it_generates_a_sample_config(cli):
140
        cmd = cli('--init')
141
142
        expect(cmd.stderr) == ""
143
        expect(cmd.stdout).contains("Checking for Make")
144
        expect(cmd.returncode) == 0
145
146
147
def describe_vendor():
148
    @pytest.mark.skipif(sys.platform == 'win32', reason="unix only")
149
    def it_creates_a_new_file_on_unix(cli):
150
        cmd = cli('--vendor', 'bin/verchew')
151
152
        expect(cmd.stderr) == ""
153
        expect(cmd.stdout) == ""
154
        expect(cmd.returncode) == 0
155
        expect(cmd.files_created).contains('bin/verchew')
156
157
    @pytest.mark.skipif(sys.platform != 'win32', reason="windows only")
158
    def it_creates_a_new_file_on_windows(cli):
159
        cmd = cli('--vendor', 'bin\\verchew')
160
161
        expect(cmd.stderr) == ""
162
        expect(cmd.stdout) == ""
163
        expect(cmd.returncode) == 0
164
        expect(cmd.files_created).contains('bin\\verchew')
165
166
167
def describe_main():
168
    @pytest.mark.skipif(
169
        sys.version_info[0] == 2 or sys.platform == 'win32',
170
        reason="unix and python3 only",
171
    )
172
    def it_displays_results_on_unix_python_3(cli):
173
        cmd = cli('--root', EXAMPLES_DIR)
174
175
        expect(cmd.stderr) == ""
176
        expect(cmd.stdout) == STYLED_OUTPUT
177
        expect(cmd.returncode) == 0
178
179
    @pytest.mark.skipif(
180
        sys.version_info[0] == 3 or sys.platform == 'win32',
181
        reason="unix and python2 only",
182
    )
183
    def it_displays_results_on_unix_python_2(cli):
184
        cmd = cli('--root', EXAMPLES_DIR)
185
186
        expect(cmd.stderr) == ""
187
        expect(cmd.stdout) == UNSTYLED_OUTPUT
188
        expect(cmd.returncode) == 0
189
190
    @pytest.mark.skipif(sys.platform != 'win32', reason="windows only")
191
    def it_displays_results_on_windows(cli):
192
        cmd = cli('--root', EXAMPLES_DIR)
193
194
        expect(cmd.stderr) == ""
195
        expect(cmd.stdout) == UNSTYLED_OUTPUT_WINDOWS
196
        expect(cmd.returncode) == 0
197
198
    def it_exits_with_an_error_code_if_enabled(cli):
199
        cmd = cli('--root', EXAMPLES_DIR, '--exit-code')
200
201
        expect(cmd.returncode) == 1
202
203
204
def describe_quiet():
205 View Code Duplication
    @pytest.mark.skipif(sys.platform == 'win32', reason="unix only")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
206
    def it_hides_output_when_no_error(cli, tmp_path):
207
        verchew_ini = tmp_path / 'verchew.ini'
208
        verchew_ini.write_text(
209
            """
210
        [Working Program]
211
212
        cli = working-program
213
        version = 1.2
214
        """
215
        )
216
217
        cmd = cli('--root', str(tmp_path), '--quiet')
218
219
        expect(cmd.stderr) == ""
220
        expect(cmd.stdout) == ""
221
        expect(cmd.returncode) == 0
222
223 View Code Duplication
    @pytest.mark.skipif(sys.platform == 'win32', reason="unix only")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
224
    def it_shows_failing_programs(cli, tmp_path):
225
        verchew_ini = tmp_path / 'verchew.ini'
226
        verchew_ini.write_text(
227
            """
228
        [Working Program]
229
230
        cli = working-program
231
        version = 1.2
232
233
        [Newer Working Program]
234
235
        cli = working-program
236
        version =  1.3
237
        """
238
        )
239
240
        cmd = cli('--root', str(tmp_path), '--quiet')
241
242
        expect(cmd.stderr) == ""
243
        expect(cmd.stdout) == "Unmatched Newer Working Program version: 1.3\n"
244
        expect(cmd.returncode) == 0
245