How to get the path to NUnit nunit-console.exe
I'm trying to make a Nant script, it has been going well so far, but I wish to not hard code file locations. This is well and good until I have to execute nunit-console.exe which I can't seem to do. What I have found so far relating to this is:
<target name="test"&g开发者_运维百科t;
<property name="windows-path" value="${string::to-lower(environment::get-variable('PATH'))}"/>
<property name="nunit-in-path" value="${string::contains(windows-path, 'nunit')}"/>
<echo message="${nunit-in-path}"/>
</target>
But this fails every time, so I would like to know couple of things:
- What does
string::to-lower(environment::get-variable('PATH'))
actually do? - How do I make it so this doesn't break no matter what version of windows you use or where the nunit-console.exe is? (For example I have NUnit in Program Files(x86) on my PC but in Program Files on my laptop)
- it gets the PATH environment variable and converts all uppercase letters to lowercase
- Make sure that the path to nunit-console.exe is in your PATH on whatever machine you run this nant script on. to check your path, in cmd (command prompt) you can type
echo %PATH%
, in powershell you can type$env:PATH
.
So, assuming nunit-console.exe is located in c:\Program Files\Nunit\bin
To modify your path permanently, right click on my computer, go to advanced --> environment variables
-OR-
To do it dynamically just before you run this nant script, in a batch script, run:
set PATH="%PATH%;c:\Program Files\Nunit\bin"
or in powershell, run:
$env:PATH += ';c:\program files\nunit\bin'
You can also replace c:\Program Files with the relevant environment variable... in Powershell I think it's $env:ProgramFiles
and ${env:ProgramFiles(x86)}
... I think it might be %PROGRAMFILES%
in command prompt but I'm probably wrong. But you can type set
to get a list of all variables in the command prompt.
Setting nunit up in your system path is probably more like what you're trying to do since the script would work without modification on any machine containing nunit in the PATH variable.
Ok I seem to have got it now, this is how my script looks now:
<?xml version="1.0"?>
<project name="Calculator" default="execute" basedir=".">
<property name="InstallationDir" value="C:\BoolCalc" readonly="false"/>
<property name="NUnitLocation" value="${path::combine(directory::get-current-directory(), 'NUnit\bin\net-2.0\nunit-console.exe')}" readonly="false" />
<description>The build scripts for the bool calculator</description>
<target name="clean" description="Remove all previous versions and generated files"><!--This ensures that old files are deleted if they are there and does nothing if they aren't-->
<delete dir="${InstallationDir}" failonerror="false" /><!-- This deletes the directory on your computer for the previous versions, if there are any -->
<delete file="test\Calc.exe" failonerror="false" />
</target>
<target name="build" description="compiles the source code" depends="clean">
<csc target="exe" output="test\Calc.exe" >
<sources>
<include name="src\*.cs" />
</sources>
<references>
<include name="lib\nunit.framework.dll" />
</references>
</csc>
</target>
<target name="testProgram" description="Run unit tests" depends="build">
<exec program="${NUnitLocation}"
workingdir="test\"
commandline="Calc.exe /xml:TestResults.xml /nologo" />
</target>
<target name="install" depends="testProgram">
<echo message="Installing the boolean calculator to ${InstallationDir}"/>
<copy todir="${InstallationDir}" overwrite="true">
<fileset basedir="test\">
<include name="Calc.exe" />
</fileset>
</copy>
</target>
<target name="execute" depends="install">
<echo message="Executing the calculator in ${InstallationDir}"/>
<exec program="${InstallationDir}\Calc.exe" commandline="Calc.exe" />
</target>
</project>
I took the advice and stuffed the Nunit file into workingdir and then create a complete path by using the combine and get-current-directory() to get its exact location.
If you see anything wrong with this script, or something that could be improved please let me know. And thanks to calavera for explaining what I was confused about (didn't know I could do that) and thanks to Tim Robinson and Mark Simpson for the solution.
精彩评论