From e80964ee5b5ba5b9d002181b05632a3bc76d3e55 Mon Sep 17 00:00:00 2001 From: Terje Sandstrom Date: Sat, 11 Dec 2021 21:59:09 +0100 Subject: [PATCH] Updating debugging features and some code cleanup (#926) * Updating debugging features and some code cleanup * fix SA error --- .editorconfig | 368 +++++++++++++++++- build.cake | 2 +- src/NUnitTestAdapter/AdapterSettings.cs | 14 +- src/NUnitTestAdapter/CategoryList.cs | 8 +- src/NUnitTestAdapter/Execution.cs | 7 +- src/NUnitTestAdapter/Internal/TimingLogger.cs | 2 +- src/NUnitTestAdapter/NUnit3TestDiscoverer.cs | 19 +- src/NUnitTestAdapter/NUnit3TestExecutor.cs | 76 ++-- src/NUnitTestAdapter/NUnitTestAdapter.cs | 32 +- .../NUnitTestFilterBuilder.cs | 4 +- .../TestFilterConverter/Tokenizer.cs | 13 +- src/NUnitTestAdapter/TestLogger.cs | 21 + src/NUnitTestAdapter/VsTestFilter.cs | 18 +- 13 files changed, 479 insertions(+), 105 deletions(-) diff --git a/.editorconfig b/.editorconfig index d1bc3a02..c6ed9419 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,14 +1,366 @@ -; Top-most EditorConfig file +# EditorConfig is awesome: https://EditorConfig.org + root = true -[*.cs] +# All files +[*] indent_style = space -indent_size = 4 -[*.csproj] -indent_style = space +# Xml files +[*.xml] indent_size = 2 -[*.props] -indent_style = space -indent_size = 2 \ No newline at end of file +# C# files +[*.cs] + +#### Core EditorConfig Options #### + +# Indentation and spacing +indent_size = 4 +tab_width = 4 + +# New line preferences +end_of_line = crlf +insert_final_newline = false + +#### .NET Coding Conventions #### +[*.{cs,vb}] + +# Organize usings +dotnet_separate_import_directive_groups = true +dotnet_sort_system_directives_first = true +file_header_template = unset + +# this. and Me. preferences +dotnet_style_qualification_for_event = false:silent +dotnet_style_qualification_for_field = false:silent +dotnet_style_qualification_for_method = false:silent +dotnet_style_qualification_for_property = false:silent + +# Language keywords vs BCL types preferences +dotnet_style_predefined_type_for_locals_parameters_members = true:silent +dotnet_style_predefined_type_for_member_access = true:silent + +# Parentheses preferences +dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent +dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent +dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent +dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent + +# Modifier preferences +dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent + +# Expression-level preferences +dotnet_style_coalesce_expression = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_explicit_tuple_names = true:suggestion +dotnet_style_null_propagation = true:suggestion +dotnet_style_object_initializer = true:suggestion +dotnet_style_operator_placement_when_wrapping = beginning_of_line +dotnet_style_prefer_auto_properties = true:suggestion +dotnet_style_prefer_compound_assignment = true:suggestion +dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion +dotnet_style_prefer_conditional_expression_over_return = true:suggestion +dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion +dotnet_style_prefer_inferred_tuple_names = true:suggestion +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion +dotnet_style_prefer_simplified_boolean_expressions = true:suggestion +dotnet_style_prefer_simplified_interpolation = true:suggestion + +# Field preferences +dotnet_style_readonly_field = true:warning + +# Parameter preferences +dotnet_code_quality_unused_parameters = all:suggestion + +# Suppression preferences +dotnet_remove_unnecessary_suppression_exclusions = none + +#### C# Coding Conventions #### +[*.cs] + +# var preferences +csharp_style_var_elsewhere =true:silent +csharp_style_var_for_built_in_types = false:silent +csharp_style_var_when_type_is_apparent =true:silent + +# Expression-bodied members +csharp_style_expression_bodied_accessors = true:silent +csharp_style_expression_bodied_constructors = false:silent +csharp_style_expression_bodied_indexers = true:silent +csharp_style_expression_bodied_lambdas = true:suggestion +csharp_style_expression_bodied_local_functions = false:silent +csharp_style_expression_bodied_methods = false:silent +csharp_style_expression_bodied_operators = false:silent +csharp_style_expression_bodied_properties = true:silent + +# Pattern matching preferences +csharp_style_pattern_matching_over_as_with_null_check = true:suggestion +csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion +csharp_style_prefer_not_pattern = true:suggestion +csharp_style_prefer_pattern_matching = true:silent +csharp_style_prefer_switch_expression = true:suggestion + +# Null-checking preferences +csharp_style_conditional_delegate_call = true:suggestion + +# Modifier preferences +csharp_prefer_static_local_function = true:warning +csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:silent + +# Code-block preferences +csharp_prefer_braces = true:silent +csharp_prefer_simple_using_statement = true:suggestion + +# Expression-level preferences +csharp_prefer_simple_default_expression = true:suggestion +csharp_style_deconstructed_variable_declaration = true:suggestion +csharp_style_inlined_variable_declaration = true:suggestion +csharp_style_pattern_local_over_anonymous_function = true:suggestion +csharp_style_prefer_index_operator = true:suggestion +csharp_style_prefer_range_operator = true:suggestion +csharp_style_throw_expression = true:suggestion +csharp_style_unused_value_assignment_preference = discard_variable:suggestion +csharp_style_unused_value_expression_statement_preference = discard_variable:silent + +# 'using' directive preferences +csharp_using_directive_placement = outside_namespace:silent + +#### C# Formatting Rules #### + +# New line preferences +csharp_new_line_before_catch = true +csharp_new_line_before_else = true +csharp_new_line_before_finally = true +csharp_new_line_before_members_in_anonymous_types = true +csharp_new_line_before_members_in_object_initializers = true +csharp_new_line_before_open_brace = all +csharp_new_line_between_query_expression_clauses = true + +# Indentation preferences +csharp_indent_block_contents = true +csharp_indent_braces = false +csharp_indent_case_contents = true +csharp_indent_case_contents_when_block = true +csharp_indent_labels = one_less_than_current +csharp_indent_switch_labels = true + +# Space preferences +csharp_space_after_cast = false +csharp_space_after_colon_in_inheritance_clause = true +csharp_space_after_comma = true +csharp_space_after_dot = false +csharp_space_after_keywords_in_control_flow_statements = true +csharp_space_after_semicolon_in_for_statement = true +csharp_space_around_binary_operators = before_and_after +csharp_space_around_declaration_statements = false +csharp_space_before_colon_in_inheritance_clause = true +csharp_space_before_comma = false +csharp_space_before_dot = false +csharp_space_before_open_square_brackets = false +csharp_space_before_semicolon_in_for_statement = false +csharp_space_between_empty_square_brackets = false +csharp_space_between_method_call_empty_parameter_list_parentheses = false +csharp_space_between_method_call_name_and_opening_parenthesis = false +csharp_space_between_method_call_parameter_list_parentheses = false +csharp_space_between_method_declaration_empty_parameter_list_parentheses = false +csharp_space_between_method_declaration_name_and_open_parenthesis = false +csharp_space_between_method_declaration_parameter_list_parentheses = false +csharp_space_between_parentheses = false +csharp_space_between_square_brackets = false + +# Wrapping preferences +csharp_preserve_single_line_blocks = true +csharp_preserve_single_line_statements = true + +#### Naming styles #### +[*.{cs,vb}] + +# Naming rules + +dotnet_naming_rule.types_and_namespaces_should_be_pascalcase.severity = suggestion +dotnet_naming_rule.types_and_namespaces_should_be_pascalcase.symbols = types_and_namespaces +dotnet_naming_rule.types_and_namespaces_should_be_pascalcase.style = pascalcase + +dotnet_naming_rule.interfaces_should_be_ipascalcase.severity = suggestion +dotnet_naming_rule.interfaces_should_be_ipascalcase.symbols = interfaces +dotnet_naming_rule.interfaces_should_be_ipascalcase.style = ipascalcase + +dotnet_naming_rule.type_parameters_should_be_tpascalcase.severity = suggestion +dotnet_naming_rule.type_parameters_should_be_tpascalcase.symbols = type_parameters +dotnet_naming_rule.type_parameters_should_be_tpascalcase.style = tpascalcase + +dotnet_naming_rule.methods_should_be_pascalcase.severity = suggestion +dotnet_naming_rule.methods_should_be_pascalcase.symbols = methods +dotnet_naming_rule.methods_should_be_pascalcase.style = pascalcase + +dotnet_naming_rule.properties_should_be_pascalcase.severity = suggestion +dotnet_naming_rule.properties_should_be_pascalcase.symbols = properties +dotnet_naming_rule.properties_should_be_pascalcase.style = pascalcase + +dotnet_naming_rule.events_should_be_pascalcase.severity = suggestion +dotnet_naming_rule.events_should_be_pascalcase.symbols = events +dotnet_naming_rule.events_should_be_pascalcase.style = pascalcase + +dotnet_naming_rule.local_variables_should_be_camelcase.severity = suggestion +dotnet_naming_rule.local_variables_should_be_camelcase.symbols = local_variables +dotnet_naming_rule.local_variables_should_be_camelcase.style = camelcase + +dotnet_naming_rule.local_constants_should_be_camelcase.severity = suggestion +dotnet_naming_rule.local_constants_should_be_camelcase.symbols = local_constants +dotnet_naming_rule.local_constants_should_be_camelcase.style = camelcase + +dotnet_naming_rule.parameters_should_be_camelcase.severity = suggestion +dotnet_naming_rule.parameters_should_be_camelcase.symbols = parameters +dotnet_naming_rule.parameters_should_be_camelcase.style = camelcase + +dotnet_naming_rule.public_fields_should_be_pascalcase.severity = suggestion +dotnet_naming_rule.public_fields_should_be_pascalcase.symbols = public_fields +dotnet_naming_rule.public_fields_should_be_pascalcase.style = pascalcase + +dotnet_naming_rule.private_fields_should_be__camelcase.severity = none +dotnet_naming_rule.private_fields_should_be__camelcase.symbols = private_fields +dotnet_naming_rule.private_fields_should_be__camelcase.style = _camelcase + +dotnet_naming_rule.private_static_fields_should_be_s_camelcase.severity = none +dotnet_naming_rule.private_static_fields_should_be_s_camelcase.symbols = private_static_fields +dotnet_naming_rule.private_static_fields_should_be_s_camelcase.style = s_camelcase + +dotnet_naming_rule.public_constant_fields_should_be_pascalcase.severity = suggestion +dotnet_naming_rule.public_constant_fields_should_be_pascalcase.symbols = public_constant_fields +dotnet_naming_rule.public_constant_fields_should_be_pascalcase.style = pascalcase + +dotnet_naming_rule.private_constant_fields_should_be_pascalcase.severity = suggestion +dotnet_naming_rule.private_constant_fields_should_be_pascalcase.symbols = private_constant_fields +dotnet_naming_rule.private_constant_fields_should_be_pascalcase.style = pascalcase + +dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.severity = suggestion +dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.symbols = public_static_readonly_fields +dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.style = pascalcase + +dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.severity = suggestion +dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.symbols = private_static_readonly_fields +dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.style = pascalcase + +dotnet_naming_rule.enums_should_be_pascalcase.severity = suggestion +dotnet_naming_rule.enums_should_be_pascalcase.symbols = enums +dotnet_naming_rule.enums_should_be_pascalcase.style = pascalcase + +dotnet_naming_rule.local_functions_should_be_pascalcase.severity = suggestion +dotnet_naming_rule.local_functions_should_be_pascalcase.symbols = local_functions +dotnet_naming_rule.local_functions_should_be_pascalcase.style = pascalcase + +dotnet_naming_rule.non_field_members_should_be_pascalcase.severity = suggestion +dotnet_naming_rule.non_field_members_should_be_pascalcase.symbols = non_field_members +dotnet_naming_rule.non_field_members_should_be_pascalcase.style = pascalcase + +# Symbol specifications + +dotnet_naming_symbols.interfaces.applicable_kinds = interface +dotnet_naming_symbols.interfaces.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.interfaces.required_modifiers = + +dotnet_naming_symbols.enums.applicable_kinds = enum +dotnet_naming_symbols.enums.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.enums.required_modifiers = + +dotnet_naming_symbols.events.applicable_kinds = event +dotnet_naming_symbols.events.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.events.required_modifiers = + +dotnet_naming_symbols.methods.applicable_kinds = method +dotnet_naming_symbols.methods.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.methods.required_modifiers = + +dotnet_naming_symbols.properties.applicable_kinds = property +dotnet_naming_symbols.properties.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.properties.required_modifiers = + +dotnet_naming_symbols.public_fields.applicable_kinds = field +dotnet_naming_symbols.public_fields.applicable_accessibilities = public, internal +dotnet_naming_symbols.public_fields.required_modifiers = + +dotnet_naming_symbols.private_fields.applicable_kinds = field +dotnet_naming_symbols.private_fields.applicable_accessibilities = private, protected, protected_internal, private_protected +dotnet_naming_symbols.private_fields.required_modifiers = + +dotnet_naming_symbols.private_static_fields.applicable_kinds = field +dotnet_naming_symbols.private_static_fields.applicable_accessibilities = private, protected, protected_internal, private_protected +dotnet_naming_symbols.private_static_fields.required_modifiers = static + +dotnet_naming_symbols.types_and_namespaces.applicable_kinds = namespace, class, struct, interface, enum +dotnet_naming_symbols.types_and_namespaces.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.types_and_namespaces.required_modifiers = + +dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method +dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.non_field_members.required_modifiers = + +dotnet_naming_symbols.type_parameters.applicable_kinds = namespace +dotnet_naming_symbols.type_parameters.applicable_accessibilities = * +dotnet_naming_symbols.type_parameters.required_modifiers = + +dotnet_naming_symbols.private_constant_fields.applicable_kinds = field +dotnet_naming_symbols.private_constant_fields.applicable_accessibilities = private, protected, protected_internal, private_protected +dotnet_naming_symbols.private_constant_fields.required_modifiers = const + +dotnet_naming_symbols.local_variables.applicable_kinds = local +dotnet_naming_symbols.local_variables.applicable_accessibilities = local +dotnet_naming_symbols.local_variables.required_modifiers = + +dotnet_naming_symbols.local_constants.applicable_kinds = local +dotnet_naming_symbols.local_constants.applicable_accessibilities = local +dotnet_naming_symbols.local_constants.required_modifiers = const + +dotnet_naming_symbols.parameters.applicable_kinds = parameter +dotnet_naming_symbols.parameters.applicable_accessibilities = * +dotnet_naming_symbols.parameters.required_modifiers = + +dotnet_naming_symbols.public_constant_fields.applicable_kinds = field +dotnet_naming_symbols.public_constant_fields.applicable_accessibilities = public, internal +dotnet_naming_symbols.public_constant_fields.required_modifiers = const + +dotnet_naming_symbols.public_static_readonly_fields.applicable_kinds = field +dotnet_naming_symbols.public_static_readonly_fields.applicable_accessibilities = public, internal +dotnet_naming_symbols.public_static_readonly_fields.required_modifiers = readonly, static + +dotnet_naming_symbols.private_static_readonly_fields.applicable_kinds = field +dotnet_naming_symbols.private_static_readonly_fields.applicable_accessibilities = private, protected, protected_internal, private_protected +dotnet_naming_symbols.private_static_readonly_fields.required_modifiers = readonly, static + +dotnet_naming_symbols.local_functions.applicable_kinds = local_function +dotnet_naming_symbols.local_functions.applicable_accessibilities = * +dotnet_naming_symbols.local_functions.required_modifiers = + +# Naming styles + +dotnet_naming_style.pascalcase.required_prefix = +dotnet_naming_style.pascalcase.required_suffix = +dotnet_naming_style.pascalcase.word_separator = +dotnet_naming_style.pascalcase.capitalization = pascal_case + +dotnet_naming_style.ipascalcase.required_prefix = I +dotnet_naming_style.ipascalcase.required_suffix = +dotnet_naming_style.ipascalcase.word_separator = +dotnet_naming_style.ipascalcase.capitalization = pascal_case + +dotnet_naming_style.tpascalcase.required_prefix = T +dotnet_naming_style.tpascalcase.required_suffix = +dotnet_naming_style.tpascalcase.word_separator = +dotnet_naming_style.tpascalcase.capitalization = pascal_case + +dotnet_naming_style._camelcase.required_prefix = _ +dotnet_naming_style._camelcase.required_suffix = +dotnet_naming_style._camelcase.word_separator = +dotnet_naming_style._camelcase.capitalization = camel_case + +dotnet_naming_style.camelcase.required_prefix = +dotnet_naming_style.camelcase.required_suffix = +dotnet_naming_style.camelcase.word_separator = +dotnet_naming_style.camelcase.capitalization = camel_case + +dotnet_naming_style.s_camelcase.required_prefix = s_ +dotnet_naming_style.s_camelcase.required_suffix = +dotnet_naming_style.s_camelcase.word_separator = +dotnet_naming_style.s_camelcase.capitalization = camel_case + diff --git a/build.cake b/build.cake index 74bb34b8..18bc920c 100644 --- a/build.cake +++ b/build.cake @@ -13,7 +13,7 @@ var configuration = Argument("configuration", "Release"); ////////////////////////////////////////////////////////////////////// var version = "4.2.0"; -var modifier = "-alpha.919.3"; +var modifier = "-alpha.4"; var dbgSuffix = configuration.ToLower() == "debug" ? "-dbg" : ""; var packageVersion = version + modifier + dbgSuffix; diff --git a/src/NUnitTestAdapter/AdapterSettings.cs b/src/NUnitTestAdapter/AdapterSettings.cs index 8ead1a05..a9771841 100644 --- a/src/NUnitTestAdapter/AdapterSettings.cs +++ b/src/NUnitTestAdapter/AdapterSettings.cs @@ -102,7 +102,6 @@ public interface IAdapterSettings bool StopOnError { get; } TestOutcome MapWarningTo { get; } bool UseTestNameInConsoleOutput { get; } - bool FreakMode { get; } DisplayNameOptions DisplayName { get; } char FullnameSeparator { get; } DiscoveryMethod DiscoveryMethod { get; } @@ -120,6 +119,12 @@ public interface IAdapterSettings void RestoreRandomSeed(string dirname); bool EnsureAttachmentFileScheme { get; } + + // For Internal Development use + bool FreakMode { get; } // displays metadata instead of real data in Test Explorer + bool Debug { get; } + bool DebugExecution { get; } + bool DebugDiscovery { get; } } public enum VsTestCategoryType @@ -269,11 +274,15 @@ public AdapterSettings(ITestLogger logger) public bool DumpVsInput { get; private set; } public bool FreakMode { get; private set; } + public bool Debug { get; private set; } + public bool DebugExecution { get; private set; } + public bool DebugDiscovery { get; private set; } #endregion + #endregion #region Public Methods @@ -393,6 +402,9 @@ private void ExtractNUnitDiagnosticSettings(XmlNode nunitNode) FreakMode = GetInnerTextAsBool(nunitNode, nameof(FreakMode), false); InternalTraceLevel = GetInnerTextWithLog(nunitNode, nameof(InternalTraceLevel), "Off", "Error", "Warning", "Info", "Verbose", "Debug"); + Debug = GetInnerTextAsBool(nunitNode, nameof(Debug), false); + DebugExecution = Debug || GetInnerTextAsBool(nunitNode, nameof(DebugExecution), false); + DebugDiscovery = Debug || GetInnerTextAsBool(nunitNode, nameof(DebugDiscovery), false); } private void UpdateTestProperties(XmlDocument doc) diff --git a/src/NUnitTestAdapter/CategoryList.cs b/src/NUnitTestAdapter/CategoryList.cs index 34e7062b..97036b5b 100644 --- a/src/NUnitTestAdapter/CategoryList.cs +++ b/src/NUnitTestAdapter/CategoryList.cs @@ -138,10 +138,10 @@ private bool IsInternalProperty(NUnitProperty property) } // Property names starting with '_' are for internal use only, but over time this has changed, so we now use a list - if (!showInternalProperties && - _internalProperties.Contains(property.Name)) - return true; - return string.IsNullOrEmpty(property.Name) || property.Name[0] == '_' || string.IsNullOrEmpty(property.Value); + return (!showInternalProperties && + _internalProperties.Contains(property.Name)) || (string.IsNullOrEmpty(property.Name) || + property.Name[0] == '_' || + string.IsNullOrEmpty(property.Value)); } private void AddTraitsToCache(IDictionary traitsCache, string key, NUnitProperty property) diff --git a/src/NUnitTestAdapter/Execution.cs b/src/NUnitTestAdapter/Execution.cs index 5044275e..80780d1f 100644 --- a/src/NUnitTestAdapter/Execution.cs +++ b/src/NUnitTestAdapter/Execution.cs @@ -18,12 +18,7 @@ public interface IExecutionContext public static class ExecutionFactory { - public static Execution Create(IExecutionContext ctx) - { - if (ctx.Settings.DesignMode) // We come from IDE - return new IdeExecution(ctx); - return new VsTestExecution(ctx); - } + public static Execution Create(IExecutionContext ctx) => ctx.Settings.DesignMode ? new IdeExecution(ctx) : new VsTestExecution(ctx); } public abstract class Execution diff --git a/src/NUnitTestAdapter/Internal/TimingLogger.cs b/src/NUnitTestAdapter/Internal/TimingLogger.cs index 1c62af9a..e16c3735 100644 --- a/src/NUnitTestAdapter/Internal/TimingLogger.cs +++ b/src/NUnitTestAdapter/Internal/TimingLogger.cs @@ -27,7 +27,7 @@ public TimingLogger ReStart() public TimingLogger LogTime(string leadText = "") { - if (settings.Verbosity < 5) + if (settings.Verbosity < 5 || Stopwatch == null) return this; var ts = Stopwatch.Elapsed; string elapsedTime = $"{ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds / 10:00}"; diff --git a/src/NUnitTestAdapter/NUnit3TestDiscoverer.cs b/src/NUnitTestAdapter/NUnit3TestDiscoverer.cs index aa7e92f9..adc7a26b 100644 --- a/src/NUnitTestAdapter/NUnit3TestDiscoverer.cs +++ b/src/NUnitTestAdapter/NUnit3TestDiscoverer.cs @@ -21,14 +21,11 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // *********************************************************************** -// #define LAUNCHDEBUGGER using System; using System.Collections.Generic; using System.ComponentModel; -#if LAUNCHDEBUGGER using System.Diagnostics; -#endif using System.IO; using System.Linq; using System.Xml; @@ -57,11 +54,8 @@ public sealed class NUnit3TestDiscoverer : NUnitTestAdapter, ITestDiscoverer public void DiscoverTests(IEnumerable sources, IDiscoveryContext discoveryContext, IMessageLogger messageLogger, ITestCaseDiscoverySink discoverySink) { -#if LAUNCHDEBUGGER - if (!Debugger.IsAttached) - Debugger.Launch(); -#endif Initialize(discoveryContext, messageLogger); + CheckIfDebug(); TestLog.Info($"NUnit Adapter {AdapterVersion}: Test discovery starting"); // Ensure any channels registered by other adapters are unregistered @@ -182,10 +176,6 @@ private int ProcessTestCases(NUnitResults results, ITestCaseDiscoverySink discov { try { -#if LAUNCHDEBUGGER - if (!Debugger.IsAttached) - Debugger.Launch(); -#endif var testCase = testConverterForXml.ConvertTestCase(new NUnitEventTestCase(testNode)); discoverySink.SendTestCase(testCase); cases += 1; @@ -199,6 +189,13 @@ private int ProcessTestCases(NUnitResults results, ITestCaseDiscoverySink discov return cases; } + private void CheckIfDebug() + { + if (!Settings.DebugDiscovery) + return; + if (!Debugger.IsAttached) + Debugger.Launch(); + } #endregion } } diff --git a/src/NUnitTestAdapter/NUnit3TestExecutor.cs b/src/NUnitTestAdapter/NUnit3TestExecutor.cs index 58e32fe3..ed057d17 100644 --- a/src/NUnitTestAdapter/NUnit3TestExecutor.cs +++ b/src/NUnitTestAdapter/NUnit3TestExecutor.cs @@ -21,7 +21,6 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // *********************************************************************** -// #define LAUNCHDEBUGGER using System; using System.Collections.Generic; @@ -29,8 +28,10 @@ using System.IO; using System.Linq; using System.Reflection; + using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; + using NUnit.Engine; using NUnit.VisualStudio.TestAdapter.Dump; using NUnit.VisualStudio.TestAdapter.Internal; @@ -57,7 +58,8 @@ public enum RunType [ExtensionUri(ExecutorUri)] - public sealed class NUnit3TestExecutor : NUnitTestAdapter, ITestExecutor, IDisposable, INUnit3TestExecutor, IExecutionContext + public sealed class NUnit3TestExecutor : NUnitTestAdapter, ITestExecutor, IDisposable, INUnit3TestExecutor, + IExecutionContext { #region Properties @@ -96,17 +98,15 @@ public sealed class NUnit3TestExecutor : NUnitTestAdapter, ITestExecutor, IDispo /// Test log to send results and messages through. public void RunTests(IEnumerable sources, IRunContext runContext, IFrameworkHandle frameworkHandle) { -#if LAUNCHDEBUGGER - if (!Debugger.IsAttached) - Debugger.Launch(); -#endif Initialize(runContext, frameworkHandle); + CheckIfDebug(); TestLog.Debug("RunTests by IEnumerable"); InitializeForExecution(runContext, frameworkHandle); if (Settings.InProcDataCollectorsAvailable && sources.Count() > 1) { - TestLog.Error("Failed to run tests for multiple assemblies when InProcDataCollectors specified in run configuration."); + TestLog.Error( + "Failed to run tests for multiple assemblies when InProcDataCollectors specified in run configuration."); Unload(); return; } @@ -119,18 +119,22 @@ public void RunTests(IEnumerable sources, IRunContext runContext, IFrame var vsTestFilter = VsTestFilterFactory.CreateVsTestFilter(Settings, runContext); filter = builder.ConvertVsTestFilterToNUnitFilter(vsTestFilter); } + filter ??= builder.FilterByWhere(Settings.Where); foreach (string assemblyName in sources) { try { - string assemblyPath = Path.IsPathRooted(assemblyName) ? assemblyName : Path.Combine(Directory.GetCurrentDirectory(), assemblyName); + string assemblyPath = Path.IsPathRooted(assemblyName) + ? assemblyName + : Path.Combine(Directory.GetCurrentDirectory(), assemblyName); RunAssembly(assemblyPath, null, filter); } catch (Exception ex) { if (ex is TargetInvocationException) { ex = ex.InnerException; } + TestLog.Warning("Exception thrown executing tests", ex); } } @@ -156,11 +160,8 @@ private void SetRunTypeByStrings() => /// The FrameworkHandle. public void RunTests(IEnumerable tests, IRunContext runContext, IFrameworkHandle frameworkHandle) { -#if LAUNCHDEBUGGER - if (!Debugger.IsAttached) - Debugger.Launch(); -#endif Initialize(runContext, frameworkHandle); + CheckIfDebug(); TestLog.Debug("RunTests by IEnumerable"); InitializeForExecution(runContext, frameworkHandle); RunType = RunType.Ide; @@ -170,7 +171,8 @@ public void RunTests(IEnumerable tests, IRunContext runContext, IFrame var assemblyGroups = tests.GroupBy(tc => tc.Source); if (IsInProcDataCollectorsSpecifiedWithMultipleAssemblies(assemblyGroups)) { - TestLog.Error("Failed to run tests for multiple assemblies when InProcDataCollectors specified in run configuration."); + TestLog.Error( + "Failed to run tests for multiple assemblies when InProcDataCollectors specified in run configuration."); Unload(); return; } @@ -181,7 +183,9 @@ public void RunTests(IEnumerable tests, IRunContext runContext, IFrame try { string assemblyName = assemblyGroup.Key; - string assemblyPath = Path.IsPathRooted(assemblyName) ? assemblyName : Path.Combine(Directory.GetCurrentDirectory(), assemblyName); + string assemblyPath = Path.IsPathRooted(assemblyName) + ? assemblyName + : Path.Combine(Directory.GetCurrentDirectory(), assemblyName); var filterBuilder = CreateTestFilterBuilder(); var filter = filterBuilder.FilterByList(assemblyGroup); @@ -191,6 +195,7 @@ public void RunTests(IEnumerable tests, IRunContext runContext, IFrame catch (Exception ex) { if (ex is TargetInvocationException) { ex = ex.InnerException; } + TestLog.Warning("Exception thrown executing tests", ex); } @@ -202,7 +207,8 @@ public void RunTests(IEnumerable tests, IRunContext runContext, IFrame Unload(); } - private bool IsInProcDataCollectorsSpecifiedWithMultipleAssemblies(IEnumerable> assemblyGroups) + private bool IsInProcDataCollectorsSpecifiedWithMultipleAssemblies( + IEnumerable> assemblyGroups) => Settings.InProcDataCollectorsAvailable && assemblyGroups.Count() > 1; void ITestExecutor.Cancel() @@ -244,7 +250,9 @@ public void InitializeForExecution(IRunContext runContext, IFrameworkHandle fram if (VsTestFilter.IsEmpty) { - if (!(enableShutdown && !runContext.KeepAlive)) // Otherwise causes exception when run as commandline, illegal to enableshutdown when Keepalive is false, might be only VS2012 + if (!(enableShutdown && + !runContext + .KeepAlive)) // Otherwise causes exception when run as commandline, illegal to enableshutdown when Keepalive is false, might be only VS2012 frameworkHandle.EnableShutdownAfterTestRun = enableShutdown; } @@ -253,9 +261,7 @@ public void InitializeForExecution(IRunContext runContext, IFrameworkHandle fram private void RunAssembly(string assemblyPath, IGrouping testCases, TestFilter filter) { - string actionText = Debugger.IsAttached ? "Debugging " : "Running "; - string selectionText = filter == null || filter == TestFilter.Empty ? "all" : "selected"; - TestLog.Info(actionText + selectionText + " tests in " + assemblyPath); + LogActionAndSelection(assemblyPath, filter); RestoreRandomSeed(assemblyPath); Dump = DumpXml.CreateDump(assemblyPath, testCases, Settings); @@ -279,9 +285,7 @@ private void RunAssembly(string assemblyPath, IGrouping testCa } else { - TestLog.Info(discoveryResults.HasNoNUnitTests - ? " NUnit couldn't find any tests in " + assemblyPath - : " NUnit failed to load " + assemblyPath); + TestLog.InfoNoTests(discoveryResults.HasNoNUnitTests, assemblyPath); } } catch (Exception ex) when (ex is BadImageFormatException || ex.InnerException is BadImageFormatException) @@ -292,7 +296,8 @@ private void RunAssembly(string assemblyPath, IGrouping testCa catch (FileNotFoundException ex) { // Probably from the GetExportedTypes in NUnit.core, attempting to find an assembly, not a problem if it is not NUnit here - TestLog.Warning(" Dependent Assembly " + ex.FileName + " of " + assemblyPath + " not found. Can be ignored if not an NUnit project."); + TestLog.Warning(" Dependent Assembly " + ex.FileName + " of " + assemblyPath + + " not found. Can be ignored if not an NUnit project."); } catch (Exception ex) { @@ -312,12 +317,18 @@ private void RunAssembly(string assemblyPath, IGrouping testCa // can happen if CLR throws CannotUnloadAppDomainException, for example // due to a long-lasting operation in a protected region (catch/finally clause). if (ex is TargetInvocationException) { ex = ex.InnerException; } + TestLog.Warning($" Exception thrown unloading tests from {assemblyPath}", ex); } } } - + private void LogActionAndSelection(string assemblyPath, TestFilter filter) + { + string actionText = Debugger.IsAttached ? "Debugging " : "Running "; + string selectionText = filter == null || filter == TestFilter.Empty ? "all" : "selected"; + TestLog.Info(actionText + selectionText + " tests in " + assemblyPath); + } private void RestoreRandomSeed(string assemblyPath) @@ -328,12 +339,7 @@ private void RestoreRandomSeed(string assemblyPath) } - private NUnitTestFilterBuilder CreateTestFilterBuilder() - { - return new (NUnitEngineAdapter.GetService(), Settings); - } - - + private NUnitTestFilterBuilder CreateTestFilterBuilder() => new (NUnitEngineAdapter.GetService(), Settings); private void CreateTestOutputFolder() @@ -367,5 +373,13 @@ public void StopRun() } public IDumpXml Dump { get; private set; } + + private void CheckIfDebug() + { + if (!Settings.DebugExecution) + return; + if (!Debugger.IsAttached) + Debugger.Launch(); + } } -} +} \ No newline at end of file diff --git a/src/NUnitTestAdapter/NUnitTestAdapter.cs b/src/NUnitTestAdapter/NUnitTestAdapter.cs index 0ab4119d..754f662c 100644 --- a/src/NUnitTestAdapter/NUnitTestAdapter.cs +++ b/src/NUnitTestAdapter/NUnitTestAdapter.cs @@ -21,8 +21,6 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // *********************************************************************** -// #define VERBOSE - using System; using System.Collections.Generic; using System.Diagnostics; @@ -109,9 +107,9 @@ public static bool IsRunningUnderIde } return exeName != null && ( - exeName.Contains("vstest.executionengine") || - exeName.Contains("vstest.discoveryengine") || - exeName.Contains("TE.ProcessHost")); + exeName.Contains("vstest.executionengine") || + exeName.Contains("vstest.discoveryengine") || + exeName.Contains("TE.ProcessHost")); } } @@ -146,13 +144,7 @@ protected void Initialize(IDiscoveryContext context, IMessageLogger messageLogge } finally { -#if NET35 - string fw = "Net Framework"; -#else - string fw = "Net Core"; -#endif - var assLoc = Assembly.GetExecutingAssembly().Location; - TestLog.Debug($"{fw} adapter running from {assLoc}"); + TestLog.DebugRunfrom(); } } @@ -213,8 +205,10 @@ protected TestPackage CreateTestPackage(string assemblyName, IGrouping 0 ? testCase.FullyQualifiedName.Substring(0, end) : testCase.FullyQualifiedName); + prefilters.Add( + end > 0 ? testCase.FullyQualifiedName.Substring(0, end) : testCase.FullyQualifiedName); } + package.Settings[PackageSettings.LOAD] = prefilters; } @@ -256,6 +250,7 @@ protected TestPackage CreateTestPackage(string assemblyName, IGrouping /// Sets test parameters, handling backwards compatibility. /// - private static void SetTestParameters(IDictionary runSettings, IDictionary testParameters) + private static void SetTestParameters( + IDictionary runSettings, + IDictionary testParameters) { runSettings[PackageSettings.TestParametersDictionary] = testParameters; @@ -297,7 +294,8 @@ private static void SetTestParameters(IDictionary runSettings, I foreach (var parameter in testParameters) oldFrameworkSerializedParameters.Append(parameter.Key).Append('=').Append(parameter.Value).Append(';'); - runSettings[PackageSettings.TestParameters] = oldFrameworkSerializedParameters.ToString(0, oldFrameworkSerializedParameters.Length - 1); + runSettings[PackageSettings.TestParameters] = + oldFrameworkSerializedParameters.ToString(0, oldFrameworkSerializedParameters.Length - 1); } /// @@ -319,6 +317,6 @@ protected void Unload() NUnitEngineAdapter = null; } -#endregion + #endregion } -} +} \ No newline at end of file diff --git a/src/NUnitTestAdapter/NUnitTestFilterBuilder.cs b/src/NUnitTestAdapter/NUnitTestFilterBuilder.cs index f5245470..4267672c 100644 --- a/src/NUnitTestAdapter/NUnitTestFilterBuilder.cs +++ b/src/NUnitTestAdapter/NUnitTestFilterBuilder.cs @@ -60,9 +60,7 @@ public TestFilter ConvertVsTestFilterToNUnitFilter(IVsTestFilter vsFilter, IDisc if (!settings.UseNUnitFilter) return ConvertTfsFilterToNUnitFilter(vsFilter, discovery); var result = ConvertVsTestFilterToNUnitFilter(vsFilter); - if (result == null) - return ConvertTfsFilterToNUnitFilter(vsFilter, discovery); - return result; + return result ?? ConvertTfsFilterToNUnitFilter(vsFilter, discovery); } /// diff --git a/src/NUnitTestAdapter/TestFilterConverter/Tokenizer.cs b/src/NUnitTestAdapter/TestFilterConverter/Tokenizer.cs index 650b23e6..be969b30 100644 --- a/src/NUnitTestAdapter/TestFilterConverter/Tokenizer.cs +++ b/src/NUnitTestAdapter/TestFilterConverter/Tokenizer.cs @@ -74,19 +74,10 @@ public override string ToString() bool t1Null = ReferenceEquals(t1, null); bool t2Null = ReferenceEquals(t2, null); - if (t1Null && t2Null) - return true; - - if (t1Null || t2Null) - return false; - - return t1.Kind == t2.Kind && t1.Text == t2.Text; + return (t1Null && t2Null) || (!t1Null && !t2Null && (t1.Kind == t2.Kind && t1.Text == t2.Text)); } - public static bool operator !=(Token t1, Token t2) - { - return !(t1 == t2); - } + public static bool operator !=(Token t1, Token t2) => !(t1 == t2); #endregion } diff --git a/src/NUnitTestAdapter/TestLogger.cs b/src/NUnitTestAdapter/TestLogger.cs index 2623cfe4..1341ab14 100644 --- a/src/NUnitTestAdapter/TestLogger.cs +++ b/src/NUnitTestAdapter/TestLogger.cs @@ -22,6 +22,7 @@ // *********************************************************************** using System; +using System.Reflection; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; @@ -146,5 +147,25 @@ public void SendMessage(TestMessageLevel testMessageLevel, string message, Excep } } #endregion + + #region SpecializedMessages + public void DebugRunfrom() + { +#if NET35 + string fw = "Net Framework"; +#else + string fw = "Net Core"; +#endif + var assLoc = Assembly.GetExecutingAssembly().Location; + Debug($"{fw} adapter running from {assLoc}"); + } + + public void InfoNoTests(bool discoveryResultsHasNoNUnitTests, string assemblyPath) + { + Info(discoveryResultsHasNoNUnitTests + ? " NUnit couldn't find any tests in " + assemblyPath + : " NUnit failed to load " + assemblyPath); + } + #endregion } } diff --git a/src/NUnitTestAdapter/VsTestFilter.cs b/src/NUnitTestAdapter/VsTestFilter.cs index b20a7eb7..58cb230c 100644 --- a/src/NUnitTestAdapter/VsTestFilter.cs +++ b/src/NUnitTestAdapter/VsTestFilter.cs @@ -174,9 +174,7 @@ public static TestProperty PropertyProvider(string propertyName) return testProperty; } var testTrait = TraitProvider(propertyName); - if (testTrait == null) - return null; - return TraitPropertyMap.TryGetValue(testTrait, out var tp) ? tp : null; + return testTrait == null ? null : TraitPropertyMap.TryGetValue(testTrait, out var tp) ? tp : null; } public static NTrait TraitProvider(string traitName) @@ -188,14 +186,12 @@ public static NTrait TraitProvider(string traitName) public static class VsTestFilterFactory { - public static VsTestFilter CreateVsTestFilter(IAdapterSettings settings, IRunContext context) - { - if (settings.DiscoveryMethod == DiscoveryMethod.Legacy) - return new VsTestFilterLegacy(context); - if (settings.DesignMode) - return new VsTestFilterIde(context); - return new VsTestFilterNonIde(context); - } + public static VsTestFilter CreateVsTestFilter(IAdapterSettings settings, IRunContext context) => + settings.DiscoveryMethod == DiscoveryMethod.Legacy + ? new VsTestFilterLegacy(context) : + settings.DesignMode + ? new VsTestFilterIde(context) + : new VsTestFilterNonIde(context); } public class VsTestFilterLegacy : VsTestFilter