Skip to content

Commit fd917c8

Browse files
Made pipeline creation static for better Empty/standard ergonomics
1 parent 7facb86 commit fd917c8

File tree

6 files changed

+34
-41
lines changed

6 files changed

+34
-41
lines changed

src/System.CommandLine.Subsystems.Tests/PipelineTests.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ namespace System.CommandLine.Subsystems.Tests
1010
public class PipelineTests
1111
{
1212
private static Pipeline GetTestPipeline(VersionSubsystem versionSubsystem)
13-
=> new()
14-
{
15-
Version = versionSubsystem
16-
};
13+
=> Pipeline.Create(version: versionSubsystem);
1714
private static CliConfiguration GetNewTestConfiguration()
1815
=> new(new CliRootCommand { new CliOption<bool>("-x") }); // Add option expected by test data
1916

@@ -156,7 +153,7 @@ public void Subsystem_runs_when_requested_even_when_there_are_errors(string inpu
156153
[Fact]
157154
public void Standard_pipeline_contains_expected_subsystems()
158155
{
159-
var pipeline = new StandardPipeline();
156+
var pipeline = Pipeline.Create();
160157
pipeline.Version.Should().BeOfType<VersionSubsystem>();
161158
pipeline.Help.Should().BeOfType<HelpSubsystem>();
162159
pipeline.ErrorReporting.Should().BeOfType<ErrorReportingSubsystem>();
@@ -166,7 +163,7 @@ public void Standard_pipeline_contains_expected_subsystems()
166163
[Fact]
167164
public void Normal_pipeline_contains_no_subsystems()
168165
{
169-
var pipeline = new Pipeline();
166+
var pipeline = Pipeline.CreateEmpty();;
170167
pipeline.Version.Should().BeNull();
171168
pipeline.Help.Should().BeNull();
172169
pipeline.ErrorReporting.Should().BeNull();
@@ -179,10 +176,8 @@ public void Subsystems_can_access_each_others_data()
179176
// TODO: Explore a mechanism that doesn't require the reference to retrieve data, this shows that it is awkward
180177
var symbol = new CliOption<bool>("-x");
181178
var console = GetNewTestConsole();
182-
var pipeline = new StandardPipeline
183-
{
184-
Version = new AlternateSubsystems.VersionThatUsesHelpData(symbol)
185-
};
179+
var pipeline = Pipeline.Create(version : new AlternateSubsystems.VersionThatUsesHelpData(symbol));
180+
186181
if (pipeline.Help is null) throw new InvalidOperationException();
187182
var rootCommand = new CliRootCommand
188183
{

src/System.CommandLine.Subsystems.Tests/VersionFunctionalTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class VersionFunctionalTests
1919
public void When_the_version_option_is_specified_then_the_version_is_written_to_standard_out()
2020
{
2121
var configuration = new CliConfiguration(new CliRootCommand());
22-
var pipeline = new Pipeline();
22+
var pipeline = Pipeline.Create();
2323
var consoleHack = new ConsoleHack().RedirectToBuffer(true);
2424
pipeline.Version = new VersionSubsystem();
2525

src/System.CommandLine.Subsystems.Tests/VersionSubsystemTests.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ public void When_version_subsystem_is_used_the_version_option_is_added_to_the_ro
1717
new CliOption<bool>("-x") // add option that is expected for the test data used here
1818
};
1919
var configuration = new CliConfiguration(rootCommand);
20-
var pipeline = new Pipeline
21-
{
22-
Version = new VersionSubsystem()
23-
};
20+
var pipeline = Pipeline.Create(version: new VersionSubsystem());
2421

2522
// Parse is used because directly calling Initialize would be unusual
2623
var result = pipeline.Parse(configuration, "");
@@ -98,10 +95,8 @@ public void Console_output_can_be_tested()
9895
public void Custom_version_subsystem_can_be_used()
9996
{
10097
var consoleHack = new ConsoleHack().RedirectToBuffer(true);
101-
var pipeline = new Pipeline
102-
{
103-
Version = new AlternateSubsystems.AlternateVersion()
104-
};
98+
var pipeline = Pipeline.Create(version: new AlternateSubsystems.AlternateVersion());
99+
105100
pipeline.Execute(new CliConfiguration(new CliRootCommand()), "-v", consoleHack);
106101
consoleHack.GetBuffer().Trim().Should().Be($"***{Constants.version}***");
107102
}
@@ -110,10 +105,8 @@ public void Custom_version_subsystem_can_be_used()
110105
public void Custom_version_subsystem_can_replace_standard()
111106
{
112107
var consoleHack = new ConsoleHack().RedirectToBuffer(true);
113-
var pipeline = new StandardPipeline
114-
{
115-
Version = new AlternateSubsystems.AlternateVersion()
116-
};
108+
var pipeline = Pipeline.Create(version: new AlternateSubsystems.AlternateVersion());
109+
117110
pipeline.Execute(new CliConfiguration(new CliRootCommand()), "-v", consoleHack);
118111
consoleHack.GetBuffer().Trim().Should().Be($"***{Constants.version}***");
119112
}

src/System.CommandLine.Subsystems/Pipeline.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,33 @@ namespace System.CommandLine;
99

1010
public class Pipeline
1111
{
12+
public static Pipeline Create(HelpSubsystem? help = null,
13+
VersionSubsystem? version = null,
14+
CompletionSubsystem? completion = null,
15+
DiagramSubsystem? diagram = null,
16+
ErrorReportingSubsystem? errorReporting = null,
17+
ValueSubsystem? value = null)
18+
=> new()
19+
{
20+
Help = help ?? new HelpSubsystem(),
21+
Version = version ?? new VersionSubsystem(),
22+
Completion = completion ?? new CompletionSubsystem(),
23+
Diagram = diagram ?? new DiagramSubsystem(),
24+
ErrorReporting = errorReporting ?? new ErrorReportingSubsystem(),
25+
Value = value ?? new ValueSubsystem()
26+
};
27+
28+
public static Pipeline CreateEmpty()
29+
=> new();
30+
31+
private Pipeline() { }
32+
1233
public HelpSubsystem? Help { get; set; }
1334
public VersionSubsystem? Version { get; set; }
1435
public CompletionSubsystem? Completion { get; set; }
1536
public DiagramSubsystem? Diagram { get; set; }
1637
public ErrorReportingSubsystem? ErrorReporting { get; set; }
38+
public ValueSubsystem? Value { get; set; }
1739

1840
public ParseResult Parse(CliConfiguration configuration, string rawInput)
1941
=> Parse(configuration, CliParser.SplitCommandLine(rawInput).ToArray());

src/System.CommandLine.Subsystems/StandardPipeline.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/System.CommandLine.Subsystems/Subsystems/PipelineContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class PipelineContext(ParseResult? parseResult, string rawInput, Pipeline
77
{
88
public ParseResult? ParseResult { get; } = parseResult;
99
public string RawInput { get; } = rawInput;
10-
public Pipeline Pipeline { get; } = pipeline ?? new Pipeline();
10+
public Pipeline Pipeline { get; } = pipeline ?? Pipeline.Create();
1111
public ConsoleHack ConsoleHack { get; } = consoleHack ?? new ConsoleHack();
1212

1313
public bool AlreadyHandled { get; set; }

0 commit comments

Comments
 (0)