param(
[Parameter(Mandatory = $true)]
[string]$PackagePath
)
$ErrorActionPreference = 'Stop'
$resolvedPackage = (Resolve-Path -LiteralPath $PackagePath).Path
$package = Get-Item -LiteralPath $resolvedPackage
$packageId = 'XFEExtension.NetCore.AutoConfig'
$expectedPrefix = "$packageId."
if (-not $package.BaseName.StartsWith($expectedPrefix, [StringComparison]::OrdinalIgnoreCase)) {
throw "Unexpected package name: $($package.Name)"
}
$version = $package.BaseName.Substring($expectedPrefix.Length)
$tempBase = [IO.Path]::GetFullPath([IO.Path]::GetTempPath())
$testRoot = [IO.Path]::GetFullPath((Join-Path $tempBase ("XFEAutoConfigPackageTest-" + [Guid]::NewGuid().ToString('N'))))
$comparison = if ($IsWindows) { [StringComparison]::OrdinalIgnoreCase } else { [StringComparison]::Ordinal }
if (-not $testRoot.StartsWith($tempBase, $comparison)) {
throw "Refusing to use a test directory outside the system temporary directory: $testRoot"
}
try {
dotnet new console --name Consumer --output $testRoot --framework net10.0 --no-restore
if ($LASTEXITCODE -ne 0) { throw 'dotnet new failed.' }
$escapedPackageDirectory = [Security.SecurityElement]::Escape($package.DirectoryName)
$isolatedPackagesDirectory = [Security.SecurityElement]::Escape((Join-Path $testRoot '.packages'))
@"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value="$isolatedPackagesDirectory" />
</config>
<packageSources>
<clear />
<add key="local" value="$escapedPackageDirectory" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
"@ | Set-Content -LiteralPath (Join-Path $testRoot 'NuGet.Config') -Encoding utf8
dotnet add (Join-Path $testRoot 'Consumer.csproj') package $packageId --version $version --no-restore
if ($LASTEXITCODE -ne 0) { throw 'dotnet add package failed.' }
@'
using XFEExtension.NetCore.AutoConfig;
Directory.CreateDirectory("profiles");
File.WriteAllText("profiles/settings.xpf", "invalid-content-that-must-not-be-loaded");
if (Settings.Name != "default")
throw new InvalidOperationException("AutoLoadProfile(false) did not initialize Current.");
if (!File.Exists("profiles/settings.xpf") || Settings.Current.LastLoadException is not null)
throw new InvalidOperationException("AutoLoadProfile(false) unexpectedly loaded the existing file.");
if (Settings.ProfilePath != "profiles/settings")
throw new InvalidOperationException($"ProfilePathAttribute was ignored: {Settings.ProfilePath}");
Settings.Name = "saved";
await Settings.SaveProfileAsync();
await Settings.FlushAsync();
if (!File.Exists("profiles/settings.xpf"))
throw new InvalidOperationException("The generated async save API did not persist the profile.");
if (!File.ReadAllText("profiles/settings.xpf").Contains("$xfeProfileVersion", StringComparison.Ordinal))
throw new InvalidOperationException("Schema version metadata was not written.");
var tenantPath = Path.GetFullPath("profiles/tenant.json");
var tenant = new ProfileStore<TenantSettings>(tenantPath, autoLoad: false);
tenant.Update(profile =>
{
profile.InstanceName = "tenant";
profile.InstanceValues.Add("value");
});
await tenant.SaveAsync();
var reloadedTenant = new ProfileStore<TenantSettings>(tenantPath);
if (reloadedTenant.Current.InstanceName != "tenant" || reloadedTenant.Current.InstanceValues.Count != 1)
throw new InvalidOperationException("ProfileStore did not round-trip an independent instance.");
if (!ReferenceEquals(reloadedTenant.Current, reloadedTenant.Current.InstanceValues.CurrentProfile))
throw new InvalidOperationException("Profile collection ownership was not generated.");
[AutoLoadProfile(false)]
[ProfilePath("profiles/settings")]
public partial class Settings : XFEProfile
{
[ProfileProperty]
private string name = "default";
protected override int ProfileSchemaVersion => 1;
protected override void ConfigureMigrations(ProfileMigrationBuilder migrations) => migrations.NoOp(0);
}
[AutoLoadProfile(false)]
public partial class TenantSettings : XFEProfile
{
[ProfileProperty]
private string name = "default";
[ProfileProperty]
private ProfileList<string> values = [];
public TenantSettings() => DefaultProfileOperationMode = ProfileOperationMode.Json;
}
'@ | Set-Content -LiteralPath (Join-Path $testRoot 'Program.cs') -Encoding utf8
Push-Location $testRoot
try {
dotnet restore --configfile (Join-Path $testRoot 'NuGet.Config')
if ($LASTEXITCODE -ne 0) { throw 'dotnet restore failed.' }
dotnet build --configuration Release --no-restore -warnaserror
if ($LASTEXITCODE -ne 0) { throw 'dotnet build failed.' }
dotnet run --configuration Release --no-build --no-restore
if ($LASTEXITCODE -ne 0) { throw 'dotnet run failed.' }
}
finally {
Pop-Location
}
}
finally {
if (Test-Path -LiteralPath $testRoot) {
Remove-Item -LiteralPath $testRoot -Recurse -Force
}
}
param(
[Parameter(Mandatory = $true)]
[string]$PackagePath
)
$ErrorActionPreference = 'Stop'
$resolvedPackage = (Resolve-Path -LiteralPath $PackagePath).Path
$package = Get-Item -LiteralPath $resolvedPackage
$packageId = 'XFEExtension.NetCore.AutoConfig'
$expectedPrefix = "$packageId."
if (-not $package.BaseName.StartsWith($expectedPrefix, [StringComparison]::OrdinalIgnoreCase)) {
throw "Unexpected package name: $($package.Name)"
}
$version = $package.BaseName.Substring($expectedPrefix.Length)
$tempBase = [IO.Path]::GetFullPath([IO.Path]::GetTempPath())
$testRoot = [IO.Path]::GetFullPath((Join-Path $tempBase ("XFEAutoConfigPackageTest-" + [Guid]::NewGuid().ToString('N'))))
$comparison = if ($IsWindows) { [StringComparison]::OrdinalIgnoreCase } else { [StringComparison]::Ordinal }
if (-not $testRoot.StartsWith($tempBase, $comparison)) {
throw "Refusing to use a test directory outside the system temporary directory: $testRoot"
}
try {
dotnet new console --name Consumer --output $testRoot --framework net10.0 --no-restore
if ($LASTEXITCODE -ne 0) { throw 'dotnet new failed.' }
$escapedPackageDirectory = [Security.SecurityElement]::Escape($package.DirectoryName)
$isolatedPackagesDirectory = [Security.SecurityElement]::Escape((Join-Path $testRoot '.packages'))
@"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value="$isolatedPackagesDirectory" />
</config>
<packageSources>
<clear />
<add key="local" value="$escapedPackageDirectory" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
"@ | Set-Content -LiteralPath (Join-Path $testRoot 'NuGet.Config') -Encoding utf8
dotnet add (Join-Path $testRoot 'Consumer.csproj') package $packageId --version $version --no-restore
if ($LASTEXITCODE -ne 0) { throw 'dotnet add package failed.' }
@'
using XFEExtension.NetCore.AutoConfig;
Directory.CreateDirectory("profiles");
File.WriteAllText("profiles/settings.xpf", "invalid-content-that-must-not-be-loaded");
if (Settings.Name != "default")
throw new InvalidOperationException("AutoLoadProfile(false) did not initialize Current.");
if (!File.Exists("profiles/settings.xpf") || Settings.Current.LastLoadException is not null)
throw new InvalidOperationException("AutoLoadProfile(false) unexpectedly loaded the existing file.");
if (Settings.ProfilePath != "profiles/settings")
throw new InvalidOperationException($"ProfilePathAttribute was ignored: {Settings.ProfilePath}");
Settings.Name = "saved";
await Settings.SaveProfileAsync();
await Settings.FlushAsync();
if (!File.Exists("profiles/settings.xpf"))
throw new InvalidOperationException("The generated async save API did not persist the profile.");
if (!File.ReadAllText("profiles/settings.xpf").Contains("$xfeProfileVersion", StringComparison.Ordinal))
throw new InvalidOperationException("Schema version metadata was not written.");
var tenantPath = Path.GetFullPath("profiles/tenant.json");
var tenant = new ProfileStore<TenantSettings>(tenantPath, autoLoad: false);
tenant.Update(profile =>
{
profile.InstanceName = "tenant";
profile.InstanceValues.Add("value");
});
await tenant.SaveAsync();
var reloadedTenant = new ProfileStore<TenantSettings>(tenantPath);
if (reloadedTenant.Current.InstanceName != "tenant" || reloadedTenant.Current.InstanceValues.Count != 1)
throw new InvalidOperationException("ProfileStore did not round-trip an independent instance.");
if (!ReferenceEquals(reloadedTenant.Current, reloadedTenant.Current.InstanceValues.CurrentProfile))
throw new InvalidOperationException("Profile collection ownership was not generated.");
[AutoLoadProfile(false)]
[ProfilePath("profiles/settings")]
public partial class Settings : XFEProfile
{
[ProfileProperty]
private string name = "default";
protected override int ProfileSchemaVersion => 1;
protected override void ConfigureMigrations(ProfileMigrationBuilder migrations) => migrations.NoOp(0);
}
[AutoLoadProfile(false)]
public partial class TenantSettings : XFEProfile
{
[ProfileProperty]
private string name = "default";
[ProfileProperty]
private ProfileList<string> values = [];
public TenantSettings() => DefaultProfileOperationMode = ProfileOperationMode.Json;
}
'@ | Set-Content -LiteralPath (Join-Path $testRoot 'Program.cs') -Encoding utf8
Push-Location $testRoot
try {
dotnet restore --configfile (Join-Path $testRoot 'NuGet.Config')
if ($LASTEXITCODE -ne 0) { throw 'dotnet restore failed.' }
dotnet build --configuration Release --no-restore -warnaserror
if ($LASTEXITCODE -ne 0) { throw 'dotnet build failed.' }
dotnet run --configuration Release --no-build --no-restore
if ($LASTEXITCODE -ne 0) { throw 'dotnet run failed.' }
}
finally {
Pop-Location
}
}
finally {
if (Test-Path -LiteralPath $testRoot) {
Remove-Item -LiteralPath $testRoot -Recurse -Force
}
}