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

Make test libraries configuration agnostic #378

Merged
merged 7 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ public static void Equal<T>(HashSet<T> expected, HashSet<T> actual)
}
}

public static void AtLeastOneEquals<T>(T expected1, T expected2, T value)
{
EqualityComparer<T> comparer = EqualityComparer<T>.Default;
if (!(comparer.Equals(value, expected1) || comparer.Equals(value, expected2)))
throw new XunitException($"Expected: {expected1} || {expected2}{Environment.NewLine}Actual: {value}");
}

public delegate void AssertThrowsActionReadOnly<T>(ReadOnlySpan<T> span);

public delegate void AssertThrowsAction<T>(Span<T> span);
Expand Down
20 changes: 10 additions & 10 deletions src/libraries/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
https://github.com/Microsoft/msbuild/blob/3a9d1d2ae23e41b32a612ea6b0dce531fcf86be7/src/Build/Evaluation/IntrinsicFunctions.cs#L431
-->
<DefaultOSGroup Condition="$([MSBuild]::IsOSPlatform('OSX'))">OSX</DefaultOSGroup>
<DefaultOSGroup Condition="'$(DefaultOSGroup)'=='' and $([MSBuild]::IsOSPlatform('FREEBSD'))">FreeBSD</DefaultOSGroup>
<DefaultOSGroup Condition="'$(DefaultOSGroup)'=='' and $([MSBuild]::IsOSPlatform('NETBSD'))">NetBSD</DefaultOSGroup>
<DefaultOSGroup Condition="'$(DefaultOSGroup)'=='' and $([MSBuild]::IsOSUnixLike())">Linux</DefaultOSGroup>
<DefaultOSGroup Condition="'$(DefaultOSGroup)'==''">$(OS)</DefaultOSGroup>
<DefaultOSGroup Condition="'$(DefaultOSGroup)' == '' and $([MSBuild]::IsOSPlatform('FREEBSD'))">FreeBSD</DefaultOSGroup>
<DefaultOSGroup Condition="'$(DefaultOSGroup)' == '' and $([MSBuild]::IsOSPlatform('NETBSD'))">NetBSD</DefaultOSGroup>
<DefaultOSGroup Condition="'$(DefaultOSGroup)' == '' and $([MSBuild]::IsOSUnixLike())">Linux</DefaultOSGroup>
<DefaultOSGroup Condition="'$(DefaultOSGroup)' == ''">$(OS)</DefaultOSGroup>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -72,7 +72,7 @@
<IsTestProject Condition="$(MSBuildProjectName.EndsWith('.UnitTests')) or $(MSBuildProjectName.EndsWith('.Tests'))">true</IsTestProject>

<IsTestSupportProject>false</IsTestSupportProject>
<IsTestSupportProject Condition="($(MSBuildProjectFullPath.Contains('\tests\')) OR $(MSBuildProjectFullPath.Contains('/tests/'))) AND '$(IsTestProject)' != 'true'">true</IsTestSupportProject>
<IsTestSupportProject Condition="($(MSBuildProjectFullPath.Contains('\tests\')) or $(MSBuildProjectFullPath.Contains('/tests/'))) and '$(IsTestProject)' != 'true'">true</IsTestSupportProject>
<IsTestProject Condition="'$(IsTestSupportProject)' == 'true'">true</IsTestProject>

<!-- Treat test assemblies as non-shipping (do not publish or sign them). -->
Expand Down Expand Up @@ -153,8 +153,8 @@

<!-- set properties for each vertical -->
<PropertyGroup>
<BuildingNETCoreAppVertical Condition="'$(BuildingNETCoreAppVertical)' == '' and ('$(_bc_TargetGroup)'=='$(NetCoreAppCurrent)' or '$(BuildAllConfigurations)' == 'true')">true</BuildingNETCoreAppVertical>
<BuildingNETFxVertical Condition="'$(BuildingNETFxVertical)' == '' and ('$(_bc_TargetGroup)'=='$(NetFrameworkCurrent)' or '$(BuildAllConfigurations)' == 'true')">true</BuildingNETFxVertical>
<BuildingNETCoreAppVertical Condition="'$(BuildingNETCoreAppVertical)' == '' and ('$(_bc_TargetGroup)' == '$(NetCoreAppCurrent)' or '$(BuildAllConfigurations)' == 'true')">true</BuildingNETCoreAppVertical>
<BuildingNETFxVertical Condition="'$(BuildingNETFxVertical)' == '' and ('$(_bc_TargetGroup)' == '$(NetFrameworkCurrent)' or '$(BuildAllConfigurations)' == 'true')">true</BuildingNETFxVertical>
<BinPlaceNETCoreAppPackage Condition="'$(BuildingNETCoreAppVertical)' == 'true'">true</BinPlaceNETCoreAppPackage>
</PropertyGroup>

