Debian Codegraph @

extra_tests/test_os.py

raw symbols SCIP
1 import os
2 import sys
3 from pytest import raises, skip
4
5 python = sys.executable
6
7 if hasattr(os, "execv"):
8 def test_execv():
9 if not hasattr(os, "fork"):
10 skip("Need fork() to test execv()")
11 if not os.path.isdir('/tmp'):
12 skip("Need '/tmp' for test")
13 pid = os.fork()
14 if pid == 0:
15 os.execv("/usr/bin/env", ["env", python, "-c",
16 ("fid = open('/tmp/onefile0', 'w'); "
17 "fid.write('1'); "
18 "fid.close()")])
19 os.waitpid(pid, 0)
20 assert open("/tmp/onefile0").read() == "1"
21 os.unlink("/tmp/onefile0")
22
23 def test_execv_raising():
24 with raises(OSError):
25 os.execv("saddsadsadsadsa", ["saddsadsasaddsa"])
26
27 def test_execv_no_args():
28 with raises(ValueError):
29 os.execv("notepad", [])
30 # PyPy needs at least one arg, CPython 2.7 is fine without
31 with raises(ValueError):
32 os.execve("notepad", [], {})
33
34 def test_execv_raising2():
35 for n in 3, [3, "a"]:
36 with raises(TypeError):
37 os.execv("xxx", n)
38
39 def test_execv_unicode():
40 if not hasattr(os, "fork"):
41 skip("Need fork() to test execv()")
42 if not os.path.isdir('/tmp'):
43 skip("Need '/tmp' for test")
44 output = u"caf\xe9 \u1234\n"
45 pid = os.fork()
46 if pid == 0:
47 os.execv(u"/bin/sh", ["sh", "-c",
48 u"echo caf\xe9 \u1234 > /tmp/onefile1"])
49 os.waitpid(pid, 0)
50 with open("/tmp/onefile1", encoding='utf-8') as fid:
51 assert fid.read() == output
52 os.unlink("/tmp/onefile1")
53
54 def test_execve():
55 if not hasattr(os, "fork"):
56 skip("Need fork() to test execve()")
57 if not os.path.isdir('/tmp'):
58 skip("Need '/tmp' for test")
59 pid = os.fork()
60 if pid == 0:
61 os.execve("/bin/sh",
62 ["sh", "-c", "echo $ddd > /tmp/onefile2"],
63 {'ddd': 'xxx'},
64 )
65 os.waitpid(pid, 0)
66 with open("/tmp/onefile2") as fid:
67 fid.read().rstrip() == "xxx"
68 os.unlink("/tmp/onefile2")
69
70 def test_execve_unicode():
71 if not hasattr(os, "fork"):
72 skip("Need fork() to test execve()")
73 if not os.path.isdir('/tmp'):
74 skip("Need '/tmp' for test")
75 output = u"caf\xe9 \u1234\n"
76 pid = os.fork()
77 if pid == 0:
78 os.execve(u"/bin/sh", ["sh", "-c",
79 u"echo caf\xe9 \u1234 > /tmp/onefile3"],
80 {'ddd': 'xxx', 'LANG': 'en_US.UTF-8'})
81 os.waitpid(pid, 0)
82 with open("/tmp/onefile3", encoding="utf-8") as fid:
83 assert fid.read() == output
84 os.unlink("/tmp/onefile3")
85 pass # <- please, inspect.getsource(), don't crash
86
87 if hasattr(os, "spawnv"):
88 def test_spawnv():
89 ret = os.spawnv(os.P_WAIT, python,
90 [python, '-c', 'raise(SystemExit(42))'])
91 assert ret == 42
92
93 if hasattr(os, "spawnve") and os.path.exists('/bin/sh'):
94 def test_spawnve():
95 env = {'FOOBAR': '42'}
96 cmd = "exit $FOOBAR"
97 ret = os.spawnve(os.P_WAIT, "/bin/sh", ["sh", '-c', cmd], env)
98 assert ret == 42