Debian Codegraph @

tests/test_subprocess.py

raw
1 import sys
2 import os
3 import unittest
4 import warnings
5
6 import pytest
7
8 from gi.repository import GLib
9 from gi import PyGIDeprecationWarning
10
11 from .helper import capture_gi_deprecation_warnings
12
13
14 def test_child_watch_add_get_args_various():
15 def cb(pid, status):
16 return None
17
18 get_args = GLib._child_watch_add_get_args
19 pid = 42
20 with capture_gi_deprecation_warnings():
21 assert get_args(pid, cb, 2) == (0, pid, cb, (2,))
22
23 with pytest.raises(TypeError):
24 get_args(pid, cb, 2, 3, 4)
25
26 assert get_args(0, pid, 2, 3, function=cb) == (0, pid, cb, (2, 3))
27 assert get_args(0, pid, cb, 2, 3) == (0, pid, cb, (2, 3))
28 assert get_args(0, pid, cb, data=99) == (0, pid, cb, (99,))
29
30 with pytest.raises(TypeError):
31 get_args(0, pid, 24)
32
33 with pytest.raises(TypeError):
34 get_args(0, pid, cb, 2, 3, data=99)
35
36
37 @unittest.skipIf(os.name == "nt", "not on Windows")
38 class TestProcess(unittest.TestCase):
39 def test_deprecated_child_watch_no_data(self):
40 def cb(pid, status):
41 return None
42
43 pid = object()
44 with warnings.catch_warnings(record=True) as w:
45 warnings.simplefilter("always")
46 res = GLib._child_watch_add_get_args(pid, cb)
47 self.assertTrue(issubclass(w[0].category, PyGIDeprecationWarning))
48
49 self.assertEqual(len(res), 4)
50 self.assertEqual(res[0], GLib.PRIORITY_DEFAULT)
51 self.assertEqual(res[1], pid)
52 self.assertTrue(callable(cb))
53 self.assertSequenceEqual(res[3], [])
54
55 def test_deprecated_child_watch_data_priority(self):
56 def cb(pid, status):
57 return None
58
59 pid = object()
60 with warnings.catch_warnings(record=True) as w:
61 warnings.simplefilter("always")
62 res = GLib._child_watch_add_get_args(pid, cb, 12345, GLib.PRIORITY_HIGH)
63 self.assertTrue(issubclass(w[0].category, PyGIDeprecationWarning))
64
65 self.assertEqual(len(res), 4)
66 self.assertEqual(res[0], GLib.PRIORITY_HIGH)
67 self.assertEqual(res[1], pid)
68 self.assertEqual(res[2], cb)
69 self.assertSequenceEqual(res[3], [12345])
70
71 def test_deprecated_child_watch_data_priority_kwargs(self):
72 def cb(pid, status):
73 return None
74
75 pid = object()
76 with warnings.catch_warnings(record=True) as w:
77 warnings.simplefilter("always")
78 res = GLib._child_watch_add_get_args(
79 pid, cb, priority=GLib.PRIORITY_HIGH, data=12345
80 )
81 self.assertTrue(issubclass(w[0].category, PyGIDeprecationWarning))
82
83 self.assertEqual(len(res), 4)
84 self.assertEqual(res[0], GLib.PRIORITY_HIGH)
85 self.assertEqual(res[1], pid)
86 self.assertEqual(res[2], cb)
87 self.assertSequenceEqual(res[3], [12345])
88
89 @unittest.expectedFailure # using keyword args is fully supported by PyGObject machinery
90 def test_child_watch_all_kwargs(self):
91 def cb(pid, status):
92 return None
93
94 pid = object()
95
96 res = GLib._child_watch_add_get_args(
97 priority=GLib.PRIORITY_HIGH, pid=pid, function=cb, data=12345
98 )
99 self.assertEqual(len(res), 4)
100 self.assertEqual(res[0], GLib.PRIORITY_HIGH)
101 self.assertEqual(res[1], pid)
102 self.assertEqual(res[2], cb)
103 self.assertSequenceEqual(res[3], [12345])
104
105 def test_child_watch_no_data(self):
106 def cb(pid, status):
107 self.status = status
108 self.loop.quit()
109
110 self.status = None
111 self.loop = GLib.MainLoop()
112 argv = [sys.executable, "-c", "import sys"]
113 pid, _stdin, _stdout, _stderr = GLib.spawn_async(
114 argv, flags=GLib.SpawnFlags.DO_NOT_REAP_CHILD
115 )
116 pid.close()
117 id = GLib.child_watch_add(GLib.PRIORITY_HIGH, pid, cb)
118 self.assertEqual(
119 self.loop.get_context().find_source_by_id(id).priority, GLib.PRIORITY_HIGH
120 )
121 self.loop.run()
122 self.assertEqual(self.status, 0)
123
124 def test_child_watch_with_data(self):
125 def cb(pid, status, data):
126 self.status = status
127 self.data = data
128 self.loop.quit()
129
130 self.data = None
131 self.status = None
132 self.loop = GLib.MainLoop()
133 argv = [sys.executable, "-c", "import sys"]
134 pid, stdin, stdout, stderr = GLib.spawn_async(
135 argv, flags=GLib.SpawnFlags.DO_NOT_REAP_CHILD
136 )
137 self.assertEqual(stdin, None)
138 self.assertEqual(stdout, None)
139 self.assertEqual(stderr, None)
140 self.assertNotEqual(pid, 0)
141 pid.close()
142 id = GLib.child_watch_add(GLib.PRIORITY_HIGH, pid, cb, 12345)
143 self.assertEqual(
144 self.loop.get_context().find_source_by_id(id).priority, GLib.PRIORITY_HIGH
145 )
146 self.loop.run()
147 self.assertEqual(self.data, 12345)
148 self.assertEqual(self.status, 0)
149
150 def test_spawn_async_fds(self):
151 pid, stdin, stdout, stderr = GLib.spawn_async(
152 ["cat"],
153 flags=GLib.SpawnFlags.SEARCH_PATH,
154 standard_input=True,
155 standard_output=True,
156 standard_error=True,
157 )
158 os.write(stdin, b"hello world!\n")
159 os.close(stdin)
160 out = os.read(stdout, 50)
161 os.close(stdout)
162 err = os.read(stderr, 50)
163 os.close(stderr)
164 pid.close()
165 self.assertEqual(out, b"hello world!\n")
166 self.assertEqual(err, b"")
167
168 def test_spawn_async_with_pipes(self):
169 _res, pid, stdin, stdout, stderr = GLib.spawn_async_with_pipes(
170 working_directory=None,
171 argv=["cat"],
172 envp=None,
173 flags=GLib.SpawnFlags.SEARCH_PATH,
174 )
175
176 os.write(stdin, b"hello world!\n")
177 os.close(stdin)
178 out = os.read(stdout, 50)
179 os.close(stdout)
180 err = os.read(stderr, 50)
181 os.close(stderr)
182 GLib.spawn_close_pid(pid)
183 self.assertEqual(out, b"hello world!\n")
184 self.assertEqual(err, b"")
185
186 def test_spawn_async_envp(self):
187 pid, stdin, stdout, stderr = GLib.spawn_async(
188 ["sh", "-c", "echo $TEST_VAR"],
189 ["TEST_VAR=moo!"],
190 flags=GLib.SpawnFlags.SEARCH_PATH,
191 standard_output=True,
192 )
193 self.assertEqual(stdin, None)
194 self.assertEqual(stderr, None)
195 out = os.read(stdout, 50)
196 os.close(stdout)
197 pid.close()
198 self.assertEqual(out, b"moo!\n")
199
200 def test_backwards_compat_flags(self):
201 with warnings.catch_warnings():
202 warnings.simplefilter("ignore", PyGIDeprecationWarning)
203
204 self.assertEqual(
205 GLib.SpawnFlags.DO_NOT_REAP_CHILD, GLib.SPAWN_DO_NOT_REAP_CHILD
206 )