2010-11-25 141 views
3

我正在尝试创建一个Nant脚本,它一直进行得很顺利,但我希望不要硬编码文件位置。这是很好,直到我必须执行nunit-console.exe,我似乎无法做到。我发现,到目前为止有关是这样的:如何获得NUnit nunit-console.exe的路径

<target name="test"> 
     <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> 

但这没有忘记时间,所以我想知道两件事情:

  1. 是什么string::to-lower(environment::get-variable('PATH'))实际上呢?
  2. 不管你使用的是哪个版本的windows,或者nunit-console.exe在哪里,我该如何做到这一点? (例如,我在我的电脑上,但在Program Files文件有NUnit的在程序文件(x86)我的笔记本电脑)
+2

我通常不打扰这一点 - 我检查我的NUnit分布到源代码管理,然后使用构建脚本的相对路径 – 2010-11-25 18:20:09

+1

我同意蒂姆。除非您的NUnit版本在已知位置被签入源代码管理,否则很难确保PC上存在相同的_version_,更不用说安装路径。 NUnit开发者提供了一个不需要专门为此安装的软件包:) – 2010-11-25 18:26:06

+0

我该怎么做? – Siemsen 2010-11-25 18:26:33

回答

1

好吧,我现在好像已经知道了,这是我的脚本现在的样子:

<?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> 

我把意见和毛绒NUnit的文件到工作目录,然后通过组合创建一个完整的路径和get-current-directory()获取它的确切位置。

如果您发现此脚本有任何问题,或者可以改进的地方,请告诉我。 感谢calavera解释我所困惑的事情(不知道我能做到这一点),并感谢Tim Robinson和Mark Simpson提供的解决方案。

3
  1. 它得到PATH环境变量,并将所有大写字母为小写
  2. 确保nunit-console.exe的路径在你运行这个脚本的任何机器上的PATH中。检查你的路径,在cmd(命令提示符)中,你可以输入echo %PATH%,在powershell中你可以输入$env:PATH

因此,假设NUnit的-console.exe位于c:\ Program Files文件\ NUnit的\ BIN

要永久地修改您的路径,右击我的电脑上,进入高级 - >环境变量

- 或 -

要动态运行此脚本楠之前做到这一点,在一个批处理脚本,运行:

set PATH="%PATH%;c:\Program Files\Nunit\bin"

或在PowerShell中运行:

$env:PATH += ';c:\program files\nunit\bin'

您也可以替换C:\ Program Files文件与相关的环境变量......在PowerShell中,我认为这是$env:ProgramFiles${env:ProgramFiles(x86)} ......我想可能在命令提示符下为%PROGRAMFILES%,但我可能是错的。但是,您可以输入set以获取命令提示符中所有变量的列表。

在你的系统路径中设置nunit可能更像你要做的事情,因为脚本可以在任何包含PATH变量中的nunit的机器上进行修改而无需修改。