test_msvc9compiler.py 5.9 KB

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