Skip to content

Commit

Permalink
Modified Configuration's use of ITraceListener to defer resolution of…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
clrudolphi committed Nov 13, 2024
1 parent db3219f commit 01972bf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
30 changes: 16 additions & 14 deletions Reqnroll/CucumberMessages/Configuration/CucumberConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Reqnroll.CommonModels;
using Reqnroll.BoDi;
using Reqnroll.CommonModels;
using Reqnroll.EnvironmentAccess;
using Reqnroll.Tracing;
using System;
Expand Down Expand Up @@ -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<ITraceListener> _traceListenerLazy;
private IEnvironmentWrapper _environmentWrapper;

private Lazy<ResolvedConfiguration> _resolvedConfiguration;
private Lazy<ResolvedConfiguration> _resolvedConfiguration;
private bool _enablementOverrideFlag = true;

public CucumberConfiguration(ITraceListener traceListener, IEnvironmentWrapper environmentWrapper)
public CucumberConfiguration(IObjectContainer objectContainer, IEnvironmentWrapper environmentWrapper)
{
_trace = traceListener;
_objectContainer = objectContainer;
_traceListenerLazy = new Lazy<ITraceListener>(() => _objectContainer.Resolve<ITraceListener>());
_environmentWrapper = environmentWrapper;
_resolvedConfiguration = new Lazy<ResolvedConfiguration>(ResolveConfiguration);
Current = this;
Expand All @@ -50,8 +52,8 @@ public void SetEnabled(bool value)
_enablementOverrideFlag = value;
}
#endregion


private ResolvedConfiguration ResolveConfiguration()
{
var config = ApplyHierarchicalConfiguration();
Expand All @@ -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;
Expand All @@ -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<string> ?
((Success<string>)profileValue).Result :
string profileName = profileValue is Success<string> ?
((Success<string>)profileValue).Result :
!string.IsNullOrEmpty(config.ActiveProfileName) ? config.ActiveProfileName :
"DEFAULT";
var idGenStyleValue = _environmentWrapper.GetEnvironmentVariable(CucumberConfigurationConstants.REQNROLL_CUCUMBER_MESSAGES_ID_GENERATION_STYLE_ENVIRONMENT_VARIABLE);
Expand Down Expand Up @@ -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<string> ? Convert.ToBoolean(((Success<string>)enabledResult).Result) : result.Enabled;

return result;
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -121,9 +123,11 @@ protected static string ActualsResultLocationDirectory()

//var config = System.Text.Json.JsonSerializer.Deserialize<ConfigurationDTO>(File.ReadAllText(configFileLocation), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });

var objectContainerMock = new Mock<IObjectContainer>();
var tracerMock = new Mock<ITraceListener>();
objectContainerMock.Setup(x => x.Resolve<ITraceListener>()).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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 01972bf

Please sign in to comment.