Skip to content

Commit

Permalink
Add support for DesignMode.
Browse files Browse the repository at this point in the history
Turn off Source Information query when design mode is false (command line scenario).
  • Loading branch information
Arun Mahapatra authored and bradwilson committed Feb 17, 2017
1 parent 193bdb2 commit b326add
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 38 deletions.
33 changes: 30 additions & 3 deletions test.xunit.runner.visualstudio.desktop/RunSettingsHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void RunSettingsHelperShouldNotThrowExceptionOnInvalidValuesForElements()
<RunConfiguration>
<DisableAppDomain>1234</DisableAppDomain>
<DisableParallelization>smfhekhgekr</DisableParallelization>
<DesignMode>3245sax</DesignMode>
</RunConfiguration>
</RunSettings>";

Expand Down Expand Up @@ -55,8 +56,9 @@ public void RunSettingsHelperShouldUseDefaultValuesInCaseOfIncorrectSchemaAndIgn

// Attribute must be ignored
Assert.True(RunSettingsHelper.DisableAppDomain);
// Default value must be used for disableparallelization
// Default value must be used for disableparallelization, designmode
Assert.False(RunSettingsHelper.DisableParallelization);
Assert.True(RunSettingsHelper.DesignMode);
}

[Fact]
Expand All @@ -73,8 +75,9 @@ Random Text

RunSettingsHelper.ReadRunSettings(settingsXml);

