From 01972bf2c97748e74b36f762cfdf8207f153c94b Mon Sep 17 00:00:00 2001 From: Chris Rudolphi <1702962+clrudolphi@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:13:53 -0600 Subject: [PATCH] Modified Configuration's use of ITraceListener to defer resolution of the TraceListener until the moment its needed rather than as a constructor param. This avoids a conflict when the Test Frameworks provide their own implementation (which caused an 'oibject already resolved' error in the object container). Related changes in Tests to match. --- .../Configuration/CucumberConfiguration.cs | 30 ++++++++++--------- .../CucumberCompatibilityTestBase.cs | 6 +++- .../CucumberMessages.configuration.json | 4 +-- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Reqnroll/CucumberMessages/Configuration/CucumberConfiguration.cs b/Reqnroll/CucumberMessages/Configuration/CucumberConfiguration.cs index 47f6cae3..01148d82 100644 --- a/Reqnroll/CucumberMessages/Configuration/CucumberConfiguration.cs +++ b/Reqnroll/CucumberMessages/Configuration/CucumberConfiguration.cs @@ -1,4 +1,5 @@ -using Reqnroll.CommonModels; +using Reqnroll.BoDi; +using Reqnroll.CommonModels; using Reqnroll.EnvironmentAccess; using Reqnroll.Tracing; using System; @@ -29,16 +30,17 @@ public class CucumberConfiguration : ICucumberConfiguration public string OutputFileName => _resolvedConfiguration.Value.OutputFileName; public IDGenerationStyle IDGenerationStyle => _resolvedConfiguration.Value.IDGenerationStyle; - - private ITraceListener _trace; + private readonly IObjectContainer _objectContainer; + private Lazy _traceListenerLazy; private IEnvironmentWrapper _environmentWrapper; - private Lazy _resolvedConfiguration; + private Lazy _resolvedConfiguration; private bool _enablementOverrideFlag = true; - public CucumberConfiguration(ITraceListener traceListener, IEnvironmentWrapper environmentWrapper) + public CucumberConfiguration(IObjectContainer objectContainer, IEnvironmentWrapper environmentWrapper) { - _trace = traceListener; + _objectContainer = objectContainer; + _traceListenerLazy = new Lazy(() => _objectContainer.Resolve()); _environmentWrapper = environmentWrapper; _resolvedConfiguration = new Lazy(ResolveConfiguration); Current = this; @@ -50,8 +52,8 @@ public void SetEnabled(bool value) _enablementOverrideFlag = value; } #endregion - - + + private ResolvedConfiguration ResolveConfiguration() { var config = ApplyHierarchicalConfiguration(); @@ -61,7 +63,7 @@ private ResolvedConfiguration ResolveConfiguration() if (string.IsNullOrEmpty(resolved.OutputFileName)) { resolved.OutputFileName = "reqnroll_report.ndjson"; - _trace!.WriteToolOutput($"WARNING: Cucumber Messages: Output filename was empty. Setting filename to {resolved.OutputFileName}"); + _traceListenerLazy.Value.WriteToolOutput($"WARNING: Cucumber Messages: Output filename was empty. Setting filename to {resolved.OutputFileName}"); } EnsureOutputDirectory(resolved); return resolved; @@ -82,8 +84,8 @@ private ResolvedConfiguration ApplyEnvironmentOverrides(ConfigurationDTO config) var relativePathValue = _environmentWrapper.GetEnvironmentVariable(CucumberConfigurationConstants.REQNROLL_CUCUMBER_MESSAGES_OUTPUT_RELATIVE_PATH_ENVIRONMENT_VARIABLE); var fileNameValue = _environmentWrapper.GetEnvironmentVariable(CucumberConfigurationConstants.REQNROLL_CUCUMBER_MESSAGES_OUTPUT_FILENAME_ENVIRONMENT_VARIABLE); var profileValue = _environmentWrapper.GetEnvironmentVariable(CucumberConfigurationConstants.REQNROLL_CUCUMBER_MESSAGES_ACTIVE_OUTPUT_PROFILE_ENVIRONMENT_VARIABLE); - string profileName = profileValue is Success ? - ((Success)profileValue).Result : + string profileName = profileValue is Success ? + ((Success)profileValue).Result : !string.IsNullOrEmpty(config.ActiveProfileName) ? config.ActiveProfileName : "DEFAULT"; var idGenStyleValue = _environmentWrapper.GetEnvironmentVariable(CucumberConfigurationConstants.REQNROLL_CUCUMBER_MESSAGES_ID_GENERATION_STYLE_ENVIRONMENT_VARIABLE); @@ -117,7 +119,7 @@ private ResolvedConfiguration ApplyEnvironmentOverrides(ConfigurationDTO config) var enabledResult = _environmentWrapper.GetEnvironmentVariable(CucumberConfigurationConstants.REQNROLL_CUCUMBER_MESSAGES_ENABLE_ENVIRONMENT_VARIABLE); result.Enabled = enabledResult is Success ? Convert.ToBoolean(((Success)enabledResult).Result) : result.Enabled; - + return result; } @@ -129,10 +131,10 @@ private ConfigurationDTO AddConfig(ConfigurationDTO rootConfig, ConfigurationDTO { AddOrOverrideProfile(rootConfig.Profiles, overridingProfile); } - if (!String.IsNullOrEmpty(overridingConfig.ActiveProfileName) && !rootConfig.Profiles.Any(p => p.ProfileName == overridingConfig.ActiveProfileName)) + if (!String.IsNullOrEmpty(overridingConfig.ActiveProfileName) && !rootConfig.Profiles.Any(p => p.ProfileName == overridingConfig.ActiveProfileName)) { // The incoming configuration DTO points to a profile that doesn't exist. - _trace.WriteToolOutput($"WARNING: Configuration file specifies an active profile that doesn't exist: {overridingConfig.ActiveProfileName}. Using {rootConfig.ActiveProfileName} instead."); + _traceListenerLazy.Value.WriteToolOutput($"WARNING: Configuration file specifies an active profile that doesn't exist: {overridingConfig.ActiveProfileName}. Using {rootConfig.ActiveProfileName} instead."); } else if (!String.IsNullOrEmpty(overridingConfig.ActiveProfileName)) rootConfig.ActiveProfileName = overridingConfig.ActiveProfileName; diff --git a/Tests/CucumberMessages.CompatibilityTests/CucumberCompatibilityTestBase.cs b/Tests/CucumberMessages.CompatibilityTests/CucumberCompatibilityTestBase.cs index ee84d0bc..b30b7c3f 100644 --- a/Tests/CucumberMessages.CompatibilityTests/CucumberCompatibilityTestBase.cs +++ b/Tests/CucumberMessages.CompatibilityTests/CucumberCompatibilityTestBase.cs @@ -1,6 +1,7 @@ using FluentAssertions; using Io.Cucumber.Messages.Types; using Moq; +using Reqnroll.BoDi; using Reqnroll.CucumberMessages.Configuration; using Reqnroll.CucumberMessages.PayloadProcessing; using Reqnroll.EnvironmentAccess; @@ -9,6 +10,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.WebSockets; using System.Reflection; using System.Text; using System.Text.Json; @@ -121,9 +123,11 @@ protected static string ActualsResultLocationDirectory() //var config = System.Text.Json.JsonSerializer.Deserialize(File.ReadAllText(configFileLocation), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); + var objectContainerMock = new Mock(); var tracerMock = new Mock(); + objectContainerMock.Setup(x => x.Resolve()).Returns(tracerMock.Object); var env = new EnvironmentWrapper(); - CucumberConfiguration configuration = new CucumberConfiguration(tracerMock.Object, env); + CucumberConfiguration configuration = new CucumberConfiguration(objectContainerMock.Object, env); var resultLocation = Path.Combine(configuration.BaseDirectory, configuration.OutputDirectory); return resultLocation; } diff --git a/Tests/CucumberMessages.CompatibilityTests/CucumberMessages.configuration.json b/Tests/CucumberMessages.CompatibilityTests/CucumberMessages.configuration.json index 615a0973..aff2d333 100644 --- a/Tests/CucumberMessages.CompatibilityTests/CucumberMessages.configuration.json +++ b/Tests/CucumberMessages.CompatibilityTests/CucumberMessages.configuration.json @@ -1,9 +1,9 @@ { "fileOutputEnabled": true, - "activeProfileName": "LOCAL", + "activeProfileName": "DEFAULT", "profiles": [ { - "profileName": "LOCAL", + "profileName": "DEFAULT", "basePath": "C:\\Users\\clrud\\source\\repos\\scratch", "outputDirectory": "CucumberMessages", "IDGenerationStyle": "INCREMENTING"