3
3
4
4
using FluentAssertions ;
5
5
using System . CommandLine . Parsing ;
6
- using System . Reflection ;
7
6
using Xunit ;
8
7
9
8
namespace System . CommandLine . Subsystems . Tests
10
9
{
11
10
public class PipelineTests
12
11
{
13
-
14
- private static readonly string ? version = ( Assembly . GetEntryAssembly ( ) ?? Assembly . GetExecutingAssembly ( ) )
15
- ? . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( )
16
- ? . InformationalVersion ;
17
-
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
+ //}
18
33
19
34
[ Theory ]
20
- [ InlineData ( "-v" , true ) ]
21
- [ InlineData ( "--version" , true ) ]
22
- [ InlineData ( "-x" , false ) ]
23
- [ InlineData ( "" , false ) ]
24
- [ InlineData ( null , false ) ]
35
+ [ ClassData ( typeof ( TestData . Version ) ) ]
25
36
public void Subsystem_runs_in_pipeline_only_when_requested ( string input , bool shouldRun )
26
37
{
27
- var configuration = new CliConfiguration ( new CliRootCommand { } ) ;
28
- var pipeline = new Pipeline
29
- {
30
- Version = new VersionSubsystem ( )
31
- } ;
32
- var consoleHack = new ConsoleHack ( ) . RedirectToBuffer ( true ) ;
38
+ var pipeline = GetTestPipeline ( new VersionSubsystem ( ) ) ;
39
+ var console = GetNewTestConsole ( ) ;
33
40
34
- var exit = pipeline . Execute ( configuration , input , consoleHack ) ;
41
+ var exit = pipeline . Execute ( GetNewTestConfiguration ( ) , input , console ) ;
35
42
36
43
exit . ExitCode . Should ( ) . Be ( 0 ) ;
37
44
exit . Handled . Should ( ) . Be ( shouldRun ) ;
38
45
if ( shouldRun )
39
46
{
40
- consoleHack . GetBuffer ( ) . Trim ( ) . Should ( ) . Be ( version ) ;
47
+ console . GetBuffer ( ) . Trim ( ) . Should ( ) . Be ( TestData . AssemblyVersionString ) ;
41
48
}
42
49
}
43
50
44
51
[ Theory ]
45
- [ InlineData ( "-v" , true ) ]
46
- [ InlineData ( "--version" , true ) ]
47
- [ InlineData ( "-x" , false ) ]
48
- [ InlineData ( "" , false ) ]
49
- [ InlineData ( null , false ) ]
52
+ [ ClassData ( typeof ( TestData . Version ) ) ]
50
53
public void Subsystem_runs_with_explicit_parse_only_when_requested ( string input , bool shouldRun )
51
54
{
52
- var configuration = new CliConfiguration ( new CliRootCommand { } ) ;
53
- var pipeline = new Pipeline
54
- {
55
- Version = new VersionSubsystem ( )
56
- } ;
57
- var consoleHack = new ConsoleHack ( ) . RedirectToBuffer ( true ) ;
55
+ var pipeline = GetTestPipeline ( new VersionSubsystem ( ) ) ;
56
+ var console = GetNewTestConsole ( ) ;
58
57
59
- var result = pipeline . Parse ( configuration , input ) ;
60
- var exit = pipeline . Execute ( result , input , consoleHack ) ;
58
+ var result = pipeline . Parse ( GetNewTestConfiguration ( ) , input ) ;
59
+ var exit = pipeline . Execute ( result , input , console ) ;
61
60
62
61
exit . ExitCode . Should ( ) . Be ( 0 ) ;
63
62
exit . Handled . Should ( ) . Be ( shouldRun ) ;
64
63
if ( shouldRun )
65
64
{
66
- consoleHack . GetBuffer ( ) . Trim ( ) . Should ( ) . Be ( version ) ;
65
+ console . GetBuffer ( ) . Trim ( ) . Should ( ) . Be ( TestData . AssemblyVersionString ) ;
67
66
}
68
67
}
69
68
70
69
[ Theory ]
71
- [ InlineData ( "-v" , true ) ]
72
- [ InlineData ( "--version" , true ) ]
73
- [ InlineData ( "-x" , false ) ]
74
- [ InlineData ( "" , false ) ]
75
- [ InlineData ( null , false ) ]
70
+ [ ClassData ( typeof ( TestData . Version ) ) ]
76
71
public void Subsystem_runs_initialize_and_teardown_when_requested ( string input , bool shouldRun )
77
72
{
78
- var configuration = new CliConfiguration ( new CliRootCommand { } ) ;
79
- AlternateSubsystems . VersionWithInitializeAndTeardown versionSubsystem = new AlternateSubsystems . VersionWithInitializeAndTeardown ( ) ;
80
- var pipeline = new Pipeline
81
- {
82
- Version = versionSubsystem
83
- } ;
84
- var consoleHack = new ConsoleHack ( ) . RedirectToBuffer ( true ) ;
73
+ var versionSubsystem = new AlternateSubsystems . VersionWithInitializeAndTeardown ( ) ;
74
+ var pipeline = GetTestPipeline ( versionSubsystem ) ;
75
+ var console = GetNewTestConsole ( ) ;
85
76
86
- var exit = pipeline . Execute ( configuration , input , consoleHack ) ;
77
+ var exit = pipeline . Execute ( GetNewTestConfiguration ( ) , input , console ) ;
87
78
88
79
exit . ExitCode . Should ( ) . Be ( 0 ) ;
89
80
exit . Handled . Should ( ) . Be ( shouldRun ) ;
@@ -94,56 +85,72 @@ public void Subsystem_runs_initialize_and_teardown_when_requested(string input,
94
85
95
86
96
87
[ Theory ]
97
- [ InlineData ( "-v" , true ) ]
98
- [ InlineData ( "--version" , true ) ]
99
- [ InlineData ( "-x" , false ) ]
100
- [ InlineData ( "" , false ) ]
101
- [ InlineData ( null , false ) ]
102
- public void Subsystem_can_be_used_without_runner ( string input , bool shouldRun )
88
+ [ ClassData ( typeof ( TestData . Version ) ) ]
89
+ public void Subsystem_works_without_pipeline ( string input , bool shouldRun )
103
90
{
104
- var configuration = new CliConfiguration ( new CliRootCommand { } ) ;
105
91
var versionSubsystem = new VersionSubsystem ( ) ;
106
- var consoleHack = new ConsoleHack ( ) . RedirectToBuffer ( true ) ;
107
-
108
- Subsystem . Initialize ( versionSubsystem , configuration ) ;
109
- // TODO: I do not know why anyone would do this, but I do not see a reason to work to block it. See style2 below
110
- var parseResult = CliParser . Parse ( configuration . RootCommand , input , configuration ) ;
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 ) ;
111
100
bool value = parseResult . GetValue < bool > ( "--version" ) ;
112
101
102
+ parseResult . Errors . Should ( ) . BeEmpty ( ) ;
113
103
value . Should ( ) . Be ( shouldRun ) ;
114
- if ( shouldRun )
104
+ if ( shouldRun )
115
105
{
116
106
// TODO: Add an execute overload to avoid checking activated twice
117
- var exit = Subsystem . Execute ( versionSubsystem , parseResult , input , consoleHack ) ;
107
+ var exit = Subsystem . Execute ( versionSubsystem , parseResult , input , console ) ;
118
108
exit . Should ( ) . NotBeNull ( ) ;
119
109
exit . ExitCode . Should ( ) . Be ( 0 ) ;
120
110
exit . Handled . Should ( ) . BeTrue ( ) ;
121
- consoleHack . GetBuffer ( ) . Trim ( ) . Should ( ) . Be ( version ) ;
111
+ console . GetBuffer ( ) . Trim ( ) . Should ( ) . Be ( TestData . AssemblyVersionString ) ;
122
112
}
123
113
}
124
114
125
115
[ Theory ]
126
- [ InlineData ( "-v" , true ) ]
127
- [ InlineData ( "--version" , true ) ]
128
- [ InlineData ( "-x" , false ) ]
129
- [ InlineData ( "" , false ) ]
130
- [ InlineData ( null , false ) ]
131
- public void Subsystem_can_be_used_without_runner_style2 ( string input , bool shouldRun )
116
+ [ ClassData ( typeof ( TestData . Version ) ) ]
117
+ public void Subsystem_works_without_pipeline_style2 ( string input , bool shouldRun )
132
118
{
133
- var configuration = new CliConfiguration ( new CliRootCommand { } ) ;
134
119
var versionSubsystem = new VersionSubsystem ( ) ;
135
- var consoleHack = new ConsoleHack ( ) . RedirectToBuffer ( true ) ;
120
+ var args = CliParser . SplitCommandLine ( input ) . ToList ( ) . AsReadOnly ( ) ;
121
+ var console = GetNewTestConsole ( ) ;
122
+ var configuration = GetNewTestConfiguration ( ) ;
136
123
var expectedVersion = shouldRun
137
- ? version
124
+ ? TestData . AssemblyVersionString
138
125
: "" ;
139
126
140
- Subsystem . Initialize ( versionSubsystem , configuration ) ;
141
- var parseResult = CliParser . Parse ( configuration . RootCommand , input , configuration ) ;
142
- var exit = Subsystem . ExecuteIfNeeded ( versionSubsystem , parseResult , input , consoleHack ) ;
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 ) ;
143
131
144
132
exit . ExitCode . Should ( ) . Be ( 0 ) ;
145
133
exit . Handled . Should ( ) . Be ( shouldRun ) ;
146
- consoleHack . GetBuffer ( ) . Trim ( ) . Should ( ) . Be ( expectedVersion ) ;
134
+ console . GetBuffer ( ) . Trim ( ) . Should ( ) . Be ( expectedVersion ) ;
135
+ }
136
+
137
+
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 ( ) ;
146
+
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
+
152
+ parseResult . Errors . Should ( ) . NotBeEmpty ( ) ;
153
+ value . Should ( ) . Be ( shouldRun ) ;
147
154
}
148
155
149
156
[ Fact ]
@@ -171,9 +178,8 @@ public void Normal_pipeline_contains_no_subsystems()
171
178
public void Subsystems_can_access_each_others_data ( )
172
179
{
173
180
// TODO: Explore a mechanism that doesn't require the reference to retrieve data, this shows that it is awkward
174
- var consoleHack = new ConsoleHack ( ) . RedirectToBuffer ( true ) ;
175
181
var symbol = new CliOption < bool > ( "-x" ) ;
176
-
182
+ var console = GetNewTestConsole ( ) ;
177
183
var pipeline = new StandardPipeline
178
184
{
179
185
Version = new AlternateSubsystems . VersionThatUsesHelpData ( symbol )
@@ -183,9 +189,10 @@ public void Subsystems_can_access_each_others_data()
183
189
{
184
190
symbol . With ( pipeline . Help . Description , "Testing" )
185
191
} ;
186
- pipeline . Execute ( new CliConfiguration ( rootCommand ) , "-v" , consoleHack ) ;
187
- consoleHack . GetBuffer ( ) . Trim ( ) . Should ( ) . Be ( $ "Testing") ;
188
- }
189
192
193
+ pipeline . Execute ( new CliConfiguration ( rootCommand ) , "-v" , console ) ;
194
+
195
+ console . GetBuffer ( ) . Trim ( ) . Should ( ) . Be ( $ "Testing") ;
196
+ }
190
197
}
191
198
}
0 commit comments