Skip to content

Commit

Permalink
Add InitializeLifetimeService() (#97)
Browse files Browse the repository at this point in the history
* Add InitializeLifetimeService()

* Improve logging

* Remove stopwatch

* Add logging and make exeption logging consitent

* ignore if we can't report the error

* Add IFrameworkLogger instead

* Add IFrameworkLogger
  • Loading branch information
mikeblakeuk authored Apr 17, 2020
1 parent 444052d commit 9781660
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@

namespace Machine.VSTestAdapter.Discovery.BuiltIn
{

public class TestDiscoverer
#if !NETSTANDARD
: MarshalByRefObject
#endif
{

#if !NETSTANDARD
[System.Security.SecurityCritical]
public override object InitializeLifetimeService()
{
return null;
}
#endif
private readonly PropertyInfo behaviorProperty = typeof(BehaviorSpecification).GetProperty("BehaviorFieldInfo");

public IEnumerable<MSpecTestCase> DiscoverTests(string assemblyPath)
Expand Down Expand Up @@ -48,7 +53,7 @@ private IEnumerable<MSpecTestCase> CreateTestCase(Context context, SourceCodeLoc
fieldDeclaringType = spec.FieldInfo.DeclaringType.GetGenericTypeDefinition().FullName;
else
fieldDeclaringType = spec.FieldInfo.DeclaringType.FullName;

SourceCodeLocationInfo locationInfo = locationFinder.GetFieldLocation(fieldDeclaringType, spec.FieldInfo.Name);
if (locationInfo != null)
{
Expand Down Expand Up @@ -89,13 +94,6 @@ private string GetContextDisplayName(Type contextType)

return displayName;
}

#if !NETSTANDARD
public override object InitializeLifetimeService()
{
return null;
}
#endif
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;

namespace Machine.VSTestAdapter.Execution
{
public interface IFrameworkLogger
{
void SendMessage(TestMessageLevel level, string message);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Machine.VSTestAdapter.Helpers;
using Machine.VSTestAdapter.Configuration;

Expand All @@ -29,7 +27,7 @@ public void RunAssemblySpecifications(string assemblyPath,

executor.RunTestsInAssembly(assemblyPath, specifications, listener);
#if !NETSTANDARD
}
}
#endif
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public SpecificationFilterProvider()
.ToArray();
}


public IEnumerable<TestCase> FilteredTests(IEnumerable<TestCase> testCases, IRunContext runContext, IFrameworkHandle handle)
{
var filterExpression = runContext.GetTestCaseFilter(supportedProperties, propertyName =>
Expand All @@ -50,13 +49,13 @@ public IEnumerable<TestCase> FilteredTests(IEnumerable<TestCase> testCases, IRun
return null;
});

handle?.SendMessage(TestMessageLevel.Informational, $"Machine Specifications Visual Studio Test Adapter - Filter property set '{filterExpression?.TestCaseFilterValue}'");

if (filterExpression == null)
{
return testCases;
}

handle?.SendMessage(TestMessageLevel.Informational, $"Machine Specifications Visual Studio Test Adapter - Filter property set '{filterExpression.TestCaseFilterValue}'");

var filteredTests = testCases
.Where(testCase => filterExpression.MatchTestCase(testCase, propertyName =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Reflection;
using Machine.Specifications;
using Machine.VSTestAdapter.Helpers;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;

namespace Machine.VSTestAdapter.Execution
{
Expand All @@ -14,15 +16,15 @@ public class TestExecutor
: MarshalByRefObject
#endif
{

#if !NETSTANDARD
[System.Security.SecurityCritical]
public override object InitializeLifetimeService()
{
return null;
}
#endif

private DefaultRunner CreateRunner(Assembly assembly,ISpecificationRunListener specificationRunListener)
private DefaultRunner CreateRunner(Assembly assembly, ISpecificationRunListener specificationRunListener)
{
var listener = new AggregateRunListener(new[] {
specificationRunListener,
Expand Down Expand Up @@ -59,8 +61,23 @@ public void RunTestsInAssembly(string pathToAssembly, IEnumerable<VisualStudioTe
}
finally
{
if (mspecRunner != null && assemblyToRun != null)
mspecRunner.EndRun(assemblyToRun);
try
{
if (mspecRunner != null && assemblyToRun != null)
mspecRunner.EndRun(assemblyToRun);
}
catch (Exception exception)
{
try
{
var frameworkLogger = specificationRunListener as IFrameworkLogger;
frameworkLogger?.SendMessage(TestMessageLevel.Error, "Machine Specifications Visual Studio Test Adapter - Error Ending Test Run." + Environment.NewLine + exception);
}
catch
{
// ignored
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ public class VSProxyAssemblySpecificationRunListener :
#if !NETSTANDARD
MarshalByRefObject,
#endif
ISpecificationRunListener
ISpecificationRunListener, IFrameworkLogger
{
#if !NETSTANDARD
[System.Security.SecurityCritical]
public override object InitializeLifetimeService()
{
return null;
}
#endif
private readonly IFrameworkHandle frameworkHandle;
private readonly string assemblyPath;
private readonly Uri executorUri;
Expand All @@ -23,7 +30,8 @@ public class VSProxyAssemblySpecificationRunListener :
private RunStats currentRunStats;
readonly Settings settings;

public VSProxyAssemblySpecificationRunListener(string assemblyPath, IFrameworkHandle frameworkHandle, Uri executorUri, Settings settings)
public VSProxyAssemblySpecificationRunListener(string assemblyPath, IFrameworkHandle frameworkHandle,
Uri executorUri, Settings settings)
{
if (settings == null)
throw new ArgumentNullException(nameof(settings));
Expand All @@ -48,7 +56,9 @@ public void OnFatalError(ExceptionResult exception)
this.currentRunStats = null;
}

this.frameworkHandle.SendMessage(TestMessageLevel.Error, "Machine Specifications Visual Studio Test Adapter - Fatal error while executing test." + Environment.NewLine + exception.ToString());
this.frameworkHandle.SendMessage(TestMessageLevel.Error,
"Machine Specifications Visual Studio Test Adapter - Fatal error while executing test." +
Environment.NewLine + exception);
}

public void OnSpecificationStart(SpecificationInfo specification)
Expand Down Expand Up @@ -80,13 +90,17 @@ public void OnContextEnd(ContextInfo context)
}


#region Mapping
#region Mapping

private TestCase ConvertSpecificationToTestCase(SpecificationInfo specification, Settings settings)
{
VisualStudioTestIdentifier vsTestId = specification.ToVisualStudioTestIdentifier(currentContext);

return new TestCase(vsTestId.FullyQualifiedName, this.executorUri, this.assemblyPath) {
DisplayName = settings.DisableFullTestNameInOutput ? specification.Name : $"{this.currentContext?.TypeName}.{specification.FieldName}",
return new TestCase(vsTestId.FullyQualifiedName, this.executorUri, this.assemblyPath)
{
DisplayName = settings.DisableFullTestNameInOutput
? specification.Name
: $"{this.currentContext?.TypeName}.{specification.FieldName}",
};
}

Expand All @@ -109,19 +123,20 @@ private static TestOutcome MapSpecificationResultToTestOutcome(Result result)

private static TestResult ConverResultToTestResult(TestCase testCase, Result result, RunStats runStats)
{
TestResult testResult = new TestResult(testCase) {
TestResult testResult = new TestResult(testCase)
{
ComputerName = Environment.MachineName,
Outcome = MapSpecificationResultToTestOutcome(result),
DisplayName = testCase.DisplayName
};

if (result.Exception != null)
if (result.Exception != null)
{
testResult.ErrorMessage = result.Exception.Message;
testResult.ErrorStackTrace = result.Exception.ToString();
}

if (runStats != null)
if (runStats != null)
{
testResult.StartTime = runStats.Start;
testResult.EndTime = runStats.End;
Expand All @@ -131,10 +146,11 @@ private static TestResult ConverResultToTestResult(TestCase testCase, Result res
return testResult;
}

#endregion
#endregion


#region Stubs

#region Stubs
public void OnAssemblyEnd(AssemblyInfo assembly)
{
}
Expand All @@ -150,6 +166,12 @@ public void OnRunEnd()
public void OnRunStart()
{
}
#endregion

#endregion

public void SendMessage(TestMessageLevel level, string message)
{
frameworkHandle?.SendMessage(level, message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public void DiscoverTests(IEnumerable<string> sources, Settings settings, IMessa
}
catch (Exception discoverException)
{
logger.SendMessage(TestMessageLevel.Error, $"Machine Specifications Visual Studio Test Adapter - Error while discovering specifications in assembly {assemblyPath} - {discoverException}");
logger.SendMessage(TestMessageLevel.Error, $"Machine Specifications Visual Studio Test Adapter - Error while discovering specifications in assembly {assemblyPath}." + Environment.NewLine + discoverException);
}
}

logger.SendMessage(TestMessageLevel.Informational, string.Format("Machine Specifications Visual Studio Test Adapter - Discovery Complete - {0} specifications in {2} of {1} assemblies scanned.", discoveredSpecCount, sourcesArray.Count(), sourcesWithSpecs));
logger.SendMessage(TestMessageLevel.Informational, $"Machine Specifications Visual Studio Test Adapter - Discovery Complete - {discoveredSpecCount} specifications in {sourcesWithSpecs} of {sourcesArray.Length} assemblies scanned.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,26 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrame
var testCases = tests.ToArray();
foreach (var grouping in testCases.GroupBy(x => x.Source))
{
currentAssembly = grouping.Key;
totalSpecCount += grouping.Count();

frameworkHandle.SendMessage(TestMessageLevel.Informational, $"Machine Specifications Visual Studio Test Adapter - Executing test cases in {grouping.Key}");

var filteredTests = specificationFilterProvider.FilteredTests(grouping.AsEnumerable(), runContext, frameworkHandle);

var testsToRun = filteredTests
.Select(test => test.ToVisualStudioTestIdentifier())
.ToArray();

frameworkHandle.SendMessage(TestMessageLevel.Informational, $"Machine Specifications Visual Studio Test Adapter - Executing {testsToRun.Length} tests in '{currentAssembly}'.");

executor.RunAssemblySpecifications(grouping.Key, testsToRun, settings, MSpecTestAdapter.Uri, frameworkHandle);
executedSpecCount += testsToRun.Length;
}

frameworkHandle.SendMessage(TestMessageLevel.Informational, $"Machine Specifications Visual Studio Test Adapter - Execution Complete - {executedSpecCount} of {totalSpecCount} specifications in {testCases.GroupBy(x => x.Source).Count()} assemblies.");
}
catch (Exception ex)
catch (Exception exception)
{
frameworkHandle.SendMessage(TestMessageLevel.Error, $"Machine Specifications Visual Studio Test Adapter - Error while executing specifications in assembly {currentAssembly} - {ex}");
frameworkHandle.SendMessage(TestMessageLevel.Error, $"Machine Specifications Visual Studio Test Adapter - Error while executing specifications in assembly '{currentAssembly}'." + Environment.NewLine + exception);
}
}

Expand Down

0 comments on commit 9781660

Please sign in to comment.