Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/chocolatey.tests/chocolatey.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
<ItemGroup>
<Compile Include="infrastructure.app\attributes\CommandForAttributeSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyCacheCommandSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyLicenseCommandSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyListCommandSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyApiKeyCommandSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyConfigCommandSpecs.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void Should_use_the_first_unparsed_arg_as_the_subcommand()
public void Should_throw_when_more_than_one_unparsed_arg_is_passed()
{
Reset();
_unparsedArgs.Add("wtf");
_unparsedArgs.Add("abc");
_unparsedArgs.Add("bbq");
var errored = false;
Exception error = null;
Expand Down Expand Up @@ -170,7 +170,7 @@ public void Should_accept_disable_as_the_subcommand()
public void Should_set_unrecognized_values_to_list_as_the_subcommand()
{
Reset();
_unparsedArgs.Add("wtf");
_unparsedArgs.Add("abc");
_because();

Configuration.FeatureCommand.Command.Should().Be(FeatureCommandType.List);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
// Copyright © 2017 - 2025 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using chocolatey.infrastructure.app.attributes;
using chocolatey.infrastructure.app.commands;
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.app.domain;
using FluentAssertions;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace chocolatey.tests.infrastructure.app.commands
{
public class ChocolateyLicenseCommandSpecs
{
[ConcernFor("license")]
public abstract class ChocolateyLicenseCommandSpecsBase : TinySpec
{
protected ChocolateyLicenseCommand Command;
protected ChocolateyConfiguration Configuration = new ChocolateyConfiguration();

public override void Context()
{
Command = new ChocolateyLicenseCommand();
}
}

public class When_Implementing_Command_For : ChocolateyLicenseCommandSpecsBase
{
private List<CommandForAttribute> _results;

public override void Because()
{
_results = Command.GetType().GetCustomAttributes<CommandForAttribute>().ToList();
}

[Fact]
public void Should_Have_Expected_Number_Of_Commands()
{
_results.Should().HaveCount(1);
}

[InlineData("license")]
public void Should_Implement_Expected_Command(string name)
{
_results.Should().ContainSingle(r => r.CommandName == name);
}

[Fact]
public void Should_Specify_Expected_Version_For_All_Commands()
{
_results.Should().AllSatisfy(r => r.Version.Should().Be("2.5.0"));
}
}

public class When_parsing_additional_arguments_ : ChocolateyLicenseCommandSpecsBase
{
private readonly IList<string> _unparsedArgs = new List<string>();
private Action _because;

public override void Because()
{
_because = () => Command.ParseAdditionalArguments(_unparsedArgs, Configuration);
}

public new void Reset()
{
_unparsedArgs.Clear();
}

[Fact]
public void Should_use_the_first_unparsed_arg_as_the_subcommand()
{
Reset();
_unparsedArgs.Add("info");
_because();

Configuration.LicenseCommand.Command.Should().Be(LicenseCommandType.Info);
}

[Fact]
public void Should_throw_when_more_than_one_unparsed_arg_is_passed()
{
Reset();
_unparsedArgs.Add("abc");
_unparsedArgs.Add("bbq");
var errored = false;
Exception error = null;

try
{
_because();
}
catch (Exception ex)
{
errored = true;
error = ex;
}

errored.Should().BeTrue();
error.Should().NotBeNull();
error.Should().BeOfType<ApplicationException>();
error.Message.Should().Contain("A single license command must be listed");
}

[Fact]
public void Should_accept_info_as_the_subcommand()
{
Reset();
_unparsedArgs.Add("info");
_because();

Configuration.LicenseCommand.Command.Should().Be(LicenseCommandType.Info);
}

[Fact]
public void Should_accept_uppercase_info_as_the_subcommand()
{
Reset();
_unparsedArgs.Add("INFO");
_because();

Configuration.LicenseCommand.Command.Should().Be(LicenseCommandType.Info);
}

[Fact]
public void Should_set_unrecognized_values_to_info_as_the_subcommand()
{
Reset();
_unparsedArgs.Add("abc");
_because();

Configuration.LicenseCommand.Command.Should().Be(LicenseCommandType.Info);
}

[Fact]
public void Should_default_to_list_as_the_subcommand()
{
Reset();
_because();

Configuration.LicenseCommand.Command.Should().Be(LicenseCommandType.Info);
}

[Fact]
public void Should_handle_passing_in_an_empty_string()
{
Reset();
_unparsedArgs.Add(" ");
_because();

Configuration.LicenseCommand.Command.Should().Be(LicenseCommandType.Info);
}
}

public class When_Help_Is_Called : ChocolateyLicenseCommandSpecsBase
{
public override void Because()
{
Command.HelpMessage(Configuration);
}

[Fact]
public void Should_log_a_message()
{
MockLogger.Verify(l => l.Info(It.IsAny<string>()), Times.AtLeastOnce);
}

[Fact]
public void Should_log_the_message_we_expect()
{
var messages = MockLogger.MessagesFor(LogLevel.Info);
messages.Should().HaveCount(19);
messages[0].Should().Contain("License Command");
messages[2].Should().Contain("Show information about the current Chocolatey CLI license.");
}
}

public class When_DryRun_Is_Called : ChocolateyLicenseCommandSpecsBase
{
public override void Because()
{
Configuration.LicenseCommand.Command = LicenseCommandType.Info;
Command.DryRun(Configuration);
}

[Fact]
public void Should_log_a_message()
{
MockLogger.Verify(l => l.Warn(It.IsAny<string>()), Times.AtLeastOnce);
}

[Fact]
public void Should_log_the_message_we_expect()
{
var messages = MockLogger.MessagesFor(LogLevel.Warn);
messages.Should().ContainSingle();
messages[0].Should().Contain("No Chocolatey license found.");
}
}

public class When_Run_Is_Called : ChocolateyLicenseCommandSpecsBase
{
public override void Because()
{
Configuration.LicenseCommand.Command = LicenseCommandType.Info;
Command.Run(Configuration);
}

[Fact]
public void Should_log_a_message()
{
MockLogger.Verify(l => l.Warn(It.IsAny<string>()), Times.AtLeastOnce);
}

[Fact]
public void Should_log_the_message_we_expect()
{
var messages = MockLogger.MessagesFor(LogLevel.Warn);
messages.Should().ContainSingle();
messages[0].Should().Contain("No Chocolatey license found.");
}
}

public class When_Run_Is_Called_With_Limit_Output : ChocolateyLicenseCommandSpecsBase
{
public override void Because()
{
Configuration.LicenseCommand.Command = LicenseCommandType.Info;
Configuration.RegularOutput = false;
Command.Run(Configuration);
}

[Fact]
public void Should_log_a_message()
{
MockLogger.Verify(l => l.Warn(It.IsAny<string>()), Times.AtLeastOnce);
}

[Fact]
public void Should_log_the_message_we_expect()
{
var messages = MockLogger.MessagesFor(LogLevel.Warn);
messages.Should().ContainSingle();
messages[0].Should().Contain("No Chocolatey license found.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public void Should_use_the_first_unparsed_arg_as_the_subcommand()
public void Should_throw_when_more_than_one_unparsed_arg_is_passed()
{
Reset();
_unparsedArgs.Add("wtf");
_unparsedArgs.Add("abc");
_unparsedArgs.Add("bbq");
var errored = false;
Exception error = null;
Expand Down Expand Up @@ -236,7 +236,7 @@ public void Should_remove_add_as_the_subcommand()
public void Should_set_unrecognized_values_to_list_as_the_subcommand()
{
Reset();
_unparsedArgs.Add("wtf");
_unparsedArgs.Add("abc");
_because();

Configuration.PinCommand.Command.Should().Be(PinCommandType.List);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void Should_use_the_first_unparsed_arg_as_the_subcommand()
public void Should_throw_when_more_than_one_unparsed_arg_is_passed()
{
Reset();
_unparsedArgs.Add("wtf");
_unparsedArgs.Add("abc");
_unparsedArgs.Add("bbq");
var errored = false;
Exception error = null;
Expand Down Expand Up @@ -242,7 +242,7 @@ public void Should_accept_disable_as_the_subcommand()
public void Should_set_unrecognized_values_to_list_as_the_subcommand()
{
Reset();
_unparsedArgs.Add("wtf");
_unparsedArgs.Add("abc");
_because();

Configuration.SourceCommand.Command.Should().Be(SourceCommandType.List);
Expand Down
2 changes: 2 additions & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
<Compile Include="AssemblyExtensions.cs" />
<Compile Include="ExceptionExtensions.cs" />
<Compile Include="infrastructure.app\attributes\MultiServiceAttribute.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyLicenseCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyCacheCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyListCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyRuleCommand.cs" />
Expand All @@ -186,6 +187,7 @@
<Compile Include="infrastructure.app\commands\ChocolateyCommandBase.cs" />
<Compile Include="infrastructure.app\domain\ApiKeyCommandType.cs" />
<Compile Include="infrastructure.app\domain\CacheCommandType.cs" />
<Compile Include="infrastructure.app\domain\LicenseCommandType.cs" />
<Compile Include="infrastructure.app\domain\SourceTypes.cs" />
<Compile Include="infrastructure.app\domain\ChocolateyPackageMetadata.cs" />
<Compile Include="infrastructure.app\domain\TemplateCommandType.cs" />
Expand Down
Loading
Loading