Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tests/PactNet.Compatibility.Tests/pact-compatibility-suite"]
path = tests/PactNet.Compatibility.Tests/pact-compatibility-suite
url = https://github.com/pact-foundation/pact-compatibility-suite.git
15 changes: 15 additions & 0 deletions PactNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PactNet.Output.Xunit", "src\PactNet.Output.Xunit\PactNet.Output.Xunit.csproj", "{02E265A1-A7A2-4106-8F6A-5027FDC3FC50}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PactNet.Compatibility.Tests", "tests\PactNet.Compatibility.Tests\PactNet.Compatibility.Tests.csproj", "{063989E4-6B3A-4BA1-BDDD-72B105B9919C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -155,6 +157,18 @@ Global
{02E265A1-A7A2-4106-8F6A-5027FDC3FC50}.Release|x64.Build.0 = Release|Any CPU
{02E265A1-A7A2-4106-8F6A-5027FDC3FC50}.Release|x86.ActiveCfg = Release|Any CPU
{02E265A1-A7A2-4106-8F6A-5027FDC3FC50}.Release|x86.Build.0 = Release|Any CPU
{063989E4-6B3A-4BA1-BDDD-72B105B9919C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{063989E4-6B3A-4BA1-BDDD-72B105B9919C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{063989E4-6B3A-4BA1-BDDD-72B105B9919C}.Debug|x64.ActiveCfg = Debug|Any CPU
{063989E4-6B3A-4BA1-BDDD-72B105B9919C}.Debug|x64.Build.0 = Debug|Any CPU
{063989E4-6B3A-4BA1-BDDD-72B105B9919C}.Debug|x86.ActiveCfg = Debug|Any CPU
{063989E4-6B3A-4BA1-BDDD-72B105B9919C}.Debug|x86.Build.0 = Debug|Any CPU
{063989E4-6B3A-4BA1-BDDD-72B105B9919C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{063989E4-6B3A-4BA1-BDDD-72B105B9919C}.Release|Any CPU.Build.0 = Release|Any CPU
{063989E4-6B3A-4BA1-BDDD-72B105B9919C}.Release|x64.ActiveCfg = Release|Any CPU
{063989E4-6B3A-4BA1-BDDD-72B105B9919C}.Release|x64.Build.0 = Release|Any CPU
{063989E4-6B3A-4BA1-BDDD-72B105B9919C}.Release|x86.ActiveCfg = Release|Any CPU
{063989E4-6B3A-4BA1-BDDD-72B105B9919C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -170,6 +184,7 @@ Global
{5E915D66-917B-4730-B31A-C9727C196346} = {6663C12E-9912-40D0-9310-D119D1F6B023}
{D8B75E48-6E45-468B-8049-B73823C14CB8} = {6663C12E-9912-40D0-9310-D119D1F6B023}
{02E265A1-A7A2-4106-8F6A-5027FDC3FC50} = {CF67D7A1-AE96-420B-9971-65E535B903E8}
{063989E4-6B3A-4BA1-BDDD-72B105B9919C} = {A117BBC6-60BB-4282-BF10-E616DE0AFAD0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C2CBC30C-92D4-4E3A-A5B8-1E5D4E938DFC}
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@ Due to using a shared native library instead of C# for the main Pact logic only
| 4.x | Deprecated | 2, 3 | See [installation] |
| 3.x | Deprecated | 2 | |

### Pact Compatibility Suite

`tests/PactNet.Compatibility.Tests` runs the shared [pact-compatibility-suite](https://github.com/pact-foundation/pact-compatibility-suite)
feature files (a git submodule) against PactNet using [Reqnroll](https://reqnroll.net). Only the V1 HTTP consumer
steps are implemented so far; scenarios with undefined steps are reported as skipped (unsupported), not failed.

```bash
git submodule update --init
./build/download-native-libs.sh # or copy a libpact_ffi into build/<os>/<arch>/
dotnet test tests/PactNet.Compatibility.Tests --logger "junit;LogFilePath=TestResults/compat-junit.xml"
```

- `PACT_FFI_LIB=/path/to/libpact_ffi.{so,dylib,dll}` runs the suite against a different FFI build than the bundled one.
- `tests/PactNet.Compatibility.Tests/baseline.yaml` is a ratchet of scenarios that are known to fail, each with a reason.
A run fails when a scenario that is not in the baseline fails, or when a baselined scenario starts passing.
Rerun with `COMPAT_UPDATE_BASELINE=1` to rewrite it (existing reasons are kept); `COMPAT_BASELINE_FILE` points at a different file.
- The JUnit report lands in `tests/PactNet.Compatibility.Tests/TestResults/`; a plain-text summary is written to `compat-summary.txt` in the test output directory.

## Roadmap

The [roadmap](https://docs.pact.io/roadmap/) for Pact and Pact Net is outlined on our main website.
Expand Down
1 change: 1 addition & 0 deletions src/PactNet/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("PactNet.Tests")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("PactNet.Compatibility.Tests")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("DynamicProxyGenAssembly2")]
173 changes: 173 additions & 0 deletions tests/PactNet.Compatibility.Tests/Hooks/CompatHooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PactNet.Compatibility.Tests.Support;
using Reqnroll;
using Reqnroll.UnitTestProvider;

namespace PactNet.Compatibility.Tests.Hooks
{
/// <summary>
/// Native library override, per-scenario cleanup and the baseline ratchet
/// </summary>
[Binding]
public sealed class CompatHooks
{
private static readonly ConcurrentDictionary<(string Feature, string Scenario), (string Outcome, string Detail)> Results = new();

private static Baseline baseline;

private static bool UpdateMode => Environment.GetEnvironmentVariable("COMPAT_UPDATE_BASELINE") == "1";

[BeforeTestRun]
public static void BeforeTestRun()
{
NativeLibraryOverride.Install();
baseline = Baseline.Load(CompatPaths.BaselineFile);
}

[AfterScenario(Order = 0)]
public static async Task CleanUp(CompatWorld world)
{
if (world.Session != null)
{
await world.Session.DisposeAsync();
world.Session = null;
}
}

[AfterScenario(Order = 100)]
public static void Ratchet(CompatWorld world, FeatureContext feature, ScenarioContext scenario, IUnitTestRuntimeProvider runtime)
{
string featureKey = FeatureKey(feature.FeatureInfo);
string scenarioKey = scenario.ScenarioInfo.Title;

ScenarioExecutionStatus status = scenario.ScenarioExecutionStatus;
string outcome;
string detail;

// Reqnroll itself skips scenarios with undefined/pending steps; calling TestIgnore again would turn that into a failure
bool reqnrollSkips = status is ScenarioExecutionStatus.UndefinedStep or ScenarioExecutionStatus.StepDefinitionPending;

if (reqnrollSkips || world.Unsupported != null)
{
outcome = "unsupported";
detail = world.Unsupported?.Message ?? $"undefined step(s) ({status})";
}
else if (world.Failure != null || scenario.TestError != null)
{
outcome = "failing";
Exception error = world.Failure ?? scenario.TestError;
detail = $"step '{world.FailedStep}': {error.GetType().Name}: {error.Message}";
}
else
{
outcome = "passed";
detail = null;
}

Results[(featureKey, scenarioKey)] = (outcome, detail);

if (UpdateMode)
{
if (!reqnrollSkips && outcome != "passed")
{
runtime.TestIgnore($"[{outcome}] {detail}");
}

return;
}

BaselineEntry entry = baseline.Find(featureKey, scenarioKey);

switch (outcome)
{
case "unsupported" when !reqnrollSkips:
// tolerated: reported as skipped, never fails the run
runtime.TestIgnore($"Unsupported: {detail}");
break;

case "unsupported":
// undefined/pending steps: Reqnroll already marks the test as skipped (missingOrPendingStepsOutcome = Ignore)
break;

case "failing" when entry == null:
throw new CompatAssertionException(
$"Scenario failed and is not in the baseline ({featureKey}: {scenarioKey})\n{detail}");

case "failing":
runtime.TestIgnore($"Baselined ({entry.Status}): {entry.Reason}\nActual: {detail}");
break;

case "passed" when entry != null:
throw new CompatAssertionException(
$"Scenario now passes but is listed in the baseline ({featureKey}: {scenarioKey}); remove it from baseline.yaml or rerun with COMPAT_UPDATE_BASELINE=1");
}
}

[AfterTestRun]
public static void AfterTestRun()
{
var counts = Results.Values.GroupBy(r => r.Outcome).ToDictionary(g => g.Key, g => g.Count());
var summary = new StringBuilder();
summary.AppendLine($"pact_ffi: {NativeLibraryOverride.LibraryPath ?? "(bundled)"}");
summary.AppendLine($"scenarios: {Results.Count}");

foreach (string key in new[] { "passed", "failing", "unsupported" })
{
summary.AppendLine($"{key}: {counts.GetValueOrDefault(key)}");
}

foreach (var result in Results.Where(r => r.Value.Outcome == "failing").OrderBy(r => r.Key.Feature).ThenBy(r => r.Key.Scenario))
{
summary.AppendLine($"FAILING {result.Key.Feature}: {result.Key.Scenario} -- {result.Value.Detail}");
}

File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "compat-summary.txt"), summary.ToString());

if (UpdateMode)
{
WriteBaseline();
}
}

private static void WriteBaseline()
{
var entries = new List<BaselineEntry>();

// scenarios not executed in this run (e.g. filtered out) keep their entries
entries.AddRange(baseline.Entries.Where(e => !Results.ContainsKey((e.Feature, e.Scenario))));

foreach (var result in Results.Where(r => r.Value.Outcome == "failing"))
{
BaselineEntry existing = baseline.Find(result.Key.Feature, result.Key.Scenario);
entries.Add(new BaselineEntry
{
Feature = result.Key.Feature,
Scenario = result.Key.Scenario,
Status = "failing",
Reason = existing?.Reason ?? $"not yet triaged: {result.Value.Detail}"
});
}

Baseline.Write(CompatPaths.BaselineFile, entries);
}

private static string FeatureKey(FeatureInfo info)
{
string folder = (info.FolderPath ?? string.Empty).Replace('\\', '/').Trim('/');
// e.g. "pact-compatibility-suite/features/V1" -> "V1"
int features = folder.LastIndexOf("features", StringComparison.OrdinalIgnoreCase);
if (features >= 0)
{
folder = folder.Substring(features + "features".Length).Trim('/');
}

return string.IsNullOrEmpty(folder) ? info.Title : $"{folder}/{info.Title}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<LangVersion>latest</LangVersion>
<Nullable>disable</Nullable>
<!-- Feature files come from the pact-compatibility-suite submodule; they are linked below, not globbed -->
<ReqnrollUseDefaultFeatureFilesGlobbing>false</ReqnrollUseDefaultFeatureFilesGlobbing>
<!-- Generate code-behind under obj/ so nothing is written into the submodule -->
<ReqnrollUseIntermediateOutputPathForCodeBehind>true</ReqnrollUseIntermediateOutputPathForCodeBehind>
<!-- Works around a JunitXml.TestLogger crash on duplicate scenario titles; see compat.runsettings -->
<RunSettingsFilePath>$(MSBuildProjectDirectory)/compat.runsettings</RunSettingsFilePath>
<CompatSuiteDir>$(MSBuildProjectDirectory)/pact-compatibility-suite</CompatSuiteDir>
</PropertyGroup>

<ItemGroup>
<!-- Link (not copy) every feature from the submodule, keeping the V1/V2/... folder structure -->
<ReqnrollFeatureFiles Include="pact-compatibility-suite/features/**/*.feature">
<Link>features/%(RecursiveDir)%(Filename)%(Extension)</Link>
</ReqnrollFeatureFiles>
<None Remove="pact-compatibility-suite/**" />
<Compile Remove="pact-compatibility-suite/**" />
</ItemGroup>

<ItemGroup>
<!-- Fixtures and the baseline are read in place from the source tree -->
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
<_Parameter1>CompatSuiteDir</_Parameter1>
<_Parameter2>$(CompatSuiteDir)</_Parameter2>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
<_Parameter1>CompatProjectDir</_Parameter1>
<_Parameter2>$(MSBuildProjectDirectory)</_Parameter2>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="Reqnroll.xUnit" Version="3.3.4" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="JunitXml.TestLogger" Version="8.0.0" />
<PackageReference Include="YamlDotNet" Version="16.3.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\PactNet\PactNet.csproj" />
</ItemGroup>

</Project>
Loading
Loading