Skip to content

Commit

Permalink
Make it optional for Gauge to load referenced assemblies in a project. (
Browse files Browse the repository at this point in the history
#251)

Signed-off-by: Jens Johansson <jens.johansson@systemverification.com>
  • Loading branch information
jensakejohansson authored Feb 14, 2025
1 parent 6492d52 commit 84ce1f9
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 22 deletions.
2 changes: 1 addition & 1 deletion gauge-proto
5 changes: 3 additions & 2 deletions integration-test/ExecuteStepProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Gauge.Dotnet.Registries;
using Gauge.Dotnet.Wrappers;
using Gauge.Messages;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

Expand All @@ -30,7 +31,7 @@ public async Task ShouldExecuteMethodFromRequest()
var assemblyLocater = new AssemblyLocater(_fileProvider, _loggerFactory.CreateLogger<AssemblyLocater>());
var gaugeLoadContext = new GaugeLoadContext(assemblyLocater, _loggerFactory.CreateLogger<GaugeLoadContext>());
var assemblyLoader = new AssemblyLoader(assemblyLocater, gaugeLoadContext, reflectionWrapper, activatorWrapper, new StepRegistry(),
_loggerFactory.CreateLogger<AssemblyLoader>());
_loggerFactory.CreateLogger<AssemblyLoader>(), _configuration);
var dataStoreFactory = new DataStoreFactory(assemblyLoader, activatorWrapper);
var tableFormatter = new TableFormatter(assemblyLoader, activatorWrapper);
var executionInfoMapper = new ExecutionInfoMapper(assemblyLoader, activatorWrapper, dataStoreFactory, tableFormatter);
Expand Down Expand Up @@ -88,7 +89,7 @@ public async Task ShouldCaptureScreenshotOnFailure()
var assemblyLocater = new AssemblyLocater(_fileProvider, _loggerFactory.CreateLogger<AssemblyLocater>());
var gaugeLoadContext = new GaugeLoadContext(assemblyLocater, _loggerFactory.CreateLogger<GaugeLoadContext>());
var assemblyLoader = new AssemblyLoader(assemblyLocater, gaugeLoadContext, reflectionWrapper,
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>());
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>(), _configuration);
var hookRegistry = new HookRegistry(assemblyLoader);
var dataStoreFactory = new DataStoreFactory(assemblyLoader, activatorWrapper);
var tableFormatter = new TableFormatter(assemblyLoader, activatorWrapper);
Expand Down
16 changes: 8 additions & 8 deletions integration-test/ExecutionOrchestratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task RecoverableIsTrueOnExceptionThrownWhenContinueOnFailure()
var assemblyLocater = new AssemblyLocater(_fileProvider, _loggerFactory.CreateLogger<AssemblyLocater>());
var gaugeLoadContext = new GaugeLoadContext(assemblyLocater, _loggerFactory.CreateLogger<GaugeLoadContext>());
var assemblyLoader = new AssemblyLoader(assemblyLocater, gaugeLoadContext, reflectionWrapper,
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>());
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>(), _configuration);
var hookRegistry = new HookRegistry(assemblyLoader);
var dataStoreFactory = new DataStoreFactory(assemblyLoader, activatorWrapper);
var tableFormatter = new TableFormatter(assemblyLoader, activatorWrapper);
Expand All @@ -54,7 +54,7 @@ public async Task ShouldCreateTableFromTargetType()
var assemblyLocater = new AssemblyLocater(_fileProvider, _loggerFactory.CreateLogger<AssemblyLocater>());
var gaugeLoadContext = new GaugeLoadContext(assemblyLocater, _loggerFactory.CreateLogger<GaugeLoadContext>());
var assemblyLoader = new AssemblyLoader(assemblyLocater, gaugeLoadContext, reflectionWrapper,
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>());
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>(), _configuration);
var hookRegistry = new HookRegistry(assemblyLoader);
var dataStoreFactory = new DataStoreFactory(assemblyLoader, activatorWrapper);
var tableFormatter = new TableFormatter(assemblyLoader, activatorWrapper);
Expand All @@ -81,7 +81,7 @@ public async Task ShouldExecuteMethodAndReturnResult()
var assemblyLocater = new AssemblyLocater(_fileProvider, _loggerFactory.CreateLogger<AssemblyLocater>());
var gaugeLoadContext = new GaugeLoadContext(assemblyLocater, _loggerFactory.CreateLogger<GaugeLoadContext>());
var assemblyLoader = new AssemblyLoader(assemblyLocater, gaugeLoadContext, reflectionWrapper,
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>());
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>(), _configuration);
var hookRegistry = new HookRegistry(assemblyLoader);
var dataStoreFactory = new DataStoreFactory(assemblyLoader, activatorWrapper);
var tableFormatter = new TableFormatter(assemblyLoader, activatorWrapper);
Expand All @@ -107,7 +107,7 @@ public async Task ShouldGetPendingMessages()
var assemblyLocater = new AssemblyLocater(_fileProvider, _loggerFactory.CreateLogger<AssemblyLocater>());
var gaugeLoadContext = new GaugeLoadContext(assemblyLocater, _loggerFactory.CreateLogger<GaugeLoadContext>());
var assemblyLoader = new AssemblyLoader(assemblyLocater, gaugeLoadContext, reflectionWrapper,
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>());
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>(), _configuration);
var hookRegistry = new HookRegistry(assemblyLoader);
var dataStoreFactory = new DataStoreFactory(assemblyLoader, activatorWrapper);
var tableFormatter = new TableFormatter(assemblyLoader, activatorWrapper);
Expand All @@ -134,7 +134,7 @@ public async Task ShouldGetStacktraceForAggregateException()
var assemblyLocater = new AssemblyLocater(_fileProvider, _loggerFactory.CreateLogger<AssemblyLocater>());
var gaugeLoadContext = new GaugeLoadContext(assemblyLocater, _loggerFactory.CreateLogger<GaugeLoadContext>());
var assemblyLoader = new AssemblyLoader(assemblyLocater, gaugeLoadContext, reflectionWrapper,
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>());
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>(), _configuration);
var hookRegistry = new HookRegistry(assemblyLoader);
var dataStoreFactory = new DataStoreFactory(assemblyLoader, activatorWrapper);
var tableFormatter = new TableFormatter(assemblyLoader, activatorWrapper);
Expand All @@ -161,7 +161,7 @@ public void ShouldGetStepTextsForMethod()
var assemblyLocater = new AssemblyLocater(_fileProvider, _loggerFactory.CreateLogger<AssemblyLocater>());
var gaugeLoadContext = new GaugeLoadContext(assemblyLocater, _loggerFactory.CreateLogger<GaugeLoadContext>());
var assemblyLoader = new AssemblyLoader(assemblyLocater, gaugeLoadContext, reflectionWrapper,
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>());
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>(), _configuration);
var registry = assemblyLoader.GetStepRegistry();
var gaugeMethod = registry.MethodFor("and an alias");
var stepTexts = gaugeMethod.Aliases.ToList();
Expand All @@ -180,7 +180,7 @@ public async Task SuccessIsFalseOnSerializableExceptionThrown()
var assemblyLocater = new AssemblyLocater(_fileProvider, _loggerFactory.CreateLogger<AssemblyLocater>());
var gaugeLoadContext = new GaugeLoadContext(assemblyLocater, _loggerFactory.CreateLogger<GaugeLoadContext>());
var assemblyLoader = new AssemblyLoader(assemblyLocater, gaugeLoadContext, reflectionWrapper,
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>());
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>(), _configuration);
var hookRegistry = new HookRegistry(assemblyLoader);
var dataStoreFactory = new DataStoreFactory(assemblyLoader, activatorWrapper);
var tableFormatter = new TableFormatter(assemblyLoader, activatorWrapper);
Expand Down Expand Up @@ -209,7 +209,7 @@ public async Task SuccessIsFalseOnUnserializableExceptionThrown()
var assemblyLocater = new AssemblyLocater(_fileProvider, _loggerFactory.CreateLogger<AssemblyLocater>());
var gaugeLoadContext = new GaugeLoadContext(assemblyLocater, _loggerFactory.CreateLogger<GaugeLoadContext>());
var assemblyLoader = new AssemblyLoader(assemblyLocater, gaugeLoadContext, reflectionWrapper,
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>());
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>(), _configuration);
var hookRegistry = new HookRegistry(assemblyLoader);
var dataStoreFactory = new DataStoreFactory(assemblyLoader, activatorWrapper);
var tableFormatter = new TableFormatter(assemblyLoader, activatorWrapper);
Expand Down
4 changes: 2 additions & 2 deletions integration-test/ExternalReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task ShouldGetStepsFromReference(string referenceType, string stepT
var assemblyLocater = new AssemblyLocater(fileProvider, _loggerFactory.CreateLogger<AssemblyLocater>());
var gaugeLoadContext = new GaugeLoadContext(assemblyLocater, _loggerFactory.CreateLogger<GaugeLoadContext>());
var assemblyLoader = new AssemblyLoader(assemblyLocater, gaugeLoadContext, new ReflectionWrapper(),
new ActivatorWrapper(serviceProvider), new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>());
new ActivatorWrapper(serviceProvider), new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>(), config);

