Skip to content

Commit

Permalink
#2385: TeamCity logger uses wrong flowId
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed May 9, 2023
1 parent 3edaa18 commit cd43469
Show file tree
Hide file tree
Showing 12 changed files with 350 additions and 214 deletions.
13 changes: 10 additions & 3 deletions src/xunit.execution/Sdk/Frameworks/TestCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ public class TestCollection : LongLivedMarshalByRefObject, ITestCollection
/// <param name="collectionDefinition">The optional type which contains the collection definition</param>
/// <param name="displayName">The display name for the test collection</param>
public TestCollection(ITestAssembly testAssembly, ITypeInfo collectionDefinition, string displayName)
: this(testAssembly, collectionDefinition, displayName, Guid.NewGuid()) { }
: this(testAssembly, collectionDefinition, displayName, null) { }

internal TestCollection(ITestAssembly testAssembly, ITypeInfo collectionDefinition, string displayName, Guid uniqueId)
/// <summary>
/// Initializes a new instance of the <see cref="TestCollection"/> class.
/// </summary>
/// <param name="testAssembly">The test assembly the collection belongs to</param>
/// <param name="collectionDefinition">The optional type which contains the collection definition</param>
/// <param name="displayName">The display name for the test collection</param>
/// <param name="uniqueId">The test collection's unique ID</param>
public TestCollection(ITestAssembly testAssembly, ITypeInfo collectionDefinition, string displayName, Guid? uniqueId)
{
Guard.ArgumentNotNull("testAssembly", testAssembly);

CollectionDefinition = collectionDefinition;
DisplayName = displayName;
TestAssembly = testAssembly;
UniqueID = uniqueId;
UniqueID = uniqueId ?? Guid.NewGuid();
}

/// <inheritdoc/>
Expand Down
6 changes: 6 additions & 0 deletions src/xunit.runner.msbuild/Utility/MSBuildLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public void LogMessage(StackFrameInfo stackFrame, string message)
Log.LogMessage("{0}", message);
}

public void LogRaw(string message)
{
// We log with high importance, to make sure the message is always output.
Log.LogMessage(MessageImportance.High, "{0}", message);
}

public void LogWarning(StackFrameInfo stackFrame, string message)
{
if (stackFrame.IsEmpty)
Expand Down
2 changes: 1 addition & 1 deletion src/xunit.runner.reporters/TeamCityReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public string RunnerSwitch
=> "teamcity";

public IMessageSink CreateMessageHandler(IRunnerLogger logger)
=> new TeamCityReporterMessageHandler(logger);
=> new TeamCityReporterMessageHandler(logger, EnvironmentHelper.GetEnvironmentVariable("TEAMCITY_PROCESS_FLOW_ID"));
}
}
245 changes: 148 additions & 97 deletions src/xunit.runner.reporters/TeamCityReporterMessageHandler.cs

Large diffs are not rendered by default.

33 changes: 0 additions & 33 deletions src/xunit.runner.reporters/Utility/TeamCityDisplayNameFormatter.cs

This file was deleted.

8 changes: 8 additions & 0 deletions src/xunit.runner.utility/Extensibility/IRunnerLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,13 @@ public interface IRunnerLogger
/// <param name="stackFrame">The stack frame information</param>
/// <param name="message">The message to be logged</param>
void LogError(StackFrameInfo stackFrame, string message);

/// <summary>
/// Logs a messages with as little processing as possible. For example, the console runner will
/// not attempt to set the color of the text that's being logged. This is most useful when attempting
/// to render text lines that will be processed, like for TeamCity.
/// </summary>
/// <param name="message">The message to be logged.</param>
void LogRaw(string message);
}
}
23 changes: 23 additions & 0 deletions src/xunit.runner.utility/Extensions/IAssemblyInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Reflection;
using Xunit.Abstractions;

/// <summary>
/// Extension methods for <see cref="IAssemblyInfo"/>.
/// </summary>
public static class IAssemblyInfoExtensions
{
/// <summary>
/// Computes the simple assembly name from <see cref="IAssemblyInfo.Name"/>.
/// </summary>
/// <returns>The simple assembly name.</returns>
public static string SimpleAssemblyName(this IAssemblyInfo assemblyInfo)
{
Guard.ArgumentNotNull(nameof(assemblyInfo), assemblyInfo);
Guard.ArgumentNotNullOrEmpty($"{nameof(assemblyInfo)}.{nameof(IAssemblyInfo.Name)}", assemblyInfo.Name);

var parsedAssemblyName = new AssemblyName(assemblyInfo.Name);
Guard.ArgumentValid(nameof(assemblyInfo), $"{nameof(assemblyInfo)}.{nameof(IAssemblyInfo.Name)} must include a name component", !string.IsNullOrEmpty(parsedAssemblyName.Name));

return parsedAssemblyName.Name;
}
}
7 changes: 7 additions & 0 deletions src/xunit.runner.utility/Utility/ConsoleRunnerLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public void LogMessage(StackFrameInfo stackFrame, string message)
Console.WriteLine(message);
}

