|
5 | 5 | using System.CommandLine.Parsing;
|
6 | 6 | using Xunit;
|
7 | 7 |
|
8 |
| -namespace System.CommandLine.Subsystems.Tests; |
9 |
| - |
10 |
| -public class PipelineTests |
| 8 | +namespace System.CommandLine.Subsystems.Tests |
11 | 9 | {
|
12 |
| - private static Pipeline GetTestPipeline(VersionSubsystem versionSubsystem) |
13 |
| - => new() |
14 |
| - { |
15 |
| - Version = versionSubsystem |
16 |
| - }; |
17 |
| - private static CliConfiguration GetNewTestConfiguration() |
18 |
| - => new(new CliRootCommand { new CliOption<bool>("-x") }); // Add option expected by test data |
19 |
| - |
20 |
| - private static ConsoleHack GetNewTestConsole() |
21 |
| - => new ConsoleHack().RedirectToBuffer(true); |
22 |
| - |
23 |
| - //private static (Pipeline pipeline, CliConfiguration configuration, ConsoleHack consoleHack) StandardObjects(VersionSubsystem versionSubsystem) |
24 |
| - //{ |
25 |
| - // var configuration = new CliConfiguration(new CliRootCommand { new CliOption<bool>("-x") }); |
26 |
| - // var pipeline = new Pipeline |
27 |
| - // { |
28 |
| - // Version = versionSubsystem |
29 |
| - // }; |
30 |
| - // var consoleHack = new ConsoleHack().RedirectToBuffer(true); |
31 |
| - // return (pipeline, configuration, consoleHack); |
32 |
| - //} |
33 |
| - |
34 |
| - [Theory] |
35 |
| - [ClassData(typeof(TestData.Version))] |
36 |
| - public void Subsystem_runs_in_pipeline_only_when_requested(string input, bool shouldRun) |
| 10 | + public class PipelineTests |
37 | 11 | {
|
38 |
| - var pipeline = GetTestPipeline(new VersionSubsystem()); |
39 |
| - var console = GetNewTestConsole(); |
| 12 | + private static Pipeline GetTestPipeline(VersionSubsystem versionSubsystem) |
| 13 | + => new() |
| 14 | + { |
| 15 | + Version = versionSubsystem |
| 16 | + }; |
| 17 | + private static CliConfiguration GetNewTestConfiguration() |
| 18 | + => new(new CliRootCommand { new CliOption<bool>("-x") }); // Add option expected by test data |
| 19 | + |
| 20 | + private static ConsoleHack GetNewTestConsole() |
| 21 | + => new ConsoleHack().RedirectToBuffer(true); |
| 22 | + |
| 23 | + //private static (Pipeline pipeline, CliConfiguration configuration, ConsoleHack consoleHack) StandardObjects(VersionSubsystem versionSubsystem) |
| 24 | + //{ |
| 25 | + // var configuration = new CliConfiguration(new CliRootCommand { new CliOption<bool>("-x") }); |
| 26 | + // var pipeline = new Pipeline |
| 27 | + // { |
| 28 | + // Version = versionSubsystem |
| 29 | + // }; |
| 30 | + // var consoleHack = new ConsoleHack().RedirectToBuffer(true); |
| 31 | + // return (pipeline, configuration, consoleHack); |
| 32 | + //} |
| 33 | + |
| 34 | + [Theory] |
| 35 | + [ClassData(typeof(TestData.Version))] |
| 36 | + public void Subsystem_runs_in_pipeline_only_when_requested(string input, bool shouldRun) |
| 37 | + { |
| 38 | + var pipeline = GetTestPipeline(new VersionSubsystem()); |
| 39 | + var console = GetNewTestConsole(); |
40 | 40 |
|
41 |
| - var exit = pipeline.Execute(GetNewTestConfiguration(), input, console); |
| 41 | + var exit = pipeline.Execute(GetNewTestConfiguration(), input, console); |
42 | 42 |
|
43 |
| - exit.ExitCode.Should().Be(0); |
44 |
| - exit.Handled.Should().Be(shouldRun); |
45 |
| - if (shouldRun) |
46 |
| - { |
47 |
| - console.GetBuffer().Trim().Should().Be(TestData.AssemblyVersionString); |
| 43 | + exit.ExitCode.Should().Be(0); |
| 44 | + exit.Handled.Should().Be(shouldRun); |
| 45 | + if (shouldRun) |
| 46 | + { |
| 47 | + console.GetBuffer().Trim().Should().Be(TestData.AssemblyVersionString); |
| 48 | + } |
48 | 49 | }
|
49 |
| - } |
50 | 50 |
|
51 |
| - [Theory] |
52 |
| - [ClassData(typeof(TestData.Version))] |
53 |
| - public void Subsystem_runs_with_explicit_parse_only_when_requested(string input, bool shouldRun) |
54 |
| - { |
55 |
| - var pipeline = GetTestPipeline(new VersionSubsystem()); |
56 |
| - var console = GetNewTestConsole(); |
| 51 | + [Theory] |
| 52 | + [ClassData(typeof(TestData.Version))] |
| 53 | + public void Subsystem_runs_with_explicit_parse_only_when_requested(string input, bool shouldRun) |
| 54 | + { |
| 55 | + var pipeline = GetTestPipeline(new VersionSubsystem()); |
| 56 | + var console = GetNewTestConsole(); |
57 | 57 |
|
58 |
| - var result = pipeline.Parse(GetNewTestConfiguration(), input); |
59 |
| - var exit = pipeline.Execute(result, input, console); |
| 58 | + var result = pipeline.Parse(GetNewTestConfiguration(), input); |
| 59 | + var exit = pipeline.Execute(result, input, console); |
60 | 60 |
|
61 |
| - exit.ExitCode.Should().Be(0); |
62 |
| - exit.Handled.Should().Be(shouldRun); |
63 |
| - if (shouldRun) |
64 |
| - { |
65 |
| - console.GetBuffer().Trim().Should().Be(TestData.AssemblyVersionString); |
| 61 | + exit.ExitCode.Should().Be(0); |
| 62 | + exit.Handled.Should().Be(shouldRun); |
| 63 | + if (shouldRun) |
| 64 | + { |
| 65 | + console.GetBuffer().Trim().Should().Be(TestData.AssemblyVersionString); |
| 66 | + } |
66 | 67 | }
|
67 |
| - } |
68 | 68 |
|
69 |
| - [Theory] |
70 |
| - [ClassData(typeof(TestData.Version))] |
71 |
| - public void Subsystem_runs_initialize_and_teardown_when_requested(string input, bool shouldRun) |
72 |
| - { |
73 |
| - var versionSubsystem = new AlternateSubsystems.VersionWithInitializeAndTeardown(); |
74 |
| - var pipeline = GetTestPipeline(versionSubsystem); |
75 |
| - var console = GetNewTestConsole(); |
| 69 | + [Theory] |
| 70 | + [ClassData(typeof(TestData.Version))] |
| 71 | + public void Subsystem_runs_initialize_and_teardown_when_requested(string input, bool shouldRun) |
| 72 | + { |
| 73 | + var versionSubsystem = new AlternateSubsystems.VersionWithInitializeAndTeardown(); |
| 74 | + var pipeline = GetTestPipeline(versionSubsystem); |
| 75 | + var console = GetNewTestConsole(); |
76 | 76 |
|
77 |
| - var exit = pipeline.Execute(GetNewTestConfiguration(), input, console); |
| 77 | + var exit = pipeline.Execute(GetNewTestConfiguration(), input, console); |
78 | 78 |
|
79 |
| - exit.ExitCode.Should().Be(0); |
80 |
| - exit.Handled.Should().Be(shouldRun); |
81 |
| - versionSubsystem.InitializationWasRun.Should().BeTrue(); |
82 |
| - versionSubsystem.ExecutionWasRun.Should().Be(shouldRun); |
83 |
| - versionSubsystem.TeardownWasRun.Should().BeTrue(); |
84 |
| - } |
| 79 | + exit.ExitCode.Should().Be(0); |
| 80 | + exit.Handled.Should().Be(shouldRun); |
| 81 | + versionSubsystem.InitializationWasRun.Should().BeTrue(); |
| 82 | + versionSubsystem.ExecutionWasRun.Should().Be(shouldRun); |
| 83 | + versionSubsystem.TeardownWasRun.Should().BeTrue(); |
| 84 | + } |
85 | 85 |
|
86 | 86 |
|
87 |
| - [Theory] |
88 |
| - [ClassData(typeof(TestData.Version))] |
89 |
| - public void Subsystem_works_without_pipeline(string input, bool shouldRun) |
90 |
| - { |
91 |
| - var versionSubsystem = new VersionSubsystem(); |
92 |
| - // TODO: Ensure an efficient conversion as people may copy this code |
93 |
| - var args = CliParser.SplitCommandLine(input).ToList().AsReadOnly(); |
94 |
| - var console = GetNewTestConsole(); |
95 |
| - var configuration = GetNewTestConfiguration(); |
96 |
| - |
97 |
| - Subsystem.Initialize(versionSubsystem, configuration, args); |
98 |
| - // This approach might be taken if someone is using a subsystem just for initialization |
99 |
| - var parseResult = CliParser.Parse(configuration.RootCommand, args, configuration); |
100 |
| - bool value = parseResult.GetValue<bool>("--version"); |
101 |
| - |
102 |
| - parseResult.Errors.Should().BeEmpty(); |
103 |
| - value.Should().Be(shouldRun); |
104 |
| - if (shouldRun) |
| 87 | + [Theory] |
| 88 | + [ClassData(typeof(TestData.Version))] |
| 89 | + public void Subsystem_works_without_pipeline(string input, bool shouldRun) |
105 | 90 | {
|
106 |
| - // TODO: Add an execute overload to avoid checking activated twice |
107 |
| - var exit = Subsystem.Execute(versionSubsystem, parseResult, input, console); |
108 |
| - exit.Should().NotBeNull(); |
109 |
| - exit.ExitCode.Should().Be(0); |
110 |
| - exit.Handled.Should().BeTrue(); |
111 |
| - console.GetBuffer().Trim().Should().Be(TestData.AssemblyVersionString); |
| 91 | + var versionSubsystem = new VersionSubsystem(); |
| 92 | + // TODO: Ensure an efficient conversion as people may copy this code |
| 93 | + var args = CliParser.SplitCommandLine(input).ToList().AsReadOnly(); |
| 94 | + var console = GetNewTestConsole(); |
| 95 | + var configuration = GetNewTestConfiguration(); |
| 96 | + |
| 97 | + Subsystem.Initialize(versionSubsystem, configuration, args); |
| 98 | + // This approach might be taken if someone is using a subsystem just for initialization |
| 99 | + var parseResult = CliParser.Parse(configuration.RootCommand, args, configuration); |
| 100 | + bool value = parseResult.GetValue<bool>("--version"); |
| 101 | + |
| 102 | + parseResult.Errors.Should().BeEmpty(); |
| 103 | + value.Should().Be(shouldRun); |
| 104 | + if (shouldRun) |
| 105 | + { |
| 106 | + // TODO: Add an execute overload to avoid checking activated twice |
| 107 | + var exit = Subsystem.Execute(versionSubsystem, parseResult, input, console); |
| 108 | + exit.Should().NotBeNull(); |
| 109 | + exit.ExitCode.Should().Be(0); |
| 110 | + exit.Handled.Should().BeTrue(); |
| 111 | + console.GetBuffer().Trim().Should().Be(TestData.AssemblyVersionString); |
| 112 | + } |
112 | 113 | }
|
113 |
| - } |
114 |
| - |
115 |
| - [Theory] |
116 |
| - [ClassData(typeof(TestData.Version))] |
117 |
| - public void Subsystem_works_without_pipeline_style2(string input, bool shouldRun) |
118 |
| - { |
119 |
| - var versionSubsystem = new VersionSubsystem(); |
120 |
| - var args = CliParser.SplitCommandLine(input).ToList().AsReadOnly(); |
121 |
| - var console = GetNewTestConsole(); |
122 |
| - var configuration = GetNewTestConfiguration(); |
123 |
| - var expectedVersion = shouldRun |
124 |
| - ? TestData.AssemblyVersionString |
125 |
| - : ""; |
126 |
| - |
127 |
| - // Someone might use this approach if they wanted to do something with the ParseResult |
128 |
| - Subsystem.Initialize(versionSubsystem, configuration, args); |
129 |
| - var parseResult = CliParser.Parse(configuration.RootCommand, args, configuration); |
130 |
| - var exit = Subsystem.ExecuteIfNeeded(versionSubsystem, parseResult, input, console); |
131 |
| - |
132 |
| - exit.ExitCode.Should().Be(0); |
133 |
| - exit.Handled.Should().Be(shouldRun); |
134 |
| - console.GetBuffer().Trim().Should().Be(expectedVersion); |
135 |
| - } |
136 | 114 |
|
| 115 | + [Theory] |
| 116 | + [ClassData(typeof(TestData.Version))] |
| 117 | + public void Subsystem_works_without_pipeline_style2(string input, bool shouldRun) |
| 118 | + { |
| 119 | + var versionSubsystem = new VersionSubsystem(); |
| 120 | + var args = CliParser.SplitCommandLine(input).ToList().AsReadOnly(); |
| 121 | + var console = GetNewTestConsole(); |
| 122 | + var configuration = GetNewTestConfiguration(); |
| 123 | + var expectedVersion = shouldRun |
| 124 | + ? TestData.AssemblyVersionString |
| 125 | + : ""; |
| 126 | + |
| 127 | + // Someone might use this approach if they wanted to do something with the ParseResult |
| 128 | + Subsystem.Initialize(versionSubsystem, configuration, args); |
| 129 | + var parseResult = CliParser.Parse(configuration.RootCommand, args, configuration); |
| 130 | + var exit = Subsystem.ExecuteIfNeeded(versionSubsystem, parseResult, input, console); |
137 | 131 |
|
138 |
| - [Theory] |
139 |
| - [InlineData("-xy", false)] |
140 |
| - [InlineData("--versionx", false)] |
141 |
| - public void Subsystem_runs_when_requested_even_when_there_are_errors(string input, bool shouldRun) |
142 |
| - { |
143 |
| - var versionSubsystem = new VersionSubsystem(); |
144 |
| - var args = CliParser.SplitCommandLine(input).ToList().AsReadOnly(); |
145 |
| - var configuration = GetNewTestConfiguration(); |
| 132 | + exit.ExitCode.Should().Be(0); |
| 133 | + exit.Handled.Should().Be(shouldRun); |
| 134 | + console.GetBuffer().Trim().Should().Be(expectedVersion); |
| 135 | + } |
146 | 136 |
|
147 |
| - Subsystem.Initialize(versionSubsystem, configuration, args); |
148 |
| - // This approach might be taken if someone is using a subsystem just for initialization |
149 |
| - var parseResult = CliParser.Parse(configuration.RootCommand, args, configuration); |
150 |
| - bool value = parseResult.GetValue<bool>("--version"); |
151 | 137 |
|
152 |
| - parseResult.Errors.Should().NotBeEmpty(); |
153 |
| - value.Should().Be(shouldRun); |
154 |
| - } |
| 138 | + [Theory] |
| 139 | + [InlineData("-xy", false)] |
| 140 | + [InlineData("--versionx", false)] |
| 141 | + public void Subsystem_runs_when_requested_even_when_there_are_errors(string input, bool shouldRun) |
| 142 | + { |
| 143 | + var versionSubsystem = new VersionSubsystem(); |
| 144 | + var args = CliParser.SplitCommandLine(input).ToList().AsReadOnly(); |
| 145 | + var configuration = GetNewTestConfiguration(); |
155 | 146 |
|
156 |
| - [Fact] |
157 |
| - public void Standard_pipeline_contains_expected_subsystems() |
158 |
| - { |
159 |
| - var pipeline = new StandardPipeline(); |
160 |
| - pipeline.Version.Should().BeOfType<VersionSubsystem>(); |
161 |
| - pipeline.Help.Should().BeOfType<HelpSubsystem>(); |
162 |
| - pipeline.ErrorReporting.Should().BeOfType<ErrorReportingSubsystem>(); |
163 |
| - pipeline.Completion.Should().BeOfType<CompletionSubsystem>(); |
164 |
| - } |
| 147 | + Subsystem.Initialize(versionSubsystem, configuration, args); |
| 148 | + // This approach might be taken if someone is using a subsystem just for initialization |
| 149 | + var parseResult = CliParser.Parse(configuration.RootCommand, args, configuration); |
| 150 | + bool value = parseResult.GetValue<bool>("--version"); |
165 | 151 |
|
166 |
| - [Fact] |
167 |
| - public void Normal_pipeline_contains_no_subsystems() |
168 |
| - { |
169 |
| - var pipeline = new Pipeline(); |
170 |
| - pipeline.Version.Should().BeNull(); |
171 |
| - pipeline.Help.Should().BeNull(); |
172 |
| - pipeline.ErrorReporting.Should().BeNull(); |
173 |
| - pipeline.Completion.Should().BeNull(); |
174 |
| - } |
| 152 | + parseResult.Errors.Should().NotBeEmpty(); |
| 153 | + value.Should().Be(shouldRun); |
| 154 | + } |
175 | 155 |
|
176 |
| - [Fact] |
177 |
| - public void Subsystems_can_access_each_others_data() |
178 |
| - { |
179 |
| - // TODO: Explore a mechanism that doesn't require the reference to retrieve data, this shows that it is awkward |
180 |
| - var symbol = new CliOption<bool>("-x"); |
181 |
| - var console = GetNewTestConsole(); |
182 |
| - var pipeline = new StandardPipeline |
183 |
| - { |
184 |
| - Version = new AlternateSubsystems.VersionThatUsesHelpData(symbol) |
185 |
| - }; |
186 |
| - if (pipeline.Help is null) throw new InvalidOperationException(); |
187 |
| - var rootCommand = new CliRootCommand |
| 156 | + [Fact] |
| 157 | + public void Standard_pipeline_contains_expected_subsystems() |
188 | 158 | {
|
189 |
| - symbol.With(pipeline.Help.Description, "Testing") |
190 |
| - }; |
| 159 | + var pipeline = new StandardPipeline(); |
| 160 | + pipeline.Version.Should().BeOfType<VersionSubsystem>(); |
| 161 | + pipeline.Help.Should().BeOfType<HelpSubsystem>(); |
| 162 | + pipeline.ErrorReporting.Should().BeOfType<ErrorReportingSubsystem>(); |
| 163 | + pipeline.Completion.Should().BeOfType<CompletionSubsystem>(); |
| 164 | + } |
191 | 165 |
|
192 |
| - pipeline.Execute(new CliConfiguration(rootCommand), "-v", console); |
| 166 | + [Fact] |
| 167 | + public void Normal_pipeline_contains_no_subsystems() |
| 168 | + { |
| 169 | + var pipeline = new Pipeline(); |
| 170 | + pipeline.Version.Should().BeNull(); |
| 171 | + pipeline.Help.Should().BeNull(); |
| 172 | + pipeline.ErrorReporting.Should().BeNull(); |
| 173 | + pipeline.Completion.Should().BeNull(); |
| 174 | + } |
193 | 175 |
|
194 |
| - console.GetBuffer().Trim().Should().Be($"Testing"); |
| 176 | + [Fact] |
| 177 | + public void Subsystems_can_access_each_others_data() |
| 178 | + { |
| 179 | + // TODO: Explore a mechanism that doesn't require the reference to retrieve data, this shows that it is awkward |
| 180 | + var symbol = new CliOption<bool>("-x"); |
| 181 | + var console = GetNewTestConsole(); |
| 182 | + var pipeline = new StandardPipeline |
| 183 | + { |
| 184 | + Version = new AlternateSubsystems.VersionThatUsesHelpData(symbol) |
| 185 | + }; |
| 186 | + if (pipeline.Help is null) throw new InvalidOperationException(); |
| 187 | + var rootCommand = new CliRootCommand |
| 188 | + { |
| 189 | + symbol.With(pipeline.Help.Description, "Testing") |
| 190 | + }; |
| 191 | + |
| 192 | + pipeline.Execute(new CliConfiguration(rootCommand), "-v", console); |
| 193 | + |
| 194 | + console.GetBuffer().Trim().Should().Be($"Testing"); |
| 195 | + } |
195 | 196 | }
|
196 | 197 | }
|
0 commit comments