-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_overrun.py
120 lines (92 loc) · 3.98 KB
/
test_overrun.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from overrun import cmd, format_cmd
import unittest
class TestOverrun(unittest.TestCase):
shell = False
def test_simple(self):
value = cmd('printf "[%s]" "Hello world"', shell=self.shell).read()
self.assertEqual(value, '[Hello world]')
def test_value_interpolation(self):
testing = 'Testing 123'
value = cmd('printf "[%s]" {testing}', shell=self.shell).read()
self.assertEqual(value, '[Testing 123]')
def test_value_interpolation_newline(self):
testing = 'Testing\n123'
value = cmd('printf "[%s]" {testing}', shell=self.shell).read()
self.assertEqual(value, '[Testing\n123]')
def test_function_call(self):
value = cmd('printf "[%s]" {"a".join("12345")}', shell=self.shell).read()
self.assertEqual(value, '[1a2a3a4a5]')
def test_bool_result(self):
proc = cmd('ls /', shell=self.shell).run(silent=True)
self.assertTrue(proc)
def test_bool_result_fail(self):
proc = cmd('ls /non_existent_directory', shell=self.shell).run(silent=True)
self.assertFalse(proc)
def test_conditional(self):
value = cmd('printf "%s\n"',
'hello' if False else '',
'world',
shell=self.shell).read()
self.assertEqual(value, 'world')
def test_guard_operator(self):
value = cmd('printf "%s\n"',
False and '123',
'456',
True and '789',
shell=self.shell).read()
self.assertEqual(value, '456\n789')
def test_none_interpolation(self):
value = cmd('printf "[%s]\n" {None}', shell=self.shell).read()
self.assertEqual(value, '[]')
def test_non_string_interpolation(self):
value = cmd('printf "[%s]\n" {12345}', shell=self.shell).read()
self.assertEqual(value, '[12345]')
def test_manual_format(self):
value = format_cmd(
'printf "1:%s 2:%s\n" {} {name}',
'Hello there',
name='Chan Tai Man',
shell=self.shell).read()
self.assertEqual(value, '1:Hello there 2:Chan Tai Man')
def test_format_into_curly_braces(self):
'''
Even if the interpolated variable is a python format string, it should
be printed out verbatim
'''
test = '{1}'
value = cmd('printf "[%s]\n" {test}', shell=self.shell).read()
self.assertEqual(value, '[{1}]')
def test_undefined_variable(self):
with self.assertRaises(NameError):
cmd('printf "[%s]\n" {undefined_variable}')
def test_variable_scope(self):
value = cmd('printf "[%s]\n" {self}', shell=self.shell).read()
self.assertEqual(value, f'[{str(self)}]')
def test_list_interpolation(self):
test = ['1 2', '3 4', '5 6']
value = cmd('printf "[%s]" {test:l}', shell=self.shell).read()
self.assertEqual(value, '[1 2][3 4][5 6]')
def test_list_interpolation_manual(self):
test = ['1', '2', '3', '4', '5']
value = format_cmd('printf "[%s]" {test:l}', test=test, shell=self.shell).read()
self.assertEqual(value, '[1][2][3][4][5]')
def test_escaped_curly_braces(self):
value = cmd('printf "[%s]" "{{curly}}"').read()
self.assertEqual(value, '[{curly}]')
def test_option_equals(self):
test = 'testing 123'
value = cmd('printf "[%s]" --test={test} second').read()
self.assertEqual(value, '[--test=testing 123][second]')
class TestOverrunShell(TestOverrun):
shell = True
def test_raw_format(self):
value = cmd('printf "[%s]\n" Testing{"123 45":r}', shell=True).read()
self.assertEqual(value, '[Testing123]\n[45]')
def test_shell_true(self):
value = cmd('yes | head -n2', shell=True).read()
self.assertEqual(value, 'y\ny')
def test_exit_code(self):
proc = cmd('exit 123', shell=True).run()
self.assertEqual(proc.returncode, 123)
if __name__ == '__main__':
unittest.main()