MSBuild Exec任务是否会在标准输出中搜索字符串“error”?

7

我有一个包含一个被忽略测试的小型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”,以设置退出代码/状态。

出现在控制台上的确有此信息(由于某些原因,NUnit会为被忽略/跳过的测试打印“Error Message”)。

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

如果我注释掉这个测试,运行就会通过(通过msbuild)。
关于Exec任务,我的理解是否正确?如果我覆盖CustomErrorRegularExpression参数,那么我会“修复”问题吗?我找不到有关此参数的任何好信息...我应该将其设置为空字符串吗?
1个回答

9
如果提供了,Exec 首先会使用 CustomErrorRegularExpressionCustomWarningRegularExpression 对输出进行 检查。如果这些内容不匹配或未提供,Exec 然后会 使用 以下正则表达式:
            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

我通过覆盖CustomErrorRegularExpression参数来“修复”问题吗?

编辑

(自问:我的第二句话直接与此相矛盾,为什么我最初会说“是的”?)

不需要。 :) 将IgnoreStandardErrorWarningFormat设置为true。根据您在问题中提供的文档

IgnoreStandardErrorWarningFormat:可选的 Boolean 参数。

  • 如果为 false,则选择输出中与标准错误/警告格式匹配的行,并将其记录为错误/警告。
  • 如果为 true,则禁用此行为。

默认值为 false


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接