Skip to content

Commit

Permalink
Add diagnostic messages around DiaSession failures
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Oct 13, 2023
1 parent a2bcbb8 commit 6438db6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
55 changes: 42 additions & 13 deletions src/xunit.runner.visualstudio/Utility/DiaSessionWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Navigation;
using Xunit.Abstractions;
using Xunit.Internal;

namespace Xunit.Runner.VisualStudio.Utility;

Expand All @@ -13,38 +15,65 @@ class DiaSessionWrapper : IDisposable
readonly AppDomainManager? appDomainManager;
#endif
readonly DiaSessionWrapperHelper? helper;
readonly DiaSession session;
readonly DiaSession? session;
readonly DiagnosticMessageSink diagnosticMessageSink;

public DiaSessionWrapper(string assemblyFileName)
public DiaSessionWrapper(
string assemblyFileName,
DiagnosticMessageSink diagnosticMessageSink)
{
session = new DiaSession(assemblyFileName);
this.diagnosticMessageSink = Guard.ArgumentNotNull(diagnosticMessageSink);

#if NETFRAMEWORK
var adapterFileName = typeof(DiaSessionWrapperHelper).Assembly.GetLocalCodeBase();
if (adapterFileName is not null)
try
{
session = new DiaSession(assemblyFileName);
}
catch (Exception ex)
{
appDomainManager = new AppDomainManager(assemblyFileName);
helper = appDomainManager.CreateObject<DiaSessionWrapperHelper>(typeof(DiaSessionWrapperHelper).Assembly.GetName(), typeof(DiaSessionWrapperHelper).FullName!, adapterFileName);
diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Exception creating DiaSession: {ex}"));
}

try
{
#if NETFRAMEWORK
var adapterFileName = typeof(DiaSessionWrapperHelper).Assembly.GetLocalCodeBase();
if (adapterFileName is not null)
{
appDomainManager = new AppDomainManager(assemblyFileName);
helper = appDomainManager.CreateObject<DiaSessionWrapperHelper>(typeof(DiaSessionWrapperHelper).Assembly.GetName(), typeof(DiaSessionWrapperHelper).FullName!, adapterFileName);
}
#else
helper = new DiaSessionWrapperHelper(assemblyFileName);
helper = new DiaSessionWrapperHelper(assemblyFileName);
#endif
}
catch (Exception ex)
{
diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Exception creating DiaSessionWrapperHelper: {ex}"));
}
}

public INavigationData? GetNavigationData(
string typeName,
string methodName)
{
if (helper is null)
if (session is null || helper is null)
return null;

helper.Normalize(ref typeName, ref methodName);
return session.GetNavigationDataForMethod(typeName, methodName);
try
{
helper.Normalize(ref typeName, ref methodName);
return session.GetNavigationDataForMethod(typeName, methodName);
}
catch (Exception ex)
{
diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Exception getting source mapping for {typeName}.{methodName}: {ex}"));
return null;
}
}

public void Dispose()
{
session.Dispose();
session?.Dispose();
#if NETFRAMEWORK
appDomainManager?.Dispose();
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ public class VisualStudioSourceInformationProvider : LongLivedMarshalByRefObject
/// Initializes a new instance of the <see cref="VisualStudioSourceInformationProvider" /> class.
/// </summary>
/// <param name="assemblyFileName">The assembly file name.</param>
public VisualStudioSourceInformationProvider(string assemblyFileName)
/// <param name="diagnosticMessageSink">The message sink to send internal diagnostic messages to.</param>
public VisualStudioSourceInformationProvider(
string assemblyFileName,
DiagnosticMessageSink diagnosticMessageSink)
{
session = new DiaSessionWrapper(assemblyFileName);
session = new DiaSessionWrapper(assemblyFileName, diagnosticMessageSink);
}

/// <inheritdoc/>
Expand Down
4 changes: 2 additions & 2 deletions src/xunit.runner.visualstudio/VsTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ void DiscoverTests<TVisitor>(
var diagnosticSink = DiagnosticMessageSink.ForDiagnostics(logger, fileName, assembly.Configuration.DiagnosticMessagesOrDefault);
var appDomain = assembly.Configuration.AppDomain ?? AppDomainDefaultBehavior;

using var sourceInformationProvider = new VisualStudioSourceInformationProvider(assembly.AssemblyFilename);
using var sourceInformationProvider = new VisualStudioSourceInformationProvider(assembly.AssemblyFilename, diagnosticSink);
using var controller = new XunitFrontController(appDomain, assembly.AssemblyFilename, shadowCopy: shadowCopy, sourceInformationProvider: sourceInformationProvider, diagnosticMessageSink: MessageSinkAdapter.Wrap(diagnosticSink));
if (!DiscoverTestsInSource(controller, logger, testPlatformContext, runSettings, visitorFactory, visitComplete, assembly))
break;
Expand Down Expand Up @@ -428,7 +428,7 @@ void RunTestsInAssembly(

var diagnosticSink = DiagnosticMessageSink.ForDiagnostics(logger, assemblyDisplayName, runInfo.Assembly.Configuration.DiagnosticMessagesOrDefault);
var diagnosticMessageSink = MessageSinkAdapter.Wrap(diagnosticSink);
using var sourceInformationProvider = new VisualStudioSourceInformationProvider(assemblyFileName);
using var sourceInformationProvider = new VisualStudioSourceInformationProvider(assemblyFileName, diagnosticSink);
using var controller = new XunitFrontController(appDomain, assemblyFileName, shadowCopy: shadowCopy, sourceInformationProvider: sourceInformationProvider, diagnosticMessageSink: diagnosticMessageSink);
var testCasesMap = new Dictionary<string, TestCase>();
var testCases = new List<ITestCase>();
Expand Down

0 comments on commit 6438db6

Please sign in to comment.