Implement tests for the list tool command.

This commit implements tests for the `list tool` command.
This commit is contained in:
Peter Huene 2018-03-02 14:35:23 -08:00
parent 5ebbd618ec
commit da90a08ada
No known key found for this signature in database
GPG key ID: E1D265D820213D6A
3 changed files with 264 additions and 0 deletions

View file

@ -21,3 +21,4 @@ using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Microsoft.DotNet.Tools.Tests.ComponentMocks, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.DotNet.ToolPackage.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.DotNet.ShellShim.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]

View file

@ -0,0 +1,261 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FluentAssertions;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ToolPackage;
using Microsoft.DotNet.Tools;
using Microsoft.DotNet.Tools.List.Tool;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.DependencyModel.Tests;
using Microsoft.Extensions.EnvironmentAbstractions;
using Moq;
using NuGet.Versioning;
using Xunit;
using Parser = Microsoft.DotNet.Cli.Parser;
using LocalizableStrings = Microsoft.DotNet.Tools.List.Tool.LocalizableStrings;
namespace Microsoft.DotNet.Tests.Commands
{
public class ListToolCommandTests
{
private readonly BufferedReporter _reporter;
public ListToolCommandTests()
{
_reporter = new BufferedReporter();
}
[Fact]
public void GivenAMissingGlobalOptionItErrors()
{
var store = new Mock<IToolPackageStore>(MockBehavior.Strict);
var command = CreateCommand(store.Object);
Action a = () => {
command.Execute();
};
a.ShouldThrow<GracefulException>()
.And
.Message
.Should()
.Be(LocalizableStrings.ListToolCommandOnlySupportsGlobal);
}
[Fact]
public void GivenNoInstalledPackagesItPrintsEmptyTable()
{
var store = new Mock<IToolPackageStore>(MockBehavior.Strict);
store
.Setup(s => s.EnumeratePackages())
.Returns(new IToolPackage[0]);
var command = CreateCommand(store.Object, "-g");
command.Execute().Should().Be(0);
_reporter.Lines.Should().Equal(
string.Format(
"{0} {1} {2}",
LocalizableStrings.PackageIdColumn,
LocalizableStrings.VersionColumn,
LocalizableStrings.CommandsColumn
),
"-------------------------------------");
}
[Fact]
public void GivenASingleInstalledPackageItPrintsThePackage()
{
var store = new Mock<IToolPackageStore>(MockBehavior.Strict);
store
.Setup(s => s.EnumeratePackages())
.Returns(new[] {
CreateMockToolPackage(
"test.tool",
"1.3.5-preview",
new[] {
new CommandSettings("foo", "dotnet", new FilePath("tool"))
}
)
});
var command = CreateCommand(store.Object, "-g");
command.Execute().Should().Be(0);
_reporter.Lines.Should().Equal(
string.Format(
"{0} {1} {2}",
LocalizableStrings.PackageIdColumn,
LocalizableStrings.VersionColumn,
LocalizableStrings.CommandsColumn
),
"-------------------------------------------",
"test.tool 1.3.5-preview foo ");
}
[Fact]
public void GivenMultipleInstalledPackagesItPrintsThePackages()
{
var store = new Mock<IToolPackageStore>(MockBehavior.Strict);
store
.Setup(s => s.EnumeratePackages())
.Returns(new[] {
CreateMockToolPackage(
"test.tool",
"1.3.5-preview",
new[] {
new CommandSettings("foo", "dotnet", new FilePath("tool"))
}
),
CreateMockToolPackage(
"another.tool",
"2.7.3",
new[] {
new CommandSettings("bar", "dotnet", new FilePath("tool"))
}
),
CreateMockToolPackage(
"some.tool",
"1.0.0",
new[] {
new CommandSettings("fancy-foo", "dotnet", new FilePath("tool"))
}
)
});
var command = CreateCommand(store.Object, "-g");
command.Execute().Should().Be(0);
_reporter.Lines.Should().Equal(
string.Format(
"{0} {1} {2} ",
LocalizableStrings.PackageIdColumn,
LocalizableStrings.VersionColumn,
LocalizableStrings.CommandsColumn
),
"----------------------------------------------",
"another.tool 2.7.3 bar ",
"some.tool 1.0.0 fancy-foo",
"test.tool 1.3.5-preview foo ");
}
[Fact]
public void GivenAPackageWithMultipleCommandsItListsThem()
{
var store = new Mock<IToolPackageStore>(MockBehavior.Strict);
store
.Setup(s => s.EnumeratePackages())
.Returns(new[] {
CreateMockToolPackage(
"test.tool",
"1.3.5-preview",
new[] {
new CommandSettings("foo", "dotnet", new FilePath("tool")),
new CommandSettings("bar", "dotnet", new FilePath("tool")),
new CommandSettings("baz", "dotnet", new FilePath("tool"))
}
)
});
var command = CreateCommand(store.Object, "-g");
command.Execute().Should().Be(0);
_reporter.Lines.Should().Equal(
string.Format(
"{0} {1} {2} ",
LocalizableStrings.PackageIdColumn,
LocalizableStrings.VersionColumn,
LocalizableStrings.CommandsColumn
),
"------------------------------------------------",
"test.tool 1.3.5-preview foo, bar, baz");
}
[Fact]
public void GivenABrokenPackageItPrintsWarning()
{
var store = new Mock<IToolPackageStore>(MockBehavior.Strict);
store
.Setup(s => s.EnumeratePackages())
.Returns(new[] {
CreateMockToolPackage(
"test.tool",
"1.3.5-preview",
new[] {
new CommandSettings("foo", "dotnet", new FilePath("tool"))
}
),
CreateMockBrokenPackage("another.tool", "2.7.3"),
CreateMockToolPackage(
"some.tool",
"1.0.0",
new[] {
new CommandSettings("fancy-foo", "dotnet", new FilePath("tool"))
}
)
});
var command = CreateCommand(store.Object, "-g");
command.Execute().Should().Be(0);
_reporter.Lines.Should().Equal(
string.Format(
LocalizableStrings.InvalidPackageWarning,
"another.tool",
"broken"
).Yellow(),
string.Format(
"{0} {1} {2} ",
LocalizableStrings.PackageIdColumn,
LocalizableStrings.VersionColumn,
LocalizableStrings.CommandsColumn
),
"--------------------------------------------",
"some.tool 1.0.0 fancy-foo",
"test.tool 1.3.5-preview foo ");
}
private IToolPackage CreateMockToolPackage(string id, string version, IReadOnlyList<CommandSettings> commands)
{
var package = new Mock<IToolPackage>(MockBehavior.Strict);
package.SetupGet(p => p.Id).Returns(new PackageId(id));
package.SetupGet(p => p.Version).Returns(NuGetVersion.Parse(version));
package.SetupGet(p => p.Commands).Returns(commands);
return package.Object;
}
private IToolPackage CreateMockBrokenPackage(string id, string version)
{
var package = new Mock<IToolPackage>(MockBehavior.Strict);
package.SetupGet(p => p.Id).Returns(new PackageId(id));
package.SetupGet(p => p.Version).Returns(NuGetVersion.Parse(version));
package.SetupGet(p => p.Commands).Throws(new ToolConfigurationException("broken"));
return package.Object;
}
private ListToolCommand CreateCommand(IToolPackageStore store, string options = "")
{
ParseResult result = Parser.Instance.Parse("dotnet list tool " + options);
return new ListToolCommand(
result["dotnet"]["list"]["tool"],
result,
store,
_reporter);
}
}
}

View file

@ -81,7 +81,9 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="Moq" Version="4.7.25" />
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="$(MicrosoftDotNetPlatformAbstractionsPackageVersion)" />
<PackageReference Include="Microsoft.DotNet.Cli.CommandLine" Version="$(MicrosoftDotNetCliCommandLinePackageVersion)" />
<PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.0" />
</ItemGroup>
</Project>