Skip to content

Commit 4415472

Browse files
authored
Improve test failure messages (#89967)
1 parent 26103db commit 4415472

File tree

12 files changed

+52
-3
lines changed

12 files changed

+52
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
6+
namespace Mono.Linker.Tests.Cases.Expectations.Assertions;
7+
8+
[AttributeUsage (AttributeTargets.Class)]
9+
public class ExpectNonZeroExitCodeAttribute : BaseExpectedLinkedBehaviorAttribute
10+
{
11+
public ExpectNonZeroExitCodeAttribute (int value)
12+
{
13+
}
14+
}

src/tools/illink/test/Mono.Linker.Tests.Cases/CommandLine/InvalidArguments.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Mono.Linker.Tests.Cases.CommandLine
55
{
6+
[ExpectNonZeroExitCode (1)]
67
[SetupLinkerArgument ("--verbose", "--invalidArgument")]
78
[LogContains ("Unrecognized command-line option")]
89
[NoLinkedOutput]

src/tools/illink/test/Mono.Linker.Tests.Cases/FeatureSettings/FeatureSubstitutionsInvalid.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Mono.Linker.Tests.Cases.FeatureSettings
55
{
6+
[ExpectNonZeroExitCode (1)]
67
[SetupLinkerSubstitutionFile ("FeatureSubstitutionsInvalid.xml")]
78
[SetupLinkerArgument ("--feature", "NoValueFeature", "true")]
89
[LogContains ("FeatureSubstitutionsInvalid.xml'. Feature 'NoValueFeature' does not specify a 'featurevalue' attribute")]

src/tools/illink/test/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.cs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Mono.Linker.Tests.Cases.LinkAttributes
66
{
7+
[ExpectNonZeroExitCode (1)]
78
[SetupLinkAttributesFile ("TypedArgumentsErrors.xml")]
89
[IgnoreLinkAttributes (false)]
910
[NoLinkedOutput]

src/tools/illink/test/Mono.Linker.Tests.Cases/Logging/CommonLogs.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Mono.Linker.Tests.Cases.Logging
55
{
6+
[ExpectNonZeroExitCode (1)]
67
[SetupCompileBefore ("LogStep.dll", new[] { "Dependencies/LogStep.cs" }, new[] { "illink.dll", "Mono.Cecil.dll" })]
78
[SetupLinkerArgument ("--custom-step", "Log.LogStep,LogStep.dll")]
89
[SetupLinkerArgument ("--verbose")]

src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/CanDisableWarnAsError.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Mono.Linker.Tests.Cases.Warnings
55
{
6+
[ExpectNonZeroExitCode (1)]
67
[IgnoreTestCase ("Ignore in NativeAOT, see https://github.com/dotnet/runtime/issues/82447", IgnoredBy = Tool.NativeAot)]
78
[SkipKeptItemsValidation]
89
[SkipRemainingErrorsValidation]

src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/CanSingleWarnWithWarnAsError.cs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace Mono.Linker.Tests.Cases.Warnings
77
{
8+
[ExpectNonZeroExitCode (1)]
89
[IgnoreTestCase ("Ignore in NativeAOT, see https://github.com/dotnet/runtime/issues/82447", IgnoredBy = Tool.NativeAot)]
910
[SkipKeptItemsValidation]
1011
[SetupCompileBefore ("library.dll", new[] { typeof (TriggerWarnings_Lib) })]

src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/CanWarnAsError.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Mono.Linker.Tests.Cases.Warnings
55
{
6+
[ExpectNonZeroExitCode (1)]
67
[IgnoreTestCase ("Ignore in NativeAOT, see https://github.com/dotnet/runtime/issues/82447", IgnoredBy = Tool.NativeAot)]
78
[SkipKeptItemsValidation]
89
[SetupLinkerSubstitutionFile ("CanWarnAsErrorSubstitutions.xml")]

src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/InvalidWarningVersion.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Mono.Linker.Tests.Cases.Warnings
55
{
6+
[ExpectNonZeroExitCode (1)]
67
[IgnoreTestCase ("Ignore in NativeAOT, see https://github.com/dotnet/runtime/issues/82447", IgnoredBy = Tool.NativeAot)]
78
[SetupLinkerArgument ("--verbose")]
89
[SetupLinkerArgument ("--warn", "invalid")]

src/tools/illink/test/Mono.Linker.Tests/TestCasesRunner/LinkedTestCaseResult.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ public class LinkedTestCaseResult
1717
public readonly ManagedCompilationResult CompilationResult;
1818
public readonly LinkerTestLogger Logger;
1919
public readonly LinkerCustomizations Customizations;
20+
public readonly int ExitCode;
2021

21-
public LinkedTestCaseResult (TestCase testCase, NPath inputAssemblyPath, NPath outputAssemblyPath, NPath expectationsAssemblyPath, TestCaseSandbox sandbox, TestCaseMetadataProvider metadataProvider, ManagedCompilationResult compilationResult, LinkerTestLogger logger, LinkerCustomizations customizations)
22+
public LinkedTestCaseResult (TestCase testCase, NPath inputAssemblyPath, NPath outputAssemblyPath, NPath expectationsAssemblyPath, TestCaseSandbox sandbox, TestCaseMetadataProvider metadataProvider, ManagedCompilationResult compilationResult, LinkerTestLogger logger, LinkerCustomizations customizations, int exitCode)
2223
{
2324
TestCase = testCase;
2425
InputAssemblyPath = inputAssemblyPath;
@@ -29,6 +30,7 @@ public LinkedTestCaseResult (TestCase testCase, NPath inputAssemblyPath, NPath o
2930
CompilationResult = compilationResult;
3031
Logger = logger;
3132
Customizations = customizations;
33+
ExitCode = exitCode;
3234
}
3335
}
3436
}

src/tools/illink/test/Mono.Linker.Tests/TestCasesRunner/ResultChecker.cs

+25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Diagnostics.CodeAnalysis;
88
using System.IO;
99
using System.Linq;
10+
using System.Text;
1011
using System.Text.RegularExpressions;
1112
using Mono.Cecil;
1213
using Mono.Cecil.Cil;
@@ -110,8 +111,12 @@ public virtual void Check (LinkedTestCaseResult linkResult)
110111

111112
try {
112113
var original = ResolveOriginalsAssembly (linkResult.ExpectationsAssemblyPath.FileNameWithoutExtension);
114+
115+
VerifyExitCode (linkResult, original);
116+
113117
if (!HasAttribute (original, nameof (NoLinkedOutputAttribute))) {
114118
Assert.IsTrue (linkResult.OutputAssemblyPath.FileExists (), $"The linked output assembly was not found. Expected at {linkResult.OutputAssemblyPath}");
119+
115120
var linked = ResolveLinkedAssembly (linkResult.OutputAssemblyPath.FileNameWithoutExtension);
116121

117122
if (ShouldValidateIL (original)) {
@@ -239,6 +244,26 @@ void PerformOutputSymbolChecks (AssemblyDefinition original, NPath outputDirecto
239244
}
240245
}
241246

247+
void VerifyExitCode (LinkedTestCaseResult linkResult, AssemblyDefinition original)
248+
{
249+
if (TryGetCustomAttribute (original, nameof(ExpectNonZeroExitCodeAttribute), out var attr)) {
250+
var expectedExitCode = (int) attr.ConstructorArguments[0].Value;
251+
Assert.AreEqual (expectedExitCode, linkResult.ExitCode, $"Expected exit code {expectedExitCode} but got {linkResult.ExitCode}. Output was:\n{FormatLinkerOutput()}");
252+
} else {
253+
if (linkResult.ExitCode != 0) {
254+
Assert.Fail($"Linker exited with an unexpected non-zero exit code of {linkResult.ExitCode} and output:\n{FormatLinkerOutput()}");
255+
}
256+
}
257+
258+
string FormatLinkerOutput ()
259+
{
260+
var sb = new StringBuilder ();
261+
foreach (var message in linkResult.Logger.GetLoggedMessages ())
262+
sb.AppendLine (message.ToString ());
263+
return sb.ToString ();
264+
}
265+
}
266+
242267
void VerifyKeptSymbols (CustomAttribute symbolsAttribute)
243268
{
244269
var assemblyName = (string) symbolsAttribute.ConstructorArguments[0].Value;

src/tools/illink/test/Mono.Linker.Tests/TestCasesRunner/TestRunner.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ private LinkedTestCaseResult Link (TestCase testCase, TestCaseSandbox sandbox, M
109109
AddLinkOptions (sandbox, compilationResult, builder, metadataProvider);
110110

111111
LinkerTestLogger logger = new LinkerTestLogger ();
112-
linker.Link (builder.ToArgs (), linkerCustomizations, logger);
112+
var exitCode = linker.Link (builder.ToArgs (), linkerCustomizations, logger);
113113

114-
return new LinkedTestCaseResult (testCase, compilationResult.InputAssemblyPath, sandbox.OutputDirectory.Combine (compilationResult.InputAssemblyPath.FileName), compilationResult.ExpectationsAssemblyPath, sandbox, metadataProvider, compilationResult, logger, linkerCustomizations);
114+
return new LinkedTestCaseResult (testCase, compilationResult.InputAssemblyPath, sandbox.OutputDirectory.Combine (compilationResult.InputAssemblyPath.FileName), compilationResult.ExpectationsAssemblyPath, sandbox, metadataProvider, compilationResult, logger, linkerCustomizations, exitCode);
115115
}
116116

117117
protected virtual void AddLinkOptions (TestCaseSandbox sandbox, ManagedCompilationResult compilationResult, LinkerArgumentBuilder builder, TestCaseMetadataProvider metadataProvider)

0 commit comments

Comments
 (0)