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:
Peter Huene 2018-04-03 13:53:55 -07:00
parent 4883d9643b
commit 0181c67bb0
No known key found for this signature in database
GPG key ID: E1D265D820213D6A
32 changed files with 221 additions and 23 deletions

View file

@ -144,4 +144,7 @@
<data name="CommandsColumn" xml:space="preserve"> <data name="CommandsColumn" xml:space="preserve">
<value>Commands</value> <value>Commands</value>
</data> </data>
<data name="InvalidToolPathOption" xml:space="preserve">
<value>Tool path '{0}' does not exist.</value>
</data>
</root> </root>

View file

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using Microsoft.DotNet.Cli; using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Cli.CommandLine;
@ -44,6 +45,14 @@ namespace Microsoft.DotNet.Tools.Tool.List
DirectoryPath? toolPath = null; DirectoryPath? toolPath = null;
if (!string.IsNullOrWhiteSpace(toolPathOption)) if (!string.IsNullOrWhiteSpace(toolPathOption))
{ {
if (!Directory.Exists(toolPathOption))
{
throw new GracefulException(
string.Format(
LocalizableStrings.InvalidToolPathOption,
toolPathOption));
}
toolPath = new DirectoryPath(toolPathOption); toolPath = new DirectoryPath(toolPathOption);
} }

View file

