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



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

<Target Name="test" DependsOnTargets="restore">
<Exec Command="dotnet test"
WorkingDirectory="testExample.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:...srcExampleExample.csproj.
Restore completed in 57.18 ms for C:...testExample.TestsExample.Tests.csproj.
Build started, please wait...
Build completed.
Test run for C:...testExample.TestsbinDebugnetcoreapp1.1Example.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:...testExample.TestsbinDebugnetcoreapp1.1Example.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:...testExample.TestsbinDebugnetcoreapp1.1Example.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:...testExample.TestsbinDebugnetcoreapp1.1Example.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任务在STDOUT中搜索某种错误消息,可能只是"错误"一词,以便设置退出代码/状态。

这确实出现在我的控制台上(出于某种原因,NUnit 为忽略/跳过的测试打印"错误消息")。

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

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

我对执行任务的看法是否正确?我会通过覆盖CustomErrorRegularExpression参数来"修复"问题吗?我找不到有关此参数的任何好信息...我会将其设置为空字符串吗?

如果提供,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
)

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

主.cs(17,20):命令行警告 CS0168:声明了变量"foo",但从未使用 -------------- ------------ ------- ------  ---------------------------------------------- 起源子类别猫。   代码文本

我会通过覆盖 CustomErrorRegularExpression 参数来"修复"问题吗?

编辑

(问我自己:当我的第二句话直接与此相矛盾时,我为什么最初说"是的"?

不,:)将IgnoreStandardErrorWarningFormat设置为true。从您在问题中链接的文档:

IgnoreStandardErrorWarningFormat:可选Boolean参数。

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

默认值为false

最新更新