1 | n/a | """Support code for distutils test cases.""" |
---|
2 | n/a | import os |
---|
3 | n/a | import sys |
---|
4 | n/a | import shutil |
---|
5 | n/a | import tempfile |
---|
6 | n/a | import unittest |
---|
7 | n/a | import sysconfig |
---|
8 | n/a | from copy import deepcopy |
---|
9 | n/a | |
---|
10 | n/a | from distutils import log |
---|
11 | n/a | from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL |
---|
12 | n/a | from distutils.core import Distribution |
---|
13 | n/a | |
---|
14 | n/a | |
---|
15 | n/a | class LoggingSilencer(object): |
---|
16 | n/a | |
---|
17 | n/a | def setUp(self): |
---|
18 | n/a | super().setUp() |
---|
19 | n/a | self.threshold = log.set_threshold(log.FATAL) |
---|
20 | n/a | # catching warnings |
---|
21 | n/a | # when log will be replaced by logging |
---|
22 | n/a | # we won't need such monkey-patch anymore |
---|
23 | n/a | self._old_log = log.Log._log |
---|
24 | n/a | log.Log._log = self._log |
---|
25 | n/a | self.logs = [] |
---|
26 | n/a | |
---|
27 | n/a | def tearDown(self): |
---|
28 | n/a | log.set_threshold(self.threshold) |
---|
29 | n/a | log.Log._log = self._old_log |
---|
30 | n/a | super().tearDown() |
---|
31 | n/a | |
---|
32 | n/a | def _log(self, level, msg, args): |
---|
33 | n/a | if level not in (DEBUG, INFO, WARN, ERROR, FATAL): |
---|
34 | n/a | raise ValueError('%s wrong log level' % str(level)) |
---|
35 | n/a | if not isinstance(msg, str): |
---|
36 | n/a | raise TypeError("msg should be str, not '%.200s'" |
---|
37 | n/a | % (type(msg).__name__)) |
---|
38 | n/a | self.logs.append((level, msg, args)) |
---|
39 | n/a | |
---|
40 | n/a | def get_logs(self, *levels): |
---|
41 | n/a | def _format(msg, args): |
---|
42 | n/a | return msg % args |
---|
43 | n/a | return [msg % args for level, msg, args |
---|
44 | n/a | in self.logs if level in levels] |
---|
45 | n/a | |
---|
46 | n/a | def clear_logs(self): |
---|
47 | n/a | self.logs = [] |
---|
48 | n/a | |
---|
49 | n/a | |
---|
50 | n/a | class TempdirManager(object): |
---|
51 | n/a | """Mix-in class that handles temporary directories for test cases. |
---|
52 | n/a | |
---|
53 | n/a | This is intended to be used with unittest.TestCase. |
---|
54 | n/a | """ |
---|
55 | n/a | |
---|
56 | n/a | def setUp(self): |
---|
57 | n/a | super().setUp() |
---|
58 | n/a | self.old_cwd = os.getcwd() |
---|
59 | n/a | self.tempdirs = [] |
---|
60 | n/a | |
---|
61 | n/a | def tearDown(self): |
---|
62 | n/a | # Restore working dir, for Solaris and derivatives, where rmdir() |
---|
63 | n/a | # on the current directory fails. |
---|
64 | n/a | os.chdir(self.old_cwd) |
---|
65 | n/a | super().tearDown() |
---|
66 | n/a | while self.tempdirs: |
---|
67 | n/a | d = self.tempdirs.pop() |
---|
68 | n/a | shutil.rmtree(d, os.name in ('nt', 'cygwin')) |
---|
69 | n/a | |
---|
70 | n/a | def mkdtemp(self): |
---|
71 | n/a | """Create a temporary directory that will be cleaned up. |
---|
72 | n/a | |
---|
73 | n/a | Returns the path of the directory. |
---|
74 | n/a | """ |
---|
75 | n/a | d = tempfile.mkdtemp() |
---|
76 | n/a | self.tempdirs.append(d) |
---|
77 | n/a | return d |
---|
78 | n/a | |
---|
79 | n/a | def write_file(self, path, content='xxx'): |
---|
80 | n/a | """Writes a file in the given path. |
---|
81 | n/a | |
---|
82 | n/a | |
---|
83 | n/a | path can be a string or a sequence. |
---|
84 | n/a | """ |
---|
85 | n/a | if isinstance(path, (list, tuple)): |
---|
86 | n/a | path = os.path.join(*path) |
---|
87 | n/a | f = open(path, 'w') |
---|
88 | n/a | try: |
---|
89 | n/a | f.write(content) |
---|
90 | n/a | finally: |
---|
91 | n/a | f.close() |
---|
92 | n/a | |
---|
93 | n/a | def create_dist(self, pkg_name='foo', **kw): |
---|
94 | n/a | """Will generate a test environment. |
---|
95 | n/a | |
---|
96 | n/a | This function creates: |
---|
97 | n/a | - a Distribution instance using keywords |
---|
98 | n/a | - a temporary directory with a package structure |
---|
99 | n/a | |
---|
100 | n/a | It returns the package directory and the distribution |
---|
101 | n/a | instance. |
---|
102 | n/a | """ |
---|
103 | n/a | tmp_dir = self.mkdtemp() |
---|
104 | n/a | pkg_dir = os.path.join(tmp_dir, pkg_name) |
---|
105 | n/a | os.mkdir(pkg_dir) |
---|
106 | n/a | dist = Distribution(attrs=kw) |
---|
107 | n/a | |
---|
108 | n/a | return pkg_dir, dist |
---|
109 | n/a | |
---|
110 | n/a | |
---|
111 | n/a | class DummyCommand: |
---|
112 | n/a | """Class to store options for retrieval via set_undefined_options().""" |
---|
113 | n/a | |
---|
114 | n/a | def __init__(self, **kwargs): |
---|
115 | n/a | for kw, val in kwargs.items(): |
---|
116 | n/a | setattr(self, kw, val) |
---|
117 | n/a | |
---|
118 | n/a | def ensure_finalized(self): |
---|
119 | n/a | pass |
---|
120 | n/a | |
---|
121 | n/a | |
---|
122 | n/a | class EnvironGuard(object): |
---|
123 | n/a | |
---|
124 | n/a | def setUp(self): |
---|
125 | n/a | super(EnvironGuard, self).setUp() |
---|
126 | n/a | self.old_environ = deepcopy(os.environ) |
---|
127 | n/a | |
---|
128 | n/a | def tearDown(self): |
---|
129 | n/a | for key, value in self.old_environ.items(): |
---|
130 | n/a | if os.environ.get(key) != value: |
---|
131 | n/a | os.environ[key] = value |
---|
132 | n/a | |
---|
133 | n/a | for key in tuple(os.environ.keys()): |
---|
134 | n/a | if key not in self.old_environ: |
---|
135 | n/a | del os.environ[key] |
---|
136 | n/a | |
---|
137 | n/a | super(EnvironGuard, self).tearDown() |
---|
138 | n/a | |
---|
139 | n/a | |
---|
140 | n/a | def copy_xxmodule_c(directory): |
---|
141 | n/a | """Helper for tests that need the xxmodule.c source file. |
---|
142 | n/a | |
---|
143 | n/a | Example use: |
---|
144 | n/a | |
---|
145 | n/a | def test_compile(self): |
---|
146 | n/a | copy_xxmodule_c(self.tmpdir) |
---|
147 | n/a | self.assertIn('xxmodule.c', os.listdir(self.tmpdir)) |
---|
148 | n/a | |
---|
149 | n/a | If the source file can be found, it will be copied to *directory*. If not, |
---|
150 | n/a | the test will be skipped. Errors during copy are not caught. |
---|
151 | n/a | """ |
---|
152 | n/a | filename = _get_xxmodule_path() |
---|
153 | n/a | if filename is None: |
---|
154 | n/a | raise unittest.SkipTest('cannot find xxmodule.c (test must run in ' |
---|
155 | n/a | 'the python build dir)') |
---|
156 | n/a | shutil.copy(filename, directory) |
---|
157 | n/a | |
---|
158 | n/a | |
---|
159 | n/a | def _get_xxmodule_path(): |
---|
160 | n/a | srcdir = sysconfig.get_config_var('srcdir') |
---|
161 | n/a | candidates = [ |
---|
162 | n/a | # use installed copy if available |
---|
163 | n/a | os.path.join(os.path.dirname(__file__), 'xxmodule.c'), |
---|
164 | n/a | # otherwise try using copy from build directory |
---|
165 | n/a | os.path.join(srcdir, 'Modules', 'xxmodule.c'), |
---|
166 | n/a | # srcdir mysteriously can be $srcdir/Lib/distutils/tests when |
---|
167 | n/a | # this file is run from its parent directory, so walk up the |
---|
168 | n/a | # tree to find the real srcdir |
---|
169 | n/a | os.path.join(srcdir, '..', '..', '..', 'Modules', 'xxmodule.c'), |
---|
170 | n/a | ] |
---|
171 | n/a | for path in candidates: |
---|
172 | n/a | if os.path.exists(path): |
---|
173 | n/a | return path |
---|
174 | n/a | |
---|
175 | n/a | |
---|
176 | n/a | def fixup_build_ext(cmd): |
---|
177 | n/a | """Function needed to make build_ext tests pass. |
---|
178 | n/a | |
---|
179 | n/a | When Python was built with --enable-shared on Unix, -L. is not enough to |
---|
180 | n/a | find libpython<blah>.so, because regrtest runs in a tempdir, not in the |
---|
181 | n/a | source directory where the .so lives. |
---|
182 | n/a | |
---|
183 | n/a | When Python was built with in debug mode on Windows, build_ext commands |
---|
184 | n/a | need their debug attribute set, and it is not done automatically for |
---|
185 | n/a | some reason. |
---|
186 | n/a | |
---|
187 | n/a | This function handles both of these things. Example use: |
---|
188 | n/a | |
---|
189 | n/a | cmd = build_ext(dist) |
---|
190 | n/a | support.fixup_build_ext(cmd) |
---|
191 | n/a | cmd.ensure_finalized() |
---|
192 | n/a | |
---|
193 | n/a | Unlike most other Unix platforms, Mac OS X embeds absolute paths |
---|
194 | n/a | to shared libraries into executables, so the fixup is not needed there. |
---|
195 | n/a | """ |
---|
196 | n/a | if os.name == 'nt': |
---|
197 | n/a | cmd.debug = sys.executable.endswith('_d.exe') |
---|
198 | n/a | elif sysconfig.get_config_var('Py_ENABLE_SHARED'): |
---|
199 | n/a | # To further add to the shared builds fun on Unix, we can't just add |
---|
200 | n/a | # library_dirs to the Extension() instance because that doesn't get |
---|
201 | n/a | # plumbed through to the final compiler command. |
---|
202 | n/a | runshared = sysconfig.get_config_var('RUNSHARED') |
---|
203 | n/a | if runshared is None: |
---|
204 | n/a | cmd.library_dirs = ['.'] |
---|
205 | n/a | else: |
---|
206 | n/a | if sys.platform == 'darwin': |
---|
207 | n/a | cmd.library_dirs = [] |
---|
208 | n/a | else: |
---|
209 | n/a | name, equals, value = runshared.partition('=') |
---|
210 | n/a | cmd.library_dirs = [d for d in value.split(os.pathsep) if d] |
---|