Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

dotnet tool update does not throw but install when it is not installed #10205

Merged
merged 3 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 55 additions & 42 deletions src/dotnet/commands/dotnet-tool/update/ToolUpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,58 +87,29 @@ public override int Execute()

IShellShimRepository shellShimRepository = _createShellShimRepository(toolPath);

IToolPackage oldPackage;
try
{
oldPackage = toolPackageStoreQuery.EnumeratePackageVersions(_packageId).SingleOrDefault();
if (oldPackage == null)
{
throw new GracefulException(
messages: new[]
{
string.Format(
LocalizableStrings.ToolNotInstalled,
_packageId),
},
isUserError: false);
}
}
catch (InvalidOperationException)
{
throw new GracefulException(
messages: new[]
{
string.Format(
LocalizableStrings.ToolHasMultipleVersionsInstalled,
_packageId),
},
isUserError: false);
}

FilePath? configFile = null;
if (_configFilePath != null)
{
configFile = new FilePath(_configFilePath);
}
IToolPackage oldPackageNullable = GetOldPackage(toolPackageStoreQuery);

using (var scope = new TransactionScope(
TransactionScopeOption.Required,
TimeSpan.Zero))
{
RunWithHandlingUninstallError(() =>
if (oldPackageNullable != null)
{
foreach (RestoredCommand command in oldPackage.Commands)
RunWithHandlingUninstallError(() =>
{
shellShimRepository.RemoveShim(command.Name);
}
foreach (RestoredCommand command in oldPackageNullable.Commands)
{
shellShimRepository.RemoveShim(command.Name);
}

toolPackageUninstaller.Uninstall(oldPackage.PackageDirectory);
});
toolPackageUninstaller.Uninstall(oldPackageNullable.PackageDirectory);
});
}

RunWithHandlingInstallError(() =>
{
IToolPackage newInstalledPackage = toolPackageInstaller.InstallPackage(
new PackageLocation(nugetConfig: configFile, additionalFeeds: _additionalFeeds),
new PackageLocation(nugetConfig: GetConfigFile(), additionalFeeds: _additionalFeeds),
packageId: _packageId,
targetFramework: _framework,
verbosity: _verbosity);
Expand All @@ -148,7 +119,7 @@ public override int Execute()
shellShimRepository.CreateShim(command.Executable, command.Name);
}

PrintSuccessMessage(oldPackage, newInstalledPackage);
PrintSuccessMessage(oldPackageNullable, newInstalledPackage);
});

scope.Complete();
Expand Down Expand Up @@ -227,9 +198,51 @@ private void RunWithHandlingUninstallError(Action uninstallAction)
}
}

private FilePath? GetConfigFile()
{
FilePath? configFile = null;
if (_configFilePath != null)
{
configFile = new FilePath(_configFilePath);
}

return configFile;
}

private IToolPackage GetOldPackage(IToolPackageStoreQuery toolPackageStoreQuery)
{
IToolPackage oldPackageNullable;
try
{
oldPackageNullable = toolPackageStoreQuery.EnumeratePackageVersions(_packageId).SingleOrDefault();
}
catch (InvalidOperationException)
{
throw new GracefulException(
messages: new[]
{
string.Format(
LocalizableStrings.ToolHasMultipleVersionsInstalled,
_packageId),
},
isUserError: false);
}

return oldPackageNullable;
}

private void PrintSuccessMessage(IToolPackage oldPackage, IToolPackage newInstalledPackage)
{
if (oldPackage.Version != newInstalledPackage.Version)
if (oldPackage == null)
{
_reporter.WriteLine(
string.Format(
Install.LocalizableStrings.InstallationSucceeded,
string.Join(", ", newInstalledPackage.Commands.Select(c => c.Name)),
newInstalledPackage.Id,
newInstalledPackage.Version.ToNormalizedString()).Green());
}
else if (oldPackage.Version != newInstalledPackage.Version)
{
_reporter.WriteLine(
string.Format(
Expand Down
17 changes: 13 additions & 4 deletions test/dotnet.Tests/CommandTests/ToolUpdateCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public ToolUpdateCommandTests()
}

[Fact]
public void GivenANonExistentPackageItErrors()
public void GivenANonFeedExistentPackageItErrors()
{
var packageId = "does.not.exist";
var command = CreateUpdateCommand($"-g {packageId}");
Expand All @@ -78,9 +78,18 @@ public void GivenANonExistentPackageItErrors()

a.ShouldThrow<GracefulException>().And.Message
.Should().Contain(
string.Format(
LocalizableStrings.ToolNotInstalled,
packageId));
Tools.Tool.Install.LocalizableStrings.ToolInstallationRestoreFailed);
}

[Fact]
public void GivenANonExistentPackageItInstallTheLatest()
{
var command = CreateUpdateCommand($"-g {_packageId}");

command.Execute();

_store.EnumeratePackageVersions(_packageId).Single().Version.ToFullString().Should()
.Be(HigherPackageVersion);
}

[Fact]
Expand Down