@ -47,6 +47,11 @@
<target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target> <target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -47,6 +47,11 @@
<target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target> <target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -47,6 +47,11 @@
<target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target> <target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -47,6 +47,11 @@
<target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target> <target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -47,6 +47,11 @@
<target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target> <target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -47,6 +47,11 @@
<target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target> <target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -47,6 +47,11 @@
<target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target> <target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -47,6 +47,11 @@
<target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target> <target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -47,6 +47,11 @@
<target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target> <target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -47,6 +47,11 @@
<target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target> <target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -47,6 +47,11 @@
<target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target> <target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -47,6 +47,11 @@
<target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target> <target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -47,6 +47,11 @@
<target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target> <target state="new">(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -153,4 +153,7 @@
<data name="UninstallToolCommandInvalidGlobalAndToolPath" xml:space="preserve"> <data name="UninstallToolCommandInvalidGlobalAndToolPath" xml:space="preserve">
<value>(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</value> <value>(--global) conflicts with the tool path option (--tool-path). Please specify only one of the options.</value>
</data> </data>
<data name="InvalidToolPathOption" xml:space="preserve">
<value>Tool path '{0}' does not exist.</value>
</data>
</root> </root>

View file

@ -49,22 +49,30 @@ namespace Microsoft.DotNet.Tools.Tool.Uninstall
var global = _options.ValueOrDefault<bool>("global"); var global = _options.ValueOrDefault<bool>("global");
var toolPath = _options.SingleArgumentOrDefault("tool-path"); var toolPath = _options.SingleArgumentOrDefault("tool-path");
if (string.IsNullOrWhiteSpace(toolPath) && !global) DirectoryPath? toolDirectoryPath = null;
if (!string.IsNullOrWhiteSpace(toolPath))
{
if (!Directory.Exists(toolPath))
{
throw new GracefulException(
string.Format(
LocalizableStrings.InvalidToolPathOption,
toolPath));
}
toolDirectoryPath = new DirectoryPath(toolPath);
}
if (toolDirectoryPath == null && !global)
{ {
throw new GracefulException(LocalizableStrings.UninstallToolCommandNeedGlobalOrToolPath); throw new GracefulException(LocalizableStrings.UninstallToolCommandNeedGlobalOrToolPath);
} }
if (!string.IsNullOrWhiteSpace(toolPath) && global) if (toolDirectoryPath != null && global)
{ {
throw new GracefulException(LocalizableStrings.UninstallToolCommandInvalidGlobalAndToolPath); throw new GracefulException(LocalizableStrings.UninstallToolCommandInvalidGlobalAndToolPath);
} }
DirectoryPath? toolDirectoryPath = null;
if (!string.IsNullOrWhiteSpace(toolPath))
{
toolDirectoryPath = new DirectoryPath(toolPath);
}
IToolPackageStore toolPackageStore = _createToolPackageStore(toolDirectoryPath); IToolPackageStore toolPackageStore = _createToolPackageStore(toolDirectoryPath);
IShellShimRepository shellShimRepository = _createShellShimRepository(toolDirectoryPath); IShellShimRepository shellShimRepository = _createShellShimRepository(toolDirectoryPath);

View file

@ -62,6 +62,11 @@
<target state="needs-review-translation">Globální cesta a cesta k nástroji nemůžou být zadané současně.</target> <target state="needs-review-translation">Globální cesta a cesta k nástroji nemůžou být zadané současně.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -62,6 +62,11 @@
<target state="needs-review-translation">Die gleichzeitige Angabe von "global" und "tool-path" ist nicht möglich.</target> <target state="needs-review-translation">Die gleichzeitige Angabe von "global" und "tool-path" ist nicht möglich.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -62,6 +62,11 @@
<target state="needs-review-translation">No puede tener una ruta global y de herramienta como opinión al mismo tiempo."</target> <target state="needs-review-translation">No puede tener una ruta global y de herramienta como opinión al mismo tiempo."</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -62,6 +62,11 @@
<target state="needs-review-translation">Impossible d'avoir une propriété opinion avec les paramètres global et tool-path en même temps."</target> <target state="needs-review-translation">Impossible d'avoir une propriété opinion avec les paramètres global et tool-path en même temps."</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -62,6 +62,11 @@
<target state="needs-review-translation">Non è possibile specificare contemporaneamente global e tool-path come opzione."</target> <target state="needs-review-translation">Non è possibile specificare contemporaneamente global e tool-path come opzione."</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -62,6 +62,11 @@
<target state="needs-review-translation">global と tool-path を意見として同時に指定することはできません。"</target> <target state="needs-review-translation">global と tool-path を意見として同時に指定することはできません。"</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -62,6 +62,11 @@
<target state="needs-review-translation">전역 및 도구 경로를 의견으로 동시에 사용할 수 없습니다."</target> <target state="needs-review-translation">전역 및 도구 경로를 의견으로 동시에 사용할 수 없습니다."</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -62,6 +62,11 @@
<target state="needs-review-translation">Nie można jednocześnie używać ścieżki globalnej i ścieżki narzędzia jako opinii.</target> <target state="needs-review-translation">Nie można jednocześnie używać ścieżki globalnej i ścieżki narzędzia jako opinii.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -62,6 +62,11 @@
<target state="needs-review-translation">Não é possível ter o caminho de ferramenta e o global como opinião ao mesmo tempo."</target> <target state="needs-review-translation">Não é possível ter o caminho de ferramenta e o global como opinião ao mesmo tempo."</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -62,6 +62,11 @@
<target state="needs-review-translation">Невозможно указать глобальный параметр и параметр tool-path в качестве оценки одновременно.</target> <target state="needs-review-translation">Невозможно указать глобальный параметр и параметр tool-path в качестве оценки одновременно.</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -62,6 +62,11 @@
<target state="needs-review-translation">Fikir olarak aynı anda hem genel yol hem de araç yolu kullanılamaz.”</target> <target state="needs-review-translation">Fikir olarak aynı anda hem genel yol hem de araç yolu kullanılamaz.”</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -62,6 +62,11 @@
<target state="needs-review-translation">无法同时主张全局和工具路径。”</target> <target state="needs-review-translation">无法同时主张全局和工具路径。”</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -62,6 +62,11 @@
<target state="needs-review-translation">無法同時將全域與工具路徑作為選項。」</target> <target state="needs-review-translation">無法同時將全域與工具路徑作為選項。」</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="InvalidToolPathOption">
<source>Tool path '{0}' does not exist.</source>
<target state="new">Tool path '{0}' does not exist.</target>
<note />
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>

View file

@ -55,7 +55,8 @@ namespace Microsoft.DotNet.Tests.Commands
{ {
var store = new Mock<IToolPackageStore>(MockBehavior.Strict); 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 = () => { Action a = () => {
command.Execute(); command.Execute();
@ -83,6 +84,26 @@ namespace Microsoft.DotNet.Tests.Commands
_reporter.Lines.Should().Equal(EnumerateExpectedTableLines(store.Object)); _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] [Fact]
public void GivenAToolPathItPassesToolPathToStoreFactory() public void GivenAToolPathItPassesToolPathToStoreFactory()
{ {
@ -91,7 +112,8 @@ namespace Microsoft.DotNet.Tests.Commands
.Setup(s => s.EnumeratePackages()) .Setup(s => s.EnumeratePackages())
.Returns(new IToolPackage[0]); .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); command.Execute().Should().Be(0);

View file

@ -51,11 +51,11 @@ namespace Microsoft.DotNet.Tests.Commands
Action a = () => command.Execute(); Action a = () => command.Execute();
a.ShouldThrow<GracefulException>().And.Message a.ShouldThrow<GracefulException>()
.Should().Contain( .And
string.Format( .Message
LocalizableStrings.ToolNotInstalled, .Should()
packageId)); .Be(string.Format(LocalizableStrings.ToolNotInstalled, packageId));
} }
[Fact] [Fact]
@ -131,9 +131,10 @@ namespace Microsoft.DotNet.Tests.Commands
.Execute(); .Execute();
a.ShouldThrow<GracefulException>() a.ShouldThrow<GracefulException>()
.And.Message .And
.Should().Contain( .Message
string.Format( .Should()
.Be(string.Format(
CommonLocalizableStrings.FailedToUninstallToolPackage, CommonLocalizableStrings.FailedToUninstallToolPackage,
PackageId, PackageId,
"simulated error")); "simulated error"));
@ -145,12 +146,31 @@ namespace Microsoft.DotNet.Tests.Commands
[Fact] [Fact]
public void WhenRunWithBothGlobalAndToolPathShowErrorMessage() 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(); Action a = () => uninstallCommand.Execute();
a.ShouldThrow<GracefulException>().And.Message a.ShouldThrow<GracefulException>()
.Should().Contain(LocalizableStrings.UninstallToolCommandInvalidGlobalAndToolPath); .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] [Fact]
@ -160,8 +180,11 @@ namespace Microsoft.DotNet.Tests.Commands
Action a = () => uninstallCommand.Execute(); Action a = () => uninstallCommand.Execute();
a.ShouldThrow<GracefulException>().And.Message a.ShouldThrow<GracefulException>()
.Should().Contain(LocalizableStrings.UninstallToolCommandNeedGlobalOrToolPath); .And
.Message
.Should()
.Be(LocalizableStrings.UninstallToolCommandNeedGlobalOrToolPath);
} }
private ToolInstallCommand CreateInstallCommand(string options) private ToolInstallCommand CreateInstallCommand(string options)