]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/distutils/tests/support.py
1 """Support code for distutils test cases."""
5 from copy
import deepcopy
8 from distutils
import log
9 from distutils
.log
import DEBUG
, INFO
, WARN
, ERROR
, FATAL
10 from distutils
.core
import Distribution
12 def capture_warnings(func
):
13 def _capture_warnings(*args
, **kw
):
14 with warnings
.catch_warnings():
15 warnings
.simplefilter("ignore")
16 return func(*args
, **kw
)
17 return _capture_warnings
19 class LoggingSilencer(object):
22 super(LoggingSilencer
, self
).setUp()
23 self
.threshold
= log
.set_threshold(log
.FATAL
)
25 # when log will be replaced by logging
26 # we won't need such monkey-patch anymore
27 self
._old
_log
= log
.Log
._log
28 log
.Log
._log
= self
._log
32 log
.set_threshold(self
.threshold
)
33 log
.Log
._log
= self
._old
_log
34 super(LoggingSilencer
, self
).tearDown()
36 def _log(self
, level
, msg
, args
):
37 if level
not in (DEBUG
, INFO
, WARN
, ERROR
, FATAL
):
38 raise ValueError('%s wrong log level' % str(level
))
39 self
.logs
.append((level
, msg
, args
))
41 def get_logs(self
, *levels
):
42 def _format(msg
, args
):
46 return [_format(msg
, args
) for level
, msg
, args
47 in self
.logs
if level
in levels
]
52 class TempdirManager(object):
53 """Mix-in class that handles temporary directories for test cases.
55 This is intended to be used with unittest.TestCase.
59 super(TempdirManager
, self
).setUp()
63 super(TempdirManager
, self
).tearDown()
65 d
= self
.tempdirs
.pop()
66 shutil
.rmtree(d
, os
.name
in ('nt', 'cygwin'))
69 """Create a temporary directory that will be cleaned up.
71 Returns the path of the directory.
73 d
= tempfile
.mkdtemp()
74 self
.tempdirs
.append(d
)
77 def write_file(self
, path
, content
='xxx'):
78 """Writes a file in the given path.
81 path can be a string or a sequence.
83 if isinstance(path
, (list, tuple)):
84 path
= os
.path
.join(*path
)
91 def create_dist(self
, pkg_name
='foo', **kw
):
92 """Will generate a test environment.
94 This function creates:
95 - a Distribution instance using keywords
96 - a temporary directory with a package structure
98 It returns the package directory and the distribution
101 tmp_dir
= self
.mkdtemp()
102 pkg_dir
= os
.path
.join(tmp_dir
, pkg_name
)
104 dist
= Distribution(attrs
=kw
)
109 """Class to store options for retrieval via set_undefined_options()."""
111 def __init__(self
, **kwargs
):
112 for kw
, val
in kwargs
.items():
113 setattr(self
, kw
, val
)
115 def ensure_finalized(self
):
118 class EnvironGuard(object):
121 super(EnvironGuard
, self
).setUp()
122 self
.old_environ
= deepcopy(os
.environ
)
125 for key
, value
in self
.old_environ
.items():
126 if os
.environ
.get(key
) != value
:
127 os
.environ
[key
] = value
129 for key
in os
.environ
.keys():
130 if key
not in self
.old_environ
:
133 super(EnvironGuard
, self
).tearDown()