Skip to content

Commit 5306e87

Browse files
Merge pull request #2407 from KathleenDollard/static-pipeline-create
Changed Pipeline creation from constructors to factory methods
2 parents 46f703a + bdcab5e commit 5306e87

File tree

6 files changed

+42
-41
lines changed

6 files changed

+42
-41
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ 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+
{
14+
var pipeline = Pipeline.CreateEmpty();
15+
pipeline.Version = versionSubsystem;
16+
return pipeline;
17+
}
18+
1719
private static CliConfiguration GetNewTestConfiguration()
1820
=> new(new CliRootCommand { new CliOption<bool>("-x") }); // Add option expected by test data
1921

@@ -156,7 +158,7 @@ public void Subsystem_runs_when_requested_even_when_there_are_errors(string inpu
156158
[Fact]
157159
public void Standard_pipeline_contains_expected_subsystems()
158160
{
159-
var pipeline = new StandardPipeline();
161+
var pipeline = Pipeline.Create();
160162
pipeline.Version.Should().BeOfType<VersionSubsystem>();
161163
pipeline.Help.Should().BeOfType<HelpSubsystem>();
162164
pipeline.ErrorReporting.Should().BeOfType<ErrorReportingSubsystem>();
@@ -166,7 +168,7 @@ public void Standard_pipeline_contains_expected_subsystems()
166168
[Fact]
167169
public void Normal_pipeline_contains_no_subsystems()
168170
{
169-
var pipeline = new Pipeline();
171+
var pipeline = Pipeline.CreateEmpty();;
170172
pipeline.Version.Should().BeNull();
171173
pipeline.Help.Should().BeNull();
172174
pipeline.ErrorReporting.Should().BeNull();
@@ -179,10 +181,8 @@ public void Subsystems_can_access_each_others_data()
179181
// TODO: Explore a mechanism that doesn't require the reference to retrieve data, this shows that it is awkward
180182
var symbol = new CliOption<bool>("-x");
181183
var console = GetNewTestConsole();
182-
var pipeline = new StandardPipeline
183-
{
184-
Version = new AlternateSubsystems.VersionThatUsesHelpData(symbol)
185-
};
184+
var pipeline = Pipeline.Create(version : new AlternateSubsystems.VersionThatUsesHelpData(symbol));
185+
186186
if (pipeline.Help is null) throw new InvalidOperationException();
187187
var rootCommand = new CliRootCommand
188188
{

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.CreateEmpty();
2323
var consoleHack = new ConsoleHack().RedirectToBuffer(true);
2424
pipeline.Version = new VersionSubsystem();
2525

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ 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.CreateEmpty();
21+
pipeline.Version = new VersionSubsystem();
2422

2523
// Parse is used because directly calling Initialize would be unusual
2624
var result = pipeline.Parse(configuration, "");
@@ -98,10 +96,9 @@ public void Console_output_can_be_tested()
9896
public void Custom_version_subsystem_can_be_used()
9997
{
10098
var consoleHack = new ConsoleHack().RedirectToBuffer(true);
101-
var pipeline = new Pipeline
102-
{
103-
Version = new AlternateSubsystems.AlternateVersion()
104-
};
99+
var pipeline = Pipeline.CreateEmpty();
100+
pipeline.Version = new AlternateSubsystems.AlternateVersion();
101+
105102
pipeline.Execute(new CliConfiguration(new CliRootCommand()), "-v", consoleHack);
106103
consoleHack.GetBuffer().Trim().Should().Be($"***{Constants.version}***");
107104
}
@@ -110,10 +107,9 @@ public void Custom_version_subsystem_can_be_used()
110107
public void Custom_version_subsystem_can_replace_standard()
111108
{
112109
var consoleHack = new ConsoleHack().RedirectToBuffer(true);
113-
var pipeline = new StandardPipeline
114-
{
115-
Version = new AlternateSubsystems.AlternateVersion()
116-
};
110+
var pipeline = Pipeline.CreateEmpty();
111+
pipeline.Version = new AlternateSubsystems.AlternateVersion();
112+
117113
pipeline.Execute(new CliConfiguration(new CliRootCommand()), "-v", consoleHack);
118114
consoleHack.GetBuffer().Trim().Should().Be($"***{Constants.version}***");
119115
}

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.CreateEmpty();
1111
public ConsoleHack ConsoleHack { get; } = consoleHack ?? new ConsoleHack();
1212

1313
public bool AlreadyHandled { get; set; }

0 commit comments

Comments
 (0)