Expand Down Expand Up @@ -192,12 +192,12 @@

<!-- Set up Default symbol and optimization for Configuration -->
<Choose>
<When Condition="'$(ConfigurationGroup)'=='Debug'">
<When Condition="'$(ConfigurationGroup)' == 'Debug'">
<PropertyGroup>
<DebugSymbols Condition="'$(DebugSymbols)' == ''">true</DebugSymbols>
<Optimize Condition="'$(Optimize)' == ''">false</Optimize>
<DebugType Condition="'$(DebugType)' == ''">full</DebugType>
<DefineConstants>$(DefineConstants),DEBUG,TRACE</DefineConstants>
<DefineConstants>$(DefineConstants),TRACE, DEBUG</DefineConstants>
safern marked this conversation as resolved.
Show resolved Hide resolved
</PropertyGroup>
</When>
<When Condition="'$(ConfigurationGroup)' == 'Release'">
Expand All @@ -216,6 +216,7 @@
<NoExplicitReferenceToStdLib>true</NoExplicitReferenceToStdLib>
<AddAdditionalExplicitAssemblyReferences>false</AddAdditionalExplicitAssemblyReferences>
<CopyNuGetImplementations>false</CopyNuGetImplementations>
<DisableImplicitConfigurationDefines>true</DisableImplicitConfigurationDefines>
<!-- Don't reference implicit framework packages, all projects in this repo must be explicit -->
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
<!-- We can't generate an apphost without restoring the targeting pack. -->
Expand Down Expand Up @@ -288,7 +289,6 @@
<PropertyGroup>
<EnableDefaultItems>false</EnableDefaultItems>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<DisableImplicitConfigurationDefines>true</DisableImplicitConfigurationDefines>

<!-- Workaround for https://github.com/microsoft/msbuild/issues/4474 -->
<GenerateResourceUsePreserializedResources>false</GenerateResourceUsePreserializedResources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading;
using System.Runtime.ExceptionServices;
Expand Down Expand Up @@ -76,9 +77,9 @@ private static void BasicConnectionPoolingTest(string connectionString)
/// Tests if killing the connection using the InternalConnectionWrapper is working
/// </summary>
/// <param name="connectionString"></param>
[Conditional("DEBUG")]
safern marked this conversation as resolved.
Show resolved Hide resolved
private static void KillConnectionTest(string connectionString)
{
#if DEBUG
InternalConnectionWrapper wrapper = null;

using (SqlConnection connection = new SqlConnection(connectionString))
Expand All @@ -103,7 +104,6 @@ private static void KillConnectionTest(string connectionString)
DataTestUtility.AssertEqualsWithDescription(5, command.ExecuteScalar(), "Incorrect scalar result.");
}
}
#endif
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,33 +263,34 @@ public void IdGenerationInternalParent()
//start 2 children in different execution contexts
Task.Run(() => child1.Start()).Wait();
Task.Run(() => child2.Start()).Wait();
#if DEBUG
Assert.Equal($"|{parent.RootId}.{child1.OperationName}-1.", child1.Id);
Assert.Equal($"|{parent.RootId}.{child2.OperationName}-2.", child2.Id);
#else
Assert.Equal($"|{parent.RootId}.1.", child1.Id);
Assert.Equal($"|{parent.RootId}.2.", child2.Id);
#endif

safern marked this conversation as resolved.
Show resolved Hide resolved
string child1DebugString = $"|{parent.RootId}.{child1.OperationName}-1.";
string child2DebugString = $"|{parent.RootId}.{child2.OperationName}-2.";
string child1ReleaseString = $"|{parent.RootId}.1.";
string child2ReleaseString = $"|{parent.RootId}.2.";

AssertExtensions.AtLeastOneEquals(child1DebugString, child1ReleaseString, child1.Id);
AssertExtensions.AtLeastOneEquals(child2DebugString, child2ReleaseString, child2.Id);

Assert.Equal(parent.RootId, child1.RootId);
Assert.Equal(parent.RootId, child2.RootId);
child1.Stop();
child2.Stop();
var child3 = new Activity("child3");
child3.Start();
#if DEBUG
Assert.Equal($"|{parent.RootId}.{child3.OperationName}-3.", child3.Id);
#else
Assert.Equal($"|{parent.RootId}.3.", child3.Id);
#endif

