Skip to content

Commit

Permalink
(GH-450) Restore pipe separator for limited list output
Browse files Browse the repository at this point in the history
When other tools use choco to determine whether a package is installed
they use `choco list -lo -r`. The -r is to limit output, show only the
package information. At one point the output separated information with
pipes, unlike the normal output. However when commit 5782a21 was
introduced, it accidentally removed this very important distinction.
Correct this to again us pipes.

Also add integration spec scenarios to ensure this is not subject to
happen again.
  • Loading branch information
ferventcoder committed Oct 3, 2015
1 parent 02c94ef commit 38537c3
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 3 deletions.
17 changes: 17 additions & 0 deletions Scenarios.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,23 @@
* should not have inconclusive package result
* should not have warning package result

### ChocolateyListCommand [ 2 Scenario(s), 9 Observation(s) ]

#### when listing local packages happy path

* should contain a summary
* should contain debugging messages
* should contain packages and versions with a space between them
* should not contain packages and versions with a pipe between them

#### when listing local packages limiting output

* should contain packages and versions with a pipe between them
* should not contain a summary
* should not contain debugging messages
* should not contain packages and versions with a space between them
* should only have messages related to package information

### ChocolateyUninstallCommand [ 13 Scenario(s), 90 Observation(s) ]

#### when force uninstalling a package
Expand Down
10 changes: 10 additions & 0 deletions src/chocolatey.tests.integration/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,15 @@ public static ChocolateyConfiguration uninstall()

return config;
}

