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
12 changes: 10 additions & 2 deletions src/Fallout.Build/Execution/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ namespace Fallout.Common.Execution;
/// </summary>
/// <remarks>
/// FT-2 / <see href="https://github.com/Fallout-build/Fallout/issues/307">#307</see>. Intentionally
/// <c>internal</c> — not a public contract until the SDK lands (milestone #7). Subsequent steps move
/// the per-run services (parameters, logging scope, tool-path config) onto this context.
/// <c>internal</c> — not a public contract until the SDK lands (milestone #7). Later steps move the
/// remaining per-run services (logging scope, tool-path config) onto this context. The parameter
/// service is already here (<see href="https://github.com/Fallout-build/Fallout/issues/309">#309</see>).
/// </remarks>
internal sealed class BuildContext : IDisposable
{
Expand All @@ -34,6 +35,13 @@ internal sealed class BuildContext : IDisposable
private readonly ConsoleCancelEventHandler onCancelKeyPress;
private readonly EventHandler onToolOptionsCreated;

/// <summary>
/// The parameter service for this run. During a run, <see cref="ParameterService.Instance"/> returns
/// this object. Its mutable fields (arguments from files, arguments from the commit message) are
/// discarded with the run, so the next run starts empty.
/// </summary>
public ParameterService Parameters { get; } = ParameterService.CreateDefault();

private BuildContext()
{
onCancelKeyPress = (_, _) => cancellationHandlers.ForEach(x => x());
Expand Down
16 changes: 13 additions & 3 deletions src/Fallout.Build/Execution/ParameterService.Statics.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
using System;
using System.Linq.Expressions;
using System.Reflection;
using Fallout.Common.Execution;
using Fallout.Common.Utilities;

namespace Fallout.Common;

internal partial class ParameterService
{
internal static ParameterService Instance = new(
() => EnvironmentInfo.ArgumentParser,
() => EnvironmentInfo.Variables);
/// <summary>Creates a service that reads the process command-line arguments and environment variables.</summary>
internal static ParameterService CreateDefault()
=> new(() => EnvironmentInfo.ArgumentParser, () => EnvironmentInfo.Variables);

// Used when no build run is active. Every caller outside a run shares this one service.
private static readonly Lazy<ParameterService> ambientInstance = new(CreateDefault);

/// <summary>
/// The service for the current build run (<see cref="BuildContext.Parameters"/>). Outside a run,
/// the shared service that <see cref="ambientInstance"/> holds.
/// </summary>
internal static ParameterService Instance => BuildContext.Current?.Parameters ?? ambientInstance.Value;

public static T GetParameter<T>(string name, char? separator = null)
{
Expand Down
60 changes: 60 additions & 0 deletions tests/Fallout.Build.Specs/BuildContextSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,66 @@ public void Dispose_clears_current()
BuildContext.Current.Should().BeNull();
}

[Fact]
public void Parameter_service_facade_resolves_the_current_contexts_instance()
{
// Arrange
using var context = BuildContext.Activate();

// Act
var instance = ParameterService.Instance;

// Assert
instance.Should().BeSameAs(context.Parameters);
}

[Fact]
public void Parameter_service_facade_falls_back_to_a_shared_instance_outside_a_run()
{
// Arrange
BuildContext.Current.Should().BeNull();

// Act
var first = ParameterService.Instance;
var second = ParameterService.Instance;

// Assert
first.Should().NotBeNull();
first.Should().BeSameAs(second);
}

[Fact]
public void Each_run_gets_its_own_parameter_service()
{
// Arrange
ParameterService first;
ParameterService second;

// Act
using (var context = BuildContext.Activate())
first = context.Parameters;
using (var context = BuildContext.Activate())
second = context.Parameters;

// Assert
second.Should().NotBeSameAs(first);
}

[Fact]
public void Parameter_service_state_does_not_leak_into_the_next_run()
{
// Arrange
using (BuildContext.Activate())
ParameterService.Instance.ArgumentsFromCommitMessageService = new ArgumentParser(["-arg", "value"]);

// Act
using var next = BuildContext.Activate();

// Assert
// The first run set the commit-message arguments. The new run must not see them.
ParameterService.Instance.ArgumentsFromCommitMessageService.Should().BeNull();
}

[Fact]
public void Disposing_a_superseded_context_leaves_the_newer_one_current()
{
Expand Down
Loading