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