Skip to content

Commit

Permalink
Close #2: Use .runsettings framework. Merge global options, solution …
Browse files Browse the repository at this point in the history
…config file and Test>Settings>File.

Bugfix: location and execution of setup and teardown batch files is now relative to solution dir
(Merge branch 'GetOptionsViaRunsettingsNotRegistry')
  • Loading branch information
jgefele committed Nov 15, 2015
2 parents 154d7b3 + 8b9cdc7 commit da008bb
Show file tree
Hide file tree
Showing 17 changed files with 584 additions and 225 deletions.
31 changes: 23 additions & 8 deletions GoogleTestExtension/GoogleTestAdapter/GoogleTestAdapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,37 @@
<AssemblyOriginatorKeyFile>Key.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="envdte, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="envdte100, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="envdte80, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="envdte90, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="envdte90a, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.VisualStudio.TestPlatform.ObjectModel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<Reference Include="Microsoft.VisualStudio.Shell.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.TestPlatform.ObjectModel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\lib\Microsoft.VisualStudio.TestPlatform.ObjectModel.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestWindow, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<Reference Include="Microsoft.VisualStudio.TestWindow, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\lib\Microsoft.VisualStudio.TestWindow.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestWindow.Core, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<Reference Include="Microsoft.VisualStudio.TestWindow.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\lib\Microsoft.VisualStudio.TestWindow.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestWindow.Interfaces, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\lib\Microsoft.VisualStudio.TestWindow.Interfaces.dll</HintPath>
</Reference>
<Reference Include="msdia140typelib_clr0200">
<HintPath>..\lib\msdia140typelib_clr0200.dll</HintPath>
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
Expand All @@ -70,6 +85,7 @@
<Compile Include="Helpers\TestEnvironment.cs" />
<Compile Include="Helpers\RegexTraitParser.cs" />
<Compile Include="Helpers\Utils.cs" />
<Compile Include="Helpers\XmlOptions.cs" />
<Compile Include="Runners\PreparingTestRunner.cs" />
<Compile Include="Runners\SequentialTestRunner.cs" />
<Compile Include="Runners\ITestRunner.cs" />
Expand All @@ -81,7 +97,6 @@
<Compile Include="Dia\DiaResolver.cs" />
<Compile Include="Runners\CommandLineGenerator.cs" />
<Compile Include="TestResults\XmlTestResultParser.cs" />
<Compile Include="Helpers\RegistryReader.cs" />
<Compile Include="Dia\DiaMemoryStream.cs" />
<Compile Include="Scheduling\ITestsSplitter.cs" />
<Compile Include="Dia\NativeMethods.cs" />
Expand Down
3 changes: 3 additions & 0 deletions GoogleTestExtension/GoogleTestAdapter/GoogleTestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

public static class GoogleTestConstants
{
public const string SettingsName = "GoogleTestAdapter";
public const string SettingsExtension = ".gta.runsettings";
public const string DurationsExtension = ".gta.testdurations";

public const string AlsoRunDisabledTestsOption = " --gtest_also_run_disabled_tests";
public const string ShuffleTestsOption = " --gtest_shuffle";
Expand Down
17 changes: 9 additions & 8 deletions GoogleTestExtension/GoogleTestAdapter/GoogleTestDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public GoogleTestDiscoverer(TestEnvironment testEnvironment)
public void DiscoverTests(IEnumerable<string> executables, IDiscoveryContext discoveryContext,
IMessageLogger logger, ITestCaseDiscoverySink discoverySink)
{
InitTestEnvironment(logger);
InitTestEnvironment(discoveryContext.RunSettings, logger);

List<string> googleTestExecutables = GetAllGoogleTestExecutables(executables);
VsTestFrameworkReporter reporter = new VsTestFrameworkReporter(TestEnvironment);
Expand Down Expand Up @@ -110,11 +110,14 @@ public bool IsGoogleTestExecutable(string executable, string customRegex = "")
}


private void InitTestEnvironment(IMessageLogger logger)
private void InitTestEnvironment(IRunSettings runSettings, IMessageLogger logger)
{
if (TestEnvironment == null)
if (TestEnvironment == null || TestEnvironment.Options.GetType() == typeof(Options))
{
TestEnvironment = new TestEnvironment(new Options(logger), logger);
var settingsProvider = runSettings.GetSettings(GoogleTestConstants.SettingsName) as RunSettingsProvider;
RunSettings ourRunSettings = settingsProvider != null ? settingsProvider.Settings : new RunSettings();

TestEnvironment = new TestEnvironment(new Options(ourRunSettings, logger), logger);
}

TestEnvironment.CheckDebugModeForDiscoveryCode();
Expand Down Expand Up @@ -236,13 +239,11 @@ private bool SafeMatches(string executable, string regex)
}
catch (ArgumentException e)
{
TestEnvironment.LogError(
"Regex '" + regex + "' configured under Options/Google Test Adapter can not be parsed: " + e.Message);
TestEnvironment.LogError($"Regex '{regex}' can not be parsed: {e.Message}");
}
catch (RegexMatchTimeoutException e)
{
TestEnvironment.LogError(
"Regex '" + regex + "' configured under Options/Google Test Adapter timed out: " + e.Message);
TestEnvironment.LogError($"Regex '{regex}' timed out: {e.Message}");
}
return matches;
}
Expand Down
14 changes: 9 additions & 5 deletions GoogleTestExtension/GoogleTestAdapter/GoogleTestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using GoogleTestAdapter.Helpers;
using GoogleTestAdapter.Runners;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;

namespace GoogleTestAdapter
{
Expand Down Expand Up @@ -34,7 +35,7 @@ public void RunTests(IEnumerable<string> executables, IRunContext runContext, IF
{
try
{
InitTestEnvironment(frameworkHandle);
InitTestEnvironment(runContext.RunSettings, frameworkHandle);

ComputeAllTestCasesInExecutables(executables);

Expand All @@ -50,7 +51,7 @@ public void RunTests(IEnumerable<TestCase> testCasesToRun, IRunContext runContex
{
try
{
InitTestEnvironment(frameworkHandle);
InitTestEnvironment(runContext.RunSettings, frameworkHandle);

TestCase[] testCasesToRunAsArray = testCasesToRun as TestCase[] ?? testCasesToRun.ToArray();
ComputeAllTestCasesInExecutables(testCasesToRunAsArray.Select(tc => tc.Source).Distinct());
Expand All @@ -74,11 +75,14 @@ public void Cancel()
}


private void InitTestEnvironment(IFrameworkHandle frameworkHandle)
private void InitTestEnvironment(IRunSettings runSettings, IMessageLogger messageLogger)
{
if (TestEnvironment == null)
if (TestEnvironment == null || TestEnvironment.Options.GetType() == typeof(Options))
{
TestEnvironment = new TestEnvironment(new Options(frameworkHandle), frameworkHandle);
var settingsProvider = runSettings.GetSettings(GoogleTestConstants.SettingsName) as RunSettingsProvider;
RunSettings ourRunSettings = settingsProvider != null ? settingsProvider.Settings : new RunSettings();

TestEnvironment = new TestEnvironment(new Options(ourRunSettings, messageLogger), messageLogger);
}

TestEnvironment.CheckDebugModeForExecutionCode();
Expand Down
45 changes: 0 additions & 45 deletions GoogleTestExtension/GoogleTestAdapter/Helpers/RegistryReader.cs

This file was deleted.

138 changes: 138 additions & 0 deletions GoogleTestExtension/GoogleTestAdapter/Helpers/XmlOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using System;
using System.Diagnostics;
using System.Xml;
using System.Xml.Serialization;

namespace GoogleTestAdapter.Helpers
{

public interface IXmlOptions
{
string AdditionalTestExecutionParam { get; set; }
int? MaxNrOfThreads { get; set; }
int? NrOfTestRepetitions { get; set; }
bool? ParallelTestExecution { get; set; }
bool? PrintTestOutput { get; set; }
int? ReportWaitPeriod { get; set; }
bool? RunDisabledTests { get; set; }
bool? ShuffleTests { get; set; }
int? ShuffleTestsSeed { get; set; }
string TestDiscoveryRegex { get; set; }
string BatchForTestSetup { get; set; }
string BatchForTestTeardown { get; set; }
string TraitsRegexesAfter { get; set; }
string TraitsRegexesBefore { get; set; }
bool? DebugMode { get; set; }
}

public static class XmlOptionsExtension
{
public static void GetUnsetValuesFrom(this IXmlOptions self, IXmlOptions other)
{
self.AdditionalTestExecutionParam = self.AdditionalTestExecutionParam ?? other.AdditionalTestExecutionParam;
self.MaxNrOfThreads = self.MaxNrOfThreads ?? other.MaxNrOfThreads;
self.NrOfTestRepetitions = self.NrOfTestRepetitions ?? other.NrOfTestRepetitions;
self.ParallelTestExecution = self.ParallelTestExecution ?? other.ParallelTestExecution;
self.PrintTestOutput = self.PrintTestOutput ?? other.PrintTestOutput;
self.ReportWaitPeriod = self.ReportWaitPeriod ?? other.ReportWaitPeriod;
self.RunDisabledTests = self.RunDisabledTests ?? other.RunDisabledTests;
self.ShuffleTests = self.ShuffleTests ?? other.ShuffleTests;
self.ShuffleTestsSeed = self.ShuffleTestsSeed ?? other.ShuffleTestsSeed;
self.TestDiscoveryRegex = self.TestDiscoveryRegex ?? other.TestDiscoveryRegex;
self.BatchForTestSetup = self.BatchForTestSetup ?? other.BatchForTestSetup;
self.BatchForTestTeardown = self.BatchForTestTeardown ?? other.BatchForTestTeardown;
self.TraitsRegexesAfter = self.TraitsRegexesAfter ?? other.TraitsRegexesAfter;
self.TraitsRegexesBefore = self.TraitsRegexesBefore ?? other.TraitsRegexesBefore;
self.DebugMode = self.DebugMode ?? other.DebugMode;
}
}


[XmlRoot(GoogleTestConstants.SettingsName)]
public class RunSettings : TestRunSettings, IXmlOptions
{
public RunSettings()
: base(GoogleTestConstants.SettingsName)
{ }

public bool? PrintTestOutput { get; set; }
public bool ShouldSerializePrintTestOutput() { return PrintTestOutput != null; }

public string TestDiscoveryRegex { get; set; }
public bool ShouldSerializeTestDiscoveryRegex() { return TestDiscoveryRegex != null; }

public bool? RunDisabledTests { get; set; }
public bool ShouldSerializeRunDisabledTests() { return RunDisabledTests != null; }

public int? NrOfTestRepetitions { get; set; }
public bool ShouldSerializeNrOfTestRepetitions() { return NrOfTestRepetitions != null; }

public bool? ShuffleTests { get; set; }
public bool ShouldSerializeShuffleTests() { return ShuffleTests != null; }

public int? ShuffleTestsSeed { get; set; }
public bool ShouldSerializeShuffleTestsSeed() { return ShuffleTestsSeed != null; }

public string TraitsRegexesBefore { get; set; }
public bool ShouldSerializeTraitsRegexesBefore() { return TraitsRegexesBefore != null; }

public string TraitsRegexesAfter { get; set; }
public bool ShouldSerializeTraitsRegexesAfter() { return TraitsRegexesAfter != null; }

public bool? DebugMode { get; set; }
public bool ShouldSerializeDebugMode() { return DebugMode != null; }

public string AdditionalTestExecutionParam { get; set; }
public bool ShouldSerializeAdditionalTestExecutionParam() { return AdditionalTestExecutionParam != null; }

public bool? ParallelTestExecution { get; set; }
public bool ShouldSerializeParallelTestExecution() { return ParallelTestExecution != null; }

public int? MaxNrOfThreads { get; set; }
public bool ShouldSerializeMaxNrOfThreads() { return MaxNrOfThreads != null; }

public string BatchForTestSetup { get; set; }
public bool ShouldSerializeBatchForTestSetup() { return BatchForTestSetup != null; }

public string BatchForTestTeardown { get; set; }
public bool ShouldSerializeBatchForTestTeardown() { return BatchForTestTeardown != null; }

public int? ReportWaitPeriod { get; set; }
public bool ShouldSerializeReportWaitPeriod() { return ReportWaitPeriod != null; }


public override XmlElement ToXml()
{
var document = new XmlDocument();
using (XmlWriter writer = document.CreateNavigator().AppendChild())
{
new XmlSerializer(GetType()).Serialize(writer, this);
}
return document.DocumentElement;
}

public static RunSettings LoadFromXml(XmlReader reader)
{
ValidateArg.NotNull(reader, nameof(reader));

var runSettings = new RunSettings();
if (reader.Read() && reader.Name.Equals(GoogleTestConstants.SettingsName))
{
try
{
var serializer = new XmlSerializer(typeof(RunSettings));
runSettings = serializer.Deserialize(reader) as RunSettings;
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
}

return runSettings;
}

}

}
Loading

0 comments on commit da008bb

Please sign in to comment.