dotnet-installer/test/Microsoft.DotNet.Tools.Tests.ComponentMocks/ToolPackageInstallerMock.cs
William Lee 9cc2b7cd2f
Change --source to --source-feed and make it additional (#8833)
Other than change source to source-feed and make it additional instead of exclusive. I changed source to be multiple. Because restore support multiple source https://github.com/Microsoft/dotnet/issues/361

As for mock. The offline feed and source feed is considered the same, so remove the category of “source”. I renamed source to “AdditionalFeed” because that is more accurate on implementation level.

Note:
NuGet feed don’t have order. Whichever responses the fastest, is the first.
No change on restore.

scripts/cli-test-env.sh change is due to mac 10.13 is finally added to RID graph. And it is “considered” one of the CLI supported RID
2018-03-19 22:22:45 -07:00

104 lines
4.5 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 System.Transactions;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.ToolPackage;
using Microsoft.DotNet.Tools;
using Microsoft.Extensions.EnvironmentAbstractions;
using NuGet.Versioning;
namespace Microsoft.DotNet.Tools.Tests.ComponentMocks
{
internal class ToolPackageInstallerMock : IToolPackageInstaller
{
private const string ProjectFileName = "TempProject.csproj";
private readonly IToolPackageStore _store;
private readonly IProjectRestorer _projectRestorer;
private readonly IFileSystem _fileSystem;
private readonly Action _installCallback;
public ToolPackageInstallerMock(
IFileSystem fileSystem,
IToolPackageStore store,
IProjectRestorer projectRestorer,
Action installCallback = null)
{
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
_store = store ?? throw new ArgumentNullException(nameof(store));
_projectRestorer = projectRestorer ?? throw new ArgumentNullException(nameof(projectRestorer));
_installCallback = installCallback;
}
public IToolPackage InstallPackage(PackageId packageId,
VersionRange versionRange = null,
string targetFramework = null,
FilePath? nugetConfig = null,
DirectoryPath? rootConfigDirectory = null,
string[] additionalFeeds = null,
string verbosity = null)
{
var packageRootDirectory = _store.GetRootPackageDirectory(packageId);
string rollbackDirectory = null;
return TransactionalAction.Run<IToolPackage>(
action: () => {
var stageDirectory = _store.GetRandomStagingDirectory();
_fileSystem.Directory.CreateDirectory(stageDirectory.Value);
rollbackDirectory = stageDirectory.Value;
var tempProject = new FilePath(Path.Combine(stageDirectory.Value, ProjectFileName));
// Write a fake project with the requested package id, version, and framework
_fileSystem.File.WriteAllText(
tempProject.Value,
$"{packageId}:{versionRange?.ToString("S", new VersionRangeFormatter()) ?? "*"}:{targetFramework}");
// Perform a restore on the fake project
_projectRestorer.Restore(
tempProject,
stageDirectory,
nugetConfig,
verbosity);
if (_installCallback != null)
{
_installCallback();
}
var version = _store.GetStagedPackageVersion(stageDirectory, packageId);
var packageDirectory = _store.GetPackageDirectory(packageId, version);
if (_fileSystem.Directory.Exists(packageDirectory.Value))
{
throw new ToolPackageException(
string.Format(
CommonLocalizableStrings.ToolPackageConflictPackageId,
packageId,
version.ToNormalizedString()));
}
_fileSystem.Directory.CreateDirectory(packageRootDirectory.Value);
_fileSystem.Directory.Move(stageDirectory.Value, packageDirectory.Value);
rollbackDirectory = packageDirectory.Value;
return new ToolPackageMock(_fileSystem, packageId, version, packageDirectory);
},
rollback: () => {
if (rollbackDirectory != null && _fileSystem.Directory.Exists(rollbackDirectory))
{
_fileSystem.Directory.Delete(rollbackDirectory, true);
}
if (_fileSystem.Directory.Exists(packageRootDirectory.Value) &&
!_fileSystem.Directory.EnumerateFileSystemEntries(packageRootDirectory.Value).Any())
{
_fileSystem.Directory.Delete(packageRootDirectory.Value, false);
}
});
}
}
}