Skip to content

Commit

Permalink
Fix: Could not load System.CodeDom exception with xRetry.Reqnroll plu…
Browse files Browse the repository at this point in the history
…gin (#316)

* Fix: Could not load System.CodeDom exception with xRetry.Reqnroll plugin (#310)

* update CHANGELOG.md

* improve infra to load binding files from resource

* use test files from resource

* Fix Linux CI
  • Loading branch information
gasparnagy authored Nov 6, 2024
1 parent 76cabee commit bd11612
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

* Fix: Visual Studio locks Reqnroll.Tools.MsBuild.Generation task files. Using `TaskHostFactory` for our tasks on Windows. (#293)
* Fix: Project dependencies transiently refer to System.Text.Json 8.0.4 that has security vulnerability. Microsoft.Extensions.DependencyModel updated to v8.0.2. (#291)
* Fix: Could not load System.CodeDom exception with xRetry.Reqnroll plugin (#310)

*Contributors of this release (in alphabetical order):* @gasparnagy, @obligaron, @Romfos, @Tiberriver256

Expand Down
13 changes: 13 additions & 0 deletions Reqnroll.Tools.MsBuild.Generation/AssemblyResolveLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Reflection;

namespace Reqnroll.Tools.MsBuild.Generation
Expand All @@ -23,6 +24,18 @@ public Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs ar
{
_taskLoggingWrapper.LogMessage(args.Name);

try
{
var requestedAssemblyName = new AssemblyName(args.Name);
var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == requestedAssemblyName.Name);
if (loadedAssembly != null) return loadedAssembly;
}
catch (Exception ex)
{
_taskLoggingWrapper.LogError(ex.Message);
return null;
}

return null;
}

Expand Down
41 changes: 41 additions & 0 deletions Tests/Reqnroll.SystemTests/Plugins/XRetryPluginTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Reqnroll.TestProjectGenerator;
using Reqnroll.TestProjectGenerator.Driver;
using System.Linq;

namespace Reqnroll.SystemTests.Plugins;

[TestClass]
public class XRetryPluginTest : SystemTestBase
{
protected override void TestInitialize()
{
base.TestInitialize();
_testRunConfiguration.UnitTestProvider = UnitTestProvider.xUnit;
_projectsDriver.AddNuGetPackage("xRetry.Reqnroll", "1.0.0");
}

[TestMethod]
public void XRetry_should_work_with_Reqnroll()
{
AddFeatureFileFromResource("XRetryPlugin/XRetryPluginTestFeature.feature");
AddBindingClassFromResource("XRetryPlugin/XRetryPluginTestStepDefinitions.cs");

ExecuteTests();

ShouldAllScenariosPass();

var simulatedErrors = _bindingDriver.GetActualLogLines("simulated-error").ToList();
simulatedErrors.Should().HaveCount(_preparedTests * 2); // two simulated error per test
}

[TestMethod]
[TestCategory("MsBuild")]
public void XRetry_should_work_with_Reqnroll_on_DotNetFramework_generation()
{
// compiling with MsBuild forces the generation to run with .NET Framework
_compilationDriver.SetBuildTool(BuildTool.MSBuild);
XRetry_should_work_with_Reqnroll();
}
}
4 changes: 2 additions & 2 deletions Tests/Reqnroll.SystemTests/Reqnroll.SystemTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\GeneratorAllInSample1.feature" />
<EmbeddedResource Include="Resources\GeneratorAllInSample2.feature" />
<Compile Remove="Resources\**\*.cs" />
<EmbeddedResource Include="Resources\**\*" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: XRetryPluginFeature

Used by Reqnroll.SystemTests.Plugins.XRetryPluginTest

@retry
Scenario: Scenario with Retry
When fail for first 2 times A

@retry
Scenario Outline: Scenario outline with Retry
When fail for first 2 times <label>
Examples:
| label |
| B |
| C |
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Used by Reqnroll.SystemTests.Plugins.XRetryPluginTest
using System.Collections.Generic;
namespace XRetryPluginTest.StepDefinitions
{
[Binding]
public class XRetryPluginTestStepDefinitions
{
private static readonly Dictionary<string, int> RetriesByLabel = new Dictionary<string, int>();

[When("fail for first {int} times {word}")]
public void WhenFailForFirstTwoTimes(int retryCount, string label)
{
if (!RetriesByLabel.TryGetValue(label, out var retries))
{
retries = 0;
}
var failTest = retries < retryCount;
RetriesByLabel[label] = ++retries;
if (failTest)
{
Log.LogCustom("simulated-error", label);
throw new Exception($"simulated error for {label}");
}
}
}
}
6 changes: 6 additions & 0 deletions Tests/Reqnroll.SystemTests/SystemTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ protected void AddFeatureFileFromResource(string fileName, int? preparedTests =
AddFeatureFile(featureFileContent, preparedTests);
}

protected void AddBindingClassFromResource(string fileName)
{
var bindingClassContent = _testFileManager.GetTestFileContent(fileName);
AddBindingClass(bindingClassContent);
}

private int? GetTestCount(string gherkinContent)
{
var matches = Regex.Matches(gherkinContent, @"^\s*((?<scenario>Scenario:|Scenario Outline:)|(?<exmaples>Examples:)|(?<row>\|)).*?\s*$", RegexOptions.Multiline);
Expand Down

0 comments on commit bd11612

Please sign in to comment.