comparison venv/lib/python2.7/site-packages/setuptools/tests/test_msvc9compiler.py @ 0:d67268158946 draft

planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author bcclaywell
date Mon, 12 Oct 2015 17:43:33 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d67268158946
1 """
2 Tests for msvc9compiler.
3 """
4
5 import os
6 import contextlib
7 import distutils.errors
8
9 import pytest
10 try:
11 from unittest import mock
12 except ImportError:
13 import mock
14
15 from . import contexts
16
17 # importing only setuptools should apply the patch
18 __import__('setuptools')
19
20 pytest.importorskip("distutils.msvc9compiler")
21
22
23 def mock_reg(hkcu=None, hklm=None):
24 """
25 Return a mock for distutils.msvc9compiler.Reg, patched
26 to mock out the functions that access the registry.
27 """
28
29 _winreg = getattr(distutils.msvc9compiler, '_winreg', None)
30 winreg = getattr(distutils.msvc9compiler, 'winreg', _winreg)
31
32 hives = {
33 winreg.HKEY_CURRENT_USER: hkcu or {},
34 winreg.HKEY_LOCAL_MACHINE: hklm or {},
35 }
36
37 @classmethod
38 def read_keys(cls, base, key):
39 """Return list of registry keys."""
40 hive = hives.get(base, {})
41 return [
42 k.rpartition('\\')[2]
43 for k in hive if k.startswith(key.lower())
44 ]
45
46 @classmethod
47 def read_values(cls, base, key):
48 """Return dict of registry keys and values."""
49 hive = hives.get(base, {})
50 return dict(
51 (k.rpartition('\\')[2], hive[k])
52 for k in hive if k.startswith(key.lower())
53 )
54
55 return mock.patch.multiple(distutils.msvc9compiler.Reg,
56 read_keys=read_keys, read_values=read_values)
57
58
59 class TestModulePatch:
60 """
61 Ensure that importing setuptools is sufficient to replace
62 the standard find_vcvarsall function with a version that
63 recognizes the "Visual C++ for Python" package.
64 """
65
66 key_32 = r'software\microsoft\devdiv\vcforpython\9.0\installdir'
67 key_64 = r'software\wow6432node\microsoft\devdiv\vcforpython\9.0\installdir'
68
69 def test_patched(self):
70 "Test the module is actually patched"
71 mod_name = distutils.msvc9compiler.find_vcvarsall.__module__
72 assert mod_name == "setuptools.msvc9_support", "find_vcvarsall unpatched"
73
74 def test_no_registry_entryies_means_nothing_found(self):
75 """
76 No registry entries or environment variable should lead to an error
77 directing the user to download vcpython27.
78 """
79 find_vcvarsall = distutils.msvc9compiler.find_vcvarsall
80 query_vcvarsall = distutils.msvc9compiler.query_vcvarsall
81
82 with contexts.environment(VS90COMNTOOLS=None):
83 with mock_reg():
84 assert find_vcvarsall(9.0) is None
85
86 expected = distutils.errors.DistutilsPlatformError
87 with pytest.raises(expected) as exc:
88 query_vcvarsall(9.0)
89 assert 'aka.ms/vcpython27' in str(exc)
90
91 @pytest.yield_fixture
92 def user_preferred_setting(self):
93 """
94 Set up environment with different install dirs for user vs. system
95 and yield the user_install_dir for the expected result.
96 """
97 with self.mock_install_dir() as user_install_dir:
98 with self.mock_install_dir() as system_install_dir:
99 reg = mock_reg(
100 hkcu={
101 self.key_32: user_install_dir,
102 },
103 hklm={
104 self.key_32: system_install_dir,
105 self.key_64: system_install_dir,
106 },
107 )
108 with reg:
109 yield user_install_dir
110
111 def test_prefer_current_user(self, user_preferred_setting):
112 """
113 Ensure user's settings are preferred.
114 """
115 result = distutils.msvc9compiler.find_vcvarsall(9.0)
116 expected = os.path.join(user_preferred_setting, 'vcvarsall.bat')
117 assert expected == result
118
119 @pytest.yield_fixture
120 def local_machine_setting(self):
121 """
122 Set up environment with only the system environment configured.
123 """
124 with self.mock_install_dir() as system_install_dir:
125 reg = mock_reg(
126 hklm={
127 self.key_32: system_install_dir,
128 },
129 )
130 with reg:
131 yield system_install_dir
132
133 def test_local_machine_recognized(self, local_machine_setting):
134 """
135 Ensure machine setting is honored if user settings are not present.
136 """
137 result = distutils.msvc9compiler.find_vcvarsall(9.0)
138 expected = os.path.join(local_machine_setting, 'vcvarsall.bat')
139 assert expected == result
140
141 @pytest.yield_fixture
142 def x64_preferred_setting(self):
143 """
144 Set up environment with 64-bit and 32-bit system settings configured
145 and yield the canonical location.
146 """
147 with self.mock_install_dir() as x32_dir:
148 with self.mock_install_dir() as x64_dir:
149 reg = mock_reg(
150 hklm={
151 # This *should* only exist on 32-bit machines
152 self.key_32: x32_dir,
153 # This *should* only exist on 64-bit machines
154 self.key_64: x64_dir,
155 },
156 )
157 with reg:
158 yield x32_dir
159
160 def test_ensure_64_bit_preferred(self, x64_preferred_setting):
161 """
162 Ensure 64-bit system key is preferred.
163 """
164 result = distutils.msvc9compiler.find_vcvarsall(9.0)
165 expected = os.path.join(x64_preferred_setting, 'vcvarsall.bat')
166 assert expected == result
167
168 @staticmethod
169 @contextlib.contextmanager
170 def mock_install_dir():
171 """
172 Make a mock install dir in a unique location so that tests can
173 distinguish which dir was detected in a given scenario.
174 """
175 with contexts.tempdir() as result:
176 vcvarsall = os.path.join(result, 'vcvarsall.bat')
177 with open(vcvarsall, 'w'):
178 pass
179 yield result