/// <inheritdoc/>
public void LogRaw(string message)
{
lock (LockObject)
Console.WriteLine(message);
}

/// <inheritdoc/>
public void LogWarning(StackFrameInfo stackFrame, string message)
{
Expand Down
42 changes: 29 additions & 13 deletions test/test.utility/TestDoubles/Mocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

public static class Mocks
{
public static IAssemblyInfo AssemblyInfo(ITypeInfo[] types = null, IReflectionAttributeInfo[] attributes = null, string assemblyFileName = null)
public static IAssemblyInfo AssemblyInfo(ITypeInfo[] types = null, IReflectionAttributeInfo[] attributes = null, string assemblyFileName = null, string assemblyName = null)
{
attributes = attributes ?? new IReflectionAttributeInfo[0];

var result = Substitute.For<IAssemblyInfo, InterfaceProxy<IAssemblyInfo>>();
result.Name.Returns(assemblyFileName == null ? "assembly:" + Guid.NewGuid().ToString("n") : Path.GetFileNameWithoutExtension(assemblyFileName));
result.Name.Returns(assemblyName ?? (assemblyFileName == null ? "assembly:" + Guid.NewGuid().ToString("n") : Path.GetFileNameWithoutExtension(assemblyFileName)));
result.AssemblyPath.Returns(assemblyFileName);
result.GetType("").ReturnsForAnyArgs(types?.FirstOrDefault());
result.GetTypes(true).ReturnsForAnyArgs(types ?? new ITypeInfo[0]);
Expand Down Expand Up @@ -192,9 +192,9 @@ public static ITestAssembly TestAssembly(IReflectionAttributeInfo[] attributes)
return result;
}

public static ITestAssembly TestAssembly(string assemblyFileName, string configFileName = null, ITypeInfo[] types = null, IReflectionAttributeInfo[] attributes = null)
public static ITestAssembly TestAssembly(string assemblyFileName, string configFileName = null, ITypeInfo[] types = null, IReflectionAttributeInfo[] attributes = null, string assemblyName = null)
{
var assemblyInfo = AssemblyInfo(types, attributes, assemblyFileName);
var assemblyInfo = AssemblyInfo(types, attributes, assemblyFileName, assemblyName);

var result = Substitute.For<ITestAssembly, InterfaceProxy<ITestAssembly>>();
result.Assembly.Returns(assemblyInfo);
Expand Down Expand Up @@ -257,9 +257,9 @@ public static ITestAssemblyExecutionStarting TestAssemblyExecutionStarting(bool
return result;
}

public static ITestAssemblyFinished TestAssemblyFinished(int testsRun = 2112, int testsFailed = 42, int testsSkipped = 6, decimal executionTime = 123.4567M)
public static ITestAssemblyFinished TestAssemblyFinished(int testsRun = 2112, int testsFailed = 42, int testsSkipped = 6, decimal executionTime = 123.4567M, string assemblyFileName = "testAssembly.dll", string assemblyName = null)
{
var testAssembly = TestAssembly("testAssembly.dll");
var testAssembly = TestAssembly(assemblyFileName, assemblyName: assemblyName);
var result = Substitute.For<ITestAssemblyFinished, InterfaceProxy<ITestAssemblyFinished>>();
result.TestAssembly.Returns(testAssembly);
result.TestsRun.Returns(testsRun);
Expand All @@ -269,9 +269,9 @@ public static ITestAssemblyFinished TestAssemblyFinished(int testsRun = 2112, in
return result;
}

public static ITestAssemblyStarting TestAssemblyStarting()
public static ITestAssemblyStarting TestAssemblyStarting(string assemblyFileName = "testAssembly.dll", string assemblyName = null)
{
var testAssembly = TestAssembly("testAssembly.dll");
var testAssembly = TestAssembly(assemblyFileName, assemblyName: assemblyName);
var result = Substitute.For<ITestAssemblyStarting, InterfaceProxy<ITestAssemblyStarting>>();
result.TestAssembly.Returns(testAssembly);
return result;
Expand Down Expand Up @@ -346,19 +346,21 @@ public static TestClass TestClass(Type type, ITestCollection collection = null)
return new TestClass(collection, Reflector.Wrap(type));
}

public static TestCollection TestCollection(Assembly assembly = null, ITypeInfo definition = null, string displayName = null)
public static TestCollection TestCollection(Assembly assembly = null, ITypeInfo definition = null, string displayName = null, Guid? uniqueID = null)
{
if (assembly == null)
assembly = typeof(Mocks).GetTypeInfo().Assembly;
if (displayName == null)
displayName = "Mock test collection for " + assembly.CodeBase;

return new TestCollection(TestAssembly(assembly), definition, displayName);
return new TestCollection(TestAssembly(assembly), definition, displayName, uniqueID);
}

public static ITestCollectionFinished TestCollectionFinished(string displayName = "Display Name", int testsRun = 2112, int testsFailed = 42, int testsSkipped = 6, decimal executionTime = 123.4567M)
public static ITestCollectionFinished TestCollectionFinished(string displayName = "Display Name", int testsRun = 2112, int testsFailed = 42, int testsSkipped = 6, decimal executionTime = 123.4567M, string assemblyFileName = null)
{
var testAssembly = TestAssembly(assemblyFileName);
var result = Substitute.For<ITestCollectionFinished, InterfaceProxy<ITestCollectionFinished>>();
result.TestAssembly.Returns(testAssembly);
result.TestsRun.Returns(testsRun);
result.TestsFailed.Returns(testsFailed);
result.TestsSkipped.Returns(testsSkipped);
Expand All @@ -367,10 +369,12 @@ public static ITestCollectionFinished TestCollectionFinished(string displayName
return result;
}

public static ITestCollectionStarting TestCollectionStarting()
public static ITestCollectionStarting TestCollectionStarting(string displayName = "Display Name", string assemblyFileName = null)
{
var testAssembly = TestAssembly(assemblyFileName);
var result = Substitute.For<ITestCollectionStarting, InterfaceProxy<ITestCollectionStarting>>();
result.TestCollection.DisplayName.Returns("Display Name");
result.TestAssembly.Returns(testAssembly);
result.TestCollection.DisplayName.Returns(displayName);
return result;
}

Expand Down Expand Up @@ -422,6 +426,18 @@ public static ITestFailed TestFailed(string displayName, decimal executionTime,
return result;
}

public static ITestFinished TestFinished(string displayName, string output = null, decimal executionTime = 0M)
{
var testCase = TestCase();
var test = Test(testCase, displayName);
var result = Substitute.For<ITestFinished, InterfaceProxy<ITestFinished>>();
result.ExecutionTime.Returns(executionTime);
result.Output.Returns(output);
result.TestCase.Returns(testCase);
result.Test.Returns(test);
return result;
}

public static IReflectionAttributeInfo TestFrameworkAttribute(Type type)
{
var attribute = Activator.CreateInstance(type);
Expand Down
5 changes: 5 additions & 0 deletions test/test.utility/TestDoubles/SpyRunnerLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public void LogMessage(StackFrameInfo stackFrame, string message)
AddMessage("---", stackFrame, message);
}

public void LogRaw(string message)
{
AddMessage("Raw", StackFrameInfo.None, message);
}

public void LogWarning(StackFrameInfo stackFrame, string message)
{
AddMessage("Wrn", stackFrame, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public static IEnumerable<object[]> Messages
};
yield return new object[]
{
Mocks.TestCollectionStarting(),
@"{""message"":""testCollectionStarting"",""flowId"":""mappedFlow"",""assembly"":"""",""collectionName"":""Display Name"",""collectionId"":""00000000-0000-0000-0000-000000000000""}"
Mocks.TestCollectionStarting(assemblyFileName: "assembly.dll"),
@"{""message"":""testCollectionStarting"",""flowId"":""mappedFlow"",""assembly"":""assembly"",""collectionName"":""Display Name"",""collectionId"":""00000000-0000-0000-0000-000000000000""}"
};
yield return new object[]
{
Mocks.TestCollectionFinished(),
@"{""message"":""testCollectionFinished"",""flowId"":""mappedFlow"",""assembly"":"""",""collectionName"":""Display Name"",""collectionId"":""00000000-0000-0000-0000-000000000000"",""executionTime"":123.4567,""testsFailed"":42,""testsRun"":2112,""testsSkipped"":6}"
Mocks.TestCollectionFinished(assemblyFileName: "assembly.dll"),
@"{""message"":""testCollectionFinished"",""flowId"":""mappedFlow"",""assembly"":""assembly"",""collectionName"":""Display Name"",""collectionId"":""00000000-0000-0000-0000-000000000000"",""executionTime"":123.4567,""testsFailed"":42,""testsRun"":2112,""testsSkipped"":6}"
};

// IErrorMessage
Expand Down

0 comments on commit cd43469

Please sign in to comment.