<?xml version="1.0"?>
<!-- 
============================================================================ 
Name        : build.xml 
Part of     : Helium AntLib

Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
All rights reserved.
This component and the accompanying materials are made available
under the terms of the License "Eclipse Public License v1.0"
which accompanies this distribution, and is available
at the URL "http://www.eclipse.org/legal/epl-v10.html".

Initial Contributors:
Nokia Corporation - initial contribution.

Contributors:

Description:

============================================================================
-->
<project name="test-macros" xmlns:ac="antlib:net.sf.antcontrib">
    <import file="../java/common.ant.xml" />
    <dirname property="python.builder.dir" file="${ant.file.test-macros}" />
    
    <target name="unittest" depends="pt,pt-coverage"/>
    
    <target name="pt">
        <pathconvert pathsep=":" property="python.path">
            <fileset dir="${python.builder.dir}/lib" includes="*.egg" excludes="jython*"/>
            <fileset dir="${lib.dir}" includes="zipsafe/*.egg" />
            <dirset dir="${lib.dir}" includes="zipnotsafe/*.egg" />
            <dirset dir="${lib.dir}/zipsafe" />
            <!--<fileset dir="${module.bin.dir}" includes="${ant.project.name}*.egg"/>-->
            <dirset dir="${basedir}" includes="lib"/>
        </pathconvert>
        <pathconvert pathsep=" " property="python.modules.coverage">
            <fileset dir="${basedir}" includes="**/*.py"/>
        </pathconvert>
        <!---->
        <mkdir dir="${junit.result.dir}" />
        <ac:trycatch property="cpython.unittest.error">
            <try>
                <exec executable="python" failonerror="true" dir="${lib.dir}">
                    <env key="PYTHONPATH" path="${python.path}" />
                    <arg line="${python.builder.dir}/lib/coverage.py -x ${python.builder.dir}/lib/nosetests-script.py --exe -v --with-xunit --xunit-file=${junit.result.dir}/python.xml"/>
                    <arg line="${nose.args}" />
                </exec>
            </try>
        </ac:trycatch>
        <ac:trycatch property="jython.unittest.error">
            <try>
                <script language="jython" setbeans="false">
<![CDATA[
import sys
import os
import shutil

for p in project.getProperty('python.path').split(':'):
    sys.path.append(p)
    
os.chdir(project.getProperty('lib.dir'))
if os.path.exists('build'):
    shutil.rmtree('build')
import nose
args = project.getProperty('nose.args')
args2 = "--with-xunit --xunit-file=" + project.getProperty('junit.result.dir') + "/jython.xml" + args
testdir = os.path.join(project.getProperty('basedir'), 'lib', project.getProperty('ant.project.name'))

result = True
if ' ' in args:
    argv = ['-v', testdir + 'tests']
    result = nose.run(argv=argv)
elif 'cpython' not in args:
    argv = ['-v', args2]
    result = nose.run(argv=argv)
print >> sys.stderr
if not result:
    raise Exception('Jython unittest failure.')
]]>
                </script>
            </try>
        </ac:trycatch>
        <delete includeemptydirs="true">
            <fileset dir="${basedir}/lib" includes="*.egg-info/"/>
        </delete>
        <ac:if>
            <isset property="jython.unittest.error" />
            <then>
                <fail message="${jython.unittest.error}" />
            </then>
        </ac:if>
        <ac:if>
            <isset property="cpython.unittest.error" />
            <then>
                <fail message="${cpython.unittest.error}" />
            </then>
        </ac:if>
    </target>

    <target name="pt-coverage">
        <mkdir dir="${lib.dir}/../coverage"/>
        <exec executable="python" failonerror="true" dir="${lib.dir}">
            <env key="PYTHONPATH" path="${python.path}" />
            <arg line="${python.builder.dir}/lib/coverage.py -a -d ${lib.dir}/../coverage ${python.modules.coverage}"/>
        </exec>
        
        <mkdir dir="${lib.dir}/../coverage/report"/>
        <move file="${lib.dir}/../coverage/report/line_coverage.txt" tofile="${lib.dir}/../coverage/report/line_coverage_old.txt" overwrite="true" failonerror="false"/>
        <record name="${lib.dir}/../coverage/report/line_coverage.txt" action="start"/>
        <exec executable="python" failonerror="true" dir="${lib.dir}">
            <env key="PYTHONPATH" path="${python.path}" />
            <arg line="${python.builder.dir}/lib/coverage.py -r ${python.modules.coverage}"/>
        </exec>
        <record name="${lib.dir}/../coverage/report/line_coverage.txt" action="stop"/>
        
        <script language="jython" setbeans="false">
for line in open(project.getProperty('lib.dir') + r'/../coverage/report/line_coverage.txt'):
    if 'TOTAL' in line and ' 0%' in line:
        raise Exception('Coverage at 0%')
        </script>
        <delete file="${basedir}/.coverage"/>
    </target>
    
</project>
