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
11e065b3
Commit
11e065b3
authored
Aug 24, 2007
by
Collin Winter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert test_pkg to use unittest.
parent
70e79803
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
248 additions
and
280 deletions
+248
-280
Lib/test/output/test_pkg
Lib/test/output/test_pkg
+0
-43
Lib/test/test_pkg.py
Lib/test/test_pkg.py
+248
-237
No files found.
Lib/test/output/test_pkg
deleted
100644 → 0
View file @
70e79803
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
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 @
11e065b3
# Test packages (dotted-name import)
import
sys
,
os
,
tempfile
,
traceback
from
os
import
mkdir
,
rmdir
# Can't test if these fail
del
mkdir
,
rmdir
from
test.test_support
import
verify
,
verbose
,
TestFailed
import
sys
import
os
import
tempfile
import
textwrap
import
traceback
import
unittest
from
test
import
test_support
# Helpers to create and destroy hierarchies.
def
mkhier
(
root
,
descr
):
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
)
# Helpers to create and destroy hierarchies.
def
cleanout
(
root
):
names
=
os
.
listdir
(
root
)
...
...
@@ -37,220 +19,249 @@ def cleanout(root):
cleanout
(
fullname
)
else
:
os
.
remove
(
fullname
)
rmdir
(
root
)
def
rmdir
(
x
):
if
verbose
:
print
(
"rmdir"
,
x
)
os
.
rmdir
(
x
)
os
.
rmdir
(
root
)
def
fixdir
(
lst
):
try
:
lst
.
remove
(
'__builtins__'
)
except
ValueError
:
pass
if
"__builtins__"
in
lst
:
lst
.
remove
(
"__builtins__"
)
return
lst
# Helper to run a test
def
runtest
(
hier
,
code
):
root
=
tempfile
.
mkdtemp
()
mkhier
(
root
,
hier
)
savepath
=
sys
.
path
[:]
fd
,
fname
=
tempfile
.
mkstemp
(
text
=
True
)
os
.
write
(
fd
,
code
)
os
.
close
(
fd
)
try
:
# XXX Things to test
#
# import package without __init__
# import package with __init__
# __init__ importing submodule
# __init__ importing global module
# __init__ defining variables
# 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
)
if
verbose
:
print
(
"sys.path ="
,
sys
.
path
)
try
:
exec
(
open
(
fname
).
read
(),
globals
(),
{})
except
:
traceback
.
print_exc
(
file
=
sys
.
stdout
)
finally
:
sys
.
path
[:]
=
savepath
os
.
unlink
(
fname
)
try
:
cleanout
(
root
)
except
(
os
.
error
,
IOError
):
pass
# Test descriptions
tests
=
[
(
"t1"
,
[(
"t1"
,
None
),
(
"t1 __init__.py"
,
""
)],
"import t1"
),
(
"t2"
,
[
(
"t2"
,
None
),
(
"t2 __init__.py"
,
"'doc for t2'; print(__name__, 'loading')"
),
(
"t2 sub"
,
None
),
(
"t2 sub __init__.py"
,
""
),
(
"t2 sub subsub"
,
None
),
(
"t2 sub subsub __init__.py"
,
"print(__name__, 'loading'); spam = 1"
),
],
"""
import t2
print(t2.__doc__)
import t2.sub
import t2.sub.subsub
print(t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__)
import t2
from t2 import *
print(dir())
from t2 import sub
from t2.sub import subsub
from t2.sub.subsub import spam
print(sub.__name__, subsub.__name__)
print(sub.subsub.__name__)
print(dir())
import t2.sub
import t2.sub.subsub
print(t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__)
from t2 import *
print(dir())
"""
),
(
"t3"
,
[
(
"t3"
,
None
),
(
"t3 __init__.py"
,
"print(__name__, 'loading')"
),
(
"t3 sub"
,
None
),
(
"t3 sub __init__.py"
,
""
),
(
"t3 sub subsub"
,
None
),
(
"t3 sub subsub __init__.py"
,
"print(__name__, 'loading'); spam = 1"
),
],
"""
import t3.sub.subsub
print(t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__)
"""
),
(
"t4"
,
[
(
"t4.py"
,
"print('THIS SHOULD NOT BE PRINTED (t4.py)')"
),
(
"t4"
,
None
),
(
"t4 __init__.py"
,
"print(__name__, 'loading')"
),
(
"t4 sub.py"
,
"print('THIS SHOULD NOT BE PRINTED (sub.py)')"
),
(
"t4 sub"
,
None
),
(
"t4 sub __init__.py"
,
""
),
(
"t4 sub subsub.py"
,
"print('THIS SHOULD NOT BE PRINTED (subsub.py)')"
),
(
"t4 sub subsub"
,
None
),
(
"t4 sub subsub __init__.py"
,
"print(__name__, 'loading'); spam = 1"
),
],
"""
from t4.sub.subsub import *
print("t4.sub.subsub.spam =", spam)
"""
),
(
"t5"
,
[
(
"t5"
,
None
),
(
"t5 __init__.py"
,
"import t5.foo"
),
(
"t5 string.py"
,
"print(__name__, 'loading'); spam = 1"
),
(
"t5 foo.py"
,
"print(__name__, 'loading'); from . import string; print(string.spam)"
),
],
"""
import t5
from t5 import *
print(dir())
import t5
print(fixdir(dir(t5)))
print(fixdir(dir(t5.foo)))
print(fixdir(dir(t5.string)))
"""
),
(
"t6"
,
[
(
"t6"
,
None
),
(
"t6 __init__.py"
,
"__all__ = ['spam', 'ham', 'eggs']"
),
(
"t6 spam.py"
,
"print(__name__, 'loading')"
),
(
"t6 ham.py"
,
"print(__name__, 'loading')"
),
(
"t6 eggs.py"
,
"print(__name__, 'loading')"
),
],
"""
import t6
print(fixdir(dir(t6)))
from t6 import *
print(fixdir(dir(t6)))
print(dir())
"""
),
(
"t7"
,
[
(
"t7.py"
,
"print('Importing t7.py')"
),
(
"t7"
,
None
),
(
"t7 __init__.py"
,
"print(__name__, 'loading')"
),
(
"t7 sub.py"
,
"print('THIS SHOULD NOT BE PRINTED (sub.py)')"
),
(
"t7 sub"
,
None
),
(
"t7 sub __init__.py"
,
""
),
(
"t7 sub subsub.py"
,
"print('THIS SHOULD NOT BE PRINTED (subsub.py)')"
),
(
"t7 sub subsub"
,
None
),
(
"t7 sub subsub __init__.py"
,
"print(__name__, 'loading'); spam = 1"
),
],
"""
t7, sub, subsub = None, None, None
import t7 as tas
print(fixdir(dir(tas)))
verify(not t7)
from t7 import sub as subpar
print(fixdir(dir(subpar)))
verify(not t7 and not sub)
from t7.sub import subsub as subsubsub
print(fixdir(dir(subsubsub)))
verify(not t7 and not sub and not subsub)
from t7.sub.subsub import spam as ham
print("t7.sub.subsub.spam =", ham)
verify(not t7 and not sub and not subsub)
"""
),
]
nontests
=
[
(
"x5"
,
[],
(
"import a"
+
".a"
*
400
)),
(
"x6"
,
[],
(
"import a"
+
".a"
*
499
)),
(
"x7"
,
[],
(
"import a"
+
".a"
*
500
)),
(
"x8"
,
[],
(
"import a"
+
".a"
*
1100
)),
(
"x9"
,
[],
(
"import "
+
"a"
*
400
)),
(
"x10"
,
[],
(
"import "
+
"a"
*
500
)),
(
"x11"
,
[],
(
"import "
+
"a"
*
998
)),
(
"x12"
,
[],
(
"import "
+
"a"
*
999
)),
(
"x13"
,
[],
(
"import "
+
"a"
*
999
)),
(
"x14"
,
[],
(
"import "
+
"a"
*
2000
)),
]
"""XXX Things to test
import package without __init__
import package with __init__
__init__ importing submodule
__init__ importing global module
__init__ defining variables
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__)
"""
# Run the tests
args
=
[]
if
__name__
==
'__main__'
:
args
=
sys
.
argv
[
1
:]
if
args
and
args
[
0
]
==
'-q'
:
verbose
=
0
del
args
[
0
]
for
name
,
hier
,
code
in
tests
:
if
args
and
name
not
in
args
:
print
(
"skipping test"
,
name
)
continue
print
(
"running test"
,
name
)
runtest
(
hier
,
code
)
# Test
import
sys
import
imp
try
:
import
sys.imp
except
ImportError
:
# This is what we expect
pass
else
:
raise
TestFailed
,
"No ImportError exception on 'import sys.imp'"
if
not
os
.
path
.
isdir
(
root
):
os
.
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
:
os
.
mkdir
(
fullname
)
else
:
f
=
open
(
fullname
,
"w"
)
f
.
write
(
contents
)
if
contents
and
contents
[
-
1
]
!=
'
\
n
'
:
f
.
write
(
'
\
n
'
)
f
.
close
()
self
.
root
=
root
def
test_1
(
self
):
hier
=
[(
"t1"
,
None
),
(
"t1 __init__.py"
,
""
)]
self
.
mkhier
(
hier
)
import
t1
def
test_2
(
self
):
hier
=
[
(
"t2"
,
None
),
(
"t2 __init__.py"
,
"'doc for t2'"
),
(
"t2 sub"
,
None
),
(
"t2 sub __init__.py"
,
""
),
(
"t2 sub subsub"
,
None
),
(
"t2 sub subsub __init__.py"
,
"spam = 1"
),
]
self
.
mkhier
(
hier
)
import
t2
self
.
assertEqual
(
t2
.
__doc__
,
"doc for t2"
)
import
t2.sub
import
t2.sub.subsub
self
.
assertEqual
(
t2
.
__name__
,
"t2"
)
self
.
assertEqual
(
t2
.
sub
.
__name__
,
"t2.sub"
)
self
.
assertEqual
(
t2
.
sub
.
subsub
.
__name__
,
"t2.sub.subsub"
)
# This exec crap is needed because Py3k forbids 'import *' outside
# of module-scope and __import__() is insufficient for what we need.
s
=
"""
import t2
from t2 import *
self.assertEqual(dir(), ['self', 'sub', 't2'])
"""
self
.
run_code
(
s
)
from
t2
import
sub
from
t2.sub
import
subsub
from
t2.sub.subsub
import
spam
self
.
assertEqual
(
sub
.
__name__
,
"t2.sub"
)
self
.
assertEqual
(
subsub
.
__name__
,
"t2.sub.subsub"
)
self
.
assertEqual
(
sub
.
subsub
.
__name__
,
"t2.sub.subsub"
)
for
name
in
[
'spam'
,
'sub'
,
'subsub'
,
't2'
]:
self
.
failUnless
(
locals
()[
"name"
],
"Failed to import %s"
%
name
)
import
t2.sub
import
t2.sub.subsub
self
.
assertEqual
(
t2
.
__name__
,
"t2"
)
self
.
assertEqual
(
t2
.
sub
.
__name__
,
"t2.sub"
)
self
.
assertEqual
(
t2
.
sub
.
subsub
.
__name__
,
"t2.sub.subsub"
)
s
=
"""
from t2 import *
self.failUnless(dir(), ['self', 'sub'])
"""
self
.
run_code
(
s
)
def
test_3
(
self
):
hier
=
[
(
"t3"
,
None
),
(
"t3 __init__.py"
,
""
),
(
"t3 sub"
,
None
),
(
"t3 sub __init__.py"
,
""
),
(
"t3 sub subsub"
,
None
),
(
"t3 sub subsub __init__.py"
,
"spam = 1"
),
]
self
.
mkhier
(
hier
)
import
t3.sub.subsub
self
.
assertEqual
(
t3
.
__name__
,
"t3"
)
self
.
assertEqual
(
t3
.
sub
.
__name__
,
"t3.sub"
)
self
.
assertEqual
(
t3
.
sub
.
subsub
.
__name__
,
"t3.sub.subsub"
)
def
test_4
(
self
):
hier
=
[
(
"t4.py"
,
"raise RuntimeError('Shouldnt load t4.py')"
),
(
"t4"
,
None
),
(
"t4 __init__.py"
,
""
),
(
"t4 sub.py"
,
"raise RuntimeError('Shouldnt load sub.py')"
),
(
"t4 sub"
,
None
),
(
"t4 sub __init__.py"
,
""
),
(
"t4 sub subsub.py"
,
"raise RuntimeError('Shouldnt load subsub.py')"
),
(
"t4 sub subsub"
,
None
),
(
"t4 sub subsub __init__.py"
,
"spam = 1"
),
]
self
.
mkhier
(
hier
)
s
=
"""
from t4.sub.subsub import *
self.assertEqual(spam, 1)
"""
self
.
run_code
(
s
)
def
test_5
(
self
):
hier
=
[
(
"t5"
,
None
),
(
"t5 __init__.py"
,
"import t5.foo"
),
(
"t5 string.py"
,
"spam = 1"
),
(
"t5 foo.py"
,
"from . import string; assert string.spam == 1"
),
]
self
.
mkhier
(
hier
)
import
t5
s
=
"""
from t5 import *
self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])
"""
self
.
run_code
(
s
)
import
t5
self
.
assertEqual
(
fixdir
(
dir
(
t5
)),
[
'__doc__'
,
'__file__'
,
'__name__'
,
'__path__'
,
'foo'
,
'string'
,
't5'
])
self
.
assertEqual
(
fixdir
(
dir
(
t5
.
foo
)),
[
'__doc__'
,
'__file__'
,
'__name__'
,
'string'
])
self
.
assertEqual
(
fixdir
(
dir
(
t5
.
string
)),
[
'__doc__'
,
'__file__'
,
'__name__'
,
'spam'
])
def
test_6
(
self
):
hier
=
[
(
"t6"
,
None
),
(
"t6 __init__.py"
,
"__all__ = ['spam', 'ham', 'eggs']"
),
(
"t6 spam.py"
,
""
),
(
"t6 ham.py"
,
""
),
(
"t6 eggs.py"
,
""
),
]
self
.
mkhier
(
hier
)
import
t6
self
.
assertEqual
(
fixdir
(
dir
(
t6
)),
[
'__all__'
,
'__doc__'
,
'__file__'
,
'__name__'
,
'__path__'
])
s
=
"""
import t6
from t6 import *
self.assertEqual(fixdir(dir(t6)),
['__all__', '__doc__', '__file__',
'__name__', '__path__', 'eggs',
'ham', 'spam'])
self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6'])
"""
self
.
run_code
(
s
)
def
test_7
(
self
):
hier
=
[
(
"t7.py"
,
""
),
(
"t7"
,
None
),
(
"t7 __init__.py"
,
""
),
(
"t7 sub.py"
,
"raise RuntimeError('Shouldnt load sub.py')"
),
(
"t7 sub"
,
None
),
(
"t7 sub __init__.py"
,
""
),
(
"t7 sub subsub.py"
,
"raise RuntimeError('Shouldnt load subsub.py')"
),
(
"t7 sub subsub"
,
None
),
(
"t7 sub subsub __init__.py"
,
"spam = 1"
),
]
self
.
mkhier
(
hier
)
t7
,
sub
,
subsub
=
None
,
None
,
None
import
t7
as
tas
self
.
assertEqual
(
fixdir
(
dir
(
tas
)),
[
'__doc__'
,
'__file__'
,
'__name__'
,
'__path__'
])
self
.
failIf
(
t7
)
from
t7
import
sub
as
subpar
self
.
assertEqual
(
fixdir
(
dir
(
subpar
)),
[
'__doc__'
,
'__file__'
,
'__name__'
,
'__path__'
])
self
.
failIf
(
t7
)
self
.
failIf
(
sub
)
from
t7.sub
import
subsub
as
subsubsub
self
.
assertEqual
(
fixdir
(
dir
(
subsubsub
)),
[
'__doc__'
,
'__file__'
,
'__name__'
,
'__path__'
,
'spam'
])
self
.
failIf
(
t7
)
self
.
failIf
(
sub
)
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