1
- using FluentAssertions ;
2
- using System . CommandLine . Parsing ;
1
+ using System . CommandLine . Help ;
2
+ using FluentAssertions ;
3
3
using System . Linq ;
4
+ using System . Threading ;
4
5
using System . Threading . Tasks ;
5
6
6
7
using Xunit ;
@@ -9,167 +10,137 @@ namespace System.CommandLine.Tests
9
10
{
10
11
public class EnvironmentVariableDirectiveTests
11
12
{
12
- private static readonly Random randomizer = new ( Seed : 456476756 ) ;
13
- private readonly string test_variable = $ "TEST_ENVIRONMENT_VARIABLE { randomizer . Next ( ) } ";
13
+ private static readonly Random _random = new ( ) ;
14
+ private readonly string _testVariableName = $ "TEST_ENVIRONMENT_VARIABLE_ { _random . Next ( ) } ";
14
15
15
16
[ Fact ]
16
17
public async Task Sets_environment_variable_to_value ( )
17
18
{
18
19
bool asserted = false ;
19
- string variable = test_variable ;
20
- const string value = "This is a test" ;
20
+ const string value = "hello" ;
21
21
var rootCommand = new CliRootCommand ( ) ;
22
- rootCommand . SetAction ( ( _ ) =>
22
+ rootCommand . SetAction ( _ =>
23
23
{
24
24
asserted = true ;
25
- Environment . GetEnvironmentVariable ( variable ) . Should ( ) . Be ( value ) ;
25
+ Environment . GetEnvironmentVariable ( _testVariableName ) . Should ( ) . Be ( value ) ;
26
26
} ) ;
27
27
28
28
var config = new CliConfiguration ( rootCommand )
29
29
{
30
- Directives = { new EnvironmentVariablesDirective ( ) }
30
+ Directives = { new EnvironmentVariablesDirective ( ) } ,
31
+ EnableDefaultExceptionHandler = false
31
32
} ;
32
33
33
- await config . InvokeAsync ( new [ ] { $ "[env:{ variable } ={ value } ]" } ) ;
34
+ await config . InvokeAsync ( $ "[env:{ _testVariableName } ={ value } ]") ;
34
35
35
36
asserted . Should ( ) . BeTrue ( ) ;
36
37
}
37
38
38
39
[ Fact ]
39
- public async Task Trims_environment_variable_name ( )
40
+ public async Task Sets_environment_variable_value_containing_equals_sign ( )
40
41
{
41
42
bool asserted = false ;
42
- string variable = test_variable ;
43
- const string value = "This is a test" ;
43
+ const string value = "1=2" ;
44
44
var rootCommand = new CliRootCommand ( ) ;
45
- rootCommand . SetAction ( ( _ ) =>
45
+ rootCommand . SetAction ( _ =>
46
46
{
47
47
asserted = true ;
48
- Environment . GetEnvironmentVariable ( variable ) . Should ( ) . Be ( value ) ;
48
+ Environment . GetEnvironmentVariable ( _testVariableName ) . Should ( ) . Be ( value ) ;
49
49
} ) ;
50
50
51
51
var config = new CliConfiguration ( rootCommand )
52
52
{
53
- Directives = { new EnvironmentVariablesDirective ( ) }
53
+ Directives = { new EnvironmentVariablesDirective ( ) } ,
54
+ EnableDefaultExceptionHandler = false
54
55
} ;
55
56
56
- await config . InvokeAsync ( new [ ] { $ "[env: { variable } ={ value } ]" } ) ;
57
+ await config . InvokeAsync ( $ "[env:{ _testVariableName } ={ value } ]" ) ;
57
58
58
59
asserted . Should ( ) . BeTrue ( ) ;
59
60
}
60
61
61
62
[ Fact ]
62
- public async Task Trims_environment_variable_value ( )
63
+ public async Task Ignores_environment_directive_without_equals_sign ( )
63
64
{
64
65
bool asserted = false ;
65
- string variable = test_variable ;
66
- const string value = "This is a test" ;
66
+ string variable = _testVariableName ;
67
67
var rootCommand = new CliRootCommand ( ) ;
68
- rootCommand . SetAction ( ( _ ) =>
68
+ rootCommand . SetAction ( _ =>
69
69
{
70
70
asserted = true ;
71
- Environment . GetEnvironmentVariable ( variable ) . Should ( ) . Be ( value ) ;
71
+ Environment . GetEnvironmentVariable ( variable ) . Should ( ) . BeNull ( ) ;
72
72
} ) ;
73
73
74
74
var config = new CliConfiguration ( rootCommand )
75
75
{
76
- Directives = { new EnvironmentVariablesDirective ( ) }
76
+ Directives = { new EnvironmentVariablesDirective ( ) } ,
77
+ EnableDefaultExceptionHandler = false
77
78
} ;
78
79
79
- await config . InvokeAsync ( new [ ] { $ "[env:{ variable } = { value } ]" } ) ;
80
+ await config . InvokeAsync ( $ "[env:{ variable } ]" ) ;
80
81
81
82
asserted . Should ( ) . BeTrue ( ) ;
82
83
}
83
84
84
85
[ Fact ]
85
- public async Task Sets_environment_variable_value_containing_equals_sign ( )
86
+ public static async Task Ignores_environment_directive_with_empty_variable_name ( )
86
87
{
87
88
bool asserted = false ;
88
- string variable = test_variable ;
89
- const string value = "This is = a test containing equals" ;
89
+ string value = "value" ;
90
90
var rootCommand = new CliRootCommand ( ) ;
91
- rootCommand . SetAction ( ( _ ) =>
91
+ rootCommand . SetAction ( _ =>
92
92
{
93
93
asserted = true ;
94
- Environment . GetEnvironmentVariable ( variable ) . Should ( ) . Be ( value ) ;
94
+ var env = Environment . GetEnvironmentVariables ( ) ;
95
+ env . Values . Cast < string > ( ) . Should ( ) . NotContain ( value ) ;
95
96
} ) ;
96
97
97
98
var config = new CliConfiguration ( rootCommand )
98
99
{
99
- Directives = { new EnvironmentVariablesDirective ( ) }
100
+ Directives = { new EnvironmentVariablesDirective ( ) } ,
101
+ EnableDefaultExceptionHandler = false
100
102
} ;
101
103
102
- await config . InvokeAsync ( new [ ] { $ "[env:{ variable } ={ value } ]" } ) ;
104
+ var result = config . Parse ( $ "[env:={ value } ]") ;
103
105
104
- asserted . Should ( ) . BeTrue ( ) ;
105
- }
106
-
107
- [ Fact ]
108
- public async Task Ignores_environment_directive_without_equals_sign ( )
109
- {
110
- bool asserted = false ;
111
- string variable = test_variable ;
112
- var rootCommand = new CliRootCommand ( ) ;
113
- rootCommand . SetAction ( ( _ ) =>
114
- {
115
- asserted = true ;
116
- Environment . GetEnvironmentVariable ( variable ) . Should ( ) . BeNull ( ) ;
117
- } ) ;
118
-
119
- var config = new CliConfiguration ( rootCommand )
120
- {
121
- Directives = { new EnvironmentVariablesDirective ( ) }
122
- } ;
123
-
124
- await config . InvokeAsync ( new [ ] { $ "[env:{ variable } ]" } ) ;
106
+ await result . InvokeAsync ( ) ;
125
107
126
108
asserted . Should ( ) . BeTrue ( ) ;
127
109
}
128
110
129
111
[ Fact ]
130
- public static async Task Ignores_environment_directive_with_empty_variable_name ( )
112
+ public void It_does_not_prevent_help_from_being_invoked ( )
131
113
{
132
- bool asserted = false ;
133
- string value = $ "This is a test, random: { randomizer . Next ( ) } ";
134
- var rootCommand = new CliRootCommand ( ) ;
135
- rootCommand . SetAction ( ( _ ) =>
136
- {
137
- asserted = true ;
138
- var env = Environment . GetEnvironmentVariables ( ) ;
139
- env . Values . Cast < string > ( ) . Should ( ) . NotContain ( value ) ;
140
- } ) ;
114
+ var root = new CliRootCommand ( ) ;
115
+ root . SetAction ( _ => { } ) ;
141
116
142
- var config = new CliConfiguration ( rootCommand )
143
- {
144
- Directives = { new EnvironmentVariablesDirective ( ) }
145
- } ;
117
+ var customHelpAction = new CustomHelpAction ( ) ;
118
+ root . Options . OfType < HelpOption > ( ) . Single ( ) . Action = customHelpAction ;
146
119
147
- await config . InvokeAsync ( new [ ] { $ "[env:={ value } ]" } ) ;
120
+ var config = new CliConfiguration ( root ) ;
121
+ config . Directives . Add ( new EnvironmentVariablesDirective ( ) ) ;
148
122
149
- asserted . Should ( ) . BeTrue ( ) ;
123
+ root . Parse ( $ "[env:{ _testVariableName } =1] -h", config ) . Invoke ( ) ;
124
+
125
+ customHelpAction . WasCalled . Should ( ) . BeTrue ( ) ;
126
+ Environment . GetEnvironmentVariable ( _testVariableName ) . Should ( ) . Be ( "1" ) ;
150
127
}
151
128
152
- [ Fact ]
153
- public static async Task Ignores_environment_directive_with_whitespace_variable_name ( )
129
+ private class CustomHelpAction : CliAction
154
130
{
155
- bool asserted = false ;
156
- string value = $ "This is a test, random: { randomizer . Next ( ) } ";
157
- var rootCommand = new CliRootCommand ( ) ;
158
- rootCommand . SetAction ( ( _ ) =>
159
- {
160
- asserted = true ;
161
- var env = Environment . GetEnvironmentVariables ( ) ;
162
- env . Values . Cast < string > ( ) . Should ( ) . NotContain ( value ) ;
163
- } ) ;
131
+ public bool WasCalled { get ; private set ; }
164
132
165
- var config = new CliConfiguration ( rootCommand )
133
+ public override int Invoke ( ParseResult parseResult )
166
134
{
167
- Directives = { new EnvironmentVariablesDirective ( ) }
168
- } ;
135
+ WasCalled = true ;
136
+ return 0 ;
137
+ }
169
138
170
- await config . InvokeAsync ( new [ ] { $ "[env: ={ value } ]" } ) ;
171
-
172
- asserted . Should ( ) . BeTrue ( ) ;
139
+ public override Task < int > InvokeAsync ( ParseResult parseResult , CancellationToken cancellationToken = default )
140
+ {
141
+ WasCalled = true ;
142
+ return Task . FromResult ( 0 ) ;
143
+ }
173
144
}
174
145
}
175
146
}
0 commit comments