Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
0f5e87a2
Commit
0f5e87a2
authored
Aug 24, 2007
by
Collin Winter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert test_pkg to use unittest.
parent
fe3b4b95
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
251 additions
and
285 deletions
+251
-285
Lib/test/output/test_pkg
Lib/test/output/test_pkg
+0
-45
Lib/test/test_pkg.py
Lib/test/test_pkg.py
+251
-240
No files found.
Lib/test/output/test_pkg
deleted
100644 → 0
View file @
fe3b4b95
test_pkg
running test t1
running test t2
t2 loading
doc for t2
t2.sub.subsub loading
t2 t2.sub t2.sub.subsub
['sub', 't2']
t2.sub t2.sub.subsub
t2.sub.subsub
['spam', 'sub', 'subsub', 't2']
t2 t2.sub t2.sub.subsub
['spam', 'sub', 'subsub', 't2']
running test t3
t3 loading
t3.sub.subsub loading
t3 t3.sub t3.sub.subsub
t3 loading
t3.sub.subsub loading
running test t4
t4 loading
t4.sub.subsub loading
t4.sub.subsub.spam = 1
running test t5
t5.foo loading
t5.string loading
1
['foo', 'string', 't5']
['__doc__', '__file__', '__name__', '__path__', 'foo', 'string', 't5']
['__doc__', '__file__', '__name__', 'string']
['__doc__', '__file__', '__name__', 'spam']
running test t6
['__all__', '__doc__', '__file__', '__name__', '__path__']
t6.spam loading
t6.ham loading
t6.eggs loading
['__all__', '__doc__', '__file__', '__name__', '__path__', 'eggs', 'ham', 'spam']
['eggs', 'ham', 'spam', 't6']
running test t7
t7 loading
['__doc__', '__file__', '__name__', '__path__']
['__doc__', '__file__', '__name__', '__path__']
t7.sub.subsub loading
['__doc__', '__file__', '__name__', '__path__', 'spam']
t7.sub.subsub.spam = 1
Lib/test/test_pkg.py
View file @
0f5e87a2
# Test packages (dotted-name import)
# Test packages (dotted-name import)
import
sys
,
os
,
tempfile
,
traceback
import
sys
from
os
import
mkdir
,
rmdir
,
extsep
# Can't test if these fail
import
os
del
mkdir
,
rmdir
import
tempfile
from
test.test_support
import
verify
,
verbose
,
TestFailed
import
textwrap
import
traceback
import
unittest
from
test
import
test_support
# Helpers to create and destroy hierarchies.
def
mkhier
(
root
,
descr
):
# Helpers to create and destroy hierarchies.
if
not
os
.
path
.
isdir
(
root
):
mkdir
(
root
)
for
name
,
contents
in
descr
:
comps
=
name
.
split
()
fullname
=
root
for
c
in
comps
:
fullname
=
os
.
path
.
join
(
fullname
,
c
)
if
contents
is
None
:
mkdir
(
fullname
)
else
:
if
verbose
:
print
"write"
,
fullname
f
=
open
(
fullname
,
"w"
)
f
.
write
(
contents
)
if
contents
and
contents
[
-
1
]
!=
'
\
n
'
:
f
.
write
(
'
\
n
'
)
f
.
close
()
def
mkdir
(
x
):
if
verbose
:
print
"mkdir"
,
x
os
.
mkdir
(
x
)
def
cleanout
(
root
):
def
cleanout
(
root
):
names
=
os
.
listdir
(
root
)
names
=
os
.
listdir
(
root
)
...
@@ -37,223 +19,252 @@ def cleanout(root):
...
@@ -37,223 +19,252 @@ def cleanout(root):
cleanout
(
fullname
)
cleanout
(
fullname
)
else
:
else
:
os
.
remove
(
fullname
)
os
.
remove
(
fullname
)
rmdir
(
root
)
os
.
rmdir
(
root
)
def
rmdir
(
x
):
if
verbose
:
print
"rmdir"
,
x
os
.
rmdir
(
x
)
def
fixdir
(
lst
):
def
fixdir
(
lst
):
try
:
if
"__builtins__"
in
lst
:
lst
.
remove
(
'__builtins__'
)
lst
.
remove
(
"__builtins__"
)
except
ValueError
:
pass
return
lst
return
lst
# Helper to run a test
def
runtest
(
hier
,
code
):
# XXX Things to test
root
=
tempfile
.
mkdtemp
()
#
mkhier
(
root
,
hier
)
# import package without __init__
savepath
=
sys
.
path
[:]
# import package with __init__
fd
,
fname
=
tempfile
.
mkstemp
(
text
=
True
)
# __init__ importing submodule
os
.
write
(
fd
,
code
)
# __init__ importing global module
os
.
close
(
fd
)
# __init__ defining variables
try
:
# submodule importing other submodule
# submodule importing global module
# submodule import submodule via global name
# from package import submodule
# from package import subpackage
# from package import variable (defined in __init__)
# from package import * (defined in __init__)
class
Test
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
root
=
None
self
.
syspath
=
list
(
sys
.
path
)
def
tearDown
(
self
):
sys
.
path
[:]
=
self
.
syspath
cleanout
(
self
.
root
)
def
run_code
(
self
,
code
):
exec
(
textwrap
.
dedent
(
code
),
globals
(),
{
"self"
:
self
})
def
mkhier
(
self
,
descr
):
root
=
tempfile
.
mkdtemp
()
sys
.
path
.
insert
(
0
,
root
)
sys
.
path
.
insert
(
0
,
root
)
if
verbose
:
print
"sys.path ="
,
sys
.
path
if
not
os
.
path
.
isdir
(
root
):
try
:
os
.
mkdir
(
root
)
execfile
(
fname
,
globals
(),
{})
for
name
,
contents
in
descr
:
except
:
comps
=
name
.
split
()
traceback
.
print_exc
(
file
=
sys
.
stdout
)
fullname
=
root
finally
:
for
c
in
comps
:
sys
.
path
[:]
=
savepath
fullname
=
os
.
path
.
join
(
fullname
,
c
)
os
.
unlink
(
fname
)
if
contents
is
None
:
try
:
os
.
mkdir
(
fullname
)
cleanout
(
root
)
else
:
except
(
os
.
error
,
IOError
):
f
=
open
(
fullname
,
"w"
)
pass
f
.
write
(
contents
)
if
contents
and
contents
[
-
1
]
!=
'
\
n
'
:
# Test descriptions
f
.
write
(
'
\
n
'
)
f
.
close
()
tests
=
[
self
.
root
=
root
(
"t1"
,
[(
"t1"
,
None
),
(
"t1 __init__"
+
os
.
extsep
+
"py"
,
""
)],
"import t1"
),
def
test_1
(
self
):
(
"t2"
,
[
hier
=
[(
"t1"
,
None
),
(
"t1 __init__"
+
os
.
extsep
+
"py"
,
""
)]
(
"t2"
,
None
),
self
.
mkhier
(
hier
)
(
"t2 __init__"
+
os
.
extsep
+
"py"
,
"'doc for t2'; print __name__, 'loading'"
),
import
t1
(
"t2 sub"
,
None
),
(
"t2 sub __init__"
+
os
.
extsep
+
"py"
,
""
),
def
test_2
(
self
):
(
"t2 sub subsub"
,
None
),
hier
=
[
(
"t2 sub subsub __init__"
+
os
.
extsep
+
"py"
,
"print __name__, 'loading'; spam = 1"
),
(
"t2"
,
None
),
],
(
"t2 __init__"
+
os
.
extsep
+
"py"
,
"'doc for t2'"
),
"""
(
"t2 sub"
,
None
),
import t2
(
"t2 sub __init__"
+
os
.
extsep
+
"py"
,
""
),
print t2.__doc__
(
"t2 sub subsub"
,
None
),
import t2.sub
(
"t2 sub subsub __init__"
+
os
.
extsep
+
"py"
,
"spam = 1"
),
import t2.sub.subsub
]
print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
self
.
mkhier
(
hier
)
import t2
from t2 import *
import
t2
print dir()
self
.
assertEqual
(
t2
.
__doc__
,
"doc for t2"
)
from t2 import sub
from t2.sub import subsub
import
t2.sub
from t2.sub.subsub import spam
import
t2.sub.subsub
print sub.__name__, subsub.__name__
self
.
assertEqual
(
t2
.
__name__
,
"t2"
)
print sub.subsub.__name__
self
.
assertEqual
(
t2
.
sub
.
__name__
,
"t2.sub"
)
print dir()
self
.
assertEqual
(
t2
.
sub
.
subsub
.
__name__
,
"t2.sub.subsub"
)
import t2.sub
import t2.sub.subsub
# This exec crap is needed because Py3k forbids 'import *' outside
print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
# of module-scope and __import__() is insufficient for what we need.
from t2 import *
s
=
"""
print dir()
import t2
"""
),
from t2 import *
self.assertEqual(dir(), ['self', 'sub', 't2'])
(
"t3"
,
[
"""
(
"t3"
,
None
),
self
.
run_code
(
s
)
(
"t3 __init__"
+
os
.
extsep
+
"py"
,
"print __name__, 'loading'"
),
(
"t3 sub"
,
None
),
from
t2
import
sub
(
"t3 sub __init__"
+
os
.
extsep
+
"py"
,
""
),
from
t2.sub
import
subsub
(
"t3 sub subsub"
,
None
),
from
t2.sub.subsub
import
spam
(
"t3 sub subsub __init__"
+
os
.
extsep
+
"py"
,
"print __name__, 'loading'; spam = 1"
),
self
.
assertEqual
(
sub
.
__name__
,
"t2.sub"
)
],
self
.
assertEqual
(
subsub
.
__name__
,
"t2.sub.subsub"
)
"""
self
.
assertEqual
(
sub
.
subsub
.
__name__
,
"t2.sub.subsub"
)
import t3.sub.subsub
for
name
in
[
'spam'
,
'sub'
,
'subsub'
,
't2'
]:
print t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__
self
.
failUnless
(
locals
()[
"name"
],
"Failed to import %s"
%
name
)
reload(t3)
reload(t3.sub)
import
t2.sub
reload(t3.sub.subsub)
import
t2.sub.subsub
"""
),
self
.
assertEqual
(
t2
.
__name__
,
"t2"
)
self
.
assertEqual
(
t2
.
sub
.
__name__
,
"t2.sub"
)
(
"t4"
,
[
self
.
assertEqual
(
t2
.
sub
.
subsub
.
__name__
,
"t2.sub.subsub"
)
(
"t4"
+
os
.
extsep
+
"py"
,
"print 'THIS SHOULD NOT BE PRINTED (t4"
+
os
.
extsep
+
"py)'"
),
(
"t4"
,
None
),
s
=
"""
(
"t4 __init__"
+
os
.
extsep
+
"py"
,
"print __name__, 'loading'"
),
from t2 import *
(
"t4 sub"
+
os
.
extsep
+
"py"
,
"print 'THIS SHOULD NOT BE PRINTED (sub"
+
os
.
extsep
+
"py)'"
),
self.failUnless(dir(), ['self', 'sub'])
(
"t4 sub"
,
None
),
"""
(
"t4 sub __init__"
+
os
.
extsep
+
"py"
,
""
),
self
.
run_code
(
s
)
(
"t4 sub subsub"
+
os
.
extsep
+
"py"
,
"print 'THIS SHOULD NOT BE PRINTED (subsub"
+
os
.
extsep
+
"py)'"
),
(
"t4 sub subsub"
,
None
),
def
test_3
(
self
):
(
"t4 sub subsub __init__"
+
os
.
extsep
+
"py"
,
"print __name__, 'loading'; spam = 1"
),
hier
=
[
],
(
"t3"
,
None
),
"""
(
"t3 __init__"
+
os
.
extsep
+
"py"
,
""
),
from t4.sub.subsub import *
(
"t3 sub"
,
None
),
print "t4.sub.subsub.spam =", spam
(
"t3 sub __init__"
+
os
.
extsep
+
"py"
,
""
),
"""
),
(
"t3 sub subsub"
,
None
),
(
"t3 sub subsub __init__"
+
os
.
extsep
+
"py"
,
"spam = 1"
),
(
"t5"
,
[
]
(
"t5"
,
None
),
self
.
mkhier
(
hier
)
(
"t5 __init__"
+
os
.
extsep
+
"py"
,
"import t5.foo"
),
(
"t5 string"
+
os
.
extsep
+
"py"
,
"print __name__, 'loading'; spam = 1"
),
import
t3.sub.subsub
(
"t5 foo"
+
os
.
extsep
+
"py"
,
self
.
assertEqual
(
t3
.
__name__
,
"t3"
)
"print __name__, 'loading'; import string; print string.spam"
),
self
.
assertEqual
(
t3
.
sub
.
__name__
,
"t3.sub"
)
],
self
.
assertEqual
(
t3
.
sub
.
subsub
.
__name__
,
"t3.sub.subsub"
)
"""
import t5
def
test_4
(
self
):
from t5 import *
hier
=
[
print dir()
(
"t4.py"
,
"raise RuntimeError('Shouldnt load t4.py')"
),
import t5
(
"t4"
,
None
),
print fixdir(dir(t5))
(
"t4 __init__"
+
os
.
extsep
+
"py"
,
""
),
print fixdir(dir(t5.foo))
(
"t4 sub.py"
,
"raise RuntimeError('Shouldnt load sub.py')"
),
print fixdir(dir(t5.string))
(
"t4 sub"
,
None
),
"""
),
(
"t4 sub __init__"
+
os
.
extsep
+
"py"
,
""
),
(
"t4 sub subsub"
+
os
.
extsep
+
"py"
,
(
"t6"
,
[
"raise RuntimeError('Shouldnt load subsub.py')"
),
(
"t6"
,
None
),
(
"t4 sub subsub"
,
None
),
(
"t6 __init__"
+
os
.
extsep
+
"py"
,
"__all__ = ['spam', 'ham', 'eggs']"
),
(
"t4 sub subsub __init__"
+
os
.
extsep
+
"py"
,
"spam = 1"
),
(
"t6 spam"
+
os
.
extsep
+
"py"
,
"print __name__, 'loading'"
),
]
(
"t6 ham"
+
os
.
extsep
+
"py"
,
"print __name__, 'loading'"
),
self
.
mkhier
(
hier
)
(
"t6 eggs"
+
os
.
extsep
+
"py"
,
"print __name__, 'loading'"
),
],
s
=
"""
"""
from t4.sub.subsub import *
import t6
self.assertEqual(spam, 1)
print fixdir(dir(t6))
"""
from t6 import *
self
.
run_code
(
s
)
print fixdir(dir(t6))
print dir()
def
test_5
(
self
):
"""
),
hier
=
[
(
"t5"
,
None
),
(
"t7"
,
[
(
"t5 __init__"
+
os
.
extsep
+
"py"
,
"import t5.foo"
),
(
"t7"
+
os
.
extsep
+
"py"
,
"print 'Importing t7"
+
os
.
extsep
+
"py'"
),
(
"t5 string"
+
os
.
extsep
+
"py"
,
"spam = 1"
),
(
"t7"
,
None
),
(
"t5 foo"
+
os
.
extsep
+
"py"
,
(
"t7 __init__"
+
os
.
extsep
+
"py"
,
"print __name__, 'loading'"
),
"from . import string; assert string.spam == 1"
),
(
"t7 sub"
+
os
.
extsep
+
"py"
,
"print 'THIS SHOULD NOT BE PRINTED (sub"
+
os
.
extsep
+
"py)'"
),
]
(
"t7 sub"
,
None
),
self
.
mkhier
(
hier
)
(
"t7 sub __init__"
+
os
.
extsep
+
"py"
,
""
),
(
"t7 sub subsub"
+
os
.
extsep
+
"py"
,
"print 'THIS SHOULD NOT BE PRINTED (subsub"
+
os
.
extsep
+
"py)'"
),
import
t5
(
"t7 sub subsub"
,
None
),
s
=
"""
(
"t7 sub subsub __init__"
+
os
.
extsep
+
"py"
,
"print __name__, 'loading'; spam = 1"
),
from t5 import *
],
self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])
"""
"""
t7, sub, subsub = None, None, None
self
.
run_code
(
s
)
import t7 as tas
print fixdir(dir(tas))
import
t5
verify(not t7)
self
.
assertEqual
(
fixdir
(
dir
(
t5
)),
from t7 import sub as subpar
[
'__doc__'
,
'__file__'
,
'__name__'
,
print fixdir(dir(subpar))
'__path__'
,
'foo'
,
'string'
,
't5'
])
verify(not t7 and not sub)
self
.
assertEqual
(
fixdir
(
dir
(
t5
.
foo
)),
from t7.sub import subsub as subsubsub
[
'__doc__'
,
'__file__'
,
'__name__'
,
'string'
])
print fixdir(dir(subsubsub))
self
.
assertEqual
(
fixdir
(
dir
(
t5
.
string
)),
verify(not t7 and not sub and not subsub)
[
'__doc__'
,
'__file__'
,
'__name__'
,
'spam'
])
from t7.sub.subsub import spam as ham
print "t7.sub.subsub.spam =", ham
def
test_6
(
self
):
verify(not t7 and not sub and not subsub)
hier
=
[
"""
),
(
"t6"
,
None
),
(
"t6 __init__"
+
os
.
extsep
+
"py"
,
]
"__all__ = ['spam', 'ham', 'eggs']"
),
(
"t6 spam"
+
os
.
extsep
+
"py"
,
""
),
nontests
=
[
(
"t6 ham"
+
os
.
extsep
+
"py"
,
""
),
(
"x5"
,
[],
(
"import a"
+
".a"
*
400
)),
(
"t6 eggs"
+
os
.
extsep
+
"py"
,
""
),
(
"x6"
,
[],
(
"import a"
+
".a"
*
499
)),
]
(
"x7"
,
[],
(
"import a"
+
".a"
*
500
)),
self
.
mkhier
(
hier
)
(
"x8"
,
[],
(
"import a"
+
".a"
*
1100
)),
(
"x9"
,
[],
(
"import "
+
"a"
*
400
)),
import
t6
(
"x10"
,
[],
(
"import "
+
"a"
*
500
)),
self
.
assertEqual
(
fixdir
(
dir
(
t6
)),
(
"x11"
,
[],
(
"import "
+
"a"
*
998
)),
[
'__all__'
,
'__doc__'
,
'__file__'
,
(
"x12"
,
[],
(
"import "
+
"a"
*
999
)),
'__name__'
,
'__path__'
])
(
"x13"
,
[],
(
"import "
+
"a"
*
999
)),
s
=
"""
(
"x14"
,
[],
(
"import "
+
"a"
*
2000
)),
import t6
]
from t6 import *
self.assertEqual(fixdir(dir(t6)),
"""XXX Things to test
['__all__', '__doc__', '__file__',
'__name__', '__path__', 'eggs',
import package without __init__
'ham', 'spam'])
import package with __init__
self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6'])
__init__ importing submodule
"""
__init__ importing global module
self
.
run_code
(
s
)
__init__ defining variables
submodule importing other submodule
def
test_7
(
self
):
submodule importing global module
hier
=
[
submodule import submodule via global name
(
"t7"
+
os
.
extsep
+
"py"
,
""
),
from package import submodule
(
"t7"
,
None
),
from package import subpackage
(
"t7 __init__"
+
os
.
extsep
+
"py"
,
""
),
from package import variable (defined in __init__)
(
"t7 sub"
+
os
.
extsep
+
"py"
,
from package import * (defined in __init__)
"raise RuntimeError('Shouldnt load sub.py')"
),
"""
(
"t7 sub"
,
None
),
(
"t7 sub __init__"
+
os
.
extsep
+
"py"
,
""
),
# Run the tests
(
"t7 sub "
+
os
.
extsep
+
"py"
,
"raise RuntimeError('Shouldnt load subsub.py')"
),
args
=
[]
(
"t7 sub subsub"
,
None
),
if
__name__
==
'__main__'
:
(
"t7 sub subsub __init__"
+
os
.
extsep
+
"py"
,
args
=
sys
.
argv
[
1
:]
"spam = 1"
),
if
args
and
args
[
0
]
==
'-q'
:
]
verbose
=
0
self
.
mkhier
(
hier
)
del
args
[
0
]
for
name
,
hier
,
code
in
tests
:
t7
,
sub
,
subsub
=
None
,
None
,
None
if
args
and
name
not
in
args
:
import
t7
as
tas
print
"skipping test"
,
name
self
.
assertEqual
(
fixdir
(
dir
(
tas
)),
continue
[
'__doc__'
,
'__file__'
,
'__name__'
,
'__path__'
])
print
"running test"
,
name
self
.
failIf
(
t7
)
runtest
(
hier
,
code
)
from
t7
import
sub
as
subpar
self
.
assertEqual
(
fixdir
(
dir
(
subpar
)),
# Test
[
'__doc__'
,
'__file__'
,
'__name__'
,
'__path__'
])
import
sys
self
.
failIf
(
t7
)
import
imp
self
.
failIf
(
sub
)
try
:
from
t7.sub
import
subsub
as
subsubsub
import
sys.imp
self
.
assertEqual
(
fixdir
(
dir
(
subsubsub
)),
except
ImportError
:
[
'__doc__'
,
'__file__'
,
'__name__'
,
'__path__'
,
# This is what we expect
'spam'
])
pass
self
.
failIf
(
t7
)
else
:
self
.
failIf
(
sub
)
raise
TestFailed
,
"No ImportError exception on 'import sys.imp'"
self
.
failIf
(
subsub
)
from
t7.sub.subsub
import
spam
as
ham
self
.
assertEqual
(
ham
,
1
)
self
.
failIf
(
t7
)
self
.
failIf
(
sub
)
self
.
failIf
(
subsub
)
def
test_main
():
test_support
.
run_unittest
(
__name__
)
if
__name__
==
"__main__"
:
test_main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment