Skip to content

Commit e80964e

Browse files
authored
Updating debugging features and some code cleanup (#926)
* Updating debugging features and some code cleanup * fix SA error
1 parent 4d47546 commit e80964e

File tree

13 files changed

+479
-105
lines changed

13 files changed

+479
-105
lines changed

.editorconfig

Lines changed: 360 additions & 8 deletions
Large diffs are not rendered by default.

build.cake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var configuration = Argument("configuration", "Release");
1313
//////////////////////////////////////////////////////////////////////
1414

1515
var version = "4.2.0";
16-
var modifier = "-alpha.919.3";
16+
var modifier = "-alpha.4";
1717

1818
var dbgSuffix = configuration.ToLower() == "debug" ? "-dbg" : "";
1919
var packageVersion = version + modifier + dbgSuffix;

src/NUnitTestAdapter/AdapterSettings.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public interface IAdapterSettings
102102
bool StopOnError { get; }
103103
TestOutcome MapWarningTo { get; }
104104
bool UseTestNameInConsoleOutput { get; }
105-
bool FreakMode { get; }
106105
DisplayNameOptions DisplayName { get; }
107106
char FullnameSeparator { get; }
108107
DiscoveryMethod DiscoveryMethod { get; }
@@ -120,6 +119,12 @@ public interface IAdapterSettings
120119
void RestoreRandomSeed(string dirname);
121120

122121
bool EnsureAttachmentFileScheme { get; }
122+
123+
// For Internal Development use
124+
bool FreakMode { get; } // displays metadata instead of real data in Test Explorer
125+
bool Debug { get; }
126+
bool DebugExecution { get; }
127+
bool DebugDiscovery { get; }
123128
}
124129

125130
public enum VsTestCategoryType
@@ -269,11 +274,15 @@ public AdapterSettings(ITestLogger logger)
269274
public bool DumpVsInput { get; private set; }
270275

271276
public bool FreakMode { get; private set; }
277+
public bool Debug { get; private set; }
278+
public bool DebugExecution { get; private set; }
279+
public bool DebugDiscovery { get; private set; }
272280

273281
#endregion
274282

275283

276284

285+
277286
#endregion
278287

279288
#region Public Methods
@@ -393,6 +402,9 @@ private void ExtractNUnitDiagnosticSettings(XmlNode nunitNode)
393402
FreakMode = GetInnerTextAsBool(nunitNode, nameof(FreakMode), false);
394403
InternalTraceLevel = GetInnerTextWithLog(nunitNode, nameof(InternalTraceLevel), "Off", "Error", "Warning",
395404
"Info", "Verbose", "Debug");
405+
Debug = GetInnerTextAsBool(nunitNode, nameof(Debug), false);
406+
DebugExecution = Debug || GetInnerTextAsBool(nunitNode, nameof(DebugExecution), false);
407+
DebugDiscovery = Debug || GetInnerTextAsBool(nunitNode, nameof(DebugDiscovery), false);
396408
}
397409

398410
private void UpdateTestProperties(XmlDocument doc)

src/NUnitTestAdapter/CategoryList.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ private bool IsInternalProperty(NUnitProperty property)
138138
}
139139

140140
// Property names starting with '_' are for internal use only, but over time this has changed, so we now use a list
141-
if (!showInternalProperties &&
142-
_internalProperties.Contains(property.Name))
143-
return true;
144-
return string.IsNullOrEmpty(property.Name) || property.Name[0] == '_' || string.IsNullOrEmpty(property.Value);
141+
return (!showInternalProperties &&
142+
_internalProperties.Contains(property.Name)) || (string.IsNullOrEmpty(property.Name) ||
143+
property.Name[0] == '_' ||
144+
string.IsNullOrEmpty(property.Value));
145145
}
146146

147147
private void AddTraitsToCache(IDictionary<string, TraitsFeature.CachedTestCaseInfo> traitsCache, string key, NUnitProperty property)

src/NUnitTestAdapter/Execution.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ public interface IExecutionContext
1818

1919
public static class ExecutionFactory
2020
{
21-
public static Execution Create(IExecutionContext ctx)
22-
{
23-
if (ctx.Settings.DesignMode) // We come from IDE
24-
return new IdeExecution(ctx);
25-
return new VsTestExecution(ctx);
26-
}
21+
public static Execution Create(IExecutionContext ctx) => ctx.Settings.DesignMode ? new IdeExecution(ctx) : new VsTestExecution(ctx);
2722
}
2823

