1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#!/usr/bin/python
import unittest
import doctest
import os
import sys
import tempfile
import shutil
import glob
import linecache
class RedirectToStdout(object):
"""A file-like object that prints to sys.stdout
A reason to use sys.stderr = RedirectToStdout() instead of assigning
sys.stderr = sys.stdout is when sys.stdout is later reassigned to a
different object (e.g. the StringIO that doctests use) and you want
sys.stderr to always refer to whatever sys.stdout is printing to.
Not all file methods are implemented, just the ones that were actually
needed.
"""
def write(self, msg):
sys.stdout.write(msg)
def setUp(test):
test.old_stderr = sys.stderr
sys.stderr = RedirectToStdout()
test.old_cwd = os.getcwd()
test.tempdir = tempfile.mkdtemp('findimports')
os.chdir(test.tempdir)
def tearDown(test):
sys.stderr = test.old_stderr
os.chdir(test.old_cwd)
shutil.rmtree(test.tempdir)
linecache.clearcache()
def additional_tests(): # hook for setuptools
# paths relative to __file__ don't work if you run 'figleaf testsuite.py'
# so we have to use paths relative to os.getcwd()
sample_tree = os.path.abspath(os.path.join('tests', 'sample-tree'))
globs = dict(sample_tree=sample_tree)
return unittest.TestSuite(
doctest.DocFileSuite(filename, setUp=setUp, tearDown=tearDown,
module_relative=False, globs=globs)
for filename in sorted(glob.glob('tests/*.txt')))
def main():
unittest.main(defaultTest='additional_tests')
if __name__ == '__main__':
main()
|