Failed Conditions
Pull Request — master (#1228)
by Mischa
01:55
created

coalib.tests.misc.RunShellCommandTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 67
Duplicated Lines 0 %
Metric Value
dl 0
loc 67
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A test_run_shell_command_kwargs_delegation() 0 7 3
A construct_testscript_command() 0 8 2
B test_run_interactive_shell_command_kwargs_delegation() 0 10 5
A test_run_input_shell_command() 0 13 1
A test_run_noninteractive_shell_command() 0 11 1
A test_run_interactive_shell_command() 0 11 2
1
import os
2
import sys
3
import unittest
4
5
sys.path.insert(0, ".")
6
from coalib.misc.Shell import (prepare_string_argument,
7
                               escape_path_argument,
8
                               run_interactive_shell_command,
9
                               run_shell_command)
10
11
12
class ShellTest(unittest.TestCase):
13
    # Tests the function that makes a path shell-argument-ready.
14
15
    def test_escape_path_argument(self):
16
        osname = "Linux"
17
        self.assertEqual(
18
            escape_path_argument("/home/usr/a-file", osname),
19
            "/home/usr/a-file")
20
        self.assertEqual(
21
            escape_path_argument("/home/usr/a-dir/", osname),
22
            "/home/usr/a-dir/")
23
        self.assertEqual(
24
            escape_path_argument("/home/us r/a-file with spaces.bla", osname),
25
            "/home/us\\ r/a-file\\ with\\ spaces.bla")
26
        self.assertEqual(
27
            escape_path_argument("/home/us r/a-dir with spaces/x/", osname),
28
            "/home/us\\ r/a-dir\\ with\\ spaces/x/")
29
        self.assertEqual(
30
            escape_path_argument(
31
                "relative something/with cherries and/pickles.delicious",
32
                osname),
33
            "relative\\ something/with\\ cherries\\ and/pickles.delicious")
34
35
        osname = "Windows"
36
        self.assertEqual(
37
            escape_path_argument("C:\\Windows\\has-a-weird-shell.txt", osname),
38
            "\"C:\\Windows\\has-a-weird-shell.txt\"")
39
        self.assertEqual(
40
            escape_path_argument("C:\\Windows\\lolrofl\\dirs\\", osname),
41
            "\"C:\\Windows\\lolrofl\\dirs\\\"")
42
        self.assertEqual(
43
            escape_path_argument("X:\\Users\\Maito Gai\\fi le.exe", osname),
44
            "\"X:\\Users\\Maito Gai\\fi le.exe\"")
45
        self.assertEqual(
46
            escape_path_argument("X:\\Users\\Mai to Gai\\director y\\",
47
                                 osname),
48
            "\"X:\\Users\\Mai to Gai\\director y\\\"")
49
        self.assertEqual(
50
            escape_path_argument("X:\\Users\\Maito Gai\\\"seven-gates\".y",
51
                                 osname),
52
            "\"X:\\Users\\Maito Gai\\^\"seven-gates^\".y\"")
53
        self.assertEqual(
54
            escape_path_argument("System32\\my-custom relative tool\\",
55
                                 osname),
56
            "\"System32\\my-custom relative tool\\\"")
57
        self.assertEqual(
58
            escape_path_argument("System32\\illegal\" name \"\".curd", osname),
59
            "\"System32\\illegal^\" name ^\"^\".curd\"")
60
61
        osname = "INVALID"
62
        self.assertEqual(
63
            escape_path_argument("/home/usr/a-file", osname),
64
            "/home/usr/a-file")
65
        self.assertEqual(
66
            escape_path_argument("/home/us r/a-file with spaces.bla", osname),
67
            "/home/us r/a-file with spaces.bla")
68
        self.assertEqual(
69
            escape_path_argument("|home|us r|a*dir with spaces|x|", osname),
70
            "|home|us r|a*dir with spaces|x|")
71
        self.assertEqual(
72
            escape_path_argument("system|a|b|c?d", osname),
73
            "system|a|b|c?d")
74
75
76
class RunShellCommandTest(unittest.TestCase):
77
78
    @staticmethod
79
    def construct_testscript_command(scriptname):
80
        return " ".join(
81
            escape_path_argument(s) for s in (
82
                sys.executable,
83
                os.path.join(os.path.dirname(os.path.realpath(__file__)),
84
                             "run_shell_command_testfiles",
85
                             scriptname)))
86
87
    def test_run_interactive_shell_command(self):
88
        command = RunShellCommandTest.construct_testscript_command(
89
            "test_interactive_program.py")
90
91
        with run_interactive_shell_command(command) as p:
92
            self.assertEqual(p.stdout.readline(), "test_program X\n")
93
            self.assertEqual(p.stdout.readline(), "Type in a number:\n")
94
            p.stdin.write("33\n")
95
            p.stdin.flush()
96
            self.assertEqual(p.stdout.readline(), "33\n")
97
            self.assertEqual(p.stdout.readline(), "Exiting program.\n")
98
99
    def test_run_interactive_shell_command_kwargs_delegation(self):
100
        with self.assertRaises(TypeError):
101
            with run_interactive_shell_command("some_command",
102
                                               weird_parameter=30):
103
                pass
104
105
        # Test one of the forbidden parameters.
106
        with self.assertRaises(TypeError):
107
            with run_interactive_shell_command("some_command", shell=False):
108
                pass
109
110
    def test_run_noninteractive_shell_command(self):
111
        command = RunShellCommandTest.construct_testscript_command(
112
            "test_program.py")
113
114
        stdout, stderr = run_shell_command(command)
115
116
        expected = ("test_program Z\n"
117
                    "non-interactive mode.\n"
118
                    "Exiting...\n")
119
        self.assertEqual(stdout, expected)
120
        self.assertEqual(stderr, "")
121
122
    def test_run_input_shell_command(self):
123
        command = RunShellCommandTest.construct_testscript_command(
124
            "test_input_program.py")
125
126
        stdout, stderr = run_shell_command(command, "1  4  10  22")
127
128
        self.assertEqual(stdout, "37\n")
129
        self.assertEqual(stderr, "")
130
131
        stdout, stderr = run_shell_command(command, "1 p 5")
132
133
        self.assertEqual(stdout, "INVALID INPUT\n")
134
        self.assertEqual(stderr, "")
135
136
    def test_run_shell_command_kwargs_delegation(self):
137
        with self.assertRaises(TypeError):
138
            run_shell_command("super-cool-command", weird_parameter2="abc")
139
140
        # Test one of the forbidden parameters.
141
        with self.assertRaises(TypeError):
142
            run_shell_command("super-cool-command", universal_newlines=False)
143
144
145
class PrepareStringArgumentTest(unittest.TestCase):
146
147
    def setUp(self):
148
        self.test_strings = ("normal_string",
149
                             "string with spaces",
150
                             'string with quotes"a',
151
                             "string with s-quotes'b",
152
                             "bsn \n A",
153
                             "unrecognized \\q escape")
154
155
    def test_prepare_string_argument_linux(self):
156
        os = "Linux"
0 ignored issues
show
Comprehensibility Bug introduced by
os is re-defining a name which is already available in the outer-scope (previously defined on line 1).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
157
        expected_results = ('"normal_string"',
158
                            '"string with spaces"',
159
                            '"string with quotes\\"a"',
160
                            '"string with s-quotes\'b"',
161
                            '"bsn \n A"',
162
                            '"unrecognized \\q escape"')
163
164
        for string, result in zip(self.test_strings, expected_results):
165
            self.assertEqual(prepare_string_argument(string, os), result)
166
167
    def test_prepare_string_argument_windows(self):
168
        os = "Windows"
0 ignored issues
show
Comprehensibility Bug introduced by
os is re-defining a name which is already available in the outer-scope (previously defined on line 1).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
169
        expected_results = ('"normal_string"',
170
                            '"string with spaces"',
171
                            '"string with quotes^"a"',
172
                            '"string with s-quotes\'b"',
173
                            '"bsn ^\n\n A"',
174
                            '"unrecognized \\q escape"')
175
176
        for string, result in zip(self.test_strings, expected_results):
177
            self.assertEqual(prepare_string_argument(string, os), result)
178
179
    def test_prepare_string_argument_unsupported_os(self):
180
        os = "WeIrD_O/S"
0 ignored issues
show
Comprehensibility Bug introduced by
os is re-defining a name which is already available in the outer-scope (previously defined on line 1).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
181
182
        for string in self.test_strings:
183
            self.assertEqual(prepare_string_argument(string, os), string)
184
185
186
if __name__ == '__main__':
187
    unittest.main(verbosity=2)
188