ยปCore Development>Code coverage>Lib/distutils/tests/test_msvc9compiler.py

Python code coverage for Lib/distutils/tests/test_msvc9compiler.py

#countcontent
1n/a"""Tests for distutils.msvc9compiler."""
2n/aimport sys
3n/aimport unittest
4n/aimport os
5n/a
6n/afrom distutils.errors import DistutilsPlatformError
7n/afrom distutils.tests import support
8n/afrom test.support import run_unittest
9n/a
10n/a# A manifest with the only assembly reference being the msvcrt assembly, so
11n/a# should have the assembly completely stripped. Note that although the
12n/a# assembly has a <security> reference the assembly is removed - that is
13n/a# currently a "feature", not a bug :)
14n/a_MANIFEST_WITH_ONLY_MSVC_REFERENCE = """\
15n/a<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
16n/a<assembly xmlns="urn:schemas-microsoft-com:asm.v1"
17n/a manifestVersion="1.0">
18n/a <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
19n/a <security>
20n/a <requestedPrivileges>
21n/a <requestedExecutionLevel level="asInvoker" uiAccess="false">
22n/a </requestedExecutionLevel>
23n/a </requestedPrivileges>
24n/a </security>
25n/a </trustInfo>
26n/a <dependency>
27n/a <dependentAssembly>
28n/a <assemblyIdentity type="win32" name="Microsoft.VC90.CRT"
29n/a version="9.0.21022.8" processorArchitecture="x86"
30n/a publicKeyToken="XXXX">
31n/a </assemblyIdentity>
32n/a </dependentAssembly>
33n/a </dependency>
34n/a</assembly>
35n/a"""
36n/a
37n/a# A manifest with references to assemblies other than msvcrt. When processed,
38n/a# this assembly should be returned with just the msvcrt part removed.
39n/a_MANIFEST_WITH_MULTIPLE_REFERENCES = """\
40n/a<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
41n/a<assembly xmlns="urn:schemas-microsoft-com:asm.v1"
42n/a manifestVersion="1.0">
43n/a <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
44n/a <security>
45n/a <requestedPrivileges>
46n/a <requestedExecutionLevel level="asInvoker" uiAccess="false">
47n/a </requestedExecutionLevel>
48n/a </requestedPrivileges>
49n/a </security>
50n/a </trustInfo>
51n/a <dependency>
52n/a <dependentAssembly>
53n/a <assemblyIdentity type="win32" name="Microsoft.VC90.CRT"
54n/a version="9.0.21022.8" processorArchitecture="x86"
55n/a publicKeyToken="XXXX">
56n/a </assemblyIdentity>
57n/a </dependentAssembly>
58n/a </dependency>
59n/a <dependency>
60n/a <dependentAssembly>
61n/a <assemblyIdentity type="win32" name="Microsoft.VC90.MFC"
62n/a version="9.0.21022.8" processorArchitecture="x86"
63n/a publicKeyToken="XXXX"></assemblyIdentity>
64n/a </dependentAssembly>
65n/a </dependency>
66n/a</assembly>
67n/a"""
68n/a
69n/a_CLEANED_MANIFEST = """\
70n/a<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
71n/a<assembly xmlns="urn:schemas-microsoft-com:asm.v1"
72n/a manifestVersion="1.0">
73n/a <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
74n/a <security>
75n/a <requestedPrivileges>
76n/a <requestedExecutionLevel level="asInvoker" uiAccess="false">
77n/a </requestedExecutionLevel>
78n/a </requestedPrivileges>
79n/a </security>
80n/a </trustInfo>
81n/a <dependency>
82n/a
83n/a </dependency>
84n/a <dependency>
85n/a <dependentAssembly>
86n/a <assemblyIdentity type="win32" name="Microsoft.VC90.MFC"
87n/a version="9.0.21022.8" processorArchitecture="x86"
88n/a publicKeyToken="XXXX"></assemblyIdentity>
89n/a </dependentAssembly>
90n/a </dependency>
91n/a</assembly>"""
92n/a
93n/aif sys.platform=="win32":
94n/a from distutils.msvccompiler import get_build_version
95n/a if get_build_version()>=8.0:
96n/a SKIP_MESSAGE = None
97n/a else:
98n/a SKIP_MESSAGE = "These tests are only for MSVC8.0 or above"
99n/aelse:
100n/a SKIP_MESSAGE = "These tests are only for win32"
101n/a
102n/a@unittest.skipUnless(SKIP_MESSAGE is None, SKIP_MESSAGE)
103n/aclass msvc9compilerTestCase(support.TempdirManager,
104n/a unittest.TestCase):
105n/a
106n/a def test_no_compiler(self):
107n/a # makes sure query_vcvarsall raises
108n/a # a DistutilsPlatformError if the compiler
109n/a # is not found
110n/a from distutils.msvc9compiler import query_vcvarsall
111n/a def _find_vcvarsall(version):
112n/a return None
113n/a
114n/a from distutils import msvc9compiler
115n/a old_find_vcvarsall = msvc9compiler.find_vcvarsall
116n/a msvc9compiler.find_vcvarsall = _find_vcvarsall
117n/a try:
118n/a self.assertRaises(DistutilsPlatformError, query_vcvarsall,
119n/a 'wont find this version')
120n/a finally:
121n/a msvc9compiler.find_vcvarsall = old_find_vcvarsall
122n/a
123n/a def test_reg_class(self):
124n/a from distutils.msvc9compiler import Reg
125n/a self.assertRaises(KeyError, Reg.get_value, 'xxx', 'xxx')
126n/a
127n/a # looking for values that should exist on all
128n/a # windows registry versions.
129n/a path = r'Control Panel\Desktop'
130n/a v = Reg.get_value(path, 'dragfullwindows')
131n/a self.assertIn(v, ('0', '1', '2'))
132n/a
133n/a import winreg
134n/a HKCU = winreg.HKEY_CURRENT_USER
135n/a keys = Reg.read_keys(HKCU, 'xxxx')
136n/a self.assertEqual(keys, None)
137n/a
138n/a keys = Reg.read_keys(HKCU, r'Control Panel')
139n/a self.assertIn('Desktop', keys)
140n/a
141n/a def test_remove_visual_c_ref(self):
142n/a from distutils.msvc9compiler import MSVCCompiler
143n/a tempdir = self.mkdtemp()
144n/a manifest = os.path.join(tempdir, 'manifest')
145n/a f = open(manifest, 'w')
146n/a try:
147n/a f.write(_MANIFEST_WITH_MULTIPLE_REFERENCES)
148n/a finally:
149n/a f.close()
150n/a
151n/a compiler = MSVCCompiler()
152n/a compiler._remove_visual_c_ref(manifest)
153n/a
154n/a # see what we got
155n/a f = open(manifest)
156n/a try:
157n/a # removing trailing spaces
158n/a content = '\n'.join([line.rstrip() for line in f.readlines()])
159n/a finally:
160n/a f.close()
161n/a
162n/a # makes sure the manifest was properly cleaned
163n/a self.assertEqual(content, _CLEANED_MANIFEST)
164n/a
165n/a def test_remove_entire_manifest(self):
166n/a from distutils.msvc9compiler import MSVCCompiler
167n/a tempdir = self.mkdtemp()
168n/a manifest = os.path.join(tempdir, 'manifest')
169n/a f = open(manifest, 'w')
170n/a try:
171n/a f.write(_MANIFEST_WITH_ONLY_MSVC_REFERENCE)
172n/a finally:
173n/a f.close()
174n/a
175n/a compiler = MSVCCompiler()
176n/a got = compiler._remove_visual_c_ref(manifest)
177n/a self.assertIsNone(got)
178n/a
179n/a
180n/adef test_suite():
181n/a return unittest.makeSuite(msvc9compilerTestCase)
182n/a
183n/aif __name__ == "__main__":
184n/a run_unittest(test_suite())