Passed
Push — master ( e8584f...303446 )
by Emmanuel
04:55
created

command_test   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 184
Duplicated Lines 81.52 %

Importance

Changes 0
Metric Value
eloc 141
dl 150
loc 184
rs 9.44
c 0
b 0
f 0
wmc 37

8 Methods

Rating   Name   Duplication   Size   Complexity  
A CommandTest.test_command_without_stderr_and_stdout_err() 21 21 5
A CommandTest.test_command_without_stdout_ok() 21 21 5
A CommandTest.test_command_with_stdout_ok() 21 21 5
A CommandTest.test_command_with_stderr_no_stdout_err() 22 22 5
A CommandTest.test_command_without_stderr_but_stdout_err() 21 21 5
A CommandTest.test_command_exception() 0 3 2
A CommandTest.test_command_with_stderr_no_stdout_ok() 21 21 5
B CommandTest.test_command_with_stderr_no_stdout_err_loop() 23 23 5

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
import io
2
import os
3
import re
4
import sys
5
import unittest
6
7
from contextlib import redirect_stdout
8
from stakkr.command import launch_cmd_displays_output
9
10
base_dir = os.path.abspath(os.path.dirname(__file__))
11
sys.path.insert(0, base_dir + '/../')
12
13
14
# https://docs.python.org/3/library/unittest.html#assert-methods
15
class CommandTest(unittest.TestCase):
16
    cmd_ok = ['echo', 'coucou']
17
    cmd_nook = ['cat', '/does/not/exist']
18
    cmd_err = ['echoo']
19
20 View Code Duplication
    def test_command_without_stdout_ok(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
21
        # TODO make it work under windows
22
        if os.name == 'nt':
23
            return
24
25
        f = io.StringIO()
26
        with redirect_stdout(f):
27
            launch_cmd_displays_output(self.cmd_ok, False, False)
28
        res = f.getvalue()
29
        self.assertEqual('.', res[:1])
30
31
        try:
32
            from contextlib import redirect_stderr
33
        except Exception:
34
            return
35
36
        f = io.StringIO()
37
        with redirect_stderr(f):
38
            launch_cmd_displays_output(self.cmd_ok, False, False)
39
        res = f.getvalue()
40
        self.assertEqual('', res)
41
42 View Code Duplication
    def test_command_with_stdout_ok(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
43
        # TODO make it work under windows
44
        if os.name == 'nt':
45
            return
46
47
        f = io.StringIO()
48
        with redirect_stdout(f):
49
            launch_cmd_displays_output(self.cmd_ok, True, False)
50
        res = f.getvalue()
51
        self.assertEqual('coucou\n\n', res)
52
53
        try:
54
            from contextlib import redirect_stderr
55
        except Exception:
56
            return
57
58
        f = io.StringIO()
59
        with redirect_stderr(f):
60
            launch_cmd_displays_output(self.cmd_ok, True, False)
61
        res = f.getvalue()
62
        self.assertEqual('', res)
63
64 View Code Duplication
    def test_command_with_stderr_no_stdout_ok(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
65
        # TODO make it work under windows
66
        if os.name == 'nt':
67
            return
68
69
        f = io.StringIO()
70
        with redirect_stdout(f):
71
            launch_cmd_displays_output(self.cmd_ok, False, True)
72
        res = f.getvalue()
73
        self.assertEqual('.', res[:1])
74
75
        try:
76
            from contextlib import redirect_stderr
77
        except Exception:
78
            return
79
80
        f = io.StringIO()
81
        with redirect_stderr(f):
82
            launch_cmd_displays_output(self.cmd_ok, False, True)
83
        res = f.getvalue()
84
        self.assertEqual('', res)
85
86
    def test_command_exception(self):
87
        with self.assertRaisesRegex(SystemError, r"Cannot run the command: \[.*Err.*2\]"):
88
            launch_cmd_displays_output(self.cmd_err, True, True)
89
90 View Code Duplication
    def test_command_without_stderr_and_stdout_err(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
91
        # TODO make it work under windows
92
        if os.name == 'nt':
93
            return
94
95
        f = io.StringIO()
96
        with redirect_stdout(f):
97
            launch_cmd_displays_output(self.cmd_nook, False, False)
98
        res = f.getvalue()
99
        self.assertEqual('\n', res)
100
101
        try:
102
            from contextlib import redirect_stderr
103
        except Exception:
104
            return
105
106
        f = io.StringIO()
107
        with redirect_stderr(f):
108
            launch_cmd_displays_output(self.cmd_nook, False, False)
109
        res = f.getvalue()
110
        self.assertEqual('', res)
111
112 View Code Duplication
    def test_command_without_stderr_but_stdout_err(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
113
        # TODO make it work under windows
114
        if os.name == 'nt':
115
            return
116
117
        f = io.StringIO()
118
        with redirect_stdout(f):
119
            launch_cmd_displays_output(self.cmd_nook, True, False)
120
        res = f.getvalue()
121
        self.assertEqual('\n', res)
122
123
        try:
124
            from contextlib import redirect_stderr
125
        except Exception:
126
            return
127
128
        f = io.StringIO()
129
        with redirect_stderr(f):
130
            launch_cmd_displays_output(self.cmd_nook, True, False)
131
        res = f.getvalue()
132
        self.assertEqual('', res)
133
134 View Code Duplication
    def test_command_with_stderr_no_stdout_err(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
135
        # TODO make it work under windows
136
        if os.name == 'nt':
137
            return
138
139
        f = io.StringIO()
140
        with redirect_stdout(f):
141
            launch_cmd_displays_output(self.cmd_nook, False, True)
142
        res = f.getvalue()
143
        expected = re.compile('.*No such file or directory.*', re.MULTILINE)
144
        self.assertRegex(res, expected)
145
146
        try:
147
            from contextlib import redirect_stderr
148
        except Exception:
149
            return
150
151
        f = io.StringIO()
152
        with redirect_stderr(f):
153
            launch_cmd_displays_output(self.cmd_nook, False, True)
154
        res = f.getvalue()
155
        self.assertEqual('', res)
156
157 View Code Duplication
    def test_command_with_stderr_no_stdout_err_loop(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
158
        # TODO make it work under windows
159
        if os.name == 'nt':
160
            return
161
162
        f = io.StringIO()
163
        with redirect_stdout(f):
164
            cmd = ['cat', 'w', 'r', 'o', 'n', 'g', 'f', 'i', 'l', 'e']
165
            launch_cmd_displays_output(cmd, False, True)
166
        res = f.getvalue()
167
        expected = re.compile(r'.*\.\.\. and more.*', re.MULTILINE)
168
        self.assertRegex(res, expected)
169
170
        try:
171
            from contextlib import redirect_stderr
172
        except Exception:
173
            return
174
175
        f = io.StringIO()
176
        with redirect_stderr(f):
177
            launch_cmd_displays_output(self.cmd_nook, False, True)
178
        res = f.getvalue()
179
        self.assertEqual('', res)
180
181
182
if __name__ == "__main__":
183
    unittest.main()
184