dotnet-installer/test/dotnet.Tests/CommandObjectTests.cs
William Li 854feefe6f
Give a different error to guide use to install via global tools (#9070)
Give a different error to guide use to install via global tools so, if several bundled DotnetTools cannot finish source build on time. The user can use global tools to get it.

The original plan that adding a different resolver is hard due to resolver can only find dll that will be used to spawn a process. However, the command constructor will give an error message when resolver find null. By adding a different error when the command name is part of the list, it can achieve the same goal.
2018-04-17 12:21:20 -07:00

77 lines
2.9 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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 Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Test.Utilities;
using System;
using Xunit;
using Xunit.Abstractions;
using FluentAssertions;
namespace Microsoft.DotNet.Tests
{
public class CommandObjectTests : TestBase
{
ITestOutputHelper _output;
public CommandObjectTests(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public void WhenItCannotResolveCommandItThrows()
{
Action a = () => { Command.Create(new ResolveNothingCommandResolverPolicy(), "non-exist-command", Array.Empty<string>() ); };
a.ShouldThrow<CommandUnknownException>();
}
[Fact]
public void WhenItCannotResolveCommandButCommandIsInListOfKnownToolsItThrows()
{
Action a = () => { Command.Create(new ResolveNothingCommandResolverPolicy(), "non-exist-command", Array.Empty<string>()); };
a.ShouldThrow<CommandUnknownException>();
}
[Fact]
public void WhenItCannotResolveCommandButCommandIsInListOfKnownToolsItThrowsWithGuideToUseTool()
{
Action a = () => { Command.Create(new ResolveNothingCommandResolverPolicy(), "dotnet-ef", Array.Empty<string>()); };
a.ShouldThrow<CommandAvailableAsDotNetToolException>()
.And.Message.Should()
.Contain(string.Format(LocalizableStrings.CannotFindCommandAvailableAsTool,
"ef",
"dotnet-ef"));
}
[Fact]
public void WhenItCannotResolveCommandButCommandIsInListOfKnownToolsItThrowsWithGuideToUseToolWithNormalizedCasing()
{
Action a = () => { Command.Create(new ResolveNothingCommandResolverPolicy(), "dotnet-EF", Array.Empty<string>()); };
a.ShouldThrow<CommandAvailableAsDotNetToolException>()
.And.Message.Should()
.Contain(string.Format(LocalizableStrings.CannotFindCommandAvailableAsTool,
"EF",
"dotnet-ef"));
}
private class ResolveNothingCommandResolverPolicy : ICommandResolverPolicy
{
public CompositeCommandResolver CreateCommandResolver()
{
var compositeCommandResolver = new CompositeCommandResolver();
compositeCommandResolver.AddCommandResolver(new ResolveNothingCommandResolver());
return compositeCommandResolver;
}
}
private class ResolveNothingCommandResolver : ICommandResolver
{
public CommandSpec Resolve(CommandResolverArguments arguments)
{
return null;
}
}
}
}