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