summaryrefslogtreecommitdiff
path: root/tests/scanner/test_ccompiler.py
blob: 9e7a62a21ba452710f3f3e5328e043aa15f02239 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import unittest
import os
import sys

from distutils.cygwinccompiler import Mingw32CCompiler
from distutils.unixccompiler import UnixCCompiler
from distutils.msvccompiler import MSVCCompiler

os.environ['GI_SCANNER_DISABLE_CACHE'] = '1'
path = os.getenv('UNINSTALLED_INTROSPECTION_SRCDIR', None)
assert path is not None
sys.path.insert(0, path)

from giscanner.ccompiler import CCompiler


class TestCompiler(unittest.TestCase):

    def check_windows_default_compiler(self, compiler):
        if os.environ.get('MSYSTEM') == 'MINGW32':
            return isinstance(compiler.compiler, Mingw32CCompiler)
        else:
            return isinstance(compiler.compiler, MSVCCompiler)

    def check_default_compiler(self, compiler, osname=None):
        # If osname is defined and is not 'nt', we always return the default
        # compiler given for the running platform.  Otherwise, on Windows,
        # we see for the presence of MSYSTEM == MINGW32 to see we create a
        # Mingw32CCompiler or MSVCCompiler instance
        if os.name == 'nt':
            if osname is None or osname == os.name:
                return self.check_windows_default_compiler(compiler)
            else:
                return isinstance(compiler.compiler, MSVCCompiler)
        else:
            return (isinstance(compiler.compiler, UnixCCompiler) and
                    not isinstance(compiler.compiler, Mingw32CCompiler))

    def test_compiler_creation(self):
        osname = 'nt'
        # Test distutils compiler detection when forcing osname = 'nt'
        compiler = CCompiler(osname=osname, environ={'MSYSTEM': 'MINGW32'})
        self.assertTrue(isinstance(compiler.compiler, Mingw32CCompiler))

        compiler = CCompiler(osname=osname, compiler_name='mingw32')
        self.assertTrue(isinstance(compiler.compiler, Mingw32CCompiler))

        compiler = CCompiler(osname=osname, compiler_name='msvc')
        self.assertTrue(isinstance(compiler.compiler, MSVCCompiler))

        with self.assertRaises(SystemExit):
            CCompiler(osname=osname, compiler_name='bad_compiler_name')

        if os.name == 'nt':
            compiler = CCompiler(osname=osname)
            self.assertTrue(self.check_windows_default_compiler(compiler))
        else:
            with self.assertRaises(SystemExit):
                CCompiler(osname=osname, compiler_name='bad_compiler_name')

        # Test distutils compiler detection when forcing osname != 'nt'
        osname = 'posix'
        compiler = CCompiler(osname=osname, environ={'MSYSTEM': 'MINGW32'})
        self.assertTrue(self.check_default_compiler(compiler, osname))

        compiler = CCompiler(osname=osname, compiler_name='mingw32')
        self.assertTrue(self.check_default_compiler(compiler, osname))

        compiler = CCompiler(osname=osname, compiler_name='msvc')
        self.assertTrue(self.check_default_compiler(compiler, osname))

        compiler = CCompiler(osname=osname, compiler_name='blah')
        self.assertTrue(self.check_default_compiler(compiler, osname))

        # Test default distutils compiler with ccompiler.py
        compiler = CCompiler()
        self.assertTrue(self.check_default_compiler(compiler))

if __name__ == '__main__':
    unittest.main()