如何获取 NUnit nunit-console.exe 的路径

发布于 2024-10-04 06:23:06 字数 703 浏览 11 评论 0原文

我正在尝试制作一个 Nant 脚本,到目前为止进展顺利,但我希望不要对文件位置进行硬编码。这很好,直到我必须执行 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 在哪里,都不会中断? (例如,我的 PC 上的 Program Files(x86) 中有 NUnit,但笔记本电脑上的 Program Files 中有 NUnit)

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">
      <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:

  1. What does string::to-lower(environment::get-variable('PATH')) actually do?
  2. 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独行侠 2024-10-11 06:23:06
  1. 它获取 PATH 环境变量并将所有大写字母转换为小写
  2. 确保 nunit-console.exe 的路径位于运行此 nant 脚本的任何计算机上的 PATH 中。要检查您的路径,在 cmd(命令提示符)中,您可以键入 echo %PATH%,在 powershell 中,您可以键入 $env:PATH

因此,假设 nunit-console.exe 位于 c:\Program Files\Nunit\bin

要永久修改路径,请右键单击我的电脑,转到高级 -->环境变量 -

或者-

要在运行此 nant 脚本之前动态执行此操作,请在批处理脚本中运行:

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 的任何计算机上运行。

  1. it gets the PATH environment variable and converts all uppercase letters to lowercase
  2. 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.

深居我梦 2024-10-11 06:23:06

好吧,我现在似乎已经明白了,这就是我的脚本现在的样子:

<?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() 创建完整路径以获得其确切的路径地点。

如果您发现此脚本有任何问题或需要改进的地方,请告诉我。
感谢卡拉维拉解释了我所困惑的事情(不知道我能做到这一点),并感谢蒂姆·罗宾逊和马克·辛普森提供的解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文