Skip to content

Commit

Permalink
Adding dumping of xml discovery and results, only .net framework so far
Browse files Browse the repository at this point in the history
  • Loading branch information
OsirisTerje committed Oct 6, 2017
1 parent 2ccd408 commit 5115add
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 39 deletions.
3 changes: 3 additions & 0 deletions copynp.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cd package
copy NUnit3TestAdapter.%1.nupkg c:\nuget
cd ..
55 changes: 52 additions & 3 deletions src/NUnitTestAdapter/AdapterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,44 @@

namespace NUnit.VisualStudio.TestAdapter
{
public class AdapterSettings
public interface IAdapterSettings
{
int MaxCpuCount { get; }
string ResultsDirectory { get; }
string TargetPlatform { get; }
string TargetFrameworkVersion { get; }
string TestAdapterPaths { get; }
bool CollectSourceInformation { get; }
IDictionary<string, string> TestProperties { get; }
string InternalTraceLevel { get; }
string WorkDirectory { get; }
int DefaultTimeout { get; }
int NumberOfTestWorkers { get; }
bool ShadowCopyFiles { get; }
int Verbosity { get; }
bool UseVsKeepEngineRunning { get; }
string BasePath { get; }
string PrivateBinPath { get; }
int? RandomSeed { get; }
bool RandomSeedSpecified { get; }
bool InProcDataCollectorsAvailable { get; }
bool SynchronousEvents { get; }
string DomainUsage { get; }
bool DumpXmlTestDiscovery { get; }
bool DumpXmlTestResults { get; }

/// <summary>
/// Syntax documentation <see cref="https://github.com/nunit/docs/wiki/Template-Based-Test-Naming"/>
/// </summary>
string DefaultTestNamePattern { get; }

void Load(IDiscoveryContext context);
void Load(string settingsXml);
void SaveRandomSeed(string dirname);
void RestoreRandomSeed(string dirname);
}

public class AdapterSettings : IAdapterSettings
{
private const string RANDOM_SEED_FILE = "nunit_random_seed.tmp";
private TestLogger _logger;
Expand Down Expand Up @@ -92,7 +129,12 @@ public AdapterSettings(TestLogger logger)

public bool SynchronousEvents { get; private set; }

public string DomainUsage { get; set; }
public string DomainUsage { get; private set; }


public bool DumpXmlTestDiscovery { get; private set; }

public bool DumpXmlTestResults { get; private set; }

/// <summary>
/// Syntax documentation <see cref="https://github.com/nunit/docs/wiki/Template-Based-Test-Naming"/>
Expand Down Expand Up @@ -123,6 +165,7 @@ public void Load(string settingsXml)

var nunitNode = doc.SelectSingleNode("RunSettings/NUnit");
Verbosity = GetInnerTextAsInt(nunitNode, nameof(Verbosity), 0);
_logger.Verbosity = Verbosity;

var runConfiguration = doc.SelectSingleNode("RunSettings/RunConfiguration");
MaxCpuCount = GetInnerTextAsInt(runConfiguration, nameof(MaxCpuCount), -1);
Expand All @@ -141,7 +184,7 @@ public void Load(string settingsXml)
TestProperties.Add(key, value);
}


// NUnit settings
InternalTraceLevel = GetInnerTextWithLog(nunitNode, nameof(InternalTraceLevel), "Off", "Error", "Warning", "Info", "Verbose", "Debug");
WorkDirectory = GetInnerTextWithLog(nunitNode, nameof(WorkDirectory));
DefaultTimeout = GetInnerTextAsInt(nunitNode, nameof(DefaultTimeout), 0);
Expand All @@ -155,6 +198,12 @@ public void Load(string settingsXml)
if (!RandomSeedSpecified)
RandomSeed = new Random().Next();
DefaultTestNamePattern = GetInnerTextWithLog(nunitNode, nameof(DefaultTestNamePattern));

DumpXmlTestDiscovery = GetInnerTextAsBool(nunitNode, nameof(DumpXmlTestDiscovery),false);
DumpXmlTestResults= GetInnerTextAsBool(nunitNode, nameof(DumpXmlTestResults), false);



#if SUPPORT_REGISTRY_SETTINGS
// Legacy (CTP) registry settings override defaults
var registry = RegistryCurrentUser.OpenRegistryCurrentUser(@"Software\nunit.org\VSAdapter");
Expand Down
124 changes: 124 additions & 0 deletions src/NUnitTestAdapter/DumpXml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace NUnit.VisualStudio.TestAdapter.Dump
{

public interface IFile
{
void WriteAllText(string path, string txt);
bool DirectoryExist(string path);

void CreateDirectory(string path);
}

public class File : IFile
{
public void WriteAllText(string path, string txt)
{
System.IO.File.WriteAllText(path,txt);
}

public bool DirectoryExist(string path)
{
return System.IO.Directory.Exists(path);
}

public void CreateDirectory(string path)
{
System.IO.Directory.CreateDirectory(path);
}
}


public interface IDumpXml
{
void AddString(string text);
}

public class DumpXml : IDumpXml
{
private readonly IFile file;
private readonly string directory;
private readonly string filename;
private StringBuilder txt;

public DumpXml(string path, IFile file=null)
{
this.directory = Path.GetDirectoryName(path);
this.filename = Path.GetFileName(path);
this.file = file ?? new File();
txt = new StringBuilder();
}






public void Dump2File(string path)
{
EnsurePathExist(path);
file.WriteAllText(path,txt.ToString());
txt = new StringBuilder();
}

private void EnsurePathExist(string path)
{
var folder = Path.GetDirectoryName(path);
if (!file.DirectoryExist(folder))
file.CreateDirectory(folder);
}

public void Dump4Discovery()
{
var dumpfolder = Path.Combine(directory, "Dump");
var path = Path.Combine(dumpfolder, $"D_{filename}.dump");
Dump2File(path);
}

public void Dump4Execution()
{
var dumpfolder = Path.Combine(directory, "Dump");
var path = Path.Combine(dumpfolder, $"E_{filename}.dump");
Dump2File(path);
}

public string RandomName()
{
var guid = Guid.NewGuid();
var res = Convert.ToBase64String(guid.ToByteArray());
var res2 = Regex.Replace(res, @"[^a-zA-Z0-9]", "");
return res2+".dump";
}


public void AddString(string text)
{
txt.Append(text);
}

}

#if !NETCOREAPP1_0
public static class XmlNodeExtension
{
public static string AsString(this System.Xml.XmlNode node)
{
using (var swriter = new System.IO.StringWriter())
{
using (var twriter = new System.Xml.XmlTextWriter(swriter))
{
twriter.Formatting = System.Xml.Formatting.Indented;
twriter.Indentation = 3;
twriter.QuoteChar = '\'';
node.WriteContentTo(twriter);
}
return swriter.ToString();
}
}
}
#endif
}
25 changes: 19 additions & 6 deletions src/NUnitTestAdapter/NUnit3TestDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using NUnit.Engine;
using NUnit.VisualStudio.TestAdapter.Dump;

namespace NUnit.VisualStudio.TestAdapter
{
Expand All @@ -44,6 +45,8 @@ namespace NUnit.VisualStudio.TestAdapter
[DefaultExecutorUri(NUnit3TestExecutor.ExecutorUri)]
public sealed class NUnit3TestDiscoverer : NUnitTestAdapter, ITestDiscoverer
{
private Dump.DumpXml dumpXml;

#region ITestDiscoverer Members

public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discoveryContext, IMessageLogger messageLogger, ITestCaseDiscoverySink discoverySink)
Expand All @@ -54,7 +57,9 @@ public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discove
#endif
Initialize(discoveryContext, messageLogger);

TestLog.Info(string.Format("NUnit Adapter {0}: Test discovery starting", AdapterVersion));


TestLog.Info($"NUnit Adapter {AdapterVersion}: Test discovery starting");

// Ensure any channels registered by other adapters are unregistered
CleanUpRegisteredChannels();
Expand All @@ -69,17 +74,23 @@ public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discove
foreach (string sourceAssembly in sources)
{
var sourceAssemblyPath = Path.IsPathRooted(sourceAssembly) ? sourceAssembly : Path.Combine(Directory.GetCurrentDirectory(), sourceAssembly);

TestLog.Debug("Processing " + sourceAssembly);

ITestRunner runner = null;

if (Settings.DumpXmlTestDiscovery)
{
dumpXml = new Dump.DumpXml(sourceAssemblyPath);

}

try
{
runner = GetRunnerFor(sourceAssemblyPath);

XmlNode topNode = runner.Explore(TestFilter.Empty);

#if !NETCOREAPP1_0
dumpXml?.AddString(topNode.AsString());
#endif
// Currently, this will always be the case but it might change
if (topNode.Name == "test-run")
topNode = topNode.FirstChild;
Expand All @@ -90,7 +101,7 @@ public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discove

int cases = ProcessTestCases(topNode, discoverySink, testConverter);

TestLog.Debug(string.Format("Discovered {0} test cases", cases));
TestLog.Debug($"Discovered {cases} test cases");
// Only save if seed is not specified in runsettings
// This allows workaround in case there is no valid
// location in which the seed may be saved.
Expand All @@ -105,6 +116,7 @@ public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discove
else
TestLog.Info("NUnit failed to load " + sourceAssembly);
}
dumpXml?.Dump4Discovery();
}
catch (BadImageFormatException)
{
Expand Down Expand Up @@ -135,6 +147,7 @@ public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discove
}
finally
{

if (runner != null)
{
if (runner.IsTestRunning)
Expand All @@ -146,7 +159,7 @@ public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discove
}
}

TestLog.Info(string.Format("NUnit Adapter {0}: Test discovery complete", AdapterVersion));
TestLog.Info($"NUnit Adapter {AdapterVersion}: Test discovery complete");

Unload();
}
Expand Down
Loading

0 comments on commit 5115add

Please sign in to comment.