# -*- python -*-
##
## SConscript  -  SCons buildscript for the Testsuite (called by SConstruct)
##

from Buildhelper import srcSubtree
from Buildhelper import globRootdirs

Import('env','artifacts','corelib')


def SingleTestExecutableSubdir(env,tree):
    """ declare all targets needed to create a standalone
        Test executalbe of the given Sub-tree. Note that
        each subdir is built in its own Environment.
    """
    env = env.Clone()
    env.Append(CPPPATH=tree) # add Subdir to Includepath 
    tree = env.subst(tree)  # expand Construction Vars
    exeName = 'test-%s' % tree
    objects = srcSubtree(env,tree)
    return env.Program(exeName, objects + corelib)


def treatPluginTestcase(env):
    """ Special case: the test-plugin executable
    """
    env = env.Clone()
    env.Append(CPPPATH='plugin')
    prfx = 'plugin/example_plugin'
    oC   = env.SharedObject(prfx,        prfx+'.c')
    oCPP = env.SharedObject(prfx+'_cpp', prfx+'.cpp')
    testplugin = ( env.SharedLibrary('.libs/example_plugin',     oC,   SHLIBPREFIX='')
                 + env.SharedLibrary('.libs/example_plugin_cpp', oCPP, SHLIBPREFIX='')
                 )
    testExe = env.Program('test-plugin', ['plugin/plugin_main.c'] + corelib)
    env.Depends(testExe, testplugin)
    return testExe
        #-- it depentds (at the moment) on a specific isolated test-plugin,
        #   which is not integrated in the "normal procedure" for building Plugins
        #   (which is not yet implemented as of 8/07)
        #   TODO: handle this case automatically


# 
# build a Test-Executable out of every subdir...
moduledirs = globRootdirs('*')

#
# have to treat the plugin-example specially.
isnt_plugin = lambda dir : dir!='plugin'
moduledirs = filter(isnt_plugin, moduledirs)
pluginExe = treatPluginTestcase(env)

artifacts['testsuite'] = ts = [ SingleTestExecutableSubdir(env, dir) for dir in moduledirs] + pluginExe 
                              





#
# actually run the Testsuite
#
#  - the product of running the Testsuite is the ",testlog"
#  - it depends on all artifacts defined as "ts" above
#
runTs = env.Command(',testlog', ts, "./test.sh",  chdir=1)



#
# define Phony targets
#  - 'scons testcode' triggers building of the Testsuite
#  - 'scons check' triggers building and running
#
env.Alias('testcode', ts )
env.Alias('check', runTs )

# declare tempfiles of test.sh as cleanable  
env.Clean ('check', [',testlog.pre',',expect_stdout',',stdout',',stderr',',testtmp','.libs']) 
