Skip to content

Commit 46079f4

Browse files
authored
Merge pull request #2847 from JPRuskin/license-command
(#2829) Adds Basic choco license Command
2 parents fca4582 + 55011ab commit 46079f4

File tree

11 files changed

+580
-7
lines changed

11 files changed

+580
-7
lines changed

src/chocolatey.tests/chocolatey.tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
<ItemGroup>
154154
<Compile Include="infrastructure.app\attributes\CommandForAttributeSpecs.cs" />
155155
<Compile Include="infrastructure.app\commands\ChocolateyCacheCommandSpecs.cs" />
156+
<Compile Include="infrastructure.app\commands\ChocolateyLicenseCommandSpecs.cs" />
156157
<Compile Include="infrastructure.app\commands\ChocolateyListCommandSpecs.cs" />
157158
<Compile Include="infrastructure.app\commands\ChocolateyApiKeyCommandSpecs.cs" />
158159
<Compile Include="infrastructure.app\commands\ChocolateyConfigCommandSpecs.cs" />

src/chocolatey.tests/infrastructure.app/commands/ChocolateyFeatureCommandSpecs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void Should_use_the_first_unparsed_arg_as_the_subcommand()
125125
public void Should_throw_when_more_than_one_unparsed_arg_is_passed()
126126
{
127127
Reset();
128-
_unparsedArgs.Add("wtf");
128+
_unparsedArgs.Add("abc");
129129
_unparsedArgs.Add("bbq");
130130
var errored = false;
131131
Exception error = null;
@@ -170,7 +170,7 @@ public void Should_accept_disable_as_the_subcommand()
170170
public void Should_set_unrecognized_values_to_list_as_the_subcommand()
171171
{
172172
Reset();
173-
_unparsedArgs.Add("wtf");
173+
_unparsedArgs.Add("abc");
174174
_because();
175175

176176
Configuration.FeatureCommand.Command.Should().Be(FeatureCommandType.List);
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
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("abc");
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_info_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("abc");
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(19);
190+
messages[0].Should().Contain("License Command");
191+
messages[2].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 Chocolatey license found.");
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 Chocolatey license found.");
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 Chocolatey license found.");
262+
}
263+
}
264+
}
265+
}

src/chocolatey.tests/infrastructure.app/commands/ChocolateyPinCommandSpecs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public void Should_use_the_first_unparsed_arg_as_the_subcommand()
181181
public void Should_throw_when_more_than_one_unparsed_arg_is_passed()
182182
{
183183
Reset();
184-
_unparsedArgs.Add("wtf");
184+
_unparsedArgs.Add("abc");
185185
_unparsedArgs.Add("bbq");
186186
var errored = false;
187187
Exception error = null;
@@ -236,7 +236,7 @@ public void Should_remove_add_as_the_subcommand()
236236
public void Should_set_unrecognized_values_to_list_as_the_subcommand()
237237
{
238238
Reset();
239-
_unparsedArgs.Add("wtf");
239+
_unparsedArgs.Add("abc");
240240
_because();
241241

242242
Configuration.PinCommand.Command.Should().Be(PinCommandType.List);

src/chocolatey.tests/infrastructure.app/commands/ChocolateySourceCommandSpecs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void Should_use_the_first_unparsed_arg_as_the_subcommand()
167167
public void Should_throw_when_more_than_one_unparsed_arg_is_passed()
168168
{
169169
Reset();
170-
_unparsedArgs.Add("wtf");
170+
_unparsedArgs.Add("abc");
171171
_unparsedArgs.Add("bbq");
172172
var errored = false;
173173
Exception error = null;
@@ -242,7 +242,7 @@ public void Should_accept_disable_as_the_subcommand()
242242
public void Should_set_unrecognized_values_to_list_as_the_subcommand()
243243
{
244244
Reset();
245-
_unparsedArgs.Add("wtf");
245+
_unparsedArgs.Add("abc");
246246
_because();
247247

248248
Configuration.SourceCommand.Command.Should().Be(SourceCommandType.List);

src/chocolatey/chocolatey.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
<Compile Include="AssemblyExtensions.cs" />
179179
<Compile Include="ExceptionExtensions.cs" />
180180
<Compile Include="infrastructure.app\attributes\MultiServiceAttribute.cs" />
181+
<Compile Include="infrastructure.app\commands\ChocolateyLicenseCommand.cs" />
181182
<Compile Include="infrastructure.app\commands\ChocolateyCacheCommand.cs" />
182183
<Compile Include="infrastructure.app\commands\ChocolateyListCommand.cs" />
183184
<Compile Include="infrastructure.app\commands\ChocolateyRuleCommand.cs" />
@@ -186,6 +187,7 @@
186187
<Compile Include="infrastructure.app\commands\ChocolateyCommandBase.cs" />
187188
<Compile Include="infrastructure.app\domain\ApiKeyCommandType.cs" />
188189
<Compile Include="infrastructure.app\domain\CacheCommandType.cs" />
190+
<Compile Include="infrastructure.app\domain\LicenseCommandType.cs" />
189191
<Compile Include="infrastructure.app\domain\SourceTypes.cs" />
190192
<Compile Include="infrastructure.app\domain\ChocolateyPackageMetadata.cs" />
191193
<Compile Include="infrastructure.app\domain\TemplateCommandType.cs" />

0 commit comments

Comments
 (0)