var stepValidationProcessor = new StepValidationProcessor(assemblyLoader.GetStepRegistry());
var message = new StepValidateRequest
Expand Down Expand Up @@ -70,7 +70,7 @@ public async Task ShouldRegisterScreenshotWriterFromReference(string referenceTy
var assemblyLocater = new AssemblyLocater(fileProvider, _loggerFactory.CreateLogger<AssemblyLocater>());
var gaugeLoadContext = new GaugeLoadContext(assemblyLocater, _loggerFactory.CreateLogger<GaugeLoadContext>());
var assemblyLoader = new AssemblyLoader(assemblyLocater, gaugeLoadContext, reflectionWrapper,
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>());
activatorWrapper, new StepRegistry(), _loggerFactory.CreateLogger<AssemblyLoader>(), config);
var hookRegistry = new HookRegistry(assemblyLoader);
var dataStoreFactory = new DataStoreFactory(assemblyLoader, activatorWrapper);
var tableFormatter = new TableFormatter(assemblyLoader, activatorWrapper);
Expand Down
5 changes: 5 additions & 0 deletions src/Extensions/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public static string GetGaugeClearStateFlag(this IConfiguration config) =>
public static string GetGaugeExcludeDirs(this IConfiguration config) =>
config.GetValue<string>("GAUGE_EXCLUDE_DIRS");

