ant: create dirset with all dirs containing files in a fileset

467 views Asked by At

Context: a directory, recursively containing python unittest.TestCases:

projectdir/build.xml
projectdir/src/x.java
projectdir/distribute/p.jar
projectdir/tests/a/test1.py
projectdir/tests/a/test2.py
projectdir/tests/b/test1.py
projectdir/tests/c/test1.py
projectdir/tests/javaunittests/test2.java

And I want ant to call

python -m unittest discover -s a b c

Now I was trying to convert a fileset to the dirset of the containing directories? The idea is to run python unittests:

<apply command="python">
   <arg line="-m unittest"/>
   <arg value="-s"/>
   <dirset include="${python_unittests}"/>
</apply>

where ${python_unittests}" would refer to afileset` (but I don't find how) like this:

<fileset id="python_unittests" include="**/*.py">
    <containsregexp expression="\(unittest.TestCase\)"/>
</fileset>

Or am I erring, and is there a better way to achieve this?

1

There are 1 answers

1
Rebse On

You want all python files with testcases, you don't need a dirset, use :

<apply executable="python">
  <arg line="-m unittest"/>
  <arg value="-s"/>
  <fileset dir="projectdir" includes="**/*.py">
    <contains text="unittest.TestCase"/>
  </fileset>
</apply>

EDIT

Just checked the docs for unittest on python.org after your comment.
As i understand it, that should be sufficient :

<property name="projectroot" location="foo/bar/yourprojectdir"/>
<exec executable="python" failonerror="true">
 <arg line="-m unittest discover ${projectroot} 'test*.py'"/>
</exec>

as python docs section 26.3.3. Test Discovery explains :

The -s, -p, and -t options can be passed in as positional arguments in that order. The following two command lines are equivalent:

    python -m unittest discover -s project_directory -p '*_test.py'<br>
    python -m unittest discover project_directory '*_test.py'

I expect that discover works recursively starting from project_directory.