Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,15 @@ public void Readme_Should_Document_ConsumerReleaseNoteComposition_Boundary()
}

[Fact]
public void RepositoryLockFiles_ShouldNotRetainDirectMsbuildCoverageReferences()
public void ConsumerLockFiles_ShouldNotRetainDirectMsbuildCoverageReferences()
{
var repositoryRoot = GetRepositoryRoot();
// This isolated fixture intentionally exercises the supported MSBuild coverage driver.
const string msbuildDriverFixture = "tests/fixtures/coverage-hang/packages.lock.json";
var staleLockFiles = Directory.EnumerateFiles(repositoryRoot, "packages*.lock.json", SearchOption.AllDirectories)
.Where(HasDirectMsbuildCoverageReference)
.Select(path => Path.GetRelativePath(repositoryRoot, path).Replace('\\', '/'))
.Where(path => !string.Equals(path, msbuildDriverFixture, StringComparison.Ordinal))
.OrderBy(path => path, StringComparer.Ordinal)
.ToArray();

Expand All @@ -248,7 +251,7 @@ public void Readme_ShouldDocumentCoverageRunWatchdogContract()
Assert.Contains("#### Coverage Run Watchdog", readme, StringComparison.Ordinal);
Assert.Contains("`--heartbeat-interval` defaults to `30s`", readme, StringComparison.Ordinal);
Assert.Contains("`--no-progress-timeout` defaults to `10m`", readme, StringComparison.Ordinal);
Assert.Contains("`--watchdog warn` is the default", readme, StringComparison.Ordinal);
Assert.Contains("`--watchdog fail` is the default", readme, StringComparison.Ordinal);
Assert.Contains("whole-process-tree termination through supervisor-owned process leases", readme, StringComparison.Ordinal);
Assert.Contains("exits `124` with `ASCOV121`", readme, StringComparison.Ordinal);
Assert.Contains("`--watchdog off` disables stall classification", readme, StringComparison.Ordinal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,61 @@ await Assert.ThrowsAnyAsync<OperationCanceledException>(() => producer.RunAsync(
cancellation.Token));
}

[Fact]
public async Task RunAsync_ProducerDeadline_ShouldKeepTimedOutOutcomeAndPointToOwnedTerminalTimings()
{
using var directory = TestDirectory.Create();
var solutionPath = TestPathUtils.PathUnder(directory.Path, "sample.slnx");
var projectPath = TestPathUtils.PathUnder(directory.Path, "tests", "Sample.Tests", "Sample.Tests.csproj");
Directory.CreateDirectory(Path.GetDirectoryName(projectPath)!);
await File.WriteAllTextAsync(solutionPath, "{}");
await File.WriteAllTextAsync(projectPath, "<Project />");
var outputDirectory = TestPathUtils.PathUnder(directory.Path, "output");
var producer = CreateProducer(new DeadlineCoverageRunProcessRunner());
using var standardOutputWriter = new StringWriter();
using var standardErrorWriter = new StringWriter();

var result = await producer.RunAsync(
CreateDeclaration(timeoutSeconds: 3),
solutionPath,
outputDirectory,
null,
CoverageTextWriters.Create(standardOutputWriter, standardErrorWriter),
CancellationToken.None);

Assert.Equal(EvidenceProducerOutcome.TimedOut, result.Outcome);
Assert.Empty(result.SatisfiedAssertionIds);
Assert.Contains("coverage/timings.json", result.Diagnostic, StringComparison.Ordinal);
Assert.True(File.Exists(TestPathUtils.PathUnder(outputDirectory, "coverage", "timings.json")));
}

[Fact]
public async Task RunAsync_ProducerDeadlineBeforeOutput_ShouldOmitTimingsHint()
{
using var directory = TestDirectory.Create();
var solutionPath = TestPathUtils.PathUnder(directory.Path, "sample.slnx");
var projectPath = TestPathUtils.PathUnder(directory.Path, "tests", "Sample.Tests", "Sample.Tests.csproj");
Directory.CreateDirectory(Path.GetDirectoryName(projectPath)!);
await File.WriteAllTextAsync(solutionPath, "{}");
await File.WriteAllTextAsync(projectPath, "<Project />");
var outputDirectory = TestPathUtils.PathUnder(directory.Path, "output");
var producer = CreateProducer(new DeadlineCoverageRunProcessRunner(waitOperation: "msbuild"));
using var standardOutputWriter = new StringWriter();
using var standardErrorWriter = new StringWriter();

var result = await producer.RunAsync(
CreateDeclaration(timeoutSeconds: 1),
solutionPath,
outputDirectory,
null,
CoverageTextWriters.Create(standardOutputWriter, standardErrorWriter),
CancellationToken.None);

Assert.Equal(EvidenceProducerOutcome.TimedOut, result.Outcome);
Assert.DoesNotContain("coverage/timings.json", result.Diagnostic, StringComparison.Ordinal);
Assert.False(File.Exists(TestPathUtils.PathUnder(outputDirectory, "coverage", "timings.json")));
}

[Fact]
public async Task RunAsync_ShouldNotTranslateTerminalRuntimeFailures()
{
Expand Down Expand Up @@ -205,14 +260,15 @@ public async Task RunAsync_ShouldBuildCodeCovPatchGateFromThePlanningSnapshot()
private static EvidenceProducerDeclaration CreateDeclaration(
string kind = "coverage",
EvidenceCoverageGateRequirements? coverageGate = null,
bool includeCoverageGate = true) => new(
bool includeCoverageGate = true,
int timeoutSeconds = 60) => new(
"coverage",
kind,
"1.0.0",
[],
["coverage/assertion@1"],
[],
60,
timeoutSeconds,
includeCoverageGate ? coverageGate ?? new EvidenceCoverageGateRequirements(95, 85, TolerancePercent: 0) : null);

private sealed class ThrowingCoverageRunProcessRunner(Exception exception) : ICoverageRunProcessRunner
Expand All @@ -224,6 +280,38 @@ public Task<CoverageRunProcessResult> RunAsync(CoverageRunProcessRequest request
}
}

private sealed class DeadlineCoverageRunProcessRunner(string waitOperation = "test") : ICoverageRunProcessRunner
{
private const string CapabilityOutput = """
{
"Properties": { "TestingPlatformDotnetTestSupport": "false", "TargetFramework": "net10.0" },
"Items": { "PackageReference": [{ "Identity": "coverlet.collector" }] }
}
""";

public async Task<CoverageRunProcessResult> RunAsync(CoverageRunProcessRequest request, CancellationToken cancellationToken)
{
request.Lease.Complete();
if (request.Arguments.FirstOrDefault() == waitOperation)
{
return await WaitForDeadlineAsync(cancellationToken);
}

return request.Arguments.FirstOrDefault() switch
{
"sln" => new CoverageRunProcessResult(0, "Project(s)\n----------\ntests/Sample.Tests/Sample.Tests.csproj\n"),
"msbuild" => new CoverageRunProcessResult(0, CapabilityOutput, StandardOutput: CapabilityOutput),
_ => new CoverageRunProcessResult(0, "build output"),
};
}

private static async Task<CoverageRunProcessResult> WaitForDeadlineAsync(CancellationToken cancellationToken)
{
await Task.Delay(Timeout.InfiniteTimeSpan, cancellationToken);
throw new InvalidOperationException("The producer deadline should cancel the test invocation.");
}
}

private sealed class PassingCoverageRunProcessRunner : ICoverageRunProcessRunner
{
private const string CapabilityOutput = """
Expand Down
234 changes: 234 additions & 0 deletions Cli/ForgeTrust.AppSurface.Cli.Tests/CoverageRunArgumentBindingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
using System.Collections.ObjectModel;
using CliFx.Infrastructure;
using ForgeTrust.AppSurface.Cli;
using ForgeTrust.AppSurface.Console;
using ForgeTrust.AppSurface.Evidence.Coverage;
using ForgeTrust.AppSurface.Testing;
using Microsoft.Extensions.DependencyInjection;

namespace ForgeTrust.AppSurface.Cli.Tests;

public sealed class CoverageRunArgumentBindingTests
{
[Fact]
public async Task PublicCommandPipeline_ShouldForwardRestoredTestArgumentTokensInOrder()
{
using var temporaryDirectory = new TemporaryDirectory();
var project = TestPathUtils.PathUnder(temporaryDirectory.Path, "Sample.Tests.csproj");
await File.WriteAllTextAsync(project, "<Project />");
var runner = new CapturingProcessRunner();
using var console = new FakeInMemoryConsole();

await AppSurfaceCliApp.RunAsync(
[
"coverage", "run",
"--test-project", project,
"--output", TestPathUtils.PathUnder(temporaryDirectory.Path, "coverage"),
"--coverage-driver", "collector",
"--watchdog", "off",
"--test-argument", "--blame-hang",
"--test-argument=--blame-hang-timeout",
"--test-argument", "120s",
"--test-argument=--help"
],
options =>
{
options.CustomRegistrations.Add(services =>
{
services.AddSingleton<IConsole>(console);
services.AddSingleton<ICoverageRunProcessRunner>(runner);
});
});

Assert.True(
runner.TestCommandArguments.Count == 1,
$"Expected one dotnet test invocation. Output: {console.ReadOutputString()} Error: {console.ReadErrorString()}");
var testCommand = Assert.Single(runner.TestCommandArguments);
var forwardedArgumentIndex = Array.IndexOf(testCommand, "--blame-hang");
Assert.True(forwardedArgumentIndex >= 0, "Expected the literal --blame-hang test argument in dotnet test argv.");
Assert.Equal(
new[]
{
"--blame-hang", "--blame-hang-timeout", "120s", "--help"
},
testCommand.Skip(forwardedArgumentIndex).Take(4));
Assert.DoesNotContain(testCommand, argument => argument.StartsWith("appsurface-test-argument-", StringComparison.Ordinal));
}

[Fact]
public void Normalize_ShouldReplaceSplitAndJoinedValuesAndRestoreExactOrder()
{
var input = new[]
{
"coverage", "run", "--test-project", "sample.csproj",
"--test-argument", "--blame-hang",
"--configuration", "Release",
"--test-argument=--blame-hang-timeout",
"--test-argument", "120s",
"--test-argument=--help"
};

var normalized = CoverageRunArgumentBinding.Normalize(input);
var restored = normalized.Arguments
.Select(argument => normalized.RestoreValues.TryGetValue(argument, out var value) ? value : argument)
.ToArray();

Assert.Equal(
new[]
{
"coverage", "run", "--test-project", "sample.csproj",
"--test-argument", "--blame-hang",
"--configuration", "Release",
"--test-argument", "--blame-hang-timeout",
"--test-argument", "120s",
"--test-argument", "--help"
},
restored);
Assert.Equal(4, normalized.RestoreValues.Count);
Assert.All(normalized.RestoreValues.Keys, placeholder => Assert.DoesNotContain(placeholder, input));
}

[Fact]
public void Normalize_ShouldConsumeTestHostSeparatorAsOneLiteralValue()
{
var normalized = CoverageRunArgumentBinding.Normalize(
["coverage", "run", "--test-argument", "--", "--test-argument=--help"]);

var restored = normalized.Arguments
.Select(argument => normalized.RestoreValues.TryGetValue(argument, out var value) ? value : argument)
.ToArray();

Assert.Equal(
["coverage", "run", "--test-argument", "--", "--test-argument", "--help"],
restored);
}

[Theory]
[InlineData("other", "run", "--test-argument", "--help")]
[InlineData("coverage", "", "--test-argument", "--help")]
[InlineData("coverage", "run", "--test-argument-extra", "value")]
[InlineData("--help")]
public void Normalize_ShouldPassUnrelatedRoutesAndOptionsThrough(string first, params string[] remaining)
{
var input = new[] { first }.Concat(remaining).ToArray();

var normalized = CoverageRunArgumentBinding.Normalize(input);

Assert.Equal(input, normalized.Arguments);
Assert.Empty(normalized.RestoreValues);
}

[Fact]
public void Normalize_ShouldCarryMissingOrEmptyValuesToCommandValidation()
{
string[][] inputs =
[
["coverage", "run", "--test-argument"],
["coverage", "run", "--test-argument="],
["coverage", "run", "--test-argument", ""]
];

foreach (var normalized in inputs.Select(CoverageRunArgumentBinding.Normalize))
{
Assert.Contains(string.Empty, normalized.RestoreValues.Values);
}

var exception = Assert.Throws<CliFx.CommandException>(() => AppSurfaceCliApp.RestoreCoverageTestArguments([string.Empty]));
Assert.Contains("ASCOV101", exception.Message, StringComparison.Ordinal);
Assert.Contains("--test-argument", exception.Message, StringComparison.Ordinal);
}

[Theory]
[InlineData("--test-argument")]
[InlineData("--test-argument=")]
public async Task PublicCommandPipeline_ShouldReportMissingLiteralValueWithoutLaunchingCoverage(string option)
{
using var console = new FakeInMemoryConsole();
var runner = new CapturingProcessRunner();

await AppSurfaceCliApp.RunAsync(["coverage", "run", option], options =>
{
options.CustomRegistrations.Add(services =>
{
services.AddSingleton<IConsole>(console);
services.AddSingleton<ICoverageRunProcessRunner>(runner);
});
});

Assert.Contains("ASCOV101", console.ReadErrorString(), StringComparison.Ordinal);
Assert.Contains("--test-argument", console.ReadErrorString(), StringComparison.Ordinal);
Assert.Empty(runner.TestCommandArguments);
}

[Fact]
public void Normalize_ShouldNotMutateInputAndRestoreMapIsReadOnly()
{
var input = new[] { "COVERAGE", "RUN", "--test-argument=literal" };
var original = input.ToArray();

var normalized = CoverageRunArgumentBinding.Normalize(input);

Assert.Equal(original, input);
Assert.IsType<ReadOnlyDictionary<string, string>>(normalized.RestoreValues);
}

[Fact]
public void RestoreCoverageTestArguments_ShouldReturnOriginalArrayWhenNoInvocationMapIsActive()
{
var arguments = new[] { "--filter", "Category=Unit" };

var restored = AppSurfaceCliApp.RestoreCoverageTestArguments(arguments);

Assert.Same(arguments, restored);
}

private sealed class CapturingProcessRunner : ICoverageRunProcessRunner
{
public List<string[]> TestCommandArguments { get; } = [];

public Task<CoverageRunProcessResult> RunAsync(CoverageRunProcessRequest request, CancellationToken cancellationToken)
{
request.Lease.Complete();
if (request.Arguments.FirstOrDefault() == "msbuild")
{
const string capability = """
{"Properties":{"TestingPlatformDotnetTestSupport":"false","TargetFramework":"net10.0"},"Items":{"PackageReference":[{"Identity":"coverlet.collector"}]}}
""";
return Task.FromResult(new CoverageRunProcessResult(0, capability, StandardOutput: capability));
}

if (request.Arguments.FirstOrDefault() == "test")
{
var arguments = request.Arguments.ToArray();
TestCommandArguments.Add(arguments);
var resultsIndex = Array.IndexOf(arguments, "--results-directory");
var rawDirectory = request.Arguments[resultsIndex + 1];
Directory.CreateDirectory(TestPathUtils.PathUnder(rawDirectory, "coverage"));
File.WriteAllText(TestPathUtils.PathUnder(rawDirectory, "coverage", "coverage.cobertura.xml"),
"<coverage lines-covered=\"8\" lines-valid=\"10\" branches-covered=\"2\" branches-valid=\"4\" />");
return Task.FromResult(new CoverageRunProcessResult(0, "synthetic test success"));
}

return Task.FromResult(new CoverageRunProcessResult(0, string.Empty));
}
}

private sealed class TemporaryDirectory : IDisposable
{
public TemporaryDirectory()
{
Path = TestPathUtils.PathUnder(System.IO.Path.GetTempPath(), $"appsurface-argv-{Guid.NewGuid():N}");
Directory.CreateDirectory(Path);
}

public string Path { get; }

public void Dispose()
{
if (Directory.Exists(Path))
{
Directory.Delete(Path, recursive: true);
}
}
}
}
Loading
Loading