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

coalib.tests.misc.PrepareStringArgumentTest   A

Complexity

Total Complexity 1

Size/Duplication

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A test_prepare_string_argument() 0 13 1
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
                escape_path_argument(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 test_prepare_string_argument(self):
148
        self.assertEqual(prepare_string_argument("normal_string"),
149
                         "'normal_string'")
150
        self.assertEqual(prepare_string_argument("string with spaces"),
151
                         "'string with spaces'")
152
        self.assertEqual(prepare_string_argument('string with quotes"a'),
153
                         "'string with quotes\"a'")
154
        self.assertEqual(prepare_string_argument("string with s-quotes'b"),
155
                         '"string with s-quotes\'b"')
156
        self.assertEqual(prepare_string_argument("bsn \n A"),
157
                         "'bsn \n A'")
158
        self.assertEqual(prepare_string_argument("unrecognized \\q escape"),
159
                         "'unrecognized \\q escape'")
160
161
162
if __name__ == '__main__':
163
    unittest.main(verbosity=2)
164