From 65ff904682e9de8fe338668af2184e7da3512519 Mon Sep 17 00:00:00 2001 From: Gary Ewan Park Date: Thu, 6 May 2021 10:57:52 +0100 Subject: [PATCH] (#357) Add unit tests To cover the new functionality of the export command. This includes verifying the option sets, what methods are called under certain circumstances, etc. --- src/chocolatey.tests/chocolatey.tests.csproj | 1 + .../commands/ChocolateyExportCommandSpecs.cs | 220 ++++++++++++++++++ 2 files changed, 221 insertions(+) create mode 100644 src/chocolatey.tests/infrastructure.app/commands/ChocolateyExportCommandSpecs.cs diff --git a/src/chocolatey.tests/chocolatey.tests.csproj b/src/chocolatey.tests/chocolatey.tests.csproj index 92d428eade..b3197b2dc3 100644 --- a/src/chocolatey.tests/chocolatey.tests.csproj +++ b/src/chocolatey.tests/chocolatey.tests.csproj @@ -88,6 +88,7 @@ + diff --git a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyExportCommandSpecs.cs b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyExportCommandSpecs.cs new file mode 100644 index 0000000000..d195c0c3f7 --- /dev/null +++ b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyExportCommandSpecs.cs @@ -0,0 +1,220 @@ +// Copyright © 2017 - 2018 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. + +namespace chocolatey.tests.infrastructure.app.commands +{ + using System; + using System.Collections.Generic; + using System.Linq; + using chocolatey.infrastructure.app.attributes; + using chocolatey.infrastructure.app.commands; + using chocolatey.infrastructure.app.configuration; + using chocolatey.infrastructure.app.services; + using chocolatey.infrastructure.commandline; + using chocolatey.infrastructure.filesystem; + using Moq; + using Should; + + public class ChocolateyExportCommandSpecs + { + public abstract class ChocolateyExportCommandSpecsBase : TinySpec + { + protected ChocolateyExportCommand command; + protected Mock nugetService = new Mock(); + protected Mock fileSystem = new Mock(); + protected ChocolateyConfiguration configuration = new ChocolateyConfiguration(); + + public override void Context() + { + command = new ChocolateyExportCommand(nugetService.Object, fileSystem.Object); + } + + public void reset() + { + nugetService.ResetCalls(); + fileSystem.ResetCalls(); + } + } + + public class when_implementing_command_for : ChocolateyExportCommandSpecsBase + { + private List results; + + public override void Because() + { + results = command.GetType().GetCustomAttributes(typeof(CommandForAttribute), false).Cast().Select(a => a.CommandName).ToList(); + } + + [Fact] + public void should_implement_help() + { + results.ShouldContain("export"); + } + } + + public class when_configurating_the_argument_parser : ChocolateyExportCommandSpecsBase + { + private OptionSet optionSet; + + public override void Context() + { + base.Context(); + optionSet = new OptionSet(); + } + + public override void Because() + { + command.configure_argument_parser(optionSet, configuration); + } + + [Fact] + public void should_add_output_file_path_to_the_option_set() + { + optionSet.Contains("output-file-path").ShouldBeTrue(); + } + + [Fact] + public void should_add_short_version_of_output_file_path_to_the_option_set() + { + optionSet.Contains("o").ShouldBeTrue(); + } + + [Fact] + public void should_add_include_version_numbers_to_the_option_set() + { + optionSet.Contains("include-version-numbers").ShouldBeTrue(); + } + + [Fact] + public void should_add_include_version_to_the_option_set() + { + optionSet.Contains("include-version").ShouldBeTrue(); + } + } + + public class when_handling_additional_argument_parsing : ChocolateyExportCommandSpecsBase + { + private readonly IList unparsedArgs = new List(); + private Action because; + + public override void Because() + { + because = () => command.handle_additional_argument_parsing(unparsedArgs, configuration); + } + + public new void reset() + { + configuration.ExportCommand.OutputFilePath = string.Empty; + unparsedArgs.Clear(); + base.reset(); + } + + [Fact] + public void should_handle_passing_in_an_empty_string_for_output_file_path() + { + reset(); + unparsedArgs.Add(" "); + because(); + + configuration.ExportCommand.OutputFilePath.ShouldEqual("packages.config"); + } + + [Fact] + public void should_handle_passing_in_a_string_for_output_file_path() + { + reset(); + unparsedArgs.Add("custompackages.config"); + because(); + + configuration.ExportCommand.OutputFilePath.ShouldEqual("custompackages.config"); + } + } + + public class when_noop_is_called : ChocolateyExportCommandSpecsBase + { + public override void Because() + { + command.noop(configuration); + } + + [Fact] + public void should_log_a_message() + { + MockLogger.Verify(l => l.Info(It.IsAny()), Times.AtLeastOnce); + } + + [Fact] + public void should_log_the_message_we_expect() + { + var messages = MockLogger.MessagesFor(LogLevel.Info); + messages.ShouldNotBeEmpty(); + messages.Count.ShouldEqual(1); + messages[0].ShouldContain("Export would have been with options"); + } + } + + public class when_run_is_called : ChocolateyExportCommandSpecsBase + { + public new void reset() + { + Context(); + base.reset(); + } + + public override void AfterEachSpec() + { + base.AfterEachSpec(); + MockLogger.Messages.Clear(); + } + + public override void Because() + { + // because = () => command.run(configuration); + } + + [Fact] + public void should_call_nuget_service_get_all_installed_packages() + { + reset(); + command.run(configuration); + + nugetService.Verify(n => n.get_all_installed_packages(It.IsAny()), Times.Once); + } + + [Fact] + public void should_call_replace_file_when_file_already_exists() + { + fileSystem.Setup(f => f.file_exists(It.IsAny())).Returns(true); + + reset(); + command.run(configuration); + + fileSystem.Verify(n => n.replace_file(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Fact] + public void should_not_call_replace_file_when_file_doesnt_exist() + { + fileSystem.Setup(f => f.file_exists(It.IsAny())).Returns(false); + + reset(); + command.run(configuration); + + fileSystem.Verify(n => n.replace_file(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + } + } +}