annotate Lib/distutils/tests/support.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 41cabdff2686
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32365
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 """Support code for distutils test cases."""
57844
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
2 import os
72088
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
3 import sys
32365
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4 import shutil
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5 import tempfile
72088
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
6 import unittest
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
7 import sysconfig
56584
e67eb9660041 Changed distutils tests to avoid environment alteration
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56161
diff changeset
8 from copy import deepcopy
58450
fa69e891edf4 taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56584
diff changeset
9 import warnings
32365
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10
32943
9fb5138f58dd make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents: 32365
diff changeset
11 from distutils import log
56161
f8e696e4cff1 forgot to commit a file in previous commit (r74994, issue #6954)
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54110
diff changeset
12 from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL
52582
2b9c91668777 added tests for the clean command
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51667
diff changeset
13 from distutils.core import Distribution
32943
9fb5138f58dd make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents: 32365
diff changeset
14
72088
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
15
58450
fa69e891edf4 taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56584
diff changeset
16 def capture_warnings(func):
fa69e891edf4 taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56584
diff changeset
17 def _capture_warnings(*args, **kw):
fa69e891edf4 taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56584
diff changeset
18 with warnings.catch_warnings():
fa69e891edf4 taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56584
diff changeset
19 warnings.simplefilter("ignore")
fa69e891edf4 taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56584
diff changeset
20 return func(*args, **kw)
fa69e891edf4 taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56584
diff changeset
21 return _capture_warnings
32943
9fb5138f58dd make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents: 32365
diff changeset
22
72088
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
23
32943
9fb5138f58dd make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents: 32365
diff changeset
24 class LoggingSilencer(object):
9fb5138f58dd make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents: 32365
diff changeset
25
9fb5138f58dd make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents: 32365
diff changeset
26 def setUp(self):
9fb5138f58dd make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents: 32365
diff changeset
27 super(LoggingSilencer, self).setUp()
9fb5138f58dd make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents: 32365
diff changeset
28 self.threshold = log.set_threshold(log.FATAL)
58205
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
29 # catching warnings
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
30 # when log will be replaced by logging
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
31 # we won't need such monkey-patch anymore
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
32 self._old_log = log.Log._log
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
33 log.Log._log = self._log
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
34 self.logs = []
32943
9fb5138f58dd make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents: 32365
diff changeset
35
9fb5138f58dd make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents: 32365
diff changeset
36 def tearDown(self):
9fb5138f58dd make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents: 32365
diff changeset
37 log.set_threshold(self.threshold)
58205
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
38 log.Log._log = self._old_log
32943
9fb5138f58dd make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents: 32365
diff changeset
39 super(LoggingSilencer, self).tearDown()
9fb5138f58dd make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents: 32365
diff changeset
40
58205
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
41 def _log(self, level, msg, args):
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
42 if level not in (DEBUG, INFO, WARN, ERROR, FATAL):
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
43 raise ValueError('%s wrong log level' % str(level))
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
44 self.logs.append((level, msg, args))
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
45
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
46 def get_logs(self, *levels):
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
47 def _format(msg, args):
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
48 if len(args) == 0:
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
49 return msg
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
50 return msg % args
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
51 return [_format(msg, args) for level, msg, args
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
52 in self.logs if level in levels]
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
53
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
54 def clear_logs(self):
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
55 self.logs = []
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57844
diff changeset
56
72088
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
57
32365
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58 class TempdirManager(object):
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 """Mix-in class that handles temporary directories for test cases.
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61 This is intended to be used with unittest.TestCase.
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
62 """
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64 def setUp(self):
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 super(TempdirManager, self).setUp()
72316
a74483c12740 Issue #12333: fix test_distutils failures under Solaris and derivatives. Patch by Antoine Pitrou
Jesus Cea <jcea@jcea.es>
parents: 72095
diff changeset
66 self.old_cwd = os.getcwd()
32365
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 self.tempdirs = []
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
69 def tearDown(self):
72316
a74483c12740 Issue #12333: fix test_distutils failures under Solaris and derivatives. Patch by Antoine Pitrou
Jesus Cea <jcea@jcea.es>
parents: 72095
diff changeset
70 # Restore working dir, for Solaris and derivatives, where rmdir()
a74483c12740 Issue #12333: fix test_distutils failures under Solaris and derivatives. Patch by Antoine Pitrou
Jesus Cea <jcea@jcea.es>
parents: 72095
diff changeset
71 # on the current directory fails.
a74483c12740 Issue #12333: fix test_distutils failures under Solaris and derivatives. Patch by Antoine Pitrou
Jesus Cea <jcea@jcea.es>
parents: 72095
diff changeset
72 os.chdir(self.old_cwd)
32365
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73 super(TempdirManager, self).tearDown()
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
74 while self.tempdirs:
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
75 d = self.tempdirs.pop()
51626
e5522875b98c Fix for #5257: refactored all tests in distutils, so they use a temporary directory.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 32943
diff changeset
76 shutil.rmtree(d, os.name in ('nt', 'cygwin'))
32365
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
77
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78 def mkdtemp(self):
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
79 """Create a temporary directory that will be cleaned up.
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
80
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81 Returns the path of the directory.
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82 """
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
83 d = tempfile.mkdtemp()
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
84 self.tempdirs.append(d)
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
85 return d
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86
54022
15a8f4f2bc84 Merged revisions 72585 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 32943
diff changeset
87 def write_file(self, path, content='xxx'):
15a8f4f2bc84 Merged revisions 72585 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 32943
diff changeset
88 """Writes a file in the given path.
15a8f4f2bc84 Merged revisions 72585 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 32943
diff changeset
89
51667
982f9e2925a4 Fixed #2279: distutils.sdist.add_defaults now add files listed in package_data and data_files
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51626
diff changeset
90
54022
15a8f4f2bc84 Merged revisions 72585 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 32943
diff changeset
91 path can be a string or a sequence.
15a8f4f2bc84 Merged revisions 72585 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 32943
diff changeset
92 """
15a8f4f2bc84 Merged revisions 72585 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 32943
diff changeset
93 if isinstance(path, (list, tuple)):
15a8f4f2bc84 Merged revisions 72585 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 32943
diff changeset
94 path = os.path.join(*path)
15a8f4f2bc84 Merged revisions 72585 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 32943
diff changeset
95 f = open(path, 'w')
15a8f4f2bc84 Merged revisions 72585 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 32943
diff changeset
96 try:
15a8f4f2bc84 Merged revisions 72585 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 32943
diff changeset
97 f.write(content)
15a8f4f2bc84 Merged revisions 72585 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 32943
diff changeset
98 finally:
15a8f4f2bc84 Merged revisions 72585 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 32943
diff changeset
99 f.close()
32365
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
100
57844
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
101 def create_dist(self, pkg_name='foo', **kw):
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
102 """Will generate a test environment.
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
103
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
104 This function creates:
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
105 - a Distribution instance using keywords
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
106 - a temporary directory with a package structure
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
107
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
108 It returns the package directory and the distribution
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
109 instance.
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
110 """
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
111 tmp_dir = self.mkdtemp()
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
112 pkg_dir = os.path.join(tmp_dir, pkg_name)
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
113 os.mkdir(pkg_dir)
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
114 dist = Distribution(attrs=kw)
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
115
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
116 return pkg_dir, dist
1772195405e7 Merged revisions 76952 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54022
diff changeset
117
72088
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
118
32365
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
119 class DummyCommand:
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 """Class to store options for retrieval via set_undefined_options()."""
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
122 def __init__(self, **kwargs):
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 for kw, val in kwargs.items():
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
124 setattr(self, kw, val)
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
125
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
126 def ensure_finalized(self):
331e5d8b296f move support code to a helper module to ease re-use
Fred Drake <fdrake@acm.org>
parents:
diff changeset
127 pass
53976
1bec64b8dd17 refactored test_sysconfig so it uses test.test_support.EnvironmentVarGuard
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 52582
diff changeset
128
72088
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
129
53976
1bec64b8dd17 refactored test_sysconfig so it uses test.test_support.EnvironmentVarGuard
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 52582
diff changeset
130 class EnvironGuard(object):
1bec64b8dd17 refactored test_sysconfig so it uses test.test_support.EnvironmentVarGuard
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 52582
diff changeset
131
1bec64b8dd17 refactored test_sysconfig so it uses test.test_support.EnvironmentVarGuard
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 52582
diff changeset
132 def setUp(self):
1bec64b8dd17 refactored test_sysconfig so it uses test.test_support.EnvironmentVarGuard
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 52582
diff changeset
133 super(EnvironGuard, self).setUp()
56584
e67eb9660041 Changed distutils tests to avoid environment alteration
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56161
diff changeset
134 self.old_environ = deepcopy(os.environ)
53976
1bec64b8dd17 refactored test_sysconfig so it uses test.test_support.EnvironmentVarGuard
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 52582
diff changeset
135
1bec64b8dd17 refactored test_sysconfig so it uses test.test_support.EnvironmentVarGuard
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 52582
diff changeset
136 def tearDown(self):
56584
e67eb9660041 Changed distutils tests to avoid environment alteration
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56161
diff changeset
137 for key, value in self.old_environ.items():
e67eb9660041 Changed distutils tests to avoid environment alteration
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56161
diff changeset
138 if os.environ.get(key) != value:
e67eb9660041 Changed distutils tests to avoid environment alteration
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56161
diff changeset
139 os.environ[key] = value
e67eb9660041 Changed distutils tests to avoid environment alteration
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56161
diff changeset
140
e67eb9660041 Changed distutils tests to avoid environment alteration
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56161
diff changeset
141 for key in os.environ.keys():
e67eb9660041 Changed distutils tests to avoid environment alteration
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56161
diff changeset
142 if key not in self.old_environ:
e67eb9660041 Changed distutils tests to avoid environment alteration
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56161
diff changeset
143 del os.environ[key]
e67eb9660041 Changed distutils tests to avoid environment alteration
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56161
diff changeset
144
53976
1bec64b8dd17 refactored test_sysconfig so it uses test.test_support.EnvironmentVarGuard
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 52582
diff changeset
145 super(EnvironGuard, self).tearDown()
72088
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
146
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
147
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
148 def copy_xxmodule_c(directory):
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
149 """Helper for tests that need the xxmodule.c source file.
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
150
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
151 Example use:
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
152
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
153 def test_compile(self):
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
154 copy_xxmodule_c(self.tmpdir)
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
155 self.assertIn('xxmodule.c', os.listdir(self.tmpdir))
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
156
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
157 If the source file can be found, it will be copied to *directory*. If not,
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
158 the test will be skipped. Errors during copy are not caught.
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
159 """
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
160 filename = _get_xxmodule_path()
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
161 if filename is None:
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
162 raise unittest.SkipTest('cannot find xxmodule.c (test must run in '
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
163 'the python build dir)')
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
164 shutil.copy(filename, directory)
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
165
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
166
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
167 def _get_xxmodule_path():
72095
6e6d12736b3b Add FIXME note as a reminder
Éric Araujo <merwok@netwok.org>
parents: 72088
diff changeset
168 # FIXME when run from regrtest, srcdir seems to be '.', which does not help
6e6d12736b3b Add FIXME note as a reminder
Éric Araujo <merwok@netwok.org>
parents: 72088
diff changeset
169 # us find the xxmodule.c file
72088
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
170 srcdir = sysconfig.get_config_var('srcdir')
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
171 candidates = [
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
172 # use installed copy if available
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
173 os.path.join(os.path.dirname(__file__), 'xxmodule.c'),
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
174 # otherwise try using copy from build directory
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
175 os.path.join(srcdir, 'Modules', 'xxmodule.c'),
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
176 # srcdir mysteriously can be $srcdir/Lib/distutils/tests when
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
177 # this file is run from its parent directory, so walk up the
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
178 # tree to find the real srcdir
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
179 os.path.join(srcdir, '..', '..', '..', 'Modules', 'xxmodule.c'),
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
180 ]
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
181 for path in candidates:
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
182 if os.path.exists(path):
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
183 return path
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
184
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
185
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
186 def fixup_build_ext(cmd):
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
187 """Function needed to make build_ext tests pass.
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
188
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
189 When Python was build with --enable-shared on Unix, -L. is not good
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
190 enough to find the libpython<blah>.so. This is because regrtest runs
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
191 it under a tempdir, not in the top level where the .so lives. By the
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
192 time we've gotten here, Python's already been chdir'd to the tempdir.
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
193
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
194 When Python was built with in debug mode on Windows, build_ext commands
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
195 need their debug attribute set, and it is not done automatically for
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
196 some reason.
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
197
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
198 This function handles both of these things. Example use:
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
199
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
200 cmd = build_ext(dist)
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
201 support.fixup_build_ext(cmd)
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
202 cmd.ensure_finalized()
74737
41cabdff2686 Issue #13901: Prevent test_distutils failures on OS X with --enable-shared.
Ned Deily <nad@acm.org>
parents: 72316
diff changeset
203
41cabdff2686 Issue #13901: Prevent test_distutils failures on OS X with --enable-shared.
Ned Deily <nad@acm.org>
parents: 72316
diff changeset
204 Unlike most other Unix platforms, Mac OS X embeds absolute paths
41cabdff2686 Issue #13901: Prevent test_distutils failures on OS X with --enable-shared.
Ned Deily <nad@acm.org>
parents: 72316
diff changeset
205 to shared libraries into executables, so the fixup is not needed there.
72088
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
206 """
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
207 if os.name == 'nt':
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
208 cmd.debug = sys.executable.endswith('_d.exe')
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
209 elif sysconfig.get_config_var('Py_ENABLE_SHARED'):
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
210 # To further add to the shared builds fun on Unix, we can't just add
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
211 # library_dirs to the Extension() instance because that doesn't get
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
212 # plumbed through to the final compiler command.
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
213 runshared = sysconfig.get_config_var('RUNSHARED')
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
214 if runshared is None:
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
215 cmd.library_dirs = ['.']
3168c2448d77 Refactor helpers for compiling the xx module in distutils tests.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
216 else:
74737
41cabdff2686 Issue #13901: Prevent test_distutils failures on OS X with --enable-shared.
Ned Deily <nad@acm.org>
parents: 72316
diff changeset
217 if sys.platform == 'darwin':
41cabdff2686 Issue #13901: Prevent test_distutils failures on OS X with --enable-shared.
Ned Deily <nad@acm.org>
parents: 72316
diff changeset
218 cmd.library_dirs = []
41cabdff2686 Issue #13901: Prevent test_distutils failures on OS X with --enable-shared.
Ned Deily <nad@acm.org>
parents: 72316
diff changeset
219 else:
41cabdff2686 Issue #13901: Prevent test_distutils failures on OS X with --enable-shared.
Ned Deily <nad@acm.org>
parents: 72316
diff changeset
220 name, equals, value = runshared.partition('=')
41cabdff2686 Issue #13901: Prevent test_distutils failures on OS X with --enable-shared.
Ned Deily <nad@acm.org>
parents: 72316
diff changeset
221 cmd.library_dirs = value.split(os.pathsep)