Completed
Pull Request — master (#1228)
by Mischa
01:24
created

coalib.tests.misc.ShellTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 62
Duplicated Lines 0 %
Metric Value
dl 0
loc 62
rs 10
wmc 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B coalib.tests.misc.EscapePathArgumentTest.test_escape_path_argument_windows() 0 26 1
A coalib.tests.misc.EscapePathArgumentTest.test_escape_path_argument_linux_and_darwin() 0 23 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 EscapePathArgumentTest(unittest.TestCase):
13
14
    def test_escape_path_argument_linux_and_darwin(self):
15
        osnames = ("Linux", "Darwin")
16
17
        for osname in osnames:
18
            self.assertEqual(
19
                escape_path_argument("/home/usr/a-file", osname),
20
                "/home/usr/a-file")
21
            self.assertEqual(
22
                escape_path_argument("/home/usr/a-dir/", osname),
23
                "/home/usr/a-dir/")
24
            self.assertEqual(
25
                escape_path_argument("/home/us r/a-file with spaces.bla",
26
                                     osname),
27
                "/home/us\\ r/a-file\\ with\\ spaces.bla")
28
            self.assertEqual(
29
                escape_path_argument("/home/us r/a-dir with spaces/x/",
30
                                     osname),
31
                "/home/us\\ r/a-dir\\ with\\ spaces/x/")
32
            self.assertEqual(
33
                escape_path_argument(
34
                    "relative something/with cherries and/pickles.delicious",
35
                    osname),
36
                "relative\\ something/with\\ cherries\\ and/pickles.delicious")
37
38
    def test_escape_path_argument_windows(self):
39
        osname = "Windows"
40
        self.assertEqual(
41
            escape_path_argument("C:\\Windows\\has-a-weird-shell.txt", osname),
42
            "\"C:\\Windows\\has-a-weird-shell.txt\"")
43
        self.assertEqual(
44
            escape_path_argument("C:\\Windows\\lolrofl\\dirs\\", osname),
45
            "\"C:\\Windows\\lolrofl\\dirs\\\"")
46
        self.assertEqual(
47
            escape_path_argument("X:\\Users\\Maito Gai\\fi le.exe", osname),
48
            "\"X:\\Users\\Maito Gai\\fi le.exe\"")
49
        self.assertEqual(
50
            escape_path_argument("X:\\Users\\Mai to Gai\\director y\\",
51
                                 osname),
52
            "\"X:\\Users\\Mai to Gai\\director y\\\"")
53
        self.assertEqual(
54
            escape_path_argument("X:\\Users\\Maito Gai\\\"seven-gates\".y",
55
                                 osname),
56
            "\"X:\\Users\\Maito Gai\\^\"seven-gates^\".y\"")
57
        self.assertEqual(
58
            escape_path_argument("System32\\my-custom relative tool\\",
59
                                 osname),
60
            "\"System32\\my-custom relative tool\\\"")
61
        self.assertEqual(
62
            escape_path_argument("System32\\illegal\" name \"\".curd", osname),
63
            "\"System32\\illegal^\" name ^\"^\".curd\"")
64
65
    def test_escape_path_argument_unsupported_os(self):
66
        osname = "INVALID"
67
        self.assertEqual(
68
            escape_path_argument("/home/usr/a-file", osname),
69
            "/home/usr/a-file")
70
        self.assertEqual(
71
            escape_path_argument("/home/us r/a-file with spaces.bla", osname),
72
            "/home/us r/a-file with spaces.bla")
73
        self.assertEqual(
74
            escape_path_argument("|home|us r|a*dir with spaces|x|", osname),
75
            "|home|us r|a*dir with spaces|x|")
76
        self.assertEqual(
77
            escape_path_argument("system|a|b|c?d", osname),
78
            "system|a|b|c?d")
79
80
81
class RunShellCommandTest(unittest.TestCase):
82
83
    @staticmethod
84
    def construct_testscript_command(scriptname):
85
        return " ".join(
86
            escape_path_argument(s) for s in (
87
                sys.executable,
88
                os.path.join(os.path.dirname(os.path.realpath(__file__)),
89
                             "run_shell_command_testfiles",
90
                             scriptname)))
91
92
    def test_run_interactive_shell_command(self):
93
        command = RunShellCommandTest.construct_testscript_command(
94
            "test_interactive_program.py")
95
96
        with run_interactive_shell_command(command) as p:
97
            self.assertEqual(p.stdout.readline(), "test_program X\n")
98
            self.assertEqual(p.stdout.readline(), "Type in a number:\n")
99
            p.stdin.write("33\n")
100
            p.stdin.flush()
101
            self.assertEqual(p.stdout.readline(), "33\n")
102
            self.assertEqual(p.stdout.readline(), "Exiting program.\n")
103
104
    def test_run_interactive_shell_command_kwargs_delegation(self):
105
        with self.assertRaises(TypeError):
106
            with run_interactive_shell_command("some_command",
107
                                               weird_parameter=30):
108
                pass
109
110
        # Test one of the forbidden parameters.
111
        with self.assertRaises(TypeError):
112
            with run_interactive_shell_command("some_command", shell=False):
113
                pass
114
115
    def test_run_noninteractive_shell_command(self):
116
        command = RunShellCommandTest.construct_testscript_command(
117
            "test_program.py")
118
119
        stdout, stderr = run_shell_command(command)
120
121
        expected = ("test_program Z\n"
122
                    "non-interactive mode.\n"
123
                    "Exiting...\n")
124
        self.assertEqual(stdout, expected)
125
        self.assertEqual(stderr, "")
126
127
    def test_run_input_shell_command(self):
128
        command = RunShellCommandTest.construct_testscript_command(
129
            "test_input_program.py")
130
131
        stdout, stderr = run_shell_command(command, "1  4  10  22")
132
133
        self.assertEqual(stdout, "37\n")
134
        self.assertEqual(stderr, "")
135
136
        stdout, stderr = run_shell_command(command, "1 p 5")
137
138
        self.assertEqual(stdout, "INVALID INPUT\n")
139
        self.assertEqual(stderr, "")
140
141
    def test_run_shell_command_kwargs_delegation(self):
142
        with self.assertRaises(TypeError):
143
            run_shell_command("super-cool-command", weird_parameter2="abc")
144
145
        # Test one of the forbidden parameters.
146
        with self.assertRaises(TypeError):
147
            run_shell_command("super-cool-command", universal_newlines=False)
148
149
150
class PrepareStringArgumentTest(unittest.TestCase):
151
152
    def setUp(self):
153
        self.test_strings = ("normal_string",
154
                             "string with spaces",
155
                             'string with quotes"a',
156
                             "string with s-quotes'b",
157
                             "bsn \n A",
158
                             "unrecognized \\q escape")
159
160
    def test_prepare_string_argument_linux_and_darwin(self):
161
        testoses = ("Linux", "Darwin")
162
        expected_results = ('"normal_string"',
163
                            '"string with spaces"',
164
                            '"string with quotes\\"a"',
165
                            '"string with s-quotes\'b"',
166
                            '"bsn \n A"',
167
                            '"unrecognized \\q escape"')
168
169
        for string, result in zip(self.test_strings, expected_results):
170
            for testos in testoses:
171
                self.assertEqual(prepare_string_argument(string, testos),
172
                                 result)
173
174
    def test_prepare_string_argument_unsupported_os(self):
175
        testos = "WeIrD_O/S"
176
177
        for string in self.test_strings:
178
            self.assertEqual(prepare_string_argument(string, testos), string)
179
180
181
if __name__ == '__main__':
182
    unittest.main(verbosity=2)
183