// Controls if assemblies referenced by the test project should be loaded into Gauge context
// NOTE: If not needed, disabling this may decrease the time it takes to initialize Gauge
public static string GetGaugeCSharpLoadReferencedAssemblies(this IConfiguration config) =>
config.GetValue<string>("GAUGE_CSHARP_LOAD_REFERENCED_ASSEMBLIES") ?? "true";

public static string GetGaugeBinDir(this IConfiguration config)
{
var customBuildPath = config.GetValue<string>("GAUGE_CUSTOM_BUILD_PATH");
Expand Down
2 changes: 1 addition & 1 deletion src/Gauge.Dotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<PackageId>Runner.NetCore30</PackageId>
<Authors>The Gauge Team</Authors>
<Version>0.7.4</Version>
<Version>0.7.5</Version>
<Company>ThoughtWorks Inc.</Company>
<Product>Gauge</Product>
<Description>C# runner for Gauge. https://gauge.org</Description>
Expand Down
11 changes: 8 additions & 3 deletions src/Loaders/AssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ public class AssemblyLoader : IAssemblyLoader
private readonly IActivatorWrapper _activatorWrapper;
private readonly IStepRegistry _registry;
private readonly ILogger<AssemblyLoader> _logger;
private readonly IConfiguration _config;

public AssemblyLoader(IAssemblyLocater assemblyLocater, IGaugeLoadContext gaugeLoadContext, IReflectionWrapper reflectionWrapper, IActivatorWrapper activatorWrapper,
IStepRegistry registry, ILogger<AssemblyLoader> logger)
IStepRegistry registry, ILogger<AssemblyLoader> logger, IConfiguration config)
{
_reflectionWrapper = reflectionWrapper;
_activatorWrapper = activatorWrapper;
_registry = registry;
_logger = logger;
_config = config;

_gaugeLoadContext = gaugeLoadContext;
_targetLibAssembly = _gaugeLoadContext.LoadFromAssemblyName(new AssemblyName(GaugeLibAssemblyName));
Expand Down Expand Up @@ -135,9 +137,12 @@ private void ScanAndLoad(IEnumerable<string> assemblies)
foreach (var assemblyName in assemblies)
{
var assembly = _gaugeLoadContext.LoadFromAssemblyName(new AssemblyName(Path.GetFileNameWithoutExtension(assemblyName)));
foreach (var refAssembly in assembly.GetReferencedAssemblies())
if (bool.TryParse(_config.GetGaugeCSharpLoadReferencedAssemblies(), out bool isTrue) && isTrue)
{
_gaugeLoadContext.LoadFromAssemblyName(refAssembly);
foreach (var refAssembly in assembly.GetReferencedAssemblies())
{
_gaugeLoadContext.LoadFromAssemblyName(refAssembly);
}
}
}
try
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "dotnet",
"version": "0.7.4",
"version": "0.7.5",
"description": "C# support for gauge + .NET",
"run": {
"windows": [
Expand Down
16 changes: 12 additions & 4 deletions test/Loaders/AssemblyLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Gauge.Dotnet.Loaders;
using Gauge.Dotnet.Registries;
using Gauge.Dotnet.Wrappers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using static Gauge.Dotnet.Constants;

Expand All @@ -18,6 +19,9 @@ namespace Gauge.Dotnet.UnitTests.Loaders;
[TestFixture]
public class AssemblyLoaderTests
{
protected Mock<IConfiguration> _mockConfig;
protected Mock<IConfigurationSection> _mockConfigLoadReferencedAssebliesSection;

[SetUp]
public void Setup()
{
Expand Down Expand Up @@ -86,8 +90,12 @@ public void Setup()
_mockAssemblyLocater = new Mock<IAssemblyLocater>();
_mockAssemblyLocater.Setup(_ => _.GetAssembliesReferencingGaugeLib()).Returns(["Mock.Test.Assembly.dll"]);
_mockLogger = new Mock<ILogger<AssemblyLoader>>();
_mockConfig = new Mock<IConfiguration>();
_mockConfigLoadReferencedAssebliesSection = new Mock<IConfigurationSection>();
_mockConfigLoadReferencedAssebliesSection.Setup(s => s.Value).Returns("true");
_mockConfig.Setup(c => c.GetSection("GAUGE_CSHARP_LOAD_REFERENCED_ASSEMBLIES")).Returns(_mockConfigLoadReferencedAssebliesSection.Object);
_assemblyLoader = new AssemblyLoader(_mockAssemblyLocater.Object, _mockGaugeLoadContext.Object, _mockReflectionWrapper.Object,
_mockActivationWrapper.Object, new StepRegistry(), _mockLogger.Object);
_mockActivationWrapper.Object, new StepRegistry(), _mockLogger.Object, _mockConfig.Object);
}

private AssemblyName _assemblyName;
Expand Down Expand Up @@ -139,7 +147,7 @@ public void ShouldThrowExceptionWhenLibAssemblyNotFound()
var assemblyLocater = new Mock<IAssemblyLocater>();
assemblyLocater.Setup(_ => _.GetAssembliesReferencingGaugeLib()).Returns([]);
Assert.Throws<FileLoadException>(() => new AssemblyLoader(assemblyLocater.Object, mockGaugeLoadContext.Object, mockReflectionWrapper.Object,
mockActivationWrapper.Object, new StepRegistry(), mockLogger.Object));
mockActivationWrapper.Object, new StepRegistry(), mockLogger.Object, _mockConfig.Object));
}