public static ChocolateyConfiguration list()
{
var config = baseline_configuration();
config.CommandName = CommandNameType.list.to_string();
config.ListCommand.LocalOnly = true;
config.Sources = ApplicationParameters.PackagesLocation;

return config;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="NUnitSetup.cs" />
<Compile Include="Scenario.cs" />
<Compile Include="scenarios\InstallScenarios.cs" />
<Compile Include="scenarios\ListScenarios.cs" />
<Compile Include="scenarios\UninstallScenarios.cs" />
<Compile Include="scenarios\UpgradeScenarios.cs" />
<Compile Include="TODO.cs" />
Expand Down
166 changes: 166 additions & 0 deletions src/chocolatey.tests.integration/scenarios/ListScenarios.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copyright © 2011 - Present 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.integration.scenarios
{
using System.Collections.Generic;
using System.Linq;
using NuGet;
using Should;
using bdddoc.core;
using chocolatey.infrastructure.app.commands;
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.app.services;
using chocolatey.infrastructure.results;

public class ListScenarios
{
public abstract class ScenariosBase : TinySpec
{
protected IList<PackageResult> Results;
protected ChocolateyConfiguration Configuration;
protected IChocolateyPackageService Service;

public override void Context()
{
Configuration = Scenario.list();
Scenario.reset(Configuration);
Configuration.PackageNames = Configuration.Input = "upgradepackage";
Scenario.add_packages_to_source_location(Configuration, Configuration.Input + "*" + Constants.PackageExtension);
Scenario.add_packages_to_source_location(Configuration, "installpackage*" + Constants.PackageExtension);
Scenario.add_packages_to_source_location(Configuration, "badpackage*" + Constants.PackageExtension);
Scenario.install_package(Configuration, "installpackage", "1.0.0");
Scenario.install_package(Configuration, "upgradepackage", "1.0.0");
Configuration.SkipPackageInstallProvider = true;
Scenario.install_package(Configuration, "badpackage", "1.0");
Configuration.SkipPackageInstallProvider = false;

Service = NUnitSetup.Container.GetInstance<IChocolateyPackageService>();
}

public bool has_expected_message(string expectedMessage)
{
bool messageFound = false;
foreach (var messageLevel in MockLogger.Messages)
{
foreach (var message in messageLevel.Value.or_empty_list_if_null())
{
if (message.Contains(expectedMessage)) messageFound = true;
}
}

return messageFound;
}

public bool has_expected_message(string expectedMessage, LogLevel level)
{
bool messageFound = false;
foreach (var message in MockLogger.MessagesFor(level).or_empty_list_if_null())
{
if (message.Contains(expectedMessage)) messageFound = true;
}

return messageFound;
}
}

[Concern(typeof(ChocolateyListCommand))]
public class when_listing_local_packages_happy_path : ScenariosBase
{
public override void Because()
{
MockLogger.reset();
Results = Service.list_run(Configuration).ToList();
}

[Fact]
public void should_contain_packages_and_versions_with_a_space_between_them()
{
has_expected_message("upgradepackage 1.1.0").ShouldBeTrue();
}

[Fact]
public void should_not_contain_packages_and_versions_with_a_pipe_between_them()
{
has_expected_message("upgradepackage|1.1.0").ShouldBeFalse();
}

[Fact]
public void should_contain_a_summary()
{
has_expected_message("packages installed").ShouldBeTrue();
}

[Fact]
public void should_contain_debugging_messages()
{
has_expected_message("Searching for package information", LogLevel.Debug).ShouldBeTrue();
has_expected_message("Running list with the following filter", LogLevel.Debug).ShouldBeTrue();
has_expected_message("Start of List", LogLevel.Debug).ShouldBeTrue();
has_expected_message("End of List", LogLevel.Debug).ShouldBeTrue();
}
}

[Concern(typeof(ChocolateyListCommand))]
public class when_listing_local_packages_limiting_output : ScenariosBase
{
public override void Context()
{
base.Context();
Configuration.RegularOutput = false;
}

public override void Because()
{
MockLogger.reset();
Results = Service.list_run(Configuration).ToList();
}

[Fact]
public void should_contain_packages_and_versions_with_a_pipe_between_them()
{
has_expected_message("upgradepackage|1.1.0").ShouldBeTrue();
}

[Fact]
public void should_only_have_messages_related_to_package_information()
{
var count = MockLogger.Messages.SelectMany(messageLevel => messageLevel.Value.or_empty_list_if_null()).Count();
count.ShouldEqual(1);
}

[Fact]
public void should_not_contain_packages_and_versions_with_a_space_between_them()
{
has_expected_message("upgradepackage 1.1.0").ShouldBeFalse();
}

[Fact]
public void should_not_contain_a_summary()
{
has_expected_message("packages installed").ShouldBeFalse();
}

[Fact]
public void should_not_contain_debugging_messages()
{
has_expected_message("Searching for package information", LogLevel.Debug).ShouldBeFalse();
has_expected_message("Running list with the following filter", LogLevel.Debug).ShouldBeFalse();
has_expected_message("Start of List", LogLevel.Debug).ShouldBeFalse();
has_expected_message("End of List", LogLevel.Debug).ShouldBeFalse();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void list_noop(ChocolateyConfiguration config)

public IEnumerable<PackageResult> list_run(ChocolateyConfiguration config)
{
this.Log().Debug(() => "Searching for package information");
if (config.RegularOutput) this.Log().Debug(() => "Searching for package information");

if (config.Sources.is_equal_to(SpecialSourceType.webpi.to_string()))
{
Expand Down
18 changes: 16 additions & 2 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,22 @@ public IEnumerable<PackageResult> list_run(ChocolateyConfiguration config)
var package = pkg; // for lamda access
if (!config.QuietOutput)
{
this.Log().Info(config.Verbose ? ChocolateyLoggers.Important : ChocolateyLoggers.Normal, () => "{0} {1}".format_with(package.Id, package.Version.to_string()));
if (config.RegularOutput && config.Verbose) this.Log().Info(() => " {0}{1} Description: {2}{1} Tags: {3}{1} Number of Downloads: {4}{1}".format_with(package.Title.escape_curly_braces(), Environment.NewLine, package.Description.escape_curly_braces(), package.Tags.escape_curly_braces(), package.DownloadCount <= 0 ? "n/a" : package.DownloadCount.to_string()));
if (config.RegularOutput)
{
this.Log().Info(config.Verbose ? ChocolateyLoggers.Important : ChocolateyLoggers.Normal, () => "{0} {1}".format_with(package.Id, package.Version.to_string()));
if (config.Verbose) this.Log().Info(() =>
" {0}{1} Description: {2}{1} Tags: {3}{1} Number of Downloads: {4}{1}".format_with(
package.Title.escape_curly_braces(),
Environment.NewLine,
package.Description.escape_curly_braces(),
package.Tags.escape_curly_braces(),
package.DownloadCount <= 0 ? "n/a" : package.DownloadCount.to_string()
));
}
else
{
this.Log().Info(config.Verbose ? ChocolateyLoggers.Important : ChocolateyLoggers.Normal, () => "{0}|{1}".format_with(package.Id, package.Version.to_string()));
}
}
else
{
Expand Down

0 comments on commit 38537c3

Please sign in to comment.