blob: 1255413989ec87d4132d0a65f5a90fb1e082cb90 [file] [log] [blame]
Fred Drakeb8ab8b62004-06-17 20:14:50 +00001"""Support code for distutils test cases."""
Tarek Ziadéc1375d52009-02-14 14:35:51 +00002import os
Fred Drakeb8ab8b62004-06-17 20:14:50 +00003import shutil
4import tempfile
5
Fred Drakeedcac8f2004-08-03 18:53:07 +00006from distutils import log
Tarek Ziadébaf51802009-03-31 21:37:16 +00007from distutils.core import Distribution
Tarek Ziadé29bbb962009-05-10 12:02:35 +00008from test.support import EnvironmentVarGuard
Fred Drakeedcac8f2004-08-03 18:53:07 +00009
10class LoggingSilencer(object):
11
12 def setUp(self):
Guido van Rossumcd16bf62007-06-13 18:07:49 +000013 super().setUp()
Fred Drakeedcac8f2004-08-03 18:53:07 +000014 self.threshold = log.set_threshold(log.FATAL)
Tarek Ziadé5af55c62009-05-16 16:52:13 +000015 # catching warnings
16 # when log will be replaced by logging
17 # we won't need such monkey-patch anymore
18 self._old_log = log.Log._log
19 log.Log._log = self._log
20 self.logs = []
Fred Drakeedcac8f2004-08-03 18:53:07 +000021
22 def tearDown(self):
23 log.set_threshold(self.threshold)
Tarek Ziadé5af55c62009-05-16 16:52:13 +000024 log.Log._log = self._old_log
Guido van Rossumcd16bf62007-06-13 18:07:49 +000025 super().tearDown()
Fred Drakeedcac8f2004-08-03 18:53:07 +000026
Tarek Ziadé5af55c62009-05-16 16:52:13 +000027 def _log(self, level, msg, args):
28 self.logs.append((level, msg, args))
29
30 def get_logs(self, *levels):
31 def _format(msg, args):
32 if len(args) == 0:
33 return msg
34 return msg % args
35 return [_format(msg, args) for level, msg, args
36 in self.logs if level in levels]
37
38 def clear_logs(self):
39 self.logs = []
Fred Drakeb8ab8b62004-06-17 20:14:50 +000040
41class TempdirManager(object):
42 """Mix-in class that handles temporary directories for test cases.
43
44 This is intended to be used with unittest.TestCase.
45 """
46
47 def setUp(self):
Guido van Rossumcd16bf62007-06-13 18:07:49 +000048 super().setUp()
Fred Drakeb8ab8b62004-06-17 20:14:50 +000049 self.tempdirs = []
50
51 def tearDown(self):
Guido van Rossumcd16bf62007-06-13 18:07:49 +000052 super().tearDown()
Fred Drakeb8ab8b62004-06-17 20:14:50 +000053 while self.tempdirs:
54 d = self.tempdirs.pop()
Tarek Ziadéc1375d52009-02-14 14:35:51 +000055 shutil.rmtree(d, os.name in ('nt', 'cygwin'))
Fred Drakeb8ab8b62004-06-17 20:14:50 +000056
57 def mkdtemp(self):
58 """Create a temporary directory that will be cleaned up.
59
60 Returns the path of the directory.
61 """
62 d = tempfile.mkdtemp()
63 self.tempdirs.append(d)
64 return d
65
Tarek Ziadébaf51802009-03-31 21:37:16 +000066 def write_file(self, path, content='xxx'):
Tarek Ziadé0d0506e2009-02-16 21:49:12 +000067 """Writes a file in the given path.
68
69
70 path can be a string or a sequence.
71 """
72 if isinstance(path, (list, tuple)):
73 path = os.path.join(*path)
74 f = open(path, 'w')
75 try:
76 f.write(content)
77 finally:
78 f.close()
Fred Drakeb8ab8b62004-06-17 20:14:50 +000079
Tarek Ziadébaf51802009-03-31 21:37:16 +000080 def create_dist(self, pkg_name='foo', **kw):
81 """Will generate a test environment.
82
83 This function creates:
84 - a Distribution instance using keywords
85 - a temporary directory with a package structure
86
87 It returns the package directory and the distribution
88 instance.
89 """
90 tmp_dir = self.mkdtemp()
91 pkg_dir = os.path.join(tmp_dir, pkg_name)
92 os.mkdir(pkg_dir)
93 dist = Distribution(attrs=kw)
94
95 return pkg_dir, dist
96
Fred Drakeb8ab8b62004-06-17 20:14:50 +000097class DummyCommand:
98 """Class to store options for retrieval via set_undefined_options()."""
99
100 def __init__(self, **kwargs):
101 for kw, val in kwargs.items():
102 setattr(self, kw, val)
103
104 def ensure_finalized(self):
105 pass
Tarek Ziadé29bbb962009-05-10 12:02:35 +0000106
107class EnvironGuard(object):
108
109 def setUp(self):
110 super(EnvironGuard, self).setUp()
111 self.environ = EnvironmentVarGuard()
112
113 def tearDown(self):
114 self.environ.__exit__()
115 super(EnvironGuard, self).tearDown()