[Test]
Expand All @@ -156,7 +164,7 @@ public void ShouldThrowExceptionWhenLibAssemblyDoesNotMatchVersionTooHigh()
assemblyLocater.Setup(_ => _.GetAssembliesReferencingGaugeLib()).Returns([]);

var exception = Assert.Throws<GaugeLibVersionMismatchException>(() => new AssemblyLoader(assemblyLocater.Object, _mockGaugeLoadContext.Object,
_mockReflectionWrapper.Object, _mockActivationWrapper.Object, new StepRegistry(), _mockLogger.Object));
_mockReflectionWrapper.Object, _mockActivationWrapper.Object, new StepRegistry(), _mockLogger.Object, _mockConfig.Object));

Assert.That(exception.Message, Contains.Substring($"Expecting minimum version: {testVersion.Major}.{testVersion.Minor}.0"));
Assert.That(exception.Message, Contains.Substring($"and less than {testVersion.Major}.{testVersion.Minor + 1}.0"));
Expand All @@ -176,7 +184,7 @@ public void ShouldThrowExceptionWhenLibAssemblyDoesNotMatchVersionTooLow()
assemblyLocater.Setup(_ => _.GetAssembliesReferencingGaugeLib()).Returns([]);

var exception = Assert.Throws<GaugeLibVersionMismatchException>(() => new AssemblyLoader(assemblyLocater.Object, _mockGaugeLoadContext.Object,
_mockReflectionWrapper.Object, _mockActivationWrapper.Object, new StepRegistry(), _mockLogger.Object));
_mockReflectionWrapper.Object, _mockActivationWrapper.Object, new StepRegistry(), _mockLogger.Object, _mockConfig.Object));

Assert.That(exception.Message, Contains.Substring($"Expecting minimum version: {testVersion.Major}.{testVersion.Minor}.0"));
Assert.That(exception.Message, Contains.Substring($"and less than {testVersion.Major}.{testVersion.Minor + 1}.0"));
Expand Down

0 comments on commit 84ce1f9

Please sign in to comment.