Fix validation of tool-path option for tool list and uninstall commands.
This commit checks that the `--tool-path` option for the `tool list` and `tool uninstall` commands is a directory that exists. If the directory does not exist, an error and the command help is displayed. Fixes #8931.
This commit is contained in:
parent
4883d9643b
commit
0181c67bb0
32 changed files with 221 additions and 23 deletions
|
@ -55,7 +55,8 @@ namespace Microsoft.DotNet.Tests.Commands
|
|||
{
|
||||
var store = new Mock<IToolPackageStore>(MockBehavior.Strict);
|
||||
|
||||
var command = CreateCommand(store.Object, "-g --tool-path /tools", "/tools");
|
||||
var toolPath = Path.GetTempPath();
|
||||
var command = CreateCommand(store.Object, $"-g --tool-path {toolPath}", toolPath);
|
||||
|
||||
Action a = () => {
|
||||
command.Execute();
|
||||
|
@ -83,6 +84,26 @@ namespace Microsoft.DotNet.Tests.Commands
|
|||
_reporter.Lines.Should().Equal(EnumerateExpectedTableLines(store.Object));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GivenAnInvalidToolPathItThrowsException()
|
||||
{
|
||||
var store = new Mock<IToolPackageStore>(MockBehavior.Strict);
|
||||
store
|
||||
.Setup(s => s.EnumeratePackages())
|
||||
.Returns(new IToolPackage[0]);
|
||||
|
||||
var toolPath = "tool-path-does-not-exist";
|
||||
var command = CreateCommand(store.Object, $"--tool-path {toolPath}", toolPath);
|
||||
|
||||
Action a = () => command.Execute();
|
||||
|
||||
a.ShouldThrow<GracefulException>()
|
||||
.And
|
||||
.Message
|
||||
.Should()
|
||||
.Be(string.Format(LocalizableStrings.InvalidToolPathOption, toolPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GivenAToolPathItPassesToolPathToStoreFactory()
|
||||
{
|
||||
|
@ -91,7 +112,8 @@ namespace Microsoft.DotNet.Tests.Commands
|
|||
.Setup(s => s.EnumeratePackages())
|
||||
.Returns(new IToolPackage[0]);
|
||||
|
||||
var command = CreateCommand(store.Object, "--tool-path /tools", "/tools");
|
||||
var toolPath = Path.GetTempPath();
|
||||
var command = CreateCommand(store.Object, $"--tool-path {toolPath}", toolPath);
|
||||
|
||||
command.Execute().Should().Be(0);
|
||||
|
||||
|
|
|
@ -51,11 +51,11 @@ namespace Microsoft.DotNet.Tests.Commands
|
|||
|
||||
Action a = () => command.Execute();
|
||||
|
||||
a.ShouldThrow<GracefulException>().And.Message
|
||||
.Should().Contain(
|
||||
string.Format(
|
||||
LocalizableStrings.ToolNotInstalled,
|
||||
packageId));
|
||||
a.ShouldThrow<GracefulException>()
|
||||
.And
|
||||
.Message
|
||||
.Should()
|
||||
.Be(string.Format(LocalizableStrings.ToolNotInstalled, packageId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -131,9 +131,10 @@ namespace Microsoft.DotNet.Tests.Commands
|
|||
.Execute();
|
||||
|
||||
a.ShouldThrow<GracefulException>()
|
||||
.And.Message
|
||||
.Should().Contain(
|
||||
string.Format(
|
||||
.And
|
||||
.Message
|
||||
.Should()
|
||||
.Be(string.Format(
|
||||
CommonLocalizableStrings.FailedToUninstallToolPackage,
|
||||
PackageId,
|
||||
"simulated error"));
|
||||
|
@ -145,12 +146,31 @@ namespace Microsoft.DotNet.Tests.Commands
|
|||
[Fact]
|
||||
public void WhenRunWithBothGlobalAndToolPathShowErrorMessage()
|
||||
{
|
||||
var uninstallCommand = CreateUninstallCommand($"-g --tool-path /tmp/folder {PackageId}");
|
||||
var uninstallCommand = CreateUninstallCommand($"-g --tool-path {Path.GetTempPath()} {PackageId}");
|
||||
|
||||
Action a = () => uninstallCommand.Execute();
|
||||
|
||||
a.ShouldThrow<GracefulException>().And.Message
|
||||
.Should().Contain(LocalizableStrings.UninstallToolCommandInvalidGlobalAndToolPath);
|
||||
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));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -160,8 +180,11 @@ namespace Microsoft.DotNet.Tests.Commands
|
|||
|
||||
Action a = () => uninstallCommand.Execute();
|
||||
|
||||
a.ShouldThrow<GracefulException>().And.Message
|
||||
.Should().Contain(LocalizableStrings.UninstallToolCommandNeedGlobalOrToolPath);
|
||||
a.ShouldThrow<GracefulException>()
|
||||
.And
|
||||
.Message
|
||||
.Should()
|
||||
.Be(LocalizableStrings.UninstallToolCommandNeedGlobalOrToolPath);
|
||||
}
|
||||
|
||||
private ToolInstallCommand CreateInstallCommand(string options)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue