http://scons.tigris.org/issues/show_bug.cgi?id=2329
[scons.git] / src / engine / SCons / SConfTests.py
index 25c89d2afea91f4d043964525cc40cda5d8ef7d3..26f21f398b00b5e5ba82a01fb6ff12cdfa53547a 100644 (file)
@@ -25,7 +25,6 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
 import os
 import re
-import string
 import StringIO
 import sys
 from types import *
@@ -60,9 +59,9 @@ class SConfTestCase(unittest.TestCase):
         import SCons.SConsign
         SCons.SConsign.write() # simulate normal scons-finish
         for n in sys.modules.keys():
-            if string.split(n, '.')[0] == 'SCons' and n[:12] != 'SCons.compat':
+            if n.split('.')[0] == 'SCons' and n[:12] != 'SCons.compat':
                 m = sys.modules[n]
-                if type(m) is ModuleType:
+                if isinstance(m, ModuleType):
                     # if this is really a scons module, clear its namespace
                     del sys.modules[n]
                     m.__dict__.clear()
@@ -88,7 +87,21 @@ class SConfTestCase(unittest.TestCase):
         if self.scons_env['CXX'] == 'g++':
             global existing_lib
             existing_lib = 'm'
-        
+
+        if sys.platform in ['cygwin', 'win32']:
+             # On Windows, SCons.Platform.win32 redefines the builtin
+             # file() and open() functions to close the file handles.
+             # This interferes with the unittest.py infrastructure in
+             # some way.  Just sidestep the issue by restoring the
+             # original builtin functions whenever we have to reset
+             # all of our global state.
+
+             import __builtin__
+             import SCons.Platform.win32
+
+             __builtin__.file = SCons.Platform.win32._builtin_file
+             __builtin__.open = SCons.Platform.win32._builtin_open
+
     def _baseTryXXX(self, TryFunc):
         # TryCompile and TryLink are much the same, so we can test them
         # in one method, we pass the function as a string ('TryCompile',
@@ -127,9 +140,9 @@ class SConfTestCase(unittest.TestCase):
         log = self.test.read( self.test.workpath('config.log') )
         expr = re.compile( ".*failed in a previous run and all", re.DOTALL ) 
         firstOcc = expr.match( log )
-        assert firstOcc != None, log
+        assert firstOcc is not None, log
         secondOcc = expr.match( log, firstOcc.end(0) )
-        assert secondOcc == None, log
+        assert secondOcc is None, log
 
         # 2.2 test the error caching mechanism (dependencies have changed)
         self._resetSConfState()
@@ -154,7 +167,8 @@ class SConfTestCase(unittest.TestCase):
         sconf = self.SConf.SConf(self.scons_env,
                                  conf_dir=self.test.workpath('config.tests'),
                                  log_file=self.test.workpath('config.log'))
-        class MyBuilder:
+        import SCons.Builder
+        class MyBuilder(SCons.Builder.BuilderBase):
             def __init__(self):
                 self.prefix = ''
                 self.suffix = ''
@@ -193,6 +207,8 @@ class SConfTestCase(unittest.TestCase):
                         return None
                     def prepare(self):
                         pass
+                    def push_to_cache(self):
+                        pass
                     def retrieve_from_cache(self):
                         return 0
                     def build(self, **kw):
@@ -205,10 +221,11 @@ class SConfTestCase(unittest.TestCase):
                         pass
                     def get_executor(self):
                         class Executor:
-                            pass
-                        e = Executor()
-                        e.targets = [self]
-                        return e
+                            def __init__(self, targets):
+                                self.targets = targets
+                            def get_all_targets(self):
+                                return self.targets
+                        return Executor([self])
                 return [MyNode('n1'), MyNode('n2')]
         try:
             self.scons_env.Append(BUILDERS = {'SConfActionBuilder' : MyBuilder()})
@@ -268,9 +285,9 @@ int main() {
         log = self.test.read( self.test.workpath('config.log') )
         expr = re.compile( ".*failed in a previous run and all", re.DOTALL )
         firstOcc = expr.match( log )
-        assert firstOcc != None, log
+        assert firstOcc is not None, log
         secondOcc = expr.match( log, firstOcc.end(0) )
-        assert secondOcc == None, log
+        assert secondOcc is None, log
 
 
     def test_TryAction(self):
@@ -293,6 +310,100 @@ int main() {
         finally:
             sconf.Finish()
 
+    def _test_check_compilers(self, comp, func, name):
+        """This is the implementation for CheckCC and CheckCXX tests."""
+        from copy import deepcopy
+
+        # Check that Check* works
+        r = func()
+        assert r, "could not find %s ?" % comp
+
+        # Check that Check* does fail if comp is not available in env
+        oldcomp = deepcopy(self.scons_env[comp])
+        del self.scons_env[comp]
+        r = func()
+        assert not r, "%s worked wo comp ?" % name
+
+        # Check that Check* does fail if comp is set but empty
+        self.scons_env[comp] = ''
+        r = func()
+        assert not r, "%s worked with comp = '' ?" % name
+
+        # Check that Check* does fail if comp is set to buggy executable
+        self.scons_env[comp] = 'thiscccompilerdoesnotexist'
+        r = func()
+        assert not r, "%s worked with comp = thiscompilerdoesnotexist ?" % name
+
+        # Check that Check* does fail if CFLAGS is buggy
+        self.scons_env[comp] = oldcomp
+        self.scons_env['%sFLAGS' % comp] = '/WX qwertyuiop.c'
+        r = func()
+        assert not r, "%s worked with %sFLAGS = qwertyuiop ?" % (name, comp)
+
+    def test_CheckCC(self):
+        """Test SConf.CheckCC()
+        """
+        self._resetSConfState()
+        sconf = self.SConf.SConf(self.scons_env,
+                                 conf_dir=self.test.workpath('config.tests'),
+                                 log_file=self.test.workpath('config.log'))
+        try:
+            try:
+                self._test_check_compilers('CC', sconf.CheckCC, 'CheckCC')
+            except AssertionError:
+                sys.stderr.write(self.test.read('config.log'))
+                raise
+        finally:
+            sconf.Finish()
+
+    def test_CheckSHCC(self):
+        """Test SConf.CheckSHCC()
+        """
+        self._resetSConfState()
+        sconf = self.SConf.SConf(self.scons_env,
+                                 conf_dir=self.test.workpath('config.tests'),
+                                 log_file=self.test.workpath('config.log'))
+        try:
+            try:
+                self._test_check_compilers('SHCC', sconf.CheckSHCC, 'CheckSHCC')
+            except AssertionError:
+                sys.stderr.write(self.test.read('config.log'))
+                raise
+        finally:
+            sconf.Finish()
+
+    def test_CheckCXX(self):
+        """Test SConf.CheckCXX()
+        """
+        self._resetSConfState()
+        sconf = self.SConf.SConf(self.scons_env,
+                                 conf_dir=self.test.workpath('config.tests'),
+                                 log_file=self.test.workpath('config.log'))
+        try:
+            try:
+                self._test_check_compilers('CXX', sconf.CheckCXX, 'CheckCXX')
+            except AssertionError:
+                sys.stderr.write(self.test.read('config.log'))
+                raise
+        finally:
+            sconf.Finish()
+
+    def test_CheckSHCXX(self):
+        """Test SConf.CheckSHCXX()
+        """
+        self._resetSConfState()
+        sconf = self.SConf.SConf(self.scons_env,
+                                 conf_dir=self.test.workpath('config.tests'),
+                                 log_file=self.test.workpath('config.log'))
+        try:
+            try:
+                self._test_check_compilers('SHCXX', sconf.CheckSHCXX, 'CheckSHCXX')
+            except AssertionError:
+                sys.stderr.write(self.test.read('config.log'))
+                raise
+        finally:
+            sconf.Finish()
+
 
     def test_CheckHeader(self):
         """Test SConf.CheckHeader()
@@ -641,3 +752,8 @@ if __name__ == "__main__":
     if not res.wasSuccessful():
         sys.exit(1)
 
+# Local Variables:
+# tab-width:4
+# indent-tabs-mode:nil
+# End:
+# vim: set expandtab tabstop=4 shiftwidth=4: