Skip to content

Commit

Permalink
Merge pull request #402 from nunit/Verbosity
Browse files Browse the repository at this point in the history
Verbosity improvement for adapter settings  #397
  • Loading branch information
OsirisTerje authored Oct 5, 2017
2 parents d6eff56 + ffecf77 commit 2ccd408
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 55 deletions.
129 changes: 75 additions & 54 deletions src/NUnitTestAdapter/AdapterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public AdapterSettings(TestLogger logger)
public void Load(IDiscoveryContext context)
{
if (context == null)
throw new ArgumentNullException("context", "Load called with null context");
throw new ArgumentNullException(nameof(context), "Load called with null context");

Load(context?.RunSettings?.SettingsXml);
}
Expand All @@ -121,13 +121,16 @@ public void Load(string settingsXml)
var doc = new XmlDocument();
doc.LoadXml(settingsXml);

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

var runConfiguration = doc.SelectSingleNode("RunSettings/RunConfiguration");
MaxCpuCount = GetInnerTextAsInt(runConfiguration, "MaxCpuCount", -1);
ResultsDirectory = GetInnerText(runConfiguration, "ResultsDirectory");
TargetPlatform = GetInnerText(runConfiguration, "TargetPlatform");
TargetFrameworkVersion = GetInnerText(runConfiguration, "TargetFrameworkVersion");
TestAdapterPaths = GetInnerText(runConfiguration, "TestAdapterPaths");
CollectSourceInformation = GetInnerTextAsBool(runConfiguration, "CollectSourceInformation", true);
MaxCpuCount = GetInnerTextAsInt(runConfiguration, nameof(MaxCpuCount), -1);
ResultsDirectory = GetInnerTextWithLog(runConfiguration, nameof(ResultsDirectory));
TargetPlatform = GetInnerTextWithLog(runConfiguration, nameof(TargetPlatform));
TargetFrameworkVersion = GetInnerTextWithLog(runConfiguration, nameof(TargetFrameworkVersion));
TestAdapterPaths = GetInnerTextWithLog(runConfiguration, nameof(TestAdapterPaths));
CollectSourceInformation = GetInnerTextAsBool(runConfiguration, nameof(CollectSourceInformation), true);

TestProperties = new Dictionary<string, string>();
foreach (XmlNode node in doc.SelectNodes("RunSettings/TestRunParameters/Parameter"))
Expand All @@ -138,21 +141,20 @@ public void Load(string settingsXml)
TestProperties.Add(key, value);
}

var nunitNode = doc.SelectSingleNode("RunSettings/NUnit");
InternalTraceLevel = GetInnerText(nunitNode, "InternalTraceLevel", "Off", "Error", "Warning", "Info", "Verbose", "Debug");
WorkDirectory = GetInnerText(nunitNode, "WorkDirectory");
DefaultTimeout = GetInnerTextAsInt(nunitNode, "DefaultTimeout", 0);
NumberOfTestWorkers = GetInnerTextAsInt(nunitNode, "NumberOfTestWorkers", -1);
ShadowCopyFiles = GetInnerTextAsBool(nunitNode, "ShadowCopyFiles", false);
Verbosity = GetInnerTextAsInt(nunitNode, "Verbosity", 0);
UseVsKeepEngineRunning = GetInnerTextAsBool(nunitNode, "UseVsKeepEngineRunning", false);
BasePath = GetInnerText(nunitNode, "BasePath");
PrivateBinPath = GetInnerText(nunitNode, "PrivateBinPath");
RandomSeed = GetInnerTextAsNullableInt(nunitNode, "RandomSeed");

InternalTraceLevel = GetInnerTextWithLog(nunitNode, nameof(InternalTraceLevel), "Off", "Error", "Warning", "Info", "Verbose", "Debug");
WorkDirectory = GetInnerTextWithLog(nunitNode, nameof(WorkDirectory));
DefaultTimeout = GetInnerTextAsInt(nunitNode, nameof(DefaultTimeout), 0);
NumberOfTestWorkers = GetInnerTextAsInt(nunitNode, nameof(NumberOfTestWorkers), -1);
ShadowCopyFiles = GetInnerTextAsBool(nunitNode, nameof(ShadowCopyFiles), false);
UseVsKeepEngineRunning = GetInnerTextAsBool(nunitNode, nameof(UseVsKeepEngineRunning), false);
BasePath = GetInnerTextWithLog(nunitNode, nameof(BasePath));
PrivateBinPath = GetInnerTextWithLog(nunitNode, nameof(PrivateBinPath));
RandomSeed = GetInnerTextAsNullableInt(nunitNode, nameof(RandomSeed));
RandomSeedSpecified = RandomSeed.HasValue;
if (!RandomSeedSpecified)
RandomSeed = new Random().Next();
DefaultTestNamePattern = GetInnerText(nunitNode,"DefaultTestNamePattern");
DefaultTestNamePattern = GetInnerTextWithLog(nunitNode, nameof(DefaultTestNamePattern));
#if SUPPORT_REGISTRY_SETTINGS
// Legacy (CTP) registry settings override defaults
var registry = RegistryCurrentUser.OpenRegistryCurrentUser(@"Software\nunit.org\VSAdapter");
Expand All @@ -177,6 +179,10 @@ public void Load(string settingsXml)
NumberOfTestWorkers = 0;
DomainUsage = "None";
SynchronousEvents = true;
if (Verbosity >= 4)
{
_logger.Info($"InProcDataCollectors are available: turning off Parallel, DomainUsage=None, SynchronousEvents=true");
}
}
}

Expand Down Expand Up @@ -213,60 +219,75 @@ public void RestoreRandomSeed(string dirname)

#region Helper Methods

private string GetInnerText(XmlNode startNode, string xpath, params string[] validValues)
private string GetInnerTextWithLog(XmlNode startNode, string xpath, params string[] validValues)
{
if (startNode != null)
{
var targetNode = startNode.SelectSingleNode(xpath);
if (targetNode != null)
{
string val = targetNode.InnerText;
return GetInnerText(startNode, xpath, true, validValues);
}


if (validValues != null && validValues.Length > 0)
{
foreach (string valid in validValues)
if (string.Compare(valid, val, StringComparison.OrdinalIgnoreCase) == 0)
return valid;
private string GetInnerText(XmlNode startNode, string xpath, bool log, params string[] validValues)
{
string val = null;
var targetNode = startNode?.SelectSingleNode(xpath);
if (targetNode != null)
{
val = targetNode.InnerText;

throw new ArgumentException(string.Format(
"Invalid value {0} passed for element {1}.", val, xpath));
}
if (validValues != null && validValues.Length > 0)
{
foreach (string valid in validValues)
if (string.Compare(valid, val, StringComparison.OrdinalIgnoreCase) == 0)
return valid;

return val;
throw new ArgumentException(string.Format(
"Invalid value {0} passed for element {1}.", val, xpath));
}


}
if (log)
Log(xpath,val);

return null;
return val;
}

private int GetInnerTextAsInt(XmlNode startNode, string xpath, int defaultValue)
{
int? temp = GetInnerTextAsNullableInt(startNode, xpath);

if (temp == null)
return defaultValue;

return temp.Value;
var temp = GetInnerTextAsNullableInt(startNode, xpath,false);
var res = defaultValue;
if (temp != null)
res = temp.Value;
Log(xpath, res);
return res;
}

private int? GetInnerTextAsNullableInt(XmlNode startNode, string xpath)
private int? GetInnerTextAsNullableInt(XmlNode startNode, string xpath,bool log=true)
{
string temp = GetInnerText(startNode, xpath);

if (string.IsNullOrEmpty(temp))
return null;

return int.Parse(temp);
string temp = GetInnerText(startNode, xpath,log);
int? res = null;
if (!string.IsNullOrEmpty(temp))
res = int.Parse(temp);
if (log)
Log(xpath,res);
return res;
}

private bool GetInnerTextAsBool(XmlNode startNode, string xpath, bool defaultValue)
{
string temp = GetInnerText(startNode, xpath);

if (string.IsNullOrEmpty(temp))
return defaultValue;
string temp = GetInnerText(startNode, xpath,false);
bool res = defaultValue;
if (!string.IsNullOrEmpty(temp))
res = bool.Parse(temp);
Log(xpath,res);
return res;
}

return bool.Parse(temp);
private void Log<T>(string xpath, T res)
{
if (Verbosity >= 4)
{
_logger.Info($"Setting: {xpath} = {res}");
}
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitTestAdapter/NUnit3TestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

//#define LAUNCHDEBUGGER
// #define LAUNCHDEBUGGER

using System;
using System.IO;
Expand Down
6 changes: 6 additions & 0 deletions src/NUnitTestAdapter/TestLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,17 @@ public void SendMessage(TestMessageLevel testMessageLevel, string message, Excep
var type = ex.GetType();
SendMessage(testMessageLevel, string.Format(EXCEPTION_FORMAT, type, message));
SendMessage(testMessageLevel, ex.Message);
SendMessage(testMessageLevel,ex.StackTrace);
if (ex.InnerException != null)
{
SendMessage(testMessageLevel,$"Innerexception: {ex.InnerException.ToString()}");
}
break;

default:
SendMessage(testMessageLevel, message);
SendMessage(testMessageLevel, ex.ToString());
SendMessage(testMessageLevel, ex.StackTrace);
break;
}
}
Expand Down

0 comments on commit 2ccd408

Please sign in to comment.