38
38
# SOFTWARE.
39
39
40
40
import unittest
41
+ import os
42
+ import sys
43
+ import posix
44
+ import stat
41
45
42
46
43
47
class PosixTests (unittest .TestCase ):
44
48
45
49
def test_uname (self ):
46
50
# just like cpython, a simple smoke test
47
- import posix
48
51
uname = posix .uname ()
49
52
self .assertRaises (TypeError , lambda : posix .uname (1 ))
50
53
self .assertIsNotNone (uname .sysname )
@@ -56,28 +59,30 @@ def test_uname(self):
56
59
def test_execv (self ):
57
60
# test creates a shell script, which again creates a file, to ensure script execution
58
61
# Both files are deleted again in the end
59
- import os
60
- import sys
61
62
new_file_path , cwd = self .create_file ()
62
- # os.execv(new_file_path, [new_file_path, 'the_input'])
63
63
os .system ("%s -c \" import os; os.execv('%s', ['%s', 'the_input'])\" " % (sys .executable , new_file_path , new_file_path ))
64
64
assert os .path .isfile (cwd + '/test.txt' )
65
65
self .delete_file (new_file_path , cwd )
66
66
67
67
def test_execl (self ):
68
68
# test creates a shell script, which again creates a file, to ensure script execution
69
69
# Both files are deleted again in the end
70
- import os
71
- import sys
72
70
new_file_path , cwd = self .create_file ()
73
- # os.execl(new_file_path, [new_file_path, 'the_input'])
74
71
os .system ("%s -c \" import os; os.execl('%s', ['%s', 'the_input'])\" " % (sys .executable , new_file_path , new_file_path ))
75
72
assert os .path .isfile (cwd + '/test.txt' )
76
73
self .delete_file (new_file_path , cwd )
77
74
75
+ def test_execv_with_env (self ):
76
+ new_file_path , cwd = self .create_file ()
77
+ with open (new_file_path , 'w' ) as script :
78
+ script .write ('echo $ENV_VAR> {}/test.txt\n ' .format (cwd ))
79
+ os .system ("%s -c \" import os; os.environ['ENV_VAR']='the_text'; os.execl('%s', ['%s', 'the_input'])\" " % (sys .executable , new_file_path , new_file_path ))
80
+ assert os .path .isfile (cwd + '/test.txt' )
81
+ with open (cwd + '/test.txt' , 'r' ) as result :
82
+ assert 'the_text' in result .readline ()
83
+ self .delete_file (new_file_path , cwd )
84
+
78
85
def create_file (self ):
79
- import os
80
- import stat
81
86
cwd = os .getcwd ()
82
87
new_file_path = os .path .join (cwd , 'myscript.sh' )
83
88
with open (new_file_path , 'w' ) as script :
@@ -90,6 +95,5 @@ def create_file(self):
90
95
return new_file_path , cwd
91
96
92
97
def delete_file (self , new_file_path , cwd ):
93
- import os
94
98
os .remove (new_file_path )
95
99
os .remove (cwd + '/test.txt' )
0 commit comments