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