Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade xunit to get newer asserts #57398

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,15 @@
<VsWebsiteInteropVersion>8.0.50727</VsWebsiteInteropVersion>
<vswhereVersion>2.4.1</vswhereVersion>
<XamarinMacVersion>1.0.0</XamarinMacVersion>
<xunitVersion>2.4.1-pre.build.4059</xunitVersion>
<xunitVersion>2.4.2-pre.13</xunitVersion>
<xunitanalyzersVersion>0.10.0</xunitanalyzersVersion>
<xunitassertVersion>$(xunitVersion)</xunitassertVersion>
<XunitCombinatorialVersion>1.3.2</XunitCombinatorialVersion>
<XUnitXmlTestLoggerVersion>2.1.26</XUnitXmlTestLoggerVersion>
<xunitextensibilitycoreVersion>$(xunitVersion)</xunitextensibilitycoreVersion>
<xunitrunnerconsoleVersion>2.4.1-pre.build.4059</xunitrunnerconsoleVersion>
<xunitrunnerconsoleVersion>$(xunitVersion)</xunitrunnerconsoleVersion>
<xunitrunnerwpfVersion>1.0.51</xunitrunnerwpfVersion>
<xunitrunnervisualstudioVersion>$(xunitVersion)</xunitrunnervisualstudioVersion>
<xunitrunnervisualstudioVersion>2.4.3</xunitrunnervisualstudioVersion>
<xunitextensibilityexecutionVersion>$(xunitVersion)</xunitextensibilityexecutionVersion>
<runtimeWinX64MicrosoftNETCoreILAsmPackageVersion>$(ILAsmPackageVersion)</runtimeWinX64MicrosoftNETCoreILAsmPackageVersion>
<runtimeLinuxX64MicrosoftNETCoreILAsmPackageVersion>$(ILAsmPackageVersion)</runtimeLinuxX64MicrosoftNETCoreILAsmPackageVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ internal void AssertTokens(string text, params TokenDescription[] expected)
else if (!actualHadNext)
{
Assert.True(expectedHadNext);
AssertEx.Fail("Unmatched expected: " + expectedEnumerator.Current);
Assert.Fail("Unmatched expected: " + expectedEnumerator.Current);
}
else if (!expectedHadNext)
{
Assert.True(actualHadNext);
AssertEx.Fail("Unmatched actual: " + expectedEnumerator.Current);
Assert.Fail("Unmatched actual: " + expectedEnumerator.Current);
}

