Skip to content
Open
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
46 changes: 45 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,52 @@ jobs:
name: nupkgs
path: ./dist/*.*

build-dotnet-containers:
runs-on: ubuntu-latest
name: ${{ matrix.os }}-${{ matrix.distro }}-build-dotnet-container
strategy:
fail-fast: false
matrix:
os:
- ubuntu-24.04
- ubuntu-24.04-arm
distro:
- "mcr.microsoft.com/dotnet/sdk:8.0"
- "mcr.microsoft.com/dotnet/sdk:8.0-alpine3.22"
- "mcr.microsoft.com/dotnet/sdk:8.0-alpine3.23"
- "mcr.microsoft.com/dotnet/sdk:8.0-alpine3.24"

steps:
- uses: actions/checkout@v4

- name: Docker dependencies
id: docker_commands
shell: bash
run: |
if [[ ${{ matrix.distro }} == *"alpine"* ]]; then
echo "deps=apk add --no-cache curl bash gzip && " >> "$GITHUB_OUTPUT"
else
echo "deps=apt-get update && apt-get install -y --no-install-recommends unzip xz-utils && " >> "$GITHUB_OUTPUT"
fi

- name: Restore, Build & Test
run: |
docker run \
--rm \
-v $(pwd):/${{ github.workspace }} \
-w ${{ github.workspace }} \
--entrypoint /bin/sh \
${{ matrix.distro }} \
-c '${{ steps.docker_commands.outputs.deps }} \
build/download-native-libs.sh && \
dotnet restore && dotnet build --no-restore && \
dotnet test --no-build --verbosity normal'

release:
needs: build-dotnet
needs: [
build-dotnet,
build-dotnet-containers
]
if: github.ref_type == 'tag'
runs-on: ubuntu-latest
steps:
Expand Down
10 changes: 6 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,16 @@ Building PactNet
----------------

In order to build PactNet you must first download the native Rust FFI libraries. You can pull the current supported
version by executing the script in Bash (or Git Bash on Windows):
version by executing the script in Bash (or Git Bash on Windows). The script needs `curl`, `tar` with xz support, and
`unzip` (or `7z`) on the path, and verifies each archive against its `.sha256` file:

```bash
build/download-native-libs.sh
```

Alternatively you can download a particular FFI version from the [pact-reference] releases or build your own version
locally, and then copy the artifacts into the folders:
Alternatively you can download a particular FFI version from the [pact-reference] `pact_ffi-v<version>` releases (one
archive per Rust target, e.g. `pact_ffi-aarch64-apple-darwin.tar.xz`) or build your own version locally, and then copy
the shared libraries into the folders:

```
build/
Expand All @@ -69,7 +71,7 @@ build/
libpact_ffi.so
aarch64/
libpact_ffi.so
osx/
macos/
aarch64/
libpact_ffi.dylib
x86_64/
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,16 @@ For writing messaging pacts instead of requests/response pacts, see the [messagi

Due to using a shared native library instead of C# for the main Pact logic only certain OSs are supported:

| OS | Arch | Support |
| ------------ | ------------ | -------------------------------------------------------------------|
| Windows | x86 | ❌ No |
| Windows | x64 | ✔️ Yes |
| Linux (libc) | ARM64 | ✔️ Yes |
| Linux (libc) | x64 | ✔️ Yes |
| Linux (libc) | x86 | ❌ No |
| Linux (musl) | Any | ❌ [No](https://github.com/pact-foundation/pact-net/issues/374) |
| OSX | x64 | ✔️ Yes |
| OSX | ARM64 (M1/M2)| ✔️ Yes |
| OS | Arch | Support |
| ------------ | ----------- | -------------------------------------------------------------------|
| Windows | x86 | ❌ No |
| Windows | x64 | ✔️ Yes |
| Linux (libc) | ARM | ✔️ Yes |
| Linux (libc) | x86 | ❌ No |
| Linux (libc) | x64 | ✔️ Yes |
| Linux (musl) | Any | ✔️ Yes |
| OSX | x64 | ✔️ Yes |
| OSX | ARM (M1/M2) | ✔️ Yes |

### Pact Specification

Expand Down
78 changes: 50 additions & 28 deletions build/download-native-libs.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail

FFI_VERSION="0.4.27"
FFI_BASE_URL="https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v$FFI_VERSION"
FFI_VERSION="0.5.13"
FFI_BASE_URL="https://github.com/pact-foundation/pact-reference/releases/download/pact_ffi-v$FFI_VERSION"

GREEN="\e[32m"
YELLOW="\e[33m"
Expand All @@ -12,60 +12,82 @@ CLEAR="\e[0m"
base_path=$(dirname "$0")

download_native() {
file="$1"
os="$2"
platform="$3"
extension="$4"
os="$1"
platform="$2"
target="$3"
archive_ext="$4"
lib="$5"

# e.g.
# pact_ffi-windows-x86_64.dll.gz
# libpact_ffi-linux-x86_64.so.gz
# libpact_ffi-macos-x86_64.dylib.gz
src_file="$file-$os-$platform.$extension"
src_archive="$src_file.gz"
# pact_ffi-x86_64-pc-windows-msvc.zip (pact_ffi.dll at the archive root)
# pact_ffi-x86_64-unknown-linux-gnu.tar.xz (pact_ffi-<target>/libpact_ffi.so)
# pact_ffi-aarch64-apple-darwin.tar.xz (pact_ffi-<target>/libpact_ffi.dylib)
src_archive="pact_ffi-$target.$archive_ext"
src_sha="$src_archive.sha256"
dest_file="$file.$extension"
url="$FFI_BASE_URL/$src_archive"
sha="$url.sha256"

path="$base_path/$os/$platform"
mkdir -p "$path"
pushd $path > /dev/null
pushd "$path" > /dev/null

echo -e "Downloading FFI library for ${YELLOW}$os/$platform${CLEAR}"
echo -e " Destination: ${BLUE}$path/$src_archive${CLEAR}"
echo -e " Destination: ${BLUE}$path/$lib${CLEAR}"
echo -e " URL: ${BLUE}$url${CLEAR}"

echo -n " Downloading... "
curl --silent -L "$url" -o "$src_archive"
curl --silent -L "$sha" -o "$src_archive.sha256"
curl --silent --show-error --fail -L "$url" -o "$src_archive"
curl --silent --show-error --fail -L "$sha" -o "$src_sha"
echo -e "${GREEN}OK${CLEAR}"

echo -n " Verifying... "

if [[ "$OSTYPE" == "darwin"* ]]; then
shasum -a 256 --check --quiet "$src_sha"
sed '/^$/d' "$src_sha" | shasum -a 256 --check --quiet -
else
sha256sum --check --quiet "$src_sha"
# GNU coreutils and BusyBox (Alpine) both accept plain -c on a file
sed -i '/^$/d' "$src_sha"
sha256sum -c "$src_sha" > /dev/null
fi

echo -e "${GREEN}OK${CLEAR}"

echo -n " Extracting... "
gunzip -f "$src_archive"
extract_dir=$(mktemp -d)
if [[ "$archive_ext" == "zip" ]]; then
extract_zip "$src_archive" "$extract_dir"
extracted="$extract_dir/$lib"
else
tar -xJf "$src_archive" -C "$extract_dir"
extracted="$extract_dir/pact_ffi-$target/$lib"
fi
mv -f "$extracted" "$lib"
rm -rf "$extract_dir" "$src_archive" "$src_sha"
echo -e "${GREEN}OK${CLEAR}"
echo ""

mv "$src_file" "$dest_file"
rm "$src_sha"

popd > /dev/null
}

download_native "pact_ffi" "windows" "x86_64" "dll"
download_native "libpact_ffi" "linux" "x86_64" "so"
download_native "libpact_ffi" "linux" "aarch64" "so"
download_native "libpact_ffi" "macos" "x86_64" "dylib"
download_native "libpact_ffi" "macos" "aarch64" "dylib"
extract_zip() {
if command -v unzip > /dev/null; then
unzip -q -o "$1" -d "$2"
elif command -v 7z > /dev/null; then
7z x -y -o"$2" "$1" > /dev/null
elif tar --version 2>/dev/null | grep -q bsdtar; then
tar -xf "$1" -C "$2"
else
echo "error: unzip (or 7z/bsdtar) is required to extract $1" >&2
exit 1
fi
}

download_native "windows" "x86_64" "x86_64-pc-windows-msvc" "zip" "pact_ffi.dll"
download_native "linux" "x86_64" "x86_64-unknown-linux-gnu" "tar.xz" "libpact_ffi.so"
download_native "linux" "aarch64" "aarch64-unknown-linux-gnu" "tar.xz" "libpact_ffi.so"
download_native "macos" "x86_64" "x86_64-apple-darwin" "tar.xz" "libpact_ffi.dylib"
download_native "macos" "aarch64" "aarch64-apple-darwin" "tar.xz" "libpact_ffi.dylib"
download_native "linux" "x86_64-musl" "x86_64-unknown-linux-musl" "tar.xz" "libpact_ffi.so"
download_native "linux" "aarch64-musl" "aarch64-unknown-linux-musl" "tar.xz" "libpact_ffi.so"

echo "Successfully downloaded FFI libraries"
echo "Successfully downloaded FFI libraries"
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@
],
"metadata": {
"pactRust": {
"ffi": "0.4.27",
"models": "1.2.8"
"ffi": "0.5.13",
"models": "1.3.18"
},
"pactSpecification": {
"version": "4.0"
Expand Down
3 changes: 3 additions & 0 deletions samples/OrdersApi/Provider.Tests/Provider.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<!-- .NET SDK 11 generates hot reload browser-tools assets for Web SDK projects in Debug,
which fail the static web assets endpoint check in a test project. -->
<EnableHotReloadInRuntimeConfigDevFile>false</EnableHotReloadInRuntimeConfigDevFile>
</PropertyGroup>

<ItemGroup>
Expand Down
43 changes: 30 additions & 13 deletions src/PactNet/PactNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,68 @@

<Import Project="../NuGet.targets" />

<!--
The native libraries are packed under runtimes/<rid>/native, so package consumers get the right one selected by the
.NET host at run time (including linux-musl-* on Alpine) with no detection needed here.

Project references don't flow runtimes/ assets though, so for builds inside this repo the library matching the
machine running the build is copied to the output directory. The SDK already knows its own portable RID, which
distinguishes glibc (linux-x64) from musl (linux-musl-x64).
-->
<PropertyGroup>
<IsWindows>False</IsWindows>
<IsLinux>False</IsLinux>
<IsOSX>False</IsOSX>
<IsArm64>False</IsArm64>
<IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'True'">True</IsWindows>
<IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'True'">True</IsLinux>
<IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'True'">True</IsOSX>
<IsArm64 Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">True</IsArm64>
<PactNetHostRid>$(NETCoreSdkPortableRuntimeIdentifier)</PactNetHostRid>
<PactNetHostRid Condition="'$(PactNetHostRid)' == ''">$(NETCoreSdkRuntimeIdentifier)</PactNetHostRid>
<PactNetHostRid Condition="'$(PactNetHostRid)' == ''">$([System.Runtime.InteropServices.RuntimeInformation]::RuntimeIdentifier)</PactNetHostRid>
</PropertyGroup>

<ItemGroup>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\windows\x86_64\pact_ffi.dll">
<Link>pact_ffi.dll</Link>
<PackagePath>runtimes/win-x64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsWindows)'">PreserveNewest</CopyToOutputDirectory>
<CopyToOutputDirectory Condition="$(PactNetHostRid.StartsWith('win-'))">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\linux\x86_64\libpact_ffi.so">
<Link>libpact_ffi.so</Link>
<PackagePath>runtimes/linux-x64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsLinux)' And '$(IsArm64)' == 'False'">PreserveNewest</CopyToOutputDirectory>
<CopyToOutputDirectory Condition="'$(PactNetHostRid)' == 'linux-x64'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\linux\aarch64\libpact_ffi.so">
<Link>libpact_ffi.so</Link>
<PackagePath>runtimes/linux-arm64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsLinux)' And '$(IsArm64)' == 'True'">PreserveNewest</CopyToOutputDirectory>
<CopyToOutputDirectory Condition="'$(PactNetHostRid)' == 'linux-arm64'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\linux\x86_64-musl\libpact_ffi.so">
<Link>libpact_ffi.so</Link>
<PackagePath>runtimes/linux-musl-x64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(PactNetHostRid)' == 'linux-musl-x64'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\linux\aarch64-musl\libpact_ffi.so">
<Link>libpact_ffi.so</Link>
<PackagePath>runtimes/linux-musl-arm64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(PactNetHostRid)' == 'linux-musl-arm64'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\macos\x86_64\libpact_ffi.dylib">
<Link>libpact_ffi.dylib</Link>
<PackagePath>runtimes/osx-x64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsOSX)' == 'True' And '$(IsArm64)' == 'False'">PreserveNewest</CopyToOutputDirectory>
<CopyToOutputDirectory Condition="'$(PactNetHostRid)' == 'osx-x64'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\macos\aarch64\libpact_ffi.dylib">
<Link>libpact_ffi.dylib</Link>
<PackagePath>runtimes/osx-arm64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsOSX)' == 'True' And '$(IsArm64)' == 'True'">PreserveNewest</CopyToOutputDirectory>
<CopyToOutputDirectory Condition="'$(PactNetHostRid)' == 'osx-arm64'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
</ItemGroup>
Expand Down
25 changes: 23 additions & 2 deletions src/PactNet/Verifier/Messaging/MessagingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public Uri Start(JsonSerializerOptions settings)
this.server.Prefixes.Add(uri.AbsoluteUri);
this.server.Start();
}
catch (HttpListenerException e) when (e.Message == "Address already in use")
catch (HttpListenerException e) when (IsAddressInUse(e))
{
// handle intermittent race condition, mostly on MacOS, where a port says it's unused but still throws when you try to use it
// handle intermittent race condition, where a port says it's unused but still throws when you try to use it
this.config.WriteLine("Failed to start messaging provider as the port is already in use, retrying...");
continue;
}
Expand All @@ -90,6 +90,27 @@ public Uri Start(JsonSerializerOptions settings)
}
}

/// <summary>
/// Check if the listener failed to start because the port is already bound
/// </summary>
/// <remarks>
/// The error code is the platform errno for EADDRINUSE. The message text differs by libc
/// (glibc: "Address already in use", musl: "Address in use"), so it is only a fallback.
/// </remarks>
/// <param name="e">Listener exception</param>
/// <returns>Port was already in use</returns>
private static bool IsAddressInUse(HttpListenerException e)
{
const int LinuxAddressInUse = 98;
const int MacAddressInUse = 48;
const int WindowsSocketAddressInUse = 10048;

return e.ErrorCode == LinuxAddressInUse
|| e.ErrorCode == MacAddressInUse
|| e.ErrorCode == WindowsSocketAddressInUse
|| e.Message.IndexOf("in use", StringComparison.OrdinalIgnoreCase) >= 0;
}

/// <summary>
/// Find an unused port on which to host the messaging server
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions tests/PactNet.Tests/data/v2-consumer-integration.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
],
"metadata": {
"pactRust": {
"ffi": "0.4.27",
"models": "1.2.8"
"ffi": "0.5.13",
"models": "1.3.18"
},
"pactSpecification": {
"version": "2.0.0"
Expand Down
4 changes: 2 additions & 2 deletions tests/PactNet.Tests/data/v3-consumer-integration.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@
],
"metadata": {
"pactRust": {
"ffi": "0.4.27",
"models": "1.2.8"
"ffi": "0.5.13",
"models": "1.3.18"
},
"pactSpecification": {
"version": "3.0.0"
Expand Down
Loading
Loading