How do I determine which target is causing my current target in Nant?

I'm not even sure if my question is phrased correctly ... but I will try to explain what I am currently struggling with. I am not very experienced with this, so forgive me if my explanations are not the best ... and I am also trying to simplify my build script for the purposes of this question, so if I am missing some details let me know ...

I am modifying a Nant build script to run some unit tests. I have different targets for local tests and tests to be run in the team city.

<target name="run-unit-tests">  
   <property name="test.executable" value="tools\nunit\nunit-console.exe"/>
   <call target="do-unit-tests"/>
</target>

<target name="run-unit-tests-teamcity"> 
   <property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>      
   <call target="do-unit-tests"/>
</target>

      

in the target do-unit-tests . I have established which test builds are run by setting a property and calling NCover to execute a code space like this:

<target name="do-unit-test">
   <property name="test.assemblies" value="MyProject.dll">
   <call target="do-unit-test-coverage" />
</target>

<target name="do-unit-test-coverage">
   <ncover <!--snip -->
           commandLineArgs="${test.args}"
   <!--snip-->
   </ncover>
</target>

      

As you can see in the ncover part I need a property called "test.args". This property depends on "test.assemblies"

those.: <property name="test.args" value="${test.assemblies} <!--snip -->" />

test.args needs to be configured differently between running the unit test locally and the one in the team town ... so I'm trying to figure out how to set that up.

if i put the test.args property in "do-unit-test" after the "test.assemblies" property i cannot specify one test.args if do-unit-test the run-unit tests are called and the other is for the command run-unit-tests-teamcity.

I'm trying to do something like the following in "do-unit-test":

<if test="${target::exists('run-unit-tests-teamcity')}">
 <property name="test.args" value="..." />
</if>

      

but obviously it doesn't work because the goal will always exist.

Then I need to check if my current target was do-unit-test run-unit-tests-teamcity

Is it possible? I don't see this in the Nantes documentation? Since it's not there, it either means it will be a feature in the future, or I don't understand how things are specified in the Nant build script.

Any help would be appreciated on this.

+1


source to share


2 answers


You can define properties in one target and use their values ​​in another ... For example, you can define

<target name="run-unit-tests">
   <property name="test.executable" value="tools\nunit\nunit-console.exe"/>
   <property name="test.extratestargs" value="foo,bar,baz"/>
   <call target="do-unit-tests"/>
</target>

<target name="run-unit-tests-teamcity">
   <property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>         
   <property name="test.extrtestargs" value="foo,baz,quux,xyzzy"/>
   <call target="do-unit-tests"/>
</target>

<target name="do-unit-test-coverage">
   <property name="test.args" value="${test.assemblies} ${test.extratestargs} <!--snip -->" />
   <ncover <!--snip -->
           commandLineArgs="${test.args}" >
   <!--snip-->
   </ncover>
</target>



      

Or, if you need them to be structured in a completely different way and not just have different meanings, take advantage of the fact that the property is delayed:


<?xml version="1.0"?>

<project name="nanttest">

        <target name="run-unit-tests">
           <property name="test.executable" value="tools\nunit\nunit-console.exe"/>
           <property name="test.args" value="foo bar -assembly ${test.assemblies} baz" dynamic="true"/>
           <call target="do-unit-test"/>
        </target>

        <target name="run-unit-tests-teamcity">
           <property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>
           <property name="test.args" value="foo,baz,quux /a:${test.assemblies} xyzzy" dynamic="true"/>
           <call target="do-unit-test"/>
        </target>

        <target name="do-unit-test-coverage">
           <echo message="test.executable = ${test.executable}, test.args = ${test.args}" />
        </target>

        <target name="do-unit-test">
           <property name="test.assemblies" value="MyProject.dll"/>
           <call target="do-unit-test-coverage" />
        </target>


</project>


      



user @ host : / tmp / anttest $ nant run-unit-tests
[... snip ...]
run-unit-tests:
do-unit-test:
do-unit-test-coverage:
     [echo] test.executable = tools \ nunit \ nunit-console.exe, test.args = foo bar -assembly MyProject.dll baz
BUILD SUCCEEDED
Total time: 0 seconds.

user @ host : / tmp / anttest $ nant -D: teamcity.dotnet.nunitlauncher = nunitlauncher run-unit-tests-teamcity
[... snip ...]
run-unit-tests-teamcity:
do-unit-test:
do-unit-test-coverage:
     [echo] test.executable = nunitlauncher, test.args = foo, baz, quux /a:MyProject.dll xyzzy
BUILD SUCCEEDED
Total time: 0 seconds.

If you really just need to know if you are working for TeamCity, this should help:

<target name="run-unit-tests-teamcity">
   <property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>         
   <property name="running.in.teamcity" value="true"/>
   <call target="do-unit-tests"/>
</target>


      

+2


source


I managed to solve the problem. I don't know if this is the best solution, but this is a solution.

I set the test.type property and then use an if statement to determine which target it came from.



<target name="run-unit-tests">  
   <property name="test.executable" value="tools\nunit\nunit-console.exe"/>
   <property name="test.type" value="unit-tests" />
   <call target="do-unit-tests"/>
</target>

<target name="run-unit-tests-teamcity"> 
   <property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>          
   <property name="test.type" value="unit-tests-teamcity" />
   <call target="do-unit-tests"/>
</target>

<target name="do-unit-test">
   <property name="test.assemblies" value="MyProject.dll">
   <call target="do-unit-test-coverage" />
</target>

<target name="do-unit-test-coverage">

   <if test="${test.type=='unit-tests'}">
      <property name="test.args" value="${test.assemblies} ..."/>
   </if>

   <if test="${test.type=='unit-tests-teamcity'}">
      <property name="test.args" value="... ${test.assemblies}"/>
   </if>

   <ncover <!--snip -->
           commandLineArgs="${test.args}"
   <!--snip-->
   </ncover>
</target>

      

Oh, and sorry Stobor for rejecting your answer ... I hate it when people change down, but in this case I had to because someone supported your answer even though it was wrong, which means that my question will not appear in the unanswered question section of the site ...

0


source







All Articles