string child3DebugString = $"|{parent.RootId}.{child3.OperationName}-3.";
string child3ReleaseString = $"|{parent.RootId}.3.";

AssertExtensions.AtLeastOneEquals(child3DebugString, child3ReleaseString, child3.Id);

var grandChild = new Activity("grandChild");
grandChild.Start();
#if DEBUG
Assert.Equal($"{child3.Id}{grandChild.OperationName}-1.", grandChild.Id);
#else
Assert.Equal($"{child3.Id}1.", grandChild.Id);
#endif

child3DebugString = $"{child3.Id}{grandChild.OperationName}-1.";
child3ReleaseString = $"{child3.Id}1.";

AssertExtensions.AtLeastOneEquals(child3DebugString, child3ReleaseString, grandChild.Id);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -913,23 +913,20 @@ public TestDiagnosticSourceEventListener()

public int EventCount;
public DiagnosticSourceEvent LastEvent;
#if DEBUG

// Here just for debugging. Lets you see the last 3 events that were sent.
safern marked this conversation as resolved.
Show resolved Hide resolved
public DiagnosticSourceEvent SecondLast;
public DiagnosticSourceEvent ThirdLast;
#endif

/// <summary>
/// Sets the EventCount to 0 and LastEvent to null
/// Sets the EventCount to 0 and LastEvents to null
/// </summary>
public void ResetEventCountAndLastEvent()
{
EventCount = 0;
LastEvent = null;
#if DEBUG
SecondLast = null;
ThirdLast = null;
#endif
}

/// <summary>
Expand All @@ -943,10 +940,8 @@ private void UpdateLastEvent(DiagnosticSourceEvent anEvent)
if (Filter != null && !Filter(anEvent))
return;

#if DEBUG
ThirdLast = SecondLast;
SecondLast = LastEvent;
#endif

EventCount++;
LastEvent = anEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Xunit;

namespace System.Diagnostics
Expand Down Expand Up @@ -247,8 +248,6 @@ public void Ctor_Frame(StackFrame stackFrame)

public static IEnumerable<object[]> ToString_TestData()
{
// Debug mode and Release mode give different results.
#if DEBUG
yield return new object[] { new StackTrace(InvokeException()), "System.Diagnostics.Tests.StackTraceTests.ThrowException()" };
yield return new object[] { new StackTrace(new Exception()), "" };
yield return new object[] { NoParameters(), "System.Diagnostics.Tests.StackTraceTests.NoParameters()" };
Expand All @@ -260,7 +259,6 @@ public static IEnumerable<object[]> ToString_TestData()

// Methods belonging to the System.Diagnostics namespace are ignored.
yield return new object[] { InvokeIgnoredMethod(), "System.Diagnostics.Tests.StackTraceTests.InvokeIgnoredMethod()" };
#endif

yield return new object[] { InvokeIgnoredMethodWithException(), "System.Diagnostics.Ignored.MethodWithException()" };
}
Expand Down Expand Up @@ -300,16 +298,22 @@ public void ToString_NullFrame_ThrowsNullReferenceException()
Assert.Equal(Environment.NewLine, stackTrace.ToString());
}

[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
private static StackTrace NoParameters() => new StackTrace();
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
private static StackTrace OneParameter(int x) => new StackTrace();
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
private static StackTrace TwoParameters(int x, string y) => new StackTrace();

[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
private static StackTrace Generic<T>() => new StackTrace();
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
private static StackTrace Generic<T, U>() => new StackTrace();

private static StackTrace InvokeIgnoredMethod() => Ignored.Method();
private static StackTrace InvokeIgnoredMethodWithException() => Ignored.MethodWithException();

[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
private static Exception InvokeException()
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public virtual string PayloadString(int propertyIndex, string propertyName)
}
public abstract IList<string> PayloadNames { get; }

#if DEBUG
/// <summary>
/// This is a convenience function for the debugger. It is not used typically
/// </summary>
Expand All @@ -123,7 +122,6 @@ public List<object> PayloadValues
return ret;
}
}
#endif

public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,18 @@ public static void TestMethodBody()
MethodBase mbase = typeof(MethodBaseTests).GetMethod("MyOtherMethod", BindingFlags.Static | BindingFlags.Public);
MethodBody mb = mbase.GetMethodBody();
Assert.True(mb.InitLocals); // local variables are initialized
#if DEBUG
safern marked this conversation as resolved.
Show resolved Hide resolved
Assert.Equal(2, mb.MaxStackSize);
Assert.Equal(3, mb.LocalVariables.Count);

