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 _MANIFEST = """\
     11 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     12 <assembly xmlns="urn:schemas-microsoft-com:asm.v1"
     13           manifestVersion="1.0">
     14   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
     15     <security>
     16       <requestedPrivileges>
     17         <requestedExecutionLevel level="asInvoker" uiAccess="false">
     18         </requestedExecutionLevel>
     19       </requestedPrivileges>
     20     </security>
     21   </trustInfo>
     22   <dependency>
     23     <dependentAssembly>
     24       <assemblyIdentity type="win32" name="Microsoft.VC90.CRT"
     25          version="9.0.21022.8" processorArchitecture="x86"
     26          publicKeyToken="XXXX">
     27       </assemblyIdentity>
     28     </dependentAssembly>
     29   </dependency>
     30   <dependency>
     31     <dependentAssembly>
     32       <assemblyIdentity type="win32" name="Microsoft.VC90.MFC"
     33         version="9.0.21022.8" processorArchitecture="x86"
     34         publicKeyToken="XXXX"></assemblyIdentity>
     35     </dependentAssembly>
     36   </dependency>
     37 </assembly>
     38 """
     39 
     40 _CLEANED_MANIFEST = """\
     41 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     42 <assembly xmlns="urn:schemas-microsoft-com:asm.v1"
     43           manifestVersion="1.0">
     44   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
     45     <security>
     46       <requestedPrivileges>
     47         <requestedExecutionLevel level="asInvoker" uiAccess="false">
     48         </requestedExecutionLevel>
     49       </requestedPrivileges>
     50     </security>
     51   </trustInfo>
     52   <dependency>
     53 
     54   </dependency>
     55   <dependency>
     56     <dependentAssembly>
     57       <assemblyIdentity type="win32" name="Microsoft.VC90.MFC"
     58         version="9.0.21022.8" processorArchitecture="x86"
     59         publicKeyToken="XXXX"></assemblyIdentity>
     60     </dependentAssembly>
     61   </dependency>
     62 </assembly>"""
     63 
     64 if sys.platform=="win32":
     65     from distutils.msvccompiler import get_build_version
     66     if get_build_version()>=8.0:
     67         SKIP_MESSAGE = None
     68     else:
     69         SKIP_MESSAGE = "These tests are only for MSVC8.0 or above"
     70 else:
     71     SKIP_MESSAGE = "These tests are only for win32"
     72 
     73 @unittest.skipUnless(SKIP_MESSAGE is None, SKIP_MESSAGE)
     74 class msvc9compilerTestCase(support.TempdirManager,
     75                             unittest.TestCase):
     76 
     77     def test_no_compiler(self):
     78         # makes sure query_vcvarsall throws

     79         # a DistutilsPlatformError if the compiler

     80         # is not found

     81         from distutils.msvc9compiler import query_vcvarsall
     82         def _find_vcvarsall(version):
     83             return None
     84 
     85         from distutils import msvc9compiler
     86         old_find_vcvarsall = msvc9compiler.find_vcvarsall
     87         msvc9compiler.find_vcvarsall = _find_vcvarsall
     88         try:
     89             self.assertRaises(DistutilsPlatformError, query_vcvarsall,
     90                              'wont find this version')
     91         finally:
     92             msvc9compiler.find_vcvarsall = old_find_vcvarsall
     93 
     94     def test_reg_class(self):
     95         from distutils.msvc9compiler import Reg
     96         self.assertRaises(KeyError, Reg.get_value, 'xxx', 'xxx')
     97 
     98         # looking for values that should exist on all

     99         # windows registeries versions.

    100         path = r'Control Panel\Desktop'
    101         v = Reg.get_value(path, u'dragfullwindows')
    102         self.assertTrue(v in (u'0', u'1', u'2'))
    103 
    104         import _winreg
    105         HKCU = _winreg.HKEY_CURRENT_USER
    106         keys = Reg.read_keys(HKCU, 'xxxx')
    107         self.assertEqual(keys, None)
    108 
    109         keys = Reg.read_keys(HKCU, r'Control Panel')
    110         self.assertTrue('Desktop' in keys)
    111 
    112     def test_remove_visual_c_ref(self):
    113         from distutils.msvc9compiler import MSVCCompiler
    114         tempdir = self.mkdtemp()
    115         manifest = os.path.join(tempdir, 'manifest')
    116         f = open(manifest, 'w')
    117         try:
    118             f.write(_MANIFEST)
    119         finally:
    120             f.close()
    121 
    122         compiler = MSVCCompiler()
    123         compiler._remove_visual_c_ref(manifest)
    124 
    125         # see what we got

    126         f = open(manifest)
    127         try:
    128             # removing trailing spaces

    129             content = '\n'.join([line.rstrip() for line in f.readlines()])
    130         finally:
    131             f.close()
    132 
    133         # makes sure the manifest was properly cleaned

    134         self.assertEqual(content, _CLEANED_MANIFEST)
    135 
    136 
    137 def test_suite():
    138     return unittest.makeSuite(msvc9compilerTestCase)
    139 
    140 if __name__ == "__main__":
    141     run_unittest(test_suite())
    142