2018-01-28 13:35:04 -08:00
// 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 System.Runtime.InteropServices ;
using FluentAssertions ;
using Microsoft.DotNet.Cli ;
using Microsoft.DotNet.Cli.CommandLine ;
using Microsoft.DotNet.Cli.Utils ;
using Microsoft.DotNet.ToolPackage ;
using Microsoft.DotNet.Tools ;
2018-03-21 19:12:32 -07:00
using Microsoft.DotNet.Tools.Tool.Install ;
using Microsoft.DotNet.Tools.Tool.Uninstall ;
2018-01-28 13:35:04 -08:00
using Microsoft.DotNet.Tools.Tests.ComponentMocks ;
using Microsoft.DotNet.Tools.Test.Utilities ;
using Microsoft.Extensions.DependencyModel.Tests ;
using Microsoft.Extensions.EnvironmentAbstractions ;
using Xunit ;
using Parser = Microsoft . DotNet . Cli . Parser ;
2018-03-21 19:12:32 -07:00
using LocalizableStrings = Microsoft . DotNet . Tools . Tool . Uninstall . LocalizableStrings ;
using InstallLocalizableStrings = Microsoft . DotNet . Tools . Tool . Install . LocalizableStrings ;
2018-04-10 15:42:50 -07:00
using Microsoft.DotNet.ShellShim ;
2018-01-28 13:35:04 -08:00
namespace Microsoft.DotNet.Tests.Commands
{
2018-03-21 19:12:32 -07:00
public class ToolUninstallCommandTests
2018-01-28 13:35:04 -08:00
{
private readonly BufferedReporter _reporter ;
private readonly IFileSystem _fileSystem ;
private readonly EnvironmentPathInstructionMock _environmentPathInstructionMock ;
private const string PackageId = "global.tool.console.demo" ;
private const string PackageVersion = "1.0.4" ;
2018-06-06 11:22:19 -07:00
private readonly string _shimsDirectory ;
private readonly string _toolsDirectory ;
2018-01-28 13:35:04 -08:00
2018-03-21 19:12:32 -07:00
public ToolUninstallCommandTests ( )
2018-01-28 13:35:04 -08:00
{
_reporter = new BufferedReporter ( ) ;
2018-06-06 11:22:19 -07:00
_fileSystem = new FileSystemMockBuilder ( ) . UseCurrentSystemTemporaryDirectory ( ) . Build ( ) ;
var tempDirectory = _fileSystem . Directory . CreateTemporaryDirectory ( ) . DirectoryPath ;
_shimsDirectory = Path . Combine ( tempDirectory , "shims" ) ;
_toolsDirectory = Path . Combine ( tempDirectory , "tools" ) ;
_environmentPathInstructionMock = new EnvironmentPathInstructionMock ( _reporter , _shimsDirectory ) ;
2018-01-28 13:35:04 -08:00
}
[Fact]
public void GivenANonExistentPackageItErrors ( )
{
var packageId = "does.not.exist" ;
var command = CreateUninstallCommand ( $"-g {packageId}" ) ;
2018-03-08 15:49:16 -08:00
Action a = ( ) = > command . Execute ( ) ;
2018-01-28 13:35:04 -08:00
2018-04-03 13:53:55 -07:00
a . ShouldThrow < GracefulException > ( )
. And
. Message
. Should ( )
. Be ( string . Format ( LocalizableStrings . ToolNotInstalled , packageId ) ) ;
2018-01-28 13:35:04 -08:00
}
[Fact]
public void GivenAPackageItUninstalls ( )
{
CreateInstallCommand ( $"-g {PackageId}" ) . Execute ( ) . Should ( ) . Be ( 0 ) ;
_reporter
. Lines
. Last ( )
. Should ( )
. Contain ( string . Format (
InstallLocalizableStrings . InstallationSucceeded ,
ProjectRestorerMock . FakeCommandName ,
PackageId ,
PackageVersion ) ) ;
2018-06-06 11:22:19 -07:00
var packageDirectory = new DirectoryPath ( Path . GetFullPath ( _toolsDirectory ) )
2018-03-16 19:47:34 -07:00
. WithSubDirectories ( PackageId , PackageVersion ) ;
2018-01-28 13:35:04 -08:00
var shimPath = Path . Combine (
2018-06-06 11:22:19 -07:00
_shimsDirectory ,
2018-01-28 13:35:04 -08:00
ProjectRestorerMock . FakeCommandName +
( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? ".exe" : "" ) ) ;
_fileSystem . Directory . Exists ( packageDirectory . Value ) . Should ( ) . BeTrue ( ) ;
_fileSystem . File . Exists ( shimPath ) . Should ( ) . BeTrue ( ) ;
_reporter . Lines . Clear ( ) ;
CreateUninstallCommand ( $"-g {PackageId}" ) . Execute ( ) . Should ( ) . Be ( 0 ) ;
_reporter
. Lines
. Single ( )
. Should ( )
. Contain ( string . Format (
LocalizableStrings . UninstallSucceeded ,
PackageId ,
PackageVersion ) ) ;
_fileSystem . Directory . Exists ( packageDirectory . Value ) . Should ( ) . BeFalse ( ) ;
_fileSystem . File . Exists ( shimPath ) . Should ( ) . BeFalse ( ) ;
}
[Fact]
public void GivenAFailureToUninstallItLeavesItInstalled ( )
{
CreateInstallCommand ( $"-g {PackageId}" ) . Execute ( ) . Should ( ) . Be ( 0 ) ;
_reporter
. Lines
. Last ( )
. Should ( )
. Contain ( string . Format (
InstallLocalizableStrings . InstallationSucceeded ,
ProjectRestorerMock . FakeCommandName ,
PackageId ,
PackageVersion ) ) ;
2018-06-06 11:22:19 -07:00
var packageDirectory = new DirectoryPath ( Path . GetFullPath ( _toolsDirectory ) )
2018-03-16 19:47:34 -07:00
. WithSubDirectories ( PackageId , PackageVersion ) ;
2018-01-28 13:35:04 -08:00
var shimPath = Path . Combine (
2018-06-06 11:22:19 -07:00
_shimsDirectory ,
2018-01-28 13:35:04 -08:00
ProjectRestorerMock . FakeCommandName +
( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? ".exe" : "" ) ) ;
_fileSystem . Directory . Exists ( packageDirectory . Value ) . Should ( ) . BeTrue ( ) ;
_fileSystem . File . Exists ( shimPath ) . Should ( ) . BeTrue ( ) ;
2018-03-08 15:49:16 -08:00
Action a = ( ) = > CreateUninstallCommand (
2018-01-28 13:35:04 -08:00
options : $"-g {PackageId}" ,
uninstallCallback : ( ) = > throw new IOException ( "simulated error" ) )
2018-03-08 15:49:16 -08:00
. Execute ( ) ;
2018-01-28 13:35:04 -08:00
2018-03-08 15:49:16 -08:00
a . ShouldThrow < GracefulException > ( )
2018-04-03 13:53:55 -07:00
. And
. Message
. Should ( )
. Be ( string . Format (
2018-01-28 13:35:04 -08:00
CommonLocalizableStrings . FailedToUninstallToolPackage ,
PackageId ,
"simulated error" ) ) ;
_fileSystem . Directory . Exists ( packageDirectory . Value ) . Should ( ) . BeTrue ( ) ;
_fileSystem . File . Exists ( shimPath ) . Should ( ) . BeTrue ( ) ;
}
2018-03-06 15:58:05 -08:00
[Fact]
public void WhenRunWithBothGlobalAndToolPathShowErrorMessage ( )
{
2018-04-03 13:53:55 -07:00
var uninstallCommand = CreateUninstallCommand ( $"-g --tool-path {Path.GetTempPath()} {PackageId}" ) ;
2018-03-06 15:58:05 -08:00
Action a = ( ) = > uninstallCommand . Execute ( ) ;
2018-04-03 13:53:55 -07:00
a . ShouldThrow < GracefulException > ( )
. And
. Message
. Should ( )
. Be ( LocalizableStrings . UninstallToolCommandInvalidGlobalAndToolPath ) ;
}
[Fact]
public void GivenAnInvalidToolPathItThrowsException ( )
{
var toolPath = "tool-path-does-not-exist" ;
var uninstallCommand = CreateUninstallCommand ( $"--tool-path {toolPath} {PackageId}" ) ;
Action a = ( ) = > uninstallCommand . Execute ( ) ;
a . ShouldThrow < GracefulException > ( )
. And
. Message
. Should ( )
. Be ( string . Format ( LocalizableStrings . InvalidToolPathOption , toolPath ) ) ;
2018-03-06 15:58:05 -08:00
}
[Fact]
public void WhenRunWithNeitherOfGlobalNorToolPathShowErrorMessage ( )
{
var uninstallCommand = CreateUninstallCommand ( PackageId ) ;
Action a = ( ) = > uninstallCommand . Execute ( ) ;
2018-04-03 13:53:55 -07:00
a . ShouldThrow < GracefulException > ( )
. And
. Message
. Should ( )
. Be ( LocalizableStrings . UninstallToolCommandNeedGlobalOrToolPath ) ;
2018-03-06 15:58:05 -08:00
}
2018-03-21 19:12:32 -07:00
private ToolInstallCommand CreateInstallCommand ( string options )
2018-01-28 13:35:04 -08:00
{
2018-03-21 19:12:32 -07:00
ParseResult result = Parser . Instance . Parse ( "dotnet tool install " + options ) ;
2018-01-28 13:35:04 -08:00
2018-06-06 11:22:19 -07:00
var store = new ToolPackageStoreMock ( new DirectoryPath ( _toolsDirectory ) , _fileSystem ) ;
2018-03-06 15:58:05 -08:00
var packageInstallerMock = new ToolPackageInstallerMock (
_fileSystem ,
store ,
new ProjectRestorerMock (
_fileSystem ,
_reporter ) ) ;
2018-03-21 19:12:32 -07:00
return new ToolInstallCommand (
result [ "dotnet" ] [ "tool" ] [ "install" ] ,
2018-01-28 13:35:04 -08:00
result ,
2018-03-06 15:58:05 -08:00
( _ ) = > ( store , packageInstallerMock ) ,
2018-04-10 15:42:50 -07:00
( _ ) = > new ShellShimRepository (
2018-06-06 11:22:19 -07:00
new DirectoryPath ( _shimsDirectory ) ,
2018-04-10 15:42:50 -07:00
fileSystem : _fileSystem ,
appHostShellShimMaker : new AppHostShellShimMakerMock ( _fileSystem ) ) ,
2018-01-28 13:35:04 -08:00
_environmentPathInstructionMock ,
_reporter ) ;
}
2018-03-21 19:12:32 -07:00
private ToolUninstallCommand CreateUninstallCommand ( string options , Action uninstallCallback = null )
2018-01-28 13:35:04 -08:00
{
2018-03-21 19:12:32 -07:00
ParseResult result = Parser . Instance . Parse ( "dotnet tool uninstall " + options ) ;
2018-01-28 13:35:04 -08:00
2018-03-21 19:12:32 -07:00
return new ToolUninstallCommand (
result [ "dotnet" ] [ "tool" ] [ "uninstall" ] ,
2018-01-28 13:35:04 -08:00
result ,
2018-03-06 15:58:05 -08:00
( _ ) = > new ToolPackageStoreMock (
2018-06-06 11:22:19 -07:00
new DirectoryPath ( _toolsDirectory ) ,
2018-01-28 13:35:04 -08:00
_fileSystem ,
uninstallCallback ) ,
2018-04-10 15:42:50 -07:00
( _ ) = > new ShellShimRepository (
2018-06-06 11:22:19 -07:00
new DirectoryPath ( _shimsDirectory ) ,
2018-04-10 15:42:50 -07:00
fileSystem : _fileSystem ,
appHostShellShimMaker : new AppHostShellShimMakerMock ( _fileSystem ) ) ,
2018-01-28 13:35:04 -08:00
_reporter ) ;
}
}
}