foreach (LocalVariableInfo lvi in mb.LocalVariables)
{
if (lvi.LocalIndex == 0) { Assert.Equal(typeof(int), lvi.LocalType); }
if (lvi.LocalIndex == 1) { Assert.Equal(typeof(string), lvi.LocalType); }
if (lvi.LocalIndex == 2) { Assert.Equal(typeof(bool), lvi.LocalType); }
}
#else
Assert.Equal(1, mb.MaxStackSize);
Assert.Equal(2, mb.LocalVariables.Count);
// Debug code expects 2, Release 1.
AssertExtensions.AtLeastOneEquals(1, 2, mb.MaxStackSize);
// Debug code expects 3, Release 2.
AssertExtensions.AtLeastOneEquals(2, 3, mb.LocalVariables.Count);

foreach (LocalVariableInfo lvi in mb.LocalVariables)
{
if (lvi.LocalIndex == 0) { Assert.Equal(typeof(int), lvi.LocalType); }
if (lvi.LocalIndex == 1) { Assert.Equal(typeof(string), lvi.LocalType); }
if (lvi.LocalIndex == 2) { Assert.Equal(typeof(bool), lvi.LocalType); }
}
#endif
}

private static int MyAnotherMethod(int x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ public static void Test_MethodBody_ExceptionHandlingClause()
MethodBody mb = mi.GetMethodBody();

Assert.True(mb.InitLocals); // local variables are initialized
#if DEBUG
safern marked this conversation as resolved.
Show resolved Hide resolved

Assert.Equal(2, mb.MaxStackSize);
Assert.Equal(5, mb.LocalVariables.Count);
// Release expects 3, Debug 3.
AssertExtensions.AtLeastOneEquals(3, 5, mb.LocalVariables.Count);

foreach (LocalVariableInfo lvi in mb.LocalVariables)
{
if (lvi.LocalIndex == 0) { Assert.Equal(typeof(int), lvi.LocalType); }
if (lvi.LocalIndex == 1) { Assert.Equal(typeof(string), lvi.LocalType); }
if (lvi.LocalIndex == 2) { Assert.Equal(typeof(bool), lvi.LocalType); }
// Release expects Exception, Debug bool.
if (lvi.LocalIndex == 2) { AssertExtensions.AtLeastOneEquals(typeof(Exception), typeof(bool), lvi.LocalType); }
if (lvi.LocalIndex == 3) { Assert.Equal(typeof(bool), lvi.LocalType); }
if (lvi.LocalIndex == 4) { Assert.Equal(typeof(Exception), lvi.LocalType); }
}
Expand All @@ -37,37 +39,15 @@ public static void Test_MethodBody_ExceptionHandlingClause()
if (ehc.Flags != ExceptionHandlingClauseOptions.Finally && ehc.Flags != ExceptionHandlingClauseOptions.Filter)
{
Assert.Equal(typeof(Exception), ehc.CatchType);
Assert.Equal(19, ehc.HandlerLength);
Assert.Equal(70, ehc.HandlerOffset);
Assert.Equal(61, ehc.TryLength);
Assert.Equal(9, ehc.TryOffset);
return;
}
}
#else
Assert.Equal(2, mb.MaxStackSize);
Assert.Equal(3, mb.LocalVariables.Count);

foreach (LocalVariableInfo lvi in mb.LocalVariables)
{
if (lvi.LocalIndex == 0) { Assert.Equal(typeof(int), lvi.LocalType); }
if (lvi.LocalIndex == 1) { Assert.Equal(typeof(string), lvi.LocalType); }
if (lvi.LocalIndex == 2) { Assert.Equal(typeof(Exception), lvi.LocalType); }
}

foreach (ExceptionHandlingClause ehc in mb.ExceptionHandlingClauses)
{
if (ehc.Flags != ExceptionHandlingClauseOptions.Finally && ehc.Flags != ExceptionHandlingClauseOptions.Filter)
{
Assert.Equal(typeof(Exception), ehc.CatchType);
Assert.Equal(14, ehc.HandlerLength);
Assert.Equal(58, ehc.HandlerOffset);
Assert.Equal(50, ehc.TryLength);
Assert.Equal(8, ehc.TryOffset);
// First arg is for Release, second for Debug.
AssertExtensions.AtLeastOneEquals(14, 19, ehc.HandlerLength);
AssertExtensions.AtLeastOneEquals(58, 70, ehc.HandlerOffset);
AssertExtensions.AtLeastOneEquals(50, 61, ehc.TryLength);
AssertExtensions.AtLeastOneEquals(8, 9, ehc.TryOffset);
return;
}
}
#endif

Assert.True(false, "Expected to find CatchType clause.");
}
Expand Down