2924
public abstract class Execution

src/NUnitTestAdapter/Internal/TimingLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public TimingLogger ReStart()
2727

2828
public TimingLogger LogTime(string leadText = "")
2929
{
30-
if (settings.Verbosity < 5)
30+
if (settings.Verbosity < 5 || Stopwatch == null)
3131
return this;
3232
var ts = Stopwatch.Elapsed;
3333
string elapsedTime = $"{ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds / 10:00}";

src/NUnitTestAdapter/NUnit3TestDiscoverer.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,11 @@
2121
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
// ***********************************************************************
2323

24-
// #define LAUNCHDEBUGGER
2524

2625
using System;
2726
using System.Collections.Generic;
2827
using System.ComponentModel;
29-
#if LAUNCHDEBUGGER
3028
using System.Diagnostics;
31-
#endif
3229
using System.IO;
3330
using System.Linq;
3431
using System.Xml;
@@ -57,11 +54,8 @@ public sealed class NUnit3TestDiscoverer : NUnitTestAdapter, ITestDiscoverer
5754

5855
public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discoveryContext, IMessageLogger messageLogger, ITestCaseDiscoverySink discoverySink)
5956
{
60-
#if LAUNCHDEBUGGER
61-
if (!Debugger.IsAttached)
62-
Debugger.Launch();
63-
#endif
6457
Initialize(discoveryContext, messageLogger);
58+
CheckIfDebug();
6559
TestLog.Info($"NUnit Adapter {AdapterVersion}: Test discovery starting");
6660

6761
// Ensure any channels registered by other adapters are unregistered
@@ -182,10 +176,6 @@ private int ProcessTestCases(NUnitResults results, ITestCaseDiscoverySink discov
182176
{
183177
try
184178
{
185-
#if LAUNCHDEBUGGER
186-
if (!Debugger.IsAttached)
187-
Debugger.Launch();
188-
#endif
189179
var testCase = testConverterForXml.ConvertTestCase(new NUnitEventTestCase(testNode));
190180
discoverySink.SendTestCase(testCase);
191181
cases += 1;
@@ -199,6 +189,13 @@ private int ProcessTestCases(NUnitResults results, ITestCaseDiscoverySink discov
199189
return cases;
200190
}
201191

192+
private void CheckIfDebug()
193+
{
194+
if (!Settings.DebugDiscovery)
195+
return;
196+
if (!Debugger.IsAttached)
197+
Debugger.Launch();
198+
}
202199
#endregion
203200
}
204201
}

src/NUnitTestAdapter/NUnit3TestExecutor.cs

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@
2121
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
// ***********************************************************************
2323

24-
// #define LAUNCHDEBUGGER
2524

2625
using System;
2726
using System.Collections.Generic;
2827
using System.Diagnostics;
2928
using System.IO;
3029
using System.Linq;
3130
using System.Reflection;
31+
3232
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
3333
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
34+
3435
using NUnit.Engine;
3536
using NUnit.VisualStudio.TestAdapter.Dump;
3637
using NUnit.VisualStudio.TestAdapter.Internal;
@@ -57,7 +58,8 @@ public enum RunType
5758

5859

5960
[ExtensionUri(ExecutorUri)]
60-
public sealed class NUnit3TestExecutor : NUnitTestAdapter, ITestExecutor, IDisposable, INUnit3TestExecutor, IExecutionContext
61+
public sealed class NUnit3TestExecutor : NUnitTestAdapter, ITestExecutor, IDisposable, INUnit3TestExecutor,
62+
IExecutionContext
6163
{
6264
#region Properties
6365

@@ -96,17 +98,15 @@ public sealed class NUnit3TestExecutor : NUnitTestAdapter, ITestExecutor, IDispo
9698
/// <param name="frameworkHandle">Test log to send results and messages through.</param>
9799
public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrameworkHandle frameworkHandle)
98100
{
99-
#if LAUNCHDEBUGGER
100-
if (!Debugger.IsAttached)
101-
Debugger.Launch();
102-
#endif
103101
Initialize(runContext, frameworkHandle);
102+
CheckIfDebug();
104103
TestLog.Debug("RunTests by IEnumerable<string>");
105104
InitializeForExecution(runContext, frameworkHandle);
106105

107106
if (Settings.InProcDataCollectorsAvailable && sources.Count() > 1)
108107
{
109-
TestLog.Error("Failed to run tests for multiple assemblies when InProcDataCollectors specified in run configuration.");
108+
TestLog.Error(
109+
"Failed to run tests for multiple assemblies when InProcDataCollectors specified in run configuration.");
110110
Unload();
111111
return;
112112
}
@@ -119,18 +119,22 @@ public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrame
119119
var vsTestFilter = VsTestFilterFactory.CreateVsTestFilter(Settings, runContext);
120120
filter = builder.ConvertVsTestFilterToNUnitFilter(vsTestFilter);
121121
}
122+
122123
filter ??= builder.FilterByWhere(Settings.Where);
123124

124125
foreach (string assemblyName in sources)
125126
{
126127
try
127128
{
128-
string assemblyPath = Path.IsPathRooted(assemblyName) ? assemblyName : Path.Combine(Directory.GetCurrentDirectory(), assemblyName);
129+
string assemblyPath = Path.IsPathRooted(assemblyName)
130+
? assemblyName
131+
: Path.Combine(Directory.GetCurrentDirectory(), assemblyName);
129132
RunAssembly(assemblyPath, null, filter);
130133
}
131134
catch (Exception ex)
132135
{
133136
if (ex is TargetInvocationException) { ex = ex.InnerException; }
137+
134138
TestLog.Warning("Exception thrown executing tests", ex);
135139
}
136140
}
@@ -156,11 +160,8 @@ private void SetRunTypeByStrings() =>
156160
/// <param name="frameworkHandle">The FrameworkHandle.</param>
157161
public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle)
158162
{
159-
#if LAUNCHDEBUGGER
160-
if (!Debugger.IsAttached)
161-
Debugger.Launch();
162-
#endif
163163
Initialize(runContext, frameworkHandle);
164+
CheckIfDebug();
164165
TestLog.Debug("RunTests by IEnumerable<TestCase>");
165166
InitializeForExecution(runContext, frameworkHandle);
166167
RunType = RunType.Ide;
@@ -170,7 +171,8 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrame
170171
var assemblyGroups = tests.GroupBy(tc => tc.Source);
171172
if (IsInProcDataCollectorsSpecifiedWithMultipleAssemblies(assemblyGroups))
172173
{
173-
TestLog.Error("Failed to run tests for multiple assemblies when InProcDataCollectors specified in run configuration.");
174+
TestLog.Error(
175+
"Failed to run tests for multiple assemblies when InProcDataCollectors specified in run configuration.");
174176
Unload();
175177
return;
176178
}
@@ -181,7 +183,9 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrame
181183
try
182184
{
183185
string assemblyName = assemblyGroup.Key;
184-
string assemblyPath = Path.IsPathRooted(assemblyName) ? assemblyName : Path.Combine(Directory.GetCurrentDirectory(), assemblyName);
186+
string assemblyPath = Path.IsPathRooted(assemblyName)
187+
? assemblyName
188+
: Path.Combine(Directory.GetCurrentDirectory(), assemblyName);
185189

186190
var filterBuilder = CreateTestFilterBuilder();
187191
var filter = filterBuilder.FilterByList(assemblyGroup);
@@ -191,6 +195,7 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrame
191195
catch (Exception ex)
192196
{
193197
if (ex is TargetInvocationException) { ex = ex.InnerException; }
198+
194199
TestLog.Warning("Exception thrown executing tests", ex);
195200
}
196201

@@ -202,7 +207,8 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrame
202207
Unload();
203208
}
204209

205-
private bool IsInProcDataCollectorsSpecifiedWithMultipleAssemblies(IEnumerable<IGrouping<string, TestCase>> assemblyGroups)
210+
private bool IsInProcDataCollectorsSpecifiedWithMultipleAssemblies(
211+
IEnumerable<IGrouping<string, TestCase>> assemblyGroups)
206212
=> Settings.InProcDataCollectorsAvailable && assemblyGroups.Count() > 1;
207213

208214
void ITestExecutor.Cancel()
@@ -244,7 +250,9 @@ public void InitializeForExecution(IRunContext runContext, IFrameworkHandle fram
244250

245251
if (VsTestFilter.IsEmpty)
246252
{
247-
if (!(enableShutdown && !runContext.KeepAlive)) // Otherwise causes exception when run as commandline, illegal to enableshutdown when Keepalive is false, might be only VS2012
253+
if (!(enableShutdown &&
254+
!runContext
255+
.KeepAlive)) // Otherwise causes exception when run as commandline, illegal to enableshutdown when Keepalive is false, might be only VS2012
248256
frameworkHandle.EnableShutdownAfterTestRun = enableShutdown;
249257
}
250258

@@ -253,9 +261,7 @@ public void InitializeForExecution(IRunContext runContext, IFrameworkHandle fram
253261

254262
private void RunAssembly(string assemblyPath, IGrouping<string, TestCase> testCases, TestFilter filter)
255263
{
256-
string actionText = Debugger.IsAttached ? "Debugging " : "Running ";
257-
string selectionText = filter == null || filter == TestFilter.Empty ? "all" : "selected";
258-
TestLog.Info(actionText + selectionText + " tests in " + assemblyPath);
264+
LogActionAndSelection(assemblyPath, filter);
259265
RestoreRandomSeed(assemblyPath);
260266
Dump = DumpXml.CreateDump(assemblyPath, testCases, Settings);
261267

@@ -279,9 +285,7 @@ private void RunAssembly(string assemblyPath, IGrouping<string, TestCase> testCa
279285
}
280286
else
281287
{
282-
TestLog.Info(discoveryResults.HasNoNUnitTests
283-
? " NUnit couldn't find any tests in " + assemblyPath
284-
: " NUnit failed to load " + assemblyPath);
288+
TestLog.InfoNoTests(discoveryResults.HasNoNUnitTests, assemblyPath);
285289
}
286290
}
287291
catch (Exception ex) when (ex is BadImageFormatException || ex.InnerException is BadImageFormatException)
@@ -292,7 +296,8 @@ private void RunAssembly(string assemblyPath, IGrouping<string, TestCase> testCa
292296
catch (FileNotFoundException ex)
293297
{
294298
// Probably from the GetExportedTypes in NUnit.core, attempting to find an assembly, not a problem if it is not NUnit here
295-
TestLog.Warning(" Dependent Assembly " + ex.FileName + " of " + assemblyPath + " not found. Can be ignored if not an NUnit project.");
299+
TestLog.Warning(" Dependent Assembly " + ex.FileName + " of " + assemblyPath +
300+
" not found. Can be ignored if not an NUnit project.");
296301
}
297302
catch (Exception ex)
298303
{
@@ -312,12 +317,18 @@ private void RunAssembly(string assemblyPath, IGrouping<string, TestCase> testCa
312317
// can happen if CLR throws CannotUnloadAppDomainException, for example
313318
// due to a long-lasting operation in a protected region (catch/finally clause).
314319
if (ex is TargetInvocationException) { ex = ex.InnerException; }
320+
315321
TestLog.Warning($" Exception thrown unloading tests from {assemblyPath}", ex);
316322
}
317323
}
318324
}
319325

320-
326+
private void LogActionAndSelection(string assemblyPath, TestFilter filter)
327+
{
328+
string actionText = Debugger.IsAttached ? "Debugging " : "Running ";
329+
string selectionText = filter == null || filter == TestFilter.Empty ? "all" : "selected";
330+
TestLog.Info(actionText + selectionText + " tests in " + assemblyPath);
331+
}
321332

322333

323334
private void RestoreRandomSeed(string assemblyPath)
@@ -328,12 +339,7 @@ private void RestoreRandomSeed(string assemblyPath)
328339
}
329340

330341

331-
private NUnitTestFilterBuilder CreateTestFilterBuilder()
332-
{
333-
return new (NUnitEngineAdapter.GetService<ITestFilterService>(), Settings);
334-
}
335-
336-
342+
private NUnitTestFilterBuilder CreateTestFilterBuilder() => new (NUnitEngineAdapter.GetService<ITestFilterService>(), Settings);
337343

338344

339345
private void CreateTestOutputFolder()
@@ -367,5 +373,13 @@ public void StopRun()
367373
}
368374

369375
public IDumpXml Dump { get; private set; }
376+
377+
private void CheckIfDebug()
378+
{
379+
if (!Settings.DebugExecution)
380+
return;
381+
if (!Debugger.IsAttached)
382+
Debugger.Launch();
383+
}
370384
}
371-
}
385+
}

0 commit comments

Comments
 (0)