dotnet-installer/test/Microsoft.DotNet.Tools.Tests.ComponentMocks/ProjectRestorerMock.cs
Peter Huene aab9af71b8
Implement uninstall tool command.
This commit implements the `uninstall tool` command.

The `uninstall tool` command is responsible for uninstalling global tools that
are installed with the `install tool` command.

This commit heavily refactors the ToolPackage and ShellShim namespaces to
better support the operations required for the uninstall command.

Several string resources have been updated to be more informative or to correct
oddly structured sentences.

This commit also fixes `--version` on the install command not supporting ranges
and wildcards.

Fixes #8549.

Issue #8485 is partially fixed by this commit (`--prerelease` is not yet
implemented).
2018-02-19 22:46:16 -08:00

160 lines
5.7 KiB
C#

// 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 Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ToolPackage;
using Microsoft.DotNet.Tools;
using Microsoft.DotNet.Tools.Install.Tool;
using Microsoft.Extensions.EnvironmentAbstractions;
namespace Microsoft.DotNet.Tools.Tests.ComponentMocks
{
internal class ProjectRestorerMock : IProjectRestorer
{
public const string FakeEntrypointName = "SimulatorEntryPoint.dll";
public const string FakeCommandName = "SimulatorCommand";
public const string DefaultPackageName = "global.tool.console.demo";
public const string DefaultPackageVersion = "1.0.4";
private readonly IFileSystem _fileSystem;
private readonly IReporter _reporter;
private readonly List<MockFeed> _feeds;
public ProjectRestorerMock(
IFileSystem fileSystem,
IReporter reporter = null,
IEnumerable<MockFeed> feeds = null)
{
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
_reporter = reporter;
_feeds = new List<MockFeed>();
if (feeds == null)
{
_feeds.Add(new MockFeed
{
Type = MockFeedType.FeedFromLookUpNugetConfig,
Packages = new List<MockFeedPackage>
{
new MockFeedPackage
{
PackageId = DefaultPackageName,
Version = DefaultPackageVersion
}
}
});
}
else
{
_feeds.AddRange(feeds);
}
}
public void Restore(
FilePath project,
DirectoryPath assetJsonOutput,
FilePath? nugetConfig = null,
string source = null,
string verbosity = null)
{
string packageId;
string packageVersion;
string targetFramework;
try
{
// The mock installer wrote a mock project file containing id:version:framework
var contents = _fileSystem.File.ReadAllText(project.Value);
var tokens = contents.Split(':');
if (tokens.Length != 3)
{
throw new ToolPackageException(LocalizableStrings.ToolInstallationRestoreFailed);
}
packageId = tokens[0];
packageVersion = tokens[1];
targetFramework = tokens[2];
}
catch (IOException)
{
throw new ToolPackageException(LocalizableStrings.ToolInstallationRestoreFailed);
}
if (string.IsNullOrEmpty(packageId))
{
throw new ToolPackageException(LocalizableStrings.ToolInstallationRestoreFailed);
}
var feedPackage = GetPackage(
packageId,
packageVersion,
nugetConfig,
source);
packageVersion = feedPackage.Version;
targetFramework = string.IsNullOrEmpty(targetFramework) ? "targetFramework" : targetFramework;
var fakeExecutableSubDirectory = Path.Combine(
packageId,
packageVersion,
"tools",
targetFramework,
"any");
var fakeExecutablePath = Path.Combine(fakeExecutableSubDirectory, FakeEntrypointName);
_fileSystem.Directory.CreateDirectory(Path.Combine(assetJsonOutput.Value, fakeExecutableSubDirectory));
_fileSystem.File.CreateEmptyFile(Path.Combine(assetJsonOutput.Value, fakeExecutablePath));
_fileSystem.File.WriteAllText(
assetJsonOutput.WithFile("project.assets.json").Value,
fakeExecutablePath);
}
private MockFeedPackage GetPackage(
string packageId,
string packageVersion = null,
FilePath? nugetConfig = null,
string source = null)
{
var package = _feeds
.Where(f => {
if (nugetConfig != null)
{
return f.Type == MockFeedType.ExplicitNugetConfig && f.Uri == nugetConfig.Value.Value;
}
if (source != null)
{
return f.Type == MockFeedType.Source && f.Uri == source;
}
return true;
})
.SelectMany(f => f.Packages)
.Where(p => MatchPackageVersion(p, packageId, packageVersion)).OrderByDescending(p => p.Version)
.FirstOrDefault();
if (package == null)
{
if (_reporter != null)
{
_reporter.WriteLine($"Error: failed to restore package {packageId}.");
}
throw new ToolPackageException(LocalizableStrings.ToolInstallationRestoreFailed);
}
return package;
}
private static bool MatchPackageVersion(MockFeedPackage p, string packageId, string packageVersion)
{
if (string.IsNullOrEmpty(packageVersion))
{
return p.PackageId == packageId;
}
return p.PackageId == packageId && p.Version == packageVersion;
}
}
}