// Default value must be used for DisableAppDomain
// Default value must be used for DisableAppDomain, DesignMode
Assert.False(RunSettingsHelper.DisableAppDomain);
Assert.True(RunSettingsHelper.DesignMode);
// DisableParallelization can be set
Assert.True(RunSettingsHelper.DisableParallelization);
}
Expand All @@ -97,11 +100,35 @@ public void RunSettingsHelperShouldReadValuesCorrectly(bool disableAppDomain, bo

RunSettingsHelper.ReadRunSettings(settingsXml);

// Correct values must be sets
// Correct values must be set
Assert.Equal(disableAppDomain, RunSettingsHelper.DisableAppDomain);
Assert.Equal(disableParallelization, RunSettingsHelper.DisableParallelization);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void RunSettingsHelperShouldReadDesignModeSettingCorrectly(bool designMode)
{
string settingsXml =
$@"<?xml version=""1.0"" encoding=""utf-8""?>
<RunSettings>
<RunConfiguration>
<DisableAppDomain>true</DisableAppDomain>
<DisableParallelization>invalid</DisableParallelization>
<DesignMode>{designMode.ToString().ToLowerInvariant()}</DesignMode>
</RunConfiguration>
</RunSettings>";

RunSettingsHelper.ReadRunSettings(settingsXml);

// Correct values must be set
Assert.Equal(designMode, RunSettingsHelper.DesignMode);
Assert.True(RunSettingsHelper.DisableAppDomain);
// Default value should be set for DisableParallelization
Assert.False(RunSettingsHelper.DisableParallelization);
}

[Fact]
public void RunSettingsHelperShouldIgnoreEvenIfAdditionalElementsExist()
{
Expand Down
16 changes: 12 additions & 4 deletions xunit.runner.visualstudio.desktop/Sinks/VsDiscoverySink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class VsDiscoverySink : IMessageSinkWithTypes, IVsDiscoverySink, IDisposa
readonly List<ITestCase> lastTestClassTestCases = new List<ITestCase>();
readonly LoggerHelper logger;
readonly string source;
readonly bool designMode;

string lastTestClass;

Expand All @@ -45,13 +46,15 @@ public VsDiscoverySink(string source,
LoggerHelper logger,
ITestCaseDiscoverySink discoverySink,
ITestFrameworkDiscoveryOptions discoveryOptions,
bool designMode,
Func<bool> cancelThunk)
{
this.source = source;
this.discoverer = discoverer;
this.logger = logger;
this.discoverySink = discoverySink;
this.discoveryOptions = discoveryOptions;
this.designMode = designMode;
this.cancelThunk = cancelThunk;

discoveryEventSink.TestCaseDiscoveryMessageEvent += HandleTestCaseDiscoveryMessage;
Expand All @@ -68,7 +71,7 @@ public void Dispose()
discoveryEventSink.Dispose();
}

public static TestCase CreateVsTestCase(string source, ITestFrameworkDiscoverer discoverer, ITestCase xunitTestCase, bool forceUniqueName, LoggerHelper logger)
public static TestCase CreateVsTestCase(string source, ITestFrameworkDiscoverer discoverer, ITestCase xunitTestCase, bool forceUniqueName, LoggerHelper logger, bool designMode)
{
try
{
Expand All @@ -92,8 +95,13 @@ public static TestCase CreateVsTestCase(string source, ITestFrameworkDiscoverer
}
}

result.CodeFilePath = xunitTestCase.SourceInformation.FileName;
result.LineNumber = xunitTestCase.SourceInformation.LineNumber.GetValueOrDefault();
if (designMode)
{
// Source information is not required for non-design mode i.e. command line test runs.
// See RunSettingsHelper.DesignMode for more details.
result.CodeFilePath = xunitTestCase.SourceInformation.FileName;
result.LineNumber = xunitTestCase.SourceInformation.LineNumber.GetValueOrDefault();
}

return result;
}
Expand Down Expand Up @@ -206,7 +214,7 @@ private void SendExistingTestCases()

foreach (var testCase in lastTestClassTestCases)
{
var vsTestCase = CreateVsTestCase(source, discoverer, testCase, forceUniqueNames, logger);
var vsTestCase = CreateVsTestCase(source, discoverer, testCase, forceUniqueNames, logger, designMode);
if (vsTestCase != null)
{
if (discoveryOptions.GetDiagnosticMessagesOrDefault())
Expand Down
1 change: 0 additions & 1 deletion xunit.runner.visualstudio.desktop/Sinks/VsExecutionSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ public override bool OnMessageWithTypes(IMessageSinkMessage message, HashSet<str
{
var result = innerSink.OnMessageWithTypes(message, messageTypes);
return base.OnMessageWithTypes(message, messageTypes) && result;

}
}
}
16 changes: 16 additions & 0 deletions xunit.runner.visualstudio.desktop/Utility/RunSettingsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ public static class RunSettingsHelper
public static bool DisableAppDomain { get; private set; }

public static bool DisableParallelization { get; private set; }

/// <summary>
/// Design mode indicates the context of a test run. True indicates the test run is invoked from an editor
/// or IDE.
/// </summary>
public static bool DesignMode { get; private set; }
/*
public static bool NoAutoReporters { get; private set; }
public static string ReporterSwitch { get; private set; }
Expand All @@ -22,6 +28,10 @@ public static void ReadRunSettings(string runSettingsXml)
// reset first, do not want to propagate earlier settings in cases where execution host is kept alive
DisableAppDomain = false;
DisableParallelization = false;

// We're keeping the default value as true since the adapter (prior to VS 2017) shouldn't
// differentiate between VS or vstest.console.
DesignMode = true;
//NoAutoReporters = false;


Expand All @@ -42,6 +52,12 @@ public static void ReadRunSettings(string runSettingsXml)
bool disableParallelization;
if (bool.TryParse(disableParallelizationString, out disableParallelization))
DisableParallelization = disableParallelization;

// It is set to True if test is running from an Editor/IDE context.
var designModeString = element.Element("DesignMode")?.Value;
bool designMode;
if (bool.TryParse(designModeString, out designMode))
DesignMode = designMode;
/*
var noAutoReportersString = element.Element("NoAutoReporters")?.Value;
bool noAutoReporters;
Expand Down
60 changes: 30 additions & 30 deletions xunit.runner.visualstudio.desktop/VsTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,15 @@ void ITestDiscoverer.DiscoverTests(IEnumerable<string> sources, IDiscoveryContex
var stopwatch = Stopwatch.StartNew();
var loggerHelper = new LoggerHelper(logger, stopwatch);

DiscoverTests(
discoveryContext?.RunSettings,
sources,
#if NET35 || NETCOREAPP1_0
// Reads settings like disabling appdomains, parallel etc.
// Do this first before invoking any thing else to ensure correct settings for the run
RunSettingsHelper.ReadRunSettings(discoveryContext?.RunSettings?.SettingsXml);
#endif

DiscoverTests(sources,
loggerHelper,
(source, discoverer, discoveryOptions) => new VsDiscoverySink(source, discoverer, loggerHelper, discoverySink, discoveryOptions, () => cancelled)
(source, discoverer, discoveryOptions) => new VsDiscoverySink(source, discoverer, loggerHelper, discoverySink, discoveryOptions, RunSettingsHelper.DesignMode, () => cancelled)
);
}

Expand All @@ -94,6 +98,12 @@ void ITestExecutor.RunTests(IEnumerable<string> sources, IRunContext runContext,
var stopwatch = Stopwatch.StartNew();
var logger = new LoggerHelper(frameworkHandle, stopwatch);

#if NET35 || NETCOREAPP1_0
// Reads settings like disabling appdomains, parallel etc.
// Do this first before invoking any thing else to ensure correct settings for the run
RunSettingsHelper.ReadRunSettings(runContext?.RunSettings?.SettingsXml);
#endif

// In this case, we need to go thru the files manually
if (ContainsAppX(sources))
{
Expand All @@ -112,7 +122,7 @@ void ITestExecutor.RunTests(IEnumerable<string> sources, IRunContext runContext,
.Where(file => !platformAssemblies.Contains(Path.GetFileName(file))));
}

RunTests(runContext, frameworkHandle, logger, () => GetTests(sources, logger, runContext));
RunTests(runContext, frameworkHandle, logger, () => GetTests(sources, logger, runContext, RunSettingsHelper.DesignMode));
}

void ITestExecutor.RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle)
Expand All @@ -123,6 +133,12 @@ void ITestExecutor.RunTests(IEnumerable<TestCase> tests, IRunContext runContext,
var stopwatch = Stopwatch.StartNew();
var logger = new LoggerHelper(frameworkHandle, stopwatch);

#if NET35 || NETCOREAPP1_0
// Reads settings like disabling appdomains, parallel etc.
// Do this first before invoking any thing else to ensure correct settings for the run
RunSettingsHelper.ReadRunSettings(runContext?.RunSettings?.SettingsXml);
#endif

RunTests(
runContext, frameworkHandle, logger,
() => tests.GroupBy(testCase => testCase.Source)
Expand All @@ -149,10 +165,10 @@ static ITestCase Deserialize(LoggerHelper logger, ITestFrameworkExecutor executo
}
}

void DiscoverTests<TVisitor>(IRunSettings runSettings,
IEnumerable<string> sources,
void DiscoverTests<TVisitor>(IEnumerable<string> sources,
LoggerHelper logger,
Func<string, ITestFrameworkDiscoverer, ITestFrameworkDiscoveryOptions, TVisitor> visitorFactory,
Func<string, ITestFrameworkDiscoverer,
ITestFrameworkDiscoveryOptions, TVisitor> visitorFactory,
Action<string, ITestFrameworkDiscoverer, ITestFrameworkDiscoveryOptions, TVisitor> visitComplete = null)
where TVisitor : IVsDiscoverySink, IDisposable
{
Expand All @@ -162,13 +178,6 @@ void DiscoverTests<TVisitor>(IRunSettings runSettings,

using (AssemblyHelper.SubscribeResolve())
{

#if NET35 || NETCOREAPP1_0
// Reads settings like disabling appdomains, parallel etc.
// Do this first before invoking any thing else to ensure correct settings for the run
RunSettingsHelper.ReadRunSettings(runSettings?.SettingsXml);
#endif

var reporterMessageHandler = GetRunnerReporter(sources).CreateMessageHandler(new VisualStudioRunnerLogger(logger));

foreach (var assemblyFileNameCanBeWithoutAbsolutePath in sources)
Expand Down Expand Up @@ -214,7 +223,7 @@ void DiscoverTests<TVisitor>(IRunSettings runSettings,
{
reporterMessageHandler.OnMessage(new TestAssemblyDiscoveryStarting(assembly, framework.CanUseAppDomains && AppDomainDefaultBehavior != AppDomainSupport.Denied, shadowCopy, discoveryOptions));

framework.Find(includeSourceInformation: true, discoveryMessageSink: visitor, discoveryOptions: discoveryOptions);
framework.Find(includeSourceInformation: RunSettingsHelper.DesignMode, discoveryMessageSink: visitor, discoveryOptions: discoveryOptions);
var totalTests = visitor.Finish();

visitComplete?.Invoke(assemblyFileName, framework, discoveryOptions, visitor);
Expand Down Expand Up @@ -277,7 +286,7 @@ static Stream GetConfigurationStreamForAssembly(string assemblyName)
static TestProperty GetTestProperty()
=> TestProperty.Register("XunitTestCase", "xUnit.net Test Case", typeof(string), typeof(VsTestRunner));

List<AssemblyRunInfo> GetTests(IEnumerable<string> sources, LoggerHelper logger, IRunContext runContext)
List<AssemblyRunInfo> GetTests(IEnumerable<string> sources, LoggerHelper logger, IRunContext runContext, bool designMode)
{
// For store apps, the files are copied to the AppX dir, we need to load it from there
#if WINDOWS_UAP
Expand All @@ -286,9 +295,7 @@ List<AssemblyRunInfo> GetTests(IEnumerable<string> sources, LoggerHelper logger,

var assemblyDiscoveredInfos = new List<AssemblyDiscoveredInfo>();

DiscoverTests(
runContext?.RunSettings,
sources,
DiscoverTests(sources,
logger,
(source, discoverer, discoveryOptions) => new VsExecutionDiscoverySink(() => cancelled),
(source, discoverer, discoveryOptions, visitor) =>
Expand All @@ -300,7 +307,7 @@ List<AssemblyRunInfo> GetTests(IEnumerable<string> sources, LoggerHelper logger,
assemblyDiscoveredInfos.Add(new AssemblyDiscoveredInfo
{
AssemblyFileName = source,
DiscoveredTestCases = visitor.TestCases.Select(testCase => new DiscoveredTestCase(source, discoverer, testCase, logger)).ToList()
DiscoveredTestCases = visitor.TestCases.Select(testCase => new DiscoveredTestCase(source, discoverer, testCase, logger, designMode)).ToList()
});
}
);
Expand Down Expand Up @@ -384,12 +391,6 @@ void RunTests(IRunContext runContext, IFrameworkHandle frameworkHandle, LoggerHe
{
RemotingUtility.CleanUpRegisteredChannels();

#if NET35 || NETCOREAPP1_0
// Reads settings like disabling appdomains, parallel etc.
// Do this first before invoking any thing else to ensure correct settings for the run
RunSettingsHelper.ReadRunSettings(runContext?.RunSettings?.SettingsXml);
#endif

cancelled = false;

var assemblies = testCaseAccessor();
Expand Down Expand Up @@ -461,7 +462,6 @@ void RunTestsInAssembly(IRunContext runContext,

using (var vsExecutionSink = new VsExecutionSink(reporterMessageHandler, frameworkHandle, logger, xunitTestCases, executionOptions, () => cancelled))
{

IExecutionSink resultsSink = vsExecutionSink;
if (longRunningSeconds > 0)
resultsSink = new DelegatingLongRunningTestDetectionSink(resultsSink, TimeSpan.FromSeconds(longRunningSeconds), diagnosticSink);
Expand Down Expand Up @@ -648,11 +648,11 @@ class DiscoveredTestCase

string uniqueID;

public DiscoveredTestCase(string source, ITestFrameworkDiscoverer discoverer, ITestCase testCase, LoggerHelper logger)
public DiscoveredTestCase(string source, ITestFrameworkDiscoverer discoverer, ITestCase testCase, LoggerHelper logger, bool designMode)
{
Name = $"{testCase.TestMethod.TestClass.Class.Name}.{testCase.TestMethod.Method.Name}";
TraitNames = testCase.Traits.Keys;
VSTestCase = VsDiscoverySink.CreateVsTestCase(source, discoverer, testCase, forceUniqueName: false, logger: logger);
VSTestCase = VsDiscoverySink.CreateVsTestCase(source, discoverer, testCase, forceUniqueName: false, logger: logger, designMode: designMode);
uniqueID = testCase.UniqueID;
}

Expand Down

0 comments on commit b326add

Please sign in to comment.