Skip to content

Commit c4d5861

Browse files
committed
stdlib/os: improve tests
Signed-off-by: Sebastien Binet <[email protected]>
1 parent 797bab8 commit c4d5861

File tree

4 files changed

+142
-251
lines changed

4 files changed

+142
-251
lines changed
File renamed without changes.

stdlib/os/os.test.py

-251
This file was deleted.

stdlib/os/testdata/test.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Copyright 2022 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
import os
6+
7+
print("test os")
8+
print("os.error: ", os.error)
9+
print("os.getenv($GPYTHON_TEST_HOME)=", os.getenv("GPYTHON_TEST_HOME"))
10+
os.putenv("GPYTHON_TEST_HOME", "/home/go")
11+
print("os.environ($GPYTHON_TEST_HOME)=", os.environ.get("GPYTHON_TEST_HOME"))
12+
print("os.getenv($GPYTHON_TEST_HOME)=", os.getenv("GPYTHON_TEST_HOME"))
13+
os.unsetenv("GPYTHON_TEST_HOME")
14+
print("os.unsetenv($GPYTHON_TEST_HOME)=", os.getenv("GPYTHON_TEST_HOME"))
15+
16+
if not os.error is OSError:
17+
print("os.error is not OSError!")
18+
else:
19+
print("os.error is OSError [OK]")
20+
21+
## FIXME(sbinet): check returned value with a known one
22+
## (ie: when os.mkdir is implemented)
23+
if os.getcwd() == None:
24+
print("os.getcwd() == None !")
25+
else:
26+
print("os.getcwd() != None [OK]")
27+
28+
## FIXME(sbinet): check returned value with a known one
29+
## (ie: when os.mkdir is implemented)
30+
if os.getcwdb() == None:
31+
print("os.getcwdb() == None !")
32+
else:
33+
print("os.getcwdb() != None [OK]")
34+
35+
print("os.system('echo \"hello\"')...")
36+
os.system('echo "hello"')
37+
38+
if os.getpid() > 1:
39+
print("os.getpid is greater than 1 [OK]")
40+
else:
41+
print("invalid os.getpid: ", os.getpid())
42+
43+
orig = os.getcwd()
44+
os.chdir(os.getenv("HOME"))
45+
if os.getcwd() != os.getenv("HOME"):
46+
print("invalid getcwd() after os.chdir")
47+
else:
48+
print("os.chdir($HOME) [OK]")
49+
os.chdir(orig)
50+
51+
try:
52+
os.chdir(1)
53+
print("expected an error with os.chdir(1)")
54+
except TypeError:
55+
print("os.chdir(1) failed [OK]")
56+
57+
try:
58+
os.environ.get(15)
59+
print("expected an error with os.environ.get(15)")
60+
except KeyError:
61+
print("os.environ.get(15) failed [OK]")
62+
63+
try:
64+
os.putenv()
65+
print("expected an error with os.putenv()")
66+
except TypeError:
67+
print("os.putenv() failed [OK]")
68+
69+
try:
70+
os.unsetenv()
71+
print("expected an error with os.unsetenv()")
72+
except TypeError:
73+
print("os.unsetenv() failed [OK]")
74+
75+
try:
76+
os.getenv()
77+
print("expected an error with os.getenv()")
78+
except TypeError:
79+
print("os.getenv() failed [OK]")
80+
81+
try:
82+
os.unsetenv("FOO", "BAR")
83+
print("expected an error with os.unsetenv(\"FOO\", \"BAR\")")
84+
except TypeError:
85+
print("os.unsetenv(\"FOO\", \"BAR\") failed [OK]")
86+
87+
if bytes(os.getcwd(), "utf-8") == os.getcwdb():
88+
print('bytes(os.getcwd(), "utf-8") == os.getcwdb() [OK]')
89+
else:
90+
print('expected: bytes(os.getcwd(), "utf-8") == os.getcwdb()')
91+
92+
golden = {
93+
"posix": {
94+
"sep": "/",
95+
"pathsep": ":",
96+
"linesep": "\n",
97+
"devnull": "/dev/null",
98+
"altsep": None
99+
},
100+
"nt": {
101+
"sep": "\\",
102+
"pathsep": ";",
103+
"linesep": "\r\n",
104+
"devnull": "nul",
105+
"altsep": "/"
106+
},
107+
}[os.name]
108+
109+
for k in ("sep", "pathsep", "linesep", "devnull", "altsep"):
110+
if getattr(os, k) != golden[k]:
111+
print("invalid os."+k+": got=",getattr(os,k),", want=", golden[k])
112+
else:
113+
print("os."+k+": [OK]")
114+
115+
print("OK")

stdlib/os/testdata/test_golden.txt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
test os
2+
os.error: <class 'OSError'>
3+
os.getenv($GPYTHON_TEST_HOME)= None
4+
os.environ($GPYTHON_TEST_HOME)= None
5+
os.getenv($GPYTHON_TEST_HOME)= /home/go
6+
os.unsetenv($GPYTHON_TEST_HOME)= None
7+
os.error is OSError [OK]
8+
os.getcwd() != None [OK]
9+
os.getcwdb() != None [OK]
10+
os.system('echo "hello"')...
11+
hello
12+
13+
os.getpid is greater than 1 [OK]
14+
os.chdir($HOME) [OK]
15+
os.chdir(1) failed [OK]
16+
os.environ.get(15) failed [OK]
17+
os.putenv() failed [OK]
18+
os.unsetenv() failed [OK]
19+
os.getenv() failed [OK]
20+
os.unsetenv("FOO", "BAR") failed [OK]
21+
bytes(os.getcwd(), "utf-8") == os.getcwdb() [OK]
22+
os.sep: [OK]
23+
os.pathsep: [OK]
24+
os.linesep: [OK]
25+
os.devnull: [OK]
26+
os.altsep: [OK]
27+
OK

0 commit comments

Comments
 (0)