var actualToken = actualEnumerator.Current;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void TestFreeFormTokenFactory_NonTokenKind()
try
{
SyntaxFactory.Token(SyntaxKind.IdentifierName);
AssertEx.Fail("Should have thrown - can't create an IdentifierName token");
Assert.Fail("Should have thrown - can't create an IdentifierName token");
return;
}
catch (Exception e)
Expand All @@ -136,7 +136,7 @@ public void TestFreeFormTokenFactory_SpecialTokenKinds()
try
{
SyntaxFactory.Token(default(SyntaxTriviaList), SyntaxKind.IdentifierToken, "text", "valueText", default(SyntaxTriviaList));
AssertEx.Fail("Should have thrown");
Assert.Fail("Should have thrown");
return;
}
catch (ArgumentException e)
Expand All @@ -147,7 +147,7 @@ public void TestFreeFormTokenFactory_SpecialTokenKinds()
try
{
SyntaxFactory.Token(default(SyntaxTriviaList), SyntaxKind.CharacterLiteralToken, "text", "valueText", default(SyntaxTriviaList));
AssertEx.Fail("Should have thrown");
Assert.Fail("Should have thrown");
return;
}
catch (ArgumentException e)
Expand All @@ -158,7 +158,7 @@ public void TestFreeFormTokenFactory_SpecialTokenKinds()
try
{
SyntaxFactory.Token(default(SyntaxTriviaList), SyntaxKind.NumericLiteralToken, "text", "valueText", default(SyntaxTriviaList));
AssertEx.Fail("Should have thrown");
Assert.Fail("Should have thrown");
return;
}
catch (ArgumentException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ public void DebuggerAttributesValid()
builder.Add("Two");
DebuggerAttributeInfo info = DebuggerAttributes.ValidateDebuggerTypeProxyProperties(builder);
PropertyInfo itemProperty = info.Properties.Single(pr => pr.GetCustomAttribute<DebuggerBrowsableAttribute>()!.State == DebuggerBrowsableState.RootHidden);
string[]? items = itemProperty.GetValue(info.Instance) as string[];
string[]? items = (string[]?)itemProperty.GetValue(info.Instance);
Assert.NotNull(items);
Assert.Equal(builder, items);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,8 @@ public void DebuggerAttributesValid()
object? rootNode = DebuggerAttributes.GetFieldValue(ImmutableSegmentedList.Create<string>("1", "2", "3"), "_root")!;
DebuggerAttributes.ValidateDebuggerDisplayReferences(rootNode);
PropertyInfo itemProperty = info.Properties.Single(pr => pr.GetCustomAttribute<DebuggerBrowsableAttribute>()!.State == DebuggerBrowsableState.RootHidden);
double[]? items = itemProperty.GetValue(info.Instance) as double[];
double[]? items = (double[]?)itemProperty.GetValue(info.Instance);
Assert.NotNull(items);
Assert.Equal(list, items);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public void LoadWithDependency()
loader.AddDependencyLocation(analyzerDependencyFile.Path);

var analyzerMainReference = new AnalyzerFileReference(analyzerMainFile.Path, loader);
analyzerMainReference.AnalyzerLoadFailed += (_, e) => AssertEx.Fail(e.Exception!.Message);
analyzerMainReference.AnalyzerLoadFailed += (_, e) => Assert.Fail(e.Exception!.Message);
var analyzerDependencyReference = new AnalyzerFileReference(analyzerDependencyFile.Path, loader);
analyzerDependencyReference.AnalyzerLoadFailed += (_, e) => AssertEx.Fail(e.Exception!.Message);
analyzerDependencyReference.AnalyzerLoadFailed += (_, e) => Assert.Fail(e.Exception!.Message);

var analyzers = analyzerMainReference.GetAnalyzersForAllLanguages();
Assert.Equal(1, analyzers.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public void LoadWithDependency()
loader.AddDependencyLocation(analyzerDependencyFile.Path);

var analyzerMainReference = new AnalyzerFileReference(analyzerMainFile.Path, loader);
analyzerMainReference.AnalyzerLoadFailed += (_, e) => AssertEx.Fail(e.Exception!.Message);
analyzerMainReference.AnalyzerLoadFailed += (_, e) => Assert.Fail(e.Exception!.Message);
var analyzerDependencyReference = new AnalyzerFileReference(analyzerDependencyFile.Path, loader);
analyzerDependencyReference.AnalyzerLoadFailed += (_, e) => AssertEx.Fail(e.Exception!.Message);
analyzerDependencyReference.AnalyzerLoadFailed += (_, e) => Assert.Fail(e.Exception!.Message);

var analyzers = analyzerMainReference.GetAnalyzersForAllLanguages();
Assert.Equal(1, analyzers.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void IncorrectPathmaps()
ImmutableArray.Create(""),
isABaseDirectory,
ImmutableArray.Create(KeyValuePairUtil.Create<string, string>("key", null)));
AssertEx.Fail("Didn't throw");
Assert.Fail("Didn't throw");
}
catch (ArgumentException argException)
{
Expand All @@ -57,7 +57,7 @@ public void BadBaseDirectory()
ImmutableArray.Create(""),
"not_a_root directory",
ImmutableArray.Create(KeyValuePairUtil.Create<string, string>("key", "value")));
AssertEx.Fail("Didn't throw");
Assert.Fail("Didn't throw");
}
catch (ArgumentException argException)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/Core/MSBuildTaskTests/MapSourceRootTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public void MetadataMerge1(bool deterministic)
"MapSourceRoots.ContainsDuplicate", "SourceRoot", path1, "SourceLinkUrl", "URL1", "URL2")) + Environment.NewLine,
engine.Log);

AssertEx.NotNull(task.MappedSourceRoots);
Assert.NotNull(task.MappedSourceRoots);
AssertEx.Equal(new[]
{
$"'{path1}' SourceControl='git' RevisionId='RevId1' NestedRoot='NR1A' ContainingRoot='{path3}' MappedPath='{(deterministic ? "/_/NR1A/" : path1)}' SourceLinkUrl='URL1'",
Expand Down Expand Up @@ -445,7 +445,7 @@ public void Error_NoTopLevelSourceRoot(bool deterministic)
}
else
{
AssertEx.NotNull(task.MappedSourceRoots);
Assert.NotNull(task.MappedSourceRoots);
AssertEx.Equal(new[]
{
$"'{path1}' SourceControl='' RevisionId='' NestedRoot='a/b' ContainingRoot='{path1}' MappedPath='{path1}' SourceLinkUrl=''",
Expand Down
20 changes: 10 additions & 10 deletions src/Compilers/Core/MSBuildTaskTests/TargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ public void GenerateEditorConfigCoreEvaluatesMetadata()
Assert.Equal("_GeneratedEditorConfigMetadata", item.ItemType);

var itemType = item.Metadata.SingleOrDefault(m => m.Name == "ItemType");
AssertEx.NotNull(itemType);
Assert.NotNull(itemType);
Assert.Equal("Compile", itemType.EvaluatedValue);

var metaName = item.Metadata.SingleOrDefault(m => m.Name == "MetadataName");
AssertEx.NotNull(metaName);
Assert.NotNull(metaName);
Assert.Equal("CustomMeta", metaName.EvaluatedValue);

var customMeta = item.Metadata.SingleOrDefault(m => m.Name == metaName.EvaluatedValue);
AssertEx.NotNull(customMeta);
Assert.NotNull(customMeta);
Assert.Equal("abc", customMeta.EvaluatedValue);
}

Expand Down Expand Up @@ -305,15 +305,15 @@ public void GenerateEditorConfigCoreEvaluatesDynamicMetadata()
Assert.Equal("_GeneratedEditorConfigMetadata", item.ItemType);

var itemType = item.Metadata.SingleOrDefault(m => m.Name == "ItemType");
AssertEx.NotNull(itemType);
Assert.NotNull(itemType);
Assert.Equal("Compile", itemType.EvaluatedValue);

var metaName = item.Metadata.SingleOrDefault(m => m.Name == "MetadataName");
AssertEx.NotNull(metaName);
Assert.NotNull(metaName);
Assert.Equal("CustomMeta", metaName.EvaluatedValue);

var customMeta = item.Metadata.SingleOrDefault(m => m.Name == metaName.EvaluatedValue);
AssertEx.NotNull(customMeta);
Assert.NotNull(customMeta);
Assert.Equal("abc", customMeta.EvaluatedValue);
}

Expand Down Expand Up @@ -345,11 +345,11 @@ public void GenerateEditorConfigCoreHandlesMissingMetadata()
Assert.Equal("_GeneratedEditorConfigMetadata", item.ItemType);

var itemType = item.Metadata.SingleOrDefault(m => m.Name == "ItemType");
AssertEx.NotNull(itemType);
Assert.NotNull(itemType);
Assert.Equal("Compile", itemType.EvaluatedValue);

var metaName = item.Metadata.SingleOrDefault(m => m.Name == "MetadataName");
AssertEx.NotNull(metaName);
Assert.NotNull(metaName);
Assert.Equal("CustomMeta", metaName.EvaluatedValue);
}

Expand Down Expand Up @@ -380,11 +380,11 @@ public void GenerateEditorConfigCoreHandlesMalformedCompilerVisibleItemMetadata(
Assert.Equal("_GeneratedEditorConfigMetadata", item.ItemType);

var itemType = item.Metadata.SingleOrDefault(m => m.Name == "ItemType");
AssertEx.NotNull(itemType);
Assert.NotNull(itemType);
Assert.Equal("Compile", itemType.EvaluatedValue);

var metaName = item.Metadata.SingleOrDefault(m => m.Name == "MetadataName");
AssertEx.NotNull(metaName);
Assert.NotNull(metaName);
Assert.Equal("", metaName.EvaluatedValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private CompilationOptionsReader GetOptionsReader(Compilation compilation)
var peBytes = compilation.EmitToArray(new EmitOptions(debugInformationFormat: DebugInformationFormat.Embedded));
var originalReader = new PEReader(peBytes);
var originalPdbReader = originalReader.GetEmbeddedPdbMetadataReader();
AssertEx.NotNull(originalPdbReader);
Assert.NotNull(originalPdbReader);
var factory = LoggerFactory.Create(configure => { });
var logger = factory.CreateLogger("RoundTripVerification");
return new CompilationOptionsReader(logger, originalPdbReader, originalReader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void VerifyRoundTrip(CommonCompiler commonCompiler, string peFilePath, s
errorLoggerOpt: null,
analyzerConfigOptions: default,
globalConfigOptions: default);
AssertEx.NotNull(compilation);
Assert.NotNull(compilation);
RoundTripUtil.VerifyCompilationOptions(commonCompiler.Arguments.CompilationOptions, compilation.Options);

RoundTripUtil.VerifyRoundTrip(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ private static (T Result, string Output) UseTextWriter<T>(Encoding encoding, Fun
currentDirectory: tempDir);
if (result.ExitCode != 0)
{
AssertEx.Fail($"Deterministic compile failed \n stdout: { result.Output }");
Assert.Fail($"Deterministic compile failed \n stdout: { result.Output }");
}
var listener = await serverData.Complete();
Assert.Equal(CompletionData.RequestCompleted, listener.CompletionDataList.Single());
}
var bytes = File.ReadAllBytes(outFile);
AssertEx.NotNull(bytes);
Assert.NotNull(bytes);

return (bytes, finalFlags);
}
Expand Down Expand Up @@ -1516,7 +1516,7 @@ private async Task RunDeterministicTest(string source)
{
if (first[i] != second[i])
{
AssertEx.Fail($"Bytes were different at position { i } ({ first[i] } vs { second[i] }). Flags used were (\"{ finalFlags1 }\" vs \"{ finalFlags2 }\")");
Assert.Fail($"Bytes were different at position { i } ({ first[i] } vs { second[i] }). Flags used were (\"{ finalFlags1 }\" vs \"{ finalFlags2 }\")");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public async Task EndListenClosesQueuedConnections()
foreach (var streamTask in list)
{
using var stream = await streamTask;
AssertEx.NotNull(stream);
Assert.NotNull(stream);
var readCount = await stream.ReadAsync(buffer, 0, buffer.Length);
Assert.Equal(0, readCount);
Assert.False(stream.IsConnected);
Expand Down
37 changes: 9 additions & 28 deletions src/Compilers/Test/Core/Assert/AssertEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,22 @@ public static void AreEqual<T>(T expected, T actual, string message = null, IEqu

if (expected == null)
{
Fail("expected was null, but actual wasn't\r\n" + message);
Assert.Fail("expected was null, but actual wasn't\r\n" + message);
}
else if (actual == null)
{
Fail("actual was null, but expected wasn't\r\n" + message);
Assert.Fail("actual was null, but expected wasn't\r\n" + message);
}
else
{
if (!(comparer != null ?
comparer.Equals(expected, actual) :
AssertEqualityComparer<T>.Equals(expected, actual)))
{
Fail("Expected and actual were different.\r\n" +
"Expected:\r\n" + expected + "\r\n" +
"Actual:\r\n" + actual + "\r\n" +
message);
Assert.Fail("Expected and actual were different.\r\n" +
"Expected:\r\n" + expected + "\r\n" +
"Actual:\r\n" + actual + "\r\n" +
message);
}
}
}
Expand Down Expand Up @@ -276,7 +276,7 @@ public static void NotEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual, I
{
if (ReferenceEquals(expected, actual))
{
Fail("expected and actual references are identical\r\n" + message);
Assert.Fail("expected and actual references are identical\r\n" + message);
}

if (expected == null || actual == null)
Expand All @@ -285,7 +285,7 @@ public static void NotEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual, I
}
else if (SequenceEqual(expected, actual, comparer))
{
Fail("expected and actual sequences match\r\n" + message);
Assert.Fail("expected and actual sequences match\r\n" + message);
}
}

Expand Down Expand Up @@ -493,16 +493,6 @@ public static string ToString<T>(IEnumerable<T> list, string separator = ", ", F
return string.Join(separator, list.Select(itemInspector));
}

public static void Fail(string message)
{
throw new Xunit.Sdk.XunitException(message);
}

public static void Fail(string format, params object[] args)
{
throw new Xunit.Sdk.XunitException(string.Format(format, args));
}

public static void NotNull<T>(T @object, string message = null)
{
Assert.False(AssertEqualityComparer<T>.IsNull(@object), message);
Expand Down Expand Up @@ -700,7 +690,7 @@ public static void Empty<T>(IEnumerable<T> items, string message = "")
var list = items.ToList();
if (list.Count != 0)
{
Fail($"Expected 0 items but found {list.Count}: {message}\r\nItems:\r\n {string.Join("\r\n ", list)}");
Assert.Fail($"Expected 0 items but found {list.Count}: {message}\r\nItems:\r\n {string.Join("\r\n ", list)}");
}
}

Expand Down Expand Up @@ -774,14 +764,5 @@ public static void Equal<T>(T[,] expected, Func<int, int, T> getResult, Func<T,
Assert.True(false, builder.ToString());
}
}

#nullable enable
public static void NotNull<T>([NotNull] T value)
{
Assert.NotNull(value);
Debug.Assert(value is object);
}

#nullable disable
}
}
4 changes: 2 additions & 2 deletions src/Compilers/Test/Core/InstrumentationChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ internal override void Dump(DynamicAnalysisDataReader reader, string[] sourceLin
}
}
}
AssertEx.Fail(output.ToStringAndFree());
Assert.Fail(output.ToStringAndFree());
}

private static string GetTermination(int index, int length)
Expand Down Expand Up @@ -208,7 +208,7 @@ internal override void Dump(DynamicAnalysisDataReader reader, string[] sourceLin
}
}
}
AssertEx.Fail(output.ToStringAndFree());
Assert.Fail(output.ToStringAndFree());
}

private static string GetTermination(int index, int length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Text;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;

namespace Roslyn.Test.Utilities.CoreClr
{
Expand Down Expand Up @@ -65,7 +66,10 @@ private Assembly LoadImageAsAssembly(ImmutableArray<byte> mainImage)
var mainAssembly = LoadImageAsAssembly(mainImage);
var entryPoint = mainAssembly.EntryPoint;

AssertEx.NotNull(entryPoint, "Attempting to execute an assembly that has no entrypoint; is your test trying to execute a DLL?");
if (entryPoint == null)
{
Assert.Fail("Attempting to execute an assembly that has no entrypoint; is your test trying to execute a DLL?");
}

int exitCode = 0;
SharedConsole.CaptureOutput(() =>
Expand Down
Loading