1
+ // Copyright © 2017 - 2025 Chocolatey Software, Inc
2
+ // Copyright © 2011 - 2017 RealDimensions Software, LLC
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ //
7
+ // You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing, software
12
+ // distributed under the License is distributed on an "AS IS" BASIS,
13
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ // See the License for the specific language governing permissions and
15
+ // limitations under the License.
16
+
17
+ using chocolatey . infrastructure . app . attributes ;
18
+ using chocolatey . infrastructure . app . commands ;
19
+ using chocolatey . infrastructure . app . configuration ;
20
+ using chocolatey . infrastructure . app . domain ;
21
+ using FluentAssertions ;
22
+ using Moq ;
23
+ using System ;
24
+ using System . Collections . Generic ;
25
+ using System . Linq ;
26
+ using System . Reflection ;
27
+
28
+ namespace chocolatey . tests . infrastructure . app . commands
29
+ {
30
+ public class ChocolateyLicenseCommandSpecs
31
+ {
32
+ [ ConcernFor ( "license" ) ]
33
+ public abstract class ChocolateyLicenseCommandSpecsBase : TinySpec
34
+ {
35
+ protected ChocolateyLicenseCommand Command ;
36
+ protected ChocolateyConfiguration Configuration = new ChocolateyConfiguration ( ) ;
37
+
38
+ public override void Context ( )
39
+ {
40
+ Command = new ChocolateyLicenseCommand ( ) ;
41
+ }
42
+ }
43
+
44
+ public class When_Implementing_Command_For : ChocolateyLicenseCommandSpecsBase
45
+ {
46
+ private List < CommandForAttribute > _results ;
47
+
48
+ public override void Because ( )
49
+ {
50
+ _results = Command . GetType ( ) . GetCustomAttributes < CommandForAttribute > ( ) . ToList ( ) ;
51
+ }
52
+
53
+ [ Fact ]
54
+ public void Should_Have_Expected_Number_Of_Commands ( )
55
+ {
56
+ _results . Should ( ) . HaveCount ( 1 ) ;
57
+ }
58
+
59
+ [ InlineData ( "license" ) ]
60
+ public void Should_Implement_Expected_Command ( string name )
61
+ {
62
+ _results . Should ( ) . ContainSingle ( r => r . CommandName == name ) ;
63
+ }
64
+
65
+ [ Fact ]
66
+ public void Should_Specify_Expected_Version_For_All_Commands ( )
67
+ {
68
+ _results . Should ( ) . AllSatisfy ( r => r . Version . Should ( ) . Be ( "2.5.0" ) ) ;
69
+ }
70
+ }
71
+
72
+ public class When_parsing_additional_arguments_ : ChocolateyLicenseCommandSpecsBase
73
+ {
74
+ private readonly IList < string > _unparsedArgs = new List < string > ( ) ;
75
+ private Action _because ;
76
+
77
+ public override void Because ( )
78
+ {
79
+ _because = ( ) => Command . ParseAdditionalArguments ( _unparsedArgs , Configuration ) ;
80
+ }
81
+
82
+ public new void Reset ( )
83
+ {
84
+ _unparsedArgs . Clear ( ) ;
85
+ }
86
+
87
+ [ Fact ]
88
+ public void Should_use_the_first_unparsed_arg_as_the_subcommand ( )
89
+ {
90
+ Reset ( ) ;
91
+ _unparsedArgs . Add ( "info" ) ;
92
+ _because ( ) ;
93
+
94
+ Configuration . LicenseCommand . Command . Should ( ) . Be ( LicenseCommandType . Info ) ;
95
+ }
96
+
97
+ [ Fact ]
98
+ public void Should_throw_when_more_than_one_unparsed_arg_is_passed ( )
99
+ {
100
+ Reset ( ) ;
101
+ _unparsedArgs . Add ( "wtf" ) ;
102
+ _unparsedArgs . Add ( "bbq" ) ;
103
+ var errored = false ;
104
+ Exception error = null ;
105
+
106
+ try
107
+ {
108
+ _because ( ) ;
109
+ }
110
+ catch ( Exception ex )
111
+ {
112
+ errored = true ;
113
+ error = ex ;
114
+ }
115
+
116
+ errored . Should ( ) . BeTrue ( ) ;
117
+ error . Should ( ) . NotBeNull ( ) ;
118
+ error . Should ( ) . BeOfType < ApplicationException > ( ) ;
119
+ error . Message . Should ( ) . Contain ( "A single license command must be listed" ) ;
120
+ }
121
+
122
+ [ Fact ]
123
+ public void Should_accept_add_as_the_subcommand ( )
124
+ {
125
+ Reset ( ) ;
126
+ _unparsedArgs . Add ( "info" ) ;
127
+ _because ( ) ;
128
+
129
+ Configuration . LicenseCommand . Command . Should ( ) . Be ( LicenseCommandType . Info ) ;
130
+ }
131
+
132
+ [ Fact ]
133
+ public void Should_accept_uppercase_info_as_the_subcommand ( )
134
+ {
135
+ Reset ( ) ;
136
+ _unparsedArgs . Add ( "INFO" ) ;
137
+ _because ( ) ;
138
+
139
+ Configuration . LicenseCommand . Command . Should ( ) . Be ( LicenseCommandType . Info ) ;
140
+ }
141
+
142
+ [ Fact ]
143
+ public void Should_set_unrecognized_values_to_info_as_the_subcommand ( )
144
+ {
145
+ Reset ( ) ;
146
+ _unparsedArgs . Add ( "wtf" ) ;
147
+ _because ( ) ;
148
+
149
+ Configuration . LicenseCommand . Command . Should ( ) . Be ( LicenseCommandType . Info ) ;
150
+ }
151
+
152
+ [ Fact ]
153
+ public void Should_default_to_list_as_the_subcommand ( )
154
+ {
155
+ Reset ( ) ;
156
+ _because ( ) ;
157
+
158
+ Configuration . LicenseCommand . Command . Should ( ) . Be ( LicenseCommandType . Info ) ;
159
+ }
160
+
161
+ [ Fact ]
162
+ public void Should_handle_passing_in_an_empty_string ( )
163
+ {
164
+ Reset ( ) ;
165
+ _unparsedArgs . Add ( " " ) ;
166
+ _because ( ) ;
167
+
168
+ Configuration . LicenseCommand . Command . Should ( ) . Be ( LicenseCommandType . Info ) ;
169
+ }
170
+ }
171
+
172
+ public class When_Help_Is_Called : ChocolateyLicenseCommandSpecsBase
173
+ {
174
+ public override void Because ( )
175
+ {
176
+ Command . HelpMessage ( Configuration ) ;
177
+ }
178
+
179
+ [ Fact ]
180
+ public void Should_log_a_message ( )
181
+ {
182
+ MockLogger . Verify ( l => l . Info ( It . IsAny < string > ( ) ) , Times . AtLeastOnce ) ;
183
+ }
184
+
185
+ [ Fact ]
186
+ public void Should_log_the_message_we_expect ( )
187
+ {
188
+ var messages = MockLogger . MessagesFor ( LogLevel . Info ) ;
189
+ messages . Should ( ) . HaveCount ( 9 ) ;
190
+ messages [ 0 ] . Should ( ) . Contain ( "License Command" ) ;
191
+ messages [ 1 ] . Should ( ) . Contain ( "Show information about the current Chocolatey CLI license." ) ;
192
+ }
193
+ }
194
+
195
+ public class When_DryRun_Is_Called : ChocolateyLicenseCommandSpecsBase
196
+ {
197
+ public override void Because ( )
198
+ {
199
+ Configuration . LicenseCommand . Command = LicenseCommandType . Info ;
200
+ Command . DryRun ( Configuration ) ;
201
+ }
202
+
203
+ [ Fact ]
204
+ public void Should_log_a_message ( )
205
+ {
206
+ MockLogger . Verify ( l => l . Warn ( It . IsAny < string > ( ) ) , Times . AtLeastOnce ) ;
207
+ }
208
+
209
+ [ Fact ]
210
+ public void Should_log_the_message_we_expect ( )
211
+ {
212
+ var messages = MockLogger . MessagesFor ( LogLevel . Warn ) ;
213
+ messages . Should ( ) . ContainSingle ( ) ;
214
+ messages [ 0 ] . Should ( ) . Contain ( "No Commercial License Found, running with Open Source License." ) ;
215
+ }
216
+ }
217
+
218
+ public class When_Run_Is_Called : ChocolateyLicenseCommandSpecsBase
219
+ {
220
+ public override void Because ( )
221
+ {
222
+ Configuration . LicenseCommand . Command = LicenseCommandType . Info ;
223
+ Command . Run ( Configuration ) ;
224
+ }
225
+
226
+ [ Fact ]
227
+ public void Should_log_a_message ( )
228
+ {
229
+ MockLogger . Verify ( l => l . Warn ( It . IsAny < string > ( ) ) , Times . AtLeastOnce ) ;
230
+ }
231
+
232
+ [ Fact ]
233
+ public void Should_log_the_message_we_expect ( )
234
+ {
235
+ var messages = MockLogger . MessagesFor ( LogLevel . Warn ) ;
236
+ messages . Should ( ) . ContainSingle ( ) ;
237
+ messages [ 0 ] . Should ( ) . Contain ( "No Commercial License Found, running with Open Source License." ) ;
238
+ }
239
+ }
240
+
241
+ public class When_Run_Is_Called_With_Limit_Output : ChocolateyLicenseCommandSpecsBase
242
+ {
243
+ public override void Because ( )
244
+ {
245
+ Configuration . LicenseCommand . Command = LicenseCommandType . Info ;
246
+ Configuration . RegularOutput = false ;
247
+ Command . Run ( Configuration ) ;
248
+ }
249
+
250
+ [ Fact ]
251
+ public void Should_log_a_message ( )
252
+ {
253
+ MockLogger . Verify ( l => l . Warn ( It . IsAny < string > ( ) ) , Times . AtLeastOnce ) ;
254
+ }
255
+
256
+ [ Fact ]
257
+ public void Should_log_the_message_we_expect ( )
258
+ {
259
+ var messages = MockLogger . MessagesFor ( LogLevel . Warn ) ;
260
+ messages . Should ( ) . ContainSingle ( ) ;
261
+ messages [ 0 ] . Should ( ) . Contain ( "No Commercial License Found, running with Open Source License." ) ;
262
+ }
263
+ }
264
+ }
265
+ }
0 commit comments