diff --git a/src/Fallout.Build/Execution/BuildContext.cs b/src/Fallout.Build/Execution/BuildContext.cs index 3e0c8673a..5116d35bd 100644 --- a/src/Fallout.Build/Execution/BuildContext.cs +++ b/src/Fallout.Build/Execution/BuildContext.cs @@ -20,8 +20,9 @@ namespace Fallout.Common.Execution; /// /// /// FT-2 / #307. Intentionally -/// internal — 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. +/// internal — 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 (#309). /// internal sealed class BuildContext : IDisposable { @@ -34,6 +35,13 @@ internal sealed class BuildContext : IDisposable private readonly ConsoleCancelEventHandler onCancelKeyPress; private readonly EventHandler onToolOptionsCreated; + /// + /// The parameter service for this run. During a run, 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. + /// + public ParameterService Parameters { get; } = ParameterService.CreateDefault(); + private BuildContext() { onCancelKeyPress = (_, _) => cancellationHandlers.ForEach(x => x()); diff --git a/src/Fallout.Build/Execution/ParameterService.Statics.cs b/src/Fallout.Build/Execution/ParameterService.Statics.cs index 6904e3f37..e3d7cdc03 100644 --- a/src/Fallout.Build/Execution/ParameterService.Statics.cs +++ b/src/Fallout.Build/Execution/ParameterService.Statics.cs @@ -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); + /// Creates a service that reads the process command-line arguments and environment variables. + 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 ambientInstance = new(CreateDefault); + + /// + /// The service for the current build run (). Outside a run, + /// the shared service that holds. + /// + internal static ParameterService Instance => BuildContext.Current?.Parameters ?? ambientInstance.Value; public static T GetParameter(string name, char? separator = null) { diff --git a/tests/Fallout.Build.Specs/BuildContextSpecs.cs b/tests/Fallout.Build.Specs/BuildContextSpecs.cs index 365b5d92f..d382f66c5 100644 --- a/tests/Fallout.Build.Specs/BuildContextSpecs.cs +++ b/tests/Fallout.Build.Specs/BuildContextSpecs.cs @@ -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() {