MSBuild Exec 任务是否在 STDOUT 中搜索字符串 "error"?

Does the MSBuild Exec task search STDOUT for the string "error"?

我有一个小型 NUnit 测试套件,其中包含一个被忽略的测试。我现在正在编写一个简单的 MSBuild 脚本来恢复和发布等。我试图让 dotnet test 任务工作

<Target Name="test" DependsOnTargets="restore">
    <Exec Command="dotnet test"
          WorkingDirectory="test\Example.Tests" />
</Target>

但是,它每次都以代码 -1 退出!

PS> dotnet msbuild /t:test build.xml
Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

    Restore completed in 50.88 ms for C:\...\src\Example\Example.csproj.
    Restore completed in 57.18 ms for C:\...\test\Example.Tests\Example.Tests.csproj.
  Build started, please wait...
  Build completed.

  Test run for C:\...\test\Example.Tests\bin\Debug\netcoreapp1.1\Example.Tests.dll(.NETCoreApp,Version=v1.1)
  Microsoft (R) Test Execution Command Line Tool Version 15.0.0.0
  Copyright (c) Microsoft Corporation.  All rights reserved.

        Starting test execution, please wait...
  NUnit Adapter 3.8.0.0: Test execution started
  Running all tests in C:\...\test\Example.Tests\bin\Debug\netcoreapp1.1\Example.Tests.dll
  NUnit3TestExecutor converted 26 of 26 NUnit test cases
  Skipped  FilterEndpointWithCompletelyFilteredSystemsReturnsNotFound("foo")
EXEC : error Message:  [C:\...\build.xml]
   OneTimeSetUp: Having trouble constructing this scenario with the current command catalog

  NUnit Adapter 3.8.0.0: Test execution complete

  Total tests: 26. Passed: 25. Failed: 0. Skipped: 1.
  Test Run Successful.
  Test execution time: 2.8322 Seconds


C:\...\build.xml(16,9): error MSB3073: The command "dotnet test" exited with code -1.

如果我直接在我的控制台中 运行 命令,没问题。

PS> dotnet test
Build started, please wait...
Build completed.

Test run for C:\...\test\Example.Tests\bin\Debug\netcoreapp1.1\Example.Tests.dll(.NETCoreApp,Version=v1.1)
Microsoft (R) Test Execution Command Line Tool Version 15.0.0.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
NUnit Adapter 3.8.0.0: Test execution started
Running all tests in C:\...\test\Example.Tests\bin\Debug\netcoreapp1.1\Example.Tests.dll
NUnit3TestExecutor converted 26 of 26 NUnit test cases
Skipped  FilterEndpointWithCompletelyFilteredSystemsReturnsNotFound("foo")
Error Message:
 OneTimeSetUp: Having trouble constructing this scenario with the current command catalog

NUnit Adapter 3.8.0.0: Test execution complete

Total tests: 26. Passed: 25. Failed: 0. Skipped: 1.
Test Run Successful.
Test execution time: 3.6485 Seconds

我四处寻找帮助,但没有找到任何明确的信息。我的印象是 Exec task 在 STDOUT 中搜索某种错误消息,可能只是单词 "error",以便设置退出 code/status.

这确实出现在我的控制台上(出于某种原因,NUnit 为 ignored/skipped 测试打印 "Error Message")。

Skipped  FilterEndpointWithCompletelyFilteredSystemsReturnsNotFound("foo")
Error Message:
 OneTimeSetUp: Having trouble constructing this scenario with the current command catalog

如果我注释掉这个测试,运行 通过(通过 msbuild)。

我对 Exec 任务的看法是否正确?我会 "fix" 通过覆盖 CustomErrorRegularExpression 参数来解决问题吗?我找不到关于此参数的任何有用信息...我可以将其设置为空字符串吗?

如果提供,Exec checks the output against CustomErrorRegularExpression and CustomWarningRegularExpression first. If those do not match or were not provided, Exec then uses 以下正则表达式:

            new Regex
            (
            // Beginning of line and any amount of whitespace.
            @"^\s*"
                // Match a [optional project number prefix 'ddd>'], single letter + colon + remaining filename, or
                // string with no colon followed by a colon.
            + @"(((?<ORIGIN>(((\d+>)?[a-zA-Z]?:[^:]*)|([^:]*))):)"
                // Origin may also be empty. In this case there's no trailing colon.
            + "|())"
                // Match the empty string or a string without a colon that ends with a space
            + "(?<SUBCATEGORY>(()|([^:]*? )))"
                // Match 'error' or 'warning'.
            + @"(?<CATEGORY>(error|warning))"
                // Match anything starting with a space that's not a colon/space, followed by a colon.
                // Error code is optional in which case "error"/"warning" can be followed immediately by a colon.
            + @"( \s*(?<CODE>[^: ]*))?\s*:"
                // Whatever's left on this line, including colons.
            + "(?<TEXT>.*)$",
            RegexOptions.IgnoreCase | RegexOptions.Compiled
            )

计算出以下 example 格式的行被解释为错误或警告(取决于这两个词中的哪一个在 "Cat." 部分)。

Main.cs(17,20):Command line warning CS0168: The variable 'foo' is declared but never used
-------------- ------------ ------- ------  ----------------------------------------------
Origin         SubCategory  Cat.    Code    Text

Would I "fix" the problem by overriding the CustomErrorRegularExpression parameter?

编辑

(我自己的问题:为什么在 地球上 我一开始就说 "Yup" 而我的第二句话直接与此相矛盾?)

没有。 :) 将 IgnoreStandardErrorWarningFormat 设置为 true。来自您在问题中链接的 documentation

IgnoreStandardErrorWarningFormat: Optional Boolean parameter.

  • If false, selects lines in the output that match the standard error/warning format, and logs them as errors/warnings.
  • If true, disable this behavior.

The default value is false.