2017-11-22 04:10:06 +00:00
|
|
|
// 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;
|
2018-01-24 18:16:27 +00:00
|
|
|
using System.Collections.Generic;
|
2017-11-22 04:10:06 +00:00
|
|
|
using System.IO;
|
2018-01-11 21:54:02 +00:00
|
|
|
using System.Reflection;
|
2018-02-06 21:38:06 +00:00
|
|
|
using System.Transactions;
|
2017-11-22 04:10:06 +00:00
|
|
|
using FluentAssertions;
|
|
|
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
|
|
|
using Microsoft.Extensions.EnvironmentAbstractions;
|
|
|
|
using Microsoft.DotNet.Cli;
|
2018-01-18 03:26:52 +00:00
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2018-01-24 18:16:27 +00:00
|
|
|
using Microsoft.DotNet.Tools;
|
2017-12-04 22:13:24 +00:00
|
|
|
using Microsoft.DotNet.Tools.Install.Tool;
|
2017-11-22 04:10:06 +00:00
|
|
|
using Xunit;
|
2018-01-24 18:16:27 +00:00
|
|
|
using Microsoft.DotNet.Tools.Tests.ComponentMocks;
|
2017-11-22 04:10:06 +00:00
|
|
|
|
2017-12-04 22:13:24 +00:00
|
|
|
namespace Microsoft.DotNet.ToolPackage.Tests
|
2017-11-22 04:10:06 +00:00
|
|
|
{
|
|
|
|
public class ToolPackageObtainerTests : TestBase
|
|
|
|
{
|
|
|
|
[Fact]
|
2018-01-20 01:15:34 +00:00
|
|
|
public void GivenNoFeedItThrows()
|
2017-11-22 04:10:06 +00:00
|
|
|
{
|
2018-01-18 03:26:52 +00:00
|
|
|
var reporter = new BufferedReporter();
|
2017-11-22 04:10:06 +00:00
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
|
2018-01-20 01:15:34 +00:00
|
|
|
ToolPackageObtainer packageObtainer =
|
2018-01-24 18:16:27 +00:00
|
|
|
new ToolPackageObtainer(
|
|
|
|
new DirectoryPath(toolsPath),
|
|
|
|
new DirectoryPath("no such path"),
|
|
|
|
GetUniqueTempProjectPathEachTest,
|
|
|
|
new Lazy<string>(),
|
2018-01-18 03:26:52 +00:00
|
|
|
new ProjectRestorer(reporter));
|
2018-01-20 01:15:34 +00:00
|
|
|
|
|
|
|
Action a = () => packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
packageVersion: TestPackageVersion,
|
|
|
|
targetframework: _testTargetframework);
|
|
|
|
|
|
|
|
a.ShouldThrow<PackageObtainException>();
|
2018-01-18 03:26:52 +00:00
|
|
|
|
|
|
|
reporter.Lines.Count.Should().Be(1);
|
|
|
|
reporter.Lines[0].Should().Contain(TestPackageId);
|
2018-01-20 01:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GivenOfflineFeedWhenCallItCanDownloadThePackage()
|
|
|
|
{
|
2018-01-18 03:26:52 +00:00
|
|
|
var reporter = new BufferedReporter();
|
2018-01-20 01:15:34 +00:00
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
|
|
|
|
ToolPackageObtainer packageObtainer =
|
|
|
|
new ToolPackageObtainer(
|
|
|
|
toolsPath: new DirectoryPath(toolsPath),
|
|
|
|
offlineFeedPath: new DirectoryPath(GetTestLocalFeedPath()),
|
|
|
|
getTempProjectPath: GetUniqueTempProjectPathEachTest,
|
|
|
|
bundledTargetFrameworkMoniker: new Lazy<string>(),
|
2018-01-18 03:26:52 +00:00
|
|
|
projectRestorer: new ProjectRestorer(reporter));
|
2018-01-20 01:15:34 +00:00
|
|
|
|
2018-02-06 21:38:06 +00:00
|
|
|
ToolConfigurationAndExecutablePath toolConfigurationAndExecutablePath = packageObtainer.ObtainAndReturnExecutablePath(
|
2017-11-22 04:10:06 +00:00
|
|
|
packageId: TestPackageId,
|
|
|
|
packageVersion: TestPackageVersion,
|
|
|
|
targetframework: _testTargetframework);
|
|
|
|
|
2018-01-18 03:26:52 +00:00
|
|
|
reporter.Lines.Should().BeEmpty();
|
|
|
|
|
2018-01-20 01:15:34 +00:00
|
|
|
var executable = toolConfigurationAndExecutablePath
|
|
|
|
.Executable;
|
|
|
|
|
|
|
|
File.Exists(executable.Value)
|
|
|
|
.Should()
|
|
|
|
.BeTrue(executable + " should have the executable");
|
|
|
|
|
2018-01-24 18:16:27 +00:00
|
|
|
executable.Value.Should().NotContain(GetTestLocalFeedPath(), "Executable should not be still in fallbackfolder");
|
|
|
|
executable.Value.Should().Contain(toolsPath, "Executable should be copied to tools Path");
|
|
|
|
|
|
|
|
File.Delete(executable.Value);
|
2018-01-20 01:15:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 18:16:27 +00:00
|
|
|
[Theory]
|
|
|
|
[InlineData(false)]
|
|
|
|
[InlineData(true)]
|
|
|
|
public void GivenNugetConfigAndPackageNameAndVersionAndTargetFrameworkWhenCallItCanDownloadThePackage(
|
|
|
|
bool testMockBehaviorIsInSync)
|
|
|
|
{
|
2018-01-18 03:26:52 +00:00
|
|
|
var reporter = new BufferedReporter();
|
2018-01-24 18:16:27 +00:00
|
|
|
FilePath nugetConfigPath = WriteNugetConfigFileToPointToTheFeed();
|
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
|
|
|
|
var packageObtainer =
|
2018-01-18 03:26:52 +00:00
|
|
|
ConstructDefaultPackageObtainer(toolsPath, reporter, testMockBehaviorIsInSync, nugetConfigPath.Value);
|
2018-01-24 18:16:27 +00:00
|
|
|
|
|
|
|
ToolConfigurationAndExecutablePath toolConfigurationAndExecutablePath
|
|
|
|
= packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
packageVersion: TestPackageVersion,
|
|
|
|
nugetconfig: nugetConfigPath,
|
|
|
|
targetframework: _testTargetframework);
|
|
|
|
|
2018-01-18 03:26:52 +00:00
|
|
|
reporter.Lines.Should().BeEmpty();
|
|
|
|
|
2018-01-24 18:16:27 +00:00
|
|
|
FilePath executable = toolConfigurationAndExecutablePath.Executable;
|
|
|
|
File.Exists(executable.Value)
|
|
|
|
.Should()
|
|
|
|
.BeTrue(executable + " should have the executable");
|
|
|
|
|
|
|
|
File.Delete(executable.Value);
|
|
|
|
}
|
|
|
|
|
2018-02-06 21:38:06 +00:00
|
|
|
[Theory]
|
|
|
|
[InlineData(false)]
|
|
|
|
[InlineData(true)]
|
|
|
|
public void GivenNugetConfigAndPackageNameAndVersionAndTargetFrameworkWhenCallItCanDownloadThePackageInTransaction(
|
|
|
|
bool testMockBehaviorIsInSync)
|
|
|
|
{
|
|
|
|
var reporter = new BufferedReporter();
|
|
|
|
FilePath nugetConfigPath = WriteNugetConfigFileToPointToTheFeed();
|
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
|
|
|
|
var packageObtainer =
|
|
|
|
ConstructDefaultPackageObtainer(toolsPath, reporter, testMockBehaviorIsInSync, nugetConfigPath.Value);
|
|
|
|
|
|
|
|
ToolConfigurationAndExecutablePath toolConfigurationAndExecutablePath;
|
|
|
|
|
|
|
|
using (var transactionScope = new TransactionScope())
|
|
|
|
{
|
|
|
|
toolConfigurationAndExecutablePath
|
|
|
|
= packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
packageVersion: TestPackageVersion,
|
|
|
|
nugetconfig: nugetConfigPath,
|
|
|
|
targetframework: _testTargetframework);
|
|
|
|
|
|
|
|
transactionScope.Complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
reporter.Lines.Should().BeEmpty();
|
|
|
|
|
|
|
|
FilePath executable = toolConfigurationAndExecutablePath.Executable;
|
|
|
|
File.Exists(executable.Value)
|
|
|
|
.Should()
|
|
|
|
.BeTrue(executable + " should have the executable");
|
|
|
|
|
|
|
|
File.Delete(executable.Value);
|
|
|
|
}
|
|
|
|
|
2018-01-24 18:16:27 +00:00
|
|
|
[Theory]
|
|
|
|
[InlineData(false)]
|
|
|
|
[InlineData(true)]
|
|
|
|
public void GivenNugetConfigAndPackageNameAndVersionAndTargetFrameworkWhenCallItCreateAssetFile(
|
|
|
|
bool testMockBehaviorIsInSync)
|
2018-01-20 01:15:34 +00:00
|
|
|
{
|
2018-01-18 03:26:52 +00:00
|
|
|
var reporter = new BufferedReporter();
|
2018-01-20 01:15:34 +00:00
|
|
|
var nugetConfigPath = WriteNugetConfigFileToPointToTheFeed();
|
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
|
|
|
|
var packageObtainer =
|
2018-01-18 03:26:52 +00:00
|
|
|
ConstructDefaultPackageObtainer(toolsPath, reporter, testMockBehaviorIsInSync, nugetConfigPath.Value);
|
2018-01-20 01:15:34 +00:00
|
|
|
|
2018-02-06 21:38:06 +00:00
|
|
|
ToolConfigurationAndExecutablePath toolConfigurationAndExecutablePath =
|
2018-01-24 18:16:27 +00:00
|
|
|
packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
packageVersion: TestPackageVersion,
|
|
|
|
nugetconfig: nugetConfigPath,
|
|
|
|
targetframework: _testTargetframework);
|
2018-01-20 01:15:34 +00:00
|
|
|
|
2018-01-18 03:26:52 +00:00
|
|
|
reporter.Lines.Should().BeEmpty();
|
|
|
|
|
2018-01-20 01:15:34 +00:00
|
|
|
/*
|
|
|
|
From mytool.dll to project.assets.json
|
|
|
|
.dotnet/.tools/packageid/version/packageid/version/mytool.dll
|
|
|
|
/dependency1 package id/
|
|
|
|
/dependency2 package id/
|
|
|
|
/project.assets.json
|
|
|
|
*/
|
2018-02-06 21:38:06 +00:00
|
|
|
var assetJsonPath = toolConfigurationAndExecutablePath
|
2018-01-20 01:15:34 +00:00
|
|
|
.Executable
|
|
|
|
.GetDirectoryPath()
|
2017-11-22 04:10:06 +00:00
|
|
|
.GetParentPath()
|
|
|
|
.GetParentPath()
|
|
|
|
.GetParentPath()
|
|
|
|
.GetParentPath()
|
2017-12-04 22:13:24 +00:00
|
|
|
.GetParentPath()
|
2017-11-22 04:10:06 +00:00
|
|
|
.WithFile("project.assets.json").Value;
|
|
|
|
|
|
|
|
File.Exists(assetJsonPath)
|
|
|
|
.Should()
|
|
|
|
.BeTrue(assetJsonPath + " should be created");
|
2018-01-24 18:16:27 +00:00
|
|
|
|
|
|
|
File.Delete(assetJsonPath);
|
2017-11-22 04:10:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 18:16:27 +00:00
|
|
|
[Theory]
|
|
|
|
[InlineData(false)]
|
|
|
|
[InlineData(true)]
|
|
|
|
public void GivenAllButNoNugetConfigFilePathItCanDownloadThePackage(bool testMockBehaviorIsInSync)
|
2017-11-22 04:10:06 +00:00
|
|
|
{
|
2018-01-18 03:26:52 +00:00
|
|
|
var reporter = new BufferedReporter();
|
2017-11-22 04:10:06 +00:00
|
|
|
var uniqueTempProjectPath = GetUniqueTempProjectPathEachTest();
|
|
|
|
var tempProjectDirectory = uniqueTempProjectPath.GetDirectoryPath();
|
|
|
|
var nugetConfigPath = WriteNugetConfigFileToPointToTheFeed();
|
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
|
|
|
|
Directory.CreateDirectory(tempProjectDirectory.Value);
|
|
|
|
|
|
|
|
/*
|
2017-12-04 22:13:24 +00:00
|
|
|
* In test, we don't want NuGet to keep look up, so we point current directory to nugetconfig.
|
2017-11-22 04:10:06 +00:00
|
|
|
*/
|
2017-12-04 22:13:24 +00:00
|
|
|
|
|
|
|
Directory.SetCurrentDirectory(nugetConfigPath.GetDirectoryPath().Value);
|
2017-11-22 04:10:06 +00:00
|
|
|
|
2018-01-24 18:16:27 +00:00
|
|
|
IToolPackageObtainer packageObtainer;
|
|
|
|
if (testMockBehaviorIsInSync)
|
|
|
|
{
|
2018-02-06 21:38:06 +00:00
|
|
|
packageObtainer = new ToolPackageObtainerMock(toolsPath: toolsPath);
|
2018-01-24 18:16:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
packageObtainer = new ToolPackageObtainer(
|
2017-11-22 04:10:06 +00:00
|
|
|
new DirectoryPath(toolsPath),
|
2018-01-20 01:15:34 +00:00
|
|
|
new DirectoryPath("no such path"),
|
2017-11-22 04:10:06 +00:00
|
|
|
() => uniqueTempProjectPath,
|
|
|
|
new Lazy<string>(),
|
2018-01-18 03:26:52 +00:00
|
|
|
new ProjectRestorer(reporter));
|
2018-01-24 18:16:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 01:15:34 +00:00
|
|
|
ToolConfigurationAndExecutablePath toolConfigurationAndExecutablePath =
|
|
|
|
packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
packageVersion: TestPackageVersion,
|
|
|
|
targetframework: _testTargetframework);
|
2017-11-22 04:10:06 +00:00
|
|
|
|
2018-01-18 03:26:52 +00:00
|
|
|
reporter.Lines.Should().BeEmpty();
|
|
|
|
|
2018-01-20 01:15:34 +00:00
|
|
|
var executable = toolConfigurationAndExecutablePath.Executable;
|
2017-11-22 04:10:06 +00:00
|
|
|
|
|
|
|
File.Exists(executable.Value)
|
|
|
|
.Should()
|
|
|
|
.BeTrue(executable + " should have the executable");
|
2018-01-24 18:16:27 +00:00
|
|
|
|
|
|
|
File.Delete(executable.Value);
|
2017-11-22 04:10:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 18:16:27 +00:00
|
|
|
[Theory]
|
|
|
|
[InlineData(false)]
|
|
|
|
[InlineData(true)]
|
|
|
|
public void GivenAllButNoPackageVersionItCanDownloadThePackage(bool testMockBehaviorIsInSync)
|
2017-11-22 04:10:06 +00:00
|
|
|
{
|
2018-01-18 03:26:52 +00:00
|
|
|
var reporter = new BufferedReporter();
|
2018-02-06 21:38:06 +00:00
|
|
|
FilePath nugetConfigPath = WriteNugetConfigFileToPointToTheFeed();
|
2017-11-22 04:10:06 +00:00
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
|
|
|
|
var packageObtainer =
|
2018-01-18 03:26:52 +00:00
|
|
|
ConstructDefaultPackageObtainer(toolsPath, reporter, testMockBehaviorIsInSync, nugetConfigPath.Value);
|
2018-01-24 18:16:27 +00:00
|
|
|
|
|
|
|
ToolConfigurationAndExecutablePath toolConfigurationAndExecutablePath =
|
|
|
|
packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
nugetconfig: nugetConfigPath,
|
|
|
|
targetframework: _testTargetframework);
|
2017-11-22 04:10:06 +00:00
|
|
|
|
2018-01-18 03:26:52 +00:00
|
|
|
reporter.Lines.Should().BeEmpty();
|
|
|
|
|
2018-01-20 01:15:34 +00:00
|
|
|
var executable = toolConfigurationAndExecutablePath.Executable;
|
2017-11-22 04:10:06 +00:00
|
|
|
|
|
|
|
File.Exists(executable.Value)
|
|
|
|
.Should()
|
|
|
|
.BeTrue(executable + " should have the executable");
|
2018-01-24 18:16:27 +00:00
|
|
|
|
|
|
|
File.Delete(executable.Value);
|
2017-11-22 04:10:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 18:16:27 +00:00
|
|
|
[Theory]
|
|
|
|
[InlineData(false)]
|
|
|
|
[InlineData(true)]
|
|
|
|
public void GivenAllButNoTargetFrameworkItCanDownloadThePackage(bool testMockBehaviorIsInSync)
|
2017-11-22 04:10:06 +00:00
|
|
|
{
|
2018-01-18 03:26:52 +00:00
|
|
|
var reporter = new BufferedReporter();
|
2017-11-22 04:10:06 +00:00
|
|
|
var nugetConfigPath = WriteNugetConfigFileToPointToTheFeed();
|
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
|
2018-01-24 18:16:27 +00:00
|
|
|
IToolPackageObtainer packageObtainer;
|
|
|
|
if (testMockBehaviorIsInSync)
|
|
|
|
{
|
|
|
|
packageObtainer = new ToolPackageObtainerMock(additionalFeeds:
|
|
|
|
new List<MockFeed>
|
|
|
|
{
|
|
|
|
new MockFeed
|
|
|
|
{
|
|
|
|
Type = MockFeedType.ExplicitNugetConfig,
|
|
|
|
Uri = nugetConfigPath.Value,
|
|
|
|
Packages = new List<MockFeedPackage>
|
|
|
|
{
|
|
|
|
new MockFeedPackage
|
|
|
|
{
|
2018-01-18 03:26:52 +00:00
|
|
|
PackageId = TestPackageId,
|
2018-01-24 18:16:27 +00:00
|
|
|
Version = "1.0.4"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-06 21:38:06 +00:00
|
|
|
},
|
|
|
|
toolsPath: toolsPath);
|
2018-01-24 18:16:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
packageObtainer = new ToolPackageObtainer(
|
2017-11-22 04:10:06 +00:00
|
|
|
new DirectoryPath(toolsPath),
|
2018-01-20 01:15:34 +00:00
|
|
|
new DirectoryPath("no such path"),
|
2017-11-22 04:10:06 +00:00
|
|
|
GetUniqueTempProjectPathEachTest,
|
|
|
|
new Lazy<string>(() => BundledTargetFramework.GetTargetFrameworkMoniker()),
|
2018-01-18 03:26:52 +00:00
|
|
|
new ProjectRestorer(reporter));
|
2018-01-24 18:16:27 +00:00
|
|
|
}
|
2018-01-20 01:15:34 +00:00
|
|
|
ToolConfigurationAndExecutablePath toolConfigurationAndExecutablePath =
|
2017-11-22 04:10:06 +00:00
|
|
|
packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
packageVersion: TestPackageVersion,
|
|
|
|
nugetconfig: nugetConfigPath);
|
|
|
|
|
2018-01-18 03:26:52 +00:00
|
|
|
reporter.Lines.Should().BeEmpty();
|
|
|
|
|
2018-01-20 01:15:34 +00:00
|
|
|
var executable = toolConfigurationAndExecutablePath.Executable;
|
2017-11-22 04:10:06 +00:00
|
|
|
|
|
|
|
File.Exists(executable.Value)
|
|
|
|
.Should()
|
|
|
|
.BeTrue(executable + " should have the executable");
|
2018-01-24 18:16:27 +00:00
|
|
|
|
|
|
|
File.Delete(executable.Value);
|
2017-11-22 04:10:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 18:16:27 +00:00
|
|
|
[Theory]
|
|
|
|
[InlineData(false)]
|
|
|
|
[InlineData(true)]
|
|
|
|
public void GivenNonExistentNugetConfigFileItThrows(bool testMockBehaviorIsInSync)
|
2017-12-04 22:13:24 +00:00
|
|
|
{
|
2018-01-18 03:26:52 +00:00
|
|
|
var reporter = new BufferedReporter();
|
2017-12-04 22:13:24 +00:00
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
|
|
|
|
var packageObtainer =
|
2018-01-18 03:26:52 +00:00
|
|
|
ConstructDefaultPackageObtainer(toolsPath, reporter, testMockBehaviorIsInSync);
|
2018-01-24 18:16:27 +00:00
|
|
|
|
|
|
|
var nonExistNugetConfigFile = new FilePath("NonExistent.file");
|
|
|
|
Action a = () =>
|
|
|
|
{
|
2018-02-06 21:38:06 +00:00
|
|
|
ToolConfigurationAndExecutablePath toolConfigurationAndExecutablePath =
|
2018-01-24 18:16:27 +00:00
|
|
|
packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
packageVersion: TestPackageVersion,
|
|
|
|
nugetconfig: nonExistNugetConfigFile,
|
|
|
|
targetframework: _testTargetframework);
|
|
|
|
};
|
2017-12-04 22:13:24 +00:00
|
|
|
|
|
|
|
a.ShouldThrow<PackageObtainException>()
|
|
|
|
.And
|
2018-01-24 18:16:27 +00:00
|
|
|
.Message.Should().Contain(string.Format(
|
|
|
|
CommonLocalizableStrings.NuGetConfigurationFileDoesNotExist,
|
|
|
|
Path.GetFullPath(nonExistNugetConfigFile.Value)));
|
2018-01-18 03:26:52 +00:00
|
|
|
|
|
|
|
reporter.Lines.Should().BeEmpty();
|
2017-12-04 22:13:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 18:16:27 +00:00
|
|
|
[Theory]
|
|
|
|
[InlineData(false)]
|
|
|
|
[InlineData(true)]
|
|
|
|
public void GivenASourceItCanObtainThePackageFromThatSource(bool testMockBehaviorIsInSync)
|
2018-01-11 21:54:02 +00:00
|
|
|
{
|
2018-01-18 03:26:52 +00:00
|
|
|
var reporter = new BufferedReporter();
|
2018-01-11 21:54:02 +00:00
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
|
2018-02-06 21:38:06 +00:00
|
|
|
var packageObtainer = ConstructDefaultPackageObtainer(
|
|
|
|
toolsPath,
|
|
|
|
reporter,
|
|
|
|
testMockBehaviorIsInSync: testMockBehaviorIsInSync,
|
|
|
|
addSourceFeedWithFilePath: GetTestLocalFeedPath());
|
|
|
|
ToolConfigurationAndExecutablePath toolConfigurationAndExecutablePath =
|
|
|
|
packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
packageVersion: TestPackageVersion,
|
|
|
|
targetframework: _testTargetframework,
|
|
|
|
source: GetTestLocalFeedPath());
|
2018-01-11 21:54:02 +00:00
|
|
|
|
2018-01-18 03:26:52 +00:00
|
|
|
reporter.Lines.Should().BeEmpty();
|
|
|
|
|
2018-02-06 21:38:06 +00:00
|
|
|
var executable = toolConfigurationAndExecutablePath.Executable;
|
2018-01-11 21:54:02 +00:00
|
|
|
|
|
|
|
File.Exists(executable.Value)
|
|
|
|
.Should()
|
|
|
|
.BeTrue(executable + " should have the executable");
|
2018-01-24 18:16:27 +00:00
|
|
|
|
|
|
|
File.Delete(executable.Value);
|
2018-01-11 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 21:38:06 +00:00
|
|
|
[Theory]
|
|
|
|
[InlineData(false)]
|
|
|
|
[InlineData(true)]
|
|
|
|
public void GivenFailedRestoreItCanRollBack(bool testMockBehaviorIsInSync)
|
|
|
|
{
|
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
|
|
|
|
var reporter = new BufferedReporter();
|
|
|
|
var packageObtainer = ConstructDefaultPackageObtainer(toolsPath, reporter, testMockBehaviorIsInSync);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
using (var t = new TransactionScope())
|
|
|
|
{
|
|
|
|
packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: "non exist package id",
|
|
|
|
packageVersion: TestPackageVersion,
|
|
|
|
targetframework: _testTargetframework);
|
|
|
|
|
|
|
|
t.Complete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (PackageObtainException)
|
|
|
|
{
|
|
|
|
// catch the intent error
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertRollBack(toolsPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
[InlineData(false)]
|
|
|
|
[InlineData(true)]
|
|
|
|
public void GiveSucessRestoreButFailedOnNextStepItCanRollBack(bool testMockBehaviorIsInSync)
|
|
|
|
{
|
|
|
|
FilePath nugetConfigPath = WriteNugetConfigFileToPointToTheFeed();
|
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
var reporter = new BufferedReporter();
|
|
|
|
var packageObtainer = ConstructDefaultPackageObtainer(toolsPath, reporter, testMockBehaviorIsInSync);
|
|
|
|
|
|
|
|
void FailedStepAfterSuccessRestore() => throw new GracefulException("simulated error");
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
using (var t = new TransactionScope())
|
|
|
|
{
|
|
|
|
ToolConfigurationAndExecutablePath obtainAndReturnExecutablePathtransactional
|
|
|
|
= packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
packageVersion: TestPackageVersion,
|
|
|
|
targetframework: _testTargetframework);
|
|
|
|
|
|
|
|
FailedStepAfterSuccessRestore();
|
|
|
|
t.Complete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (GracefulException)
|
|
|
|
{
|
|
|
|
// catch the simulated error
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertRollBack(toolsPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
[InlineData(false)]
|
|
|
|
[InlineData(true)]
|
|
|
|
public void GivenAllButNoPackageVersionAndInvokeTwiceItShouldNotThrow(bool testMockBehaviorIsInSync)
|
|
|
|
{
|
|
|
|
var reporter = new BufferedReporter();
|
|
|
|
var nugetConfigPath = WriteNugetConfigFileToPointToTheFeed();
|
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
|
|
|
|
var packageObtainer =
|
|
|
|
ConstructDefaultPackageObtainer(toolsPath, reporter, testMockBehaviorIsInSync, nugetConfigPath.Value);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
using (var t = new TransactionScope())
|
|
|
|
{
|
|
|
|
packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
packageVersion: TestPackageVersion,
|
|
|
|
targetframework: _testTargetframework);
|
|
|
|
|
|
|
|
packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
packageVersion: TestPackageVersion,
|
|
|
|
targetframework: _testTargetframework);
|
|
|
|
|
|
|
|
t.Complete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (PackageObtainException)
|
|
|
|
{
|
|
|
|
// catch the simulated error
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertRollBack(toolsPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
[InlineData(false)]
|
|
|
|
[InlineData(true)]
|
|
|
|
public void GivenAllButNoPackageVersionAndInvokeTwiceInTransactionItShouldRollback(bool testMockBehaviorIsInSync)
|
|
|
|
{
|
|
|
|
var reporter = new BufferedReporter();
|
|
|
|
var nugetConfigPath = WriteNugetConfigFileToPointToTheFeed();
|
|
|
|
var toolsPath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName());
|
|
|
|
|
|
|
|
var packageObtainer =
|
|
|
|
ConstructDefaultPackageObtainer(toolsPath, reporter, testMockBehaviorIsInSync, nugetConfigPath.Value);
|
|
|
|
|
|
|
|
packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
nugetconfig: nugetConfigPath,
|
|
|
|
targetframework: _testTargetframework);
|
|
|
|
|
|
|
|
reporter.Lines.Should().BeEmpty();
|
|
|
|
|
|
|
|
Action secondCall = () => packageObtainer.ObtainAndReturnExecutablePath(
|
|
|
|
packageId: TestPackageId,
|
|
|
|
nugetconfig: nugetConfigPath,
|
|
|
|
targetframework: _testTargetframework);
|
|
|
|
|
|
|
|
reporter.Lines.Should().BeEmpty();
|
|
|
|
|
|
|
|
secondCall.ShouldThrow<PackageObtainException>();
|
|
|
|
|
|
|
|
Directory.Exists(Path.Combine(toolsPath, TestPackageId))
|
|
|
|
.Should().BeTrue("The result of first one is still here");
|
|
|
|
|
|
|
|
Directory.GetDirectories(Path.Combine(toolsPath, ".stage"))
|
|
|
|
.Should().BeEmpty("nothing in stage folder, already rolled back");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void AssertRollBack(string toolsPath)
|
|
|
|
{
|
|
|
|
if (!Directory.Exists(toolsPath))
|
|
|
|
{
|
|
|
|
return; // nothing at all
|
|
|
|
}
|
|
|
|
|
|
|
|
Directory.GetFiles(toolsPath).Should().BeEmpty();
|
|
|
|
Directory.GetDirectories(toolsPath)
|
|
|
|
.Should().NotContain(d => !new DirectoryInfo(d).Name.Equals(".stage"),
|
|
|
|
"no broken folder, exclude stage folder");
|
|
|
|
|
|
|
|
Directory.GetDirectories(Path.Combine(toolsPath, ".stage"))
|
|
|
|
.Should().BeEmpty("nothing in stage folder");
|
|
|
|
}
|
|
|
|
|
2017-11-22 04:10:06 +00:00
|
|
|
private static readonly Func<FilePath> GetUniqueTempProjectPathEachTest = () =>
|
|
|
|
{
|
|
|
|
var tempProjectDirectory =
|
|
|
|
new DirectoryPath(Path.GetTempPath()).WithSubDirectories(Path.GetRandomFileName());
|
|
|
|
var tempProjectPath =
|
|
|
|
tempProjectDirectory.WithFile(Path.GetRandomFileName() + ".csproj");
|
|
|
|
return tempProjectPath;
|
|
|
|
};
|
|
|
|
|
2018-01-24 18:16:27 +00:00
|
|
|
private static IToolPackageObtainer ConstructDefaultPackageObtainer(
|
|
|
|
string toolsPath,
|
2018-01-18 03:26:52 +00:00
|
|
|
IReporter reporter,
|
2018-01-24 18:16:27 +00:00
|
|
|
bool testMockBehaviorIsInSync = false,
|
|
|
|
string addNugetConfigFeedWithFilePath = null,
|
|
|
|
string addSourceFeedWithFilePath = null)
|
2017-11-22 04:10:06 +00:00
|
|
|
{
|
2018-01-24 18:16:27 +00:00
|
|
|
if (testMockBehaviorIsInSync)
|
|
|
|
{
|
|
|
|
if (addNugetConfigFeedWithFilePath != null)
|
|
|
|
{
|
|
|
|
return new ToolPackageObtainerMock(additionalFeeds:
|
|
|
|
new List<MockFeed>
|
|
|
|
{
|
|
|
|
new MockFeed
|
|
|
|
{
|
|
|
|
Type = MockFeedType.ExplicitNugetConfig,
|
|
|
|
Uri = addNugetConfigFeedWithFilePath,
|
|
|
|
Packages = new List<MockFeedPackage>
|
|
|
|
{
|
|
|
|
new MockFeedPackage
|
|
|
|
{
|
2018-01-18 03:26:52 +00:00
|
|
|
PackageId = TestPackageId,
|
2018-01-24 18:16:27 +00:00
|
|
|
Version = "1.0.4"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-06 21:38:06 +00:00
|
|
|
}, toolsPath: toolsPath);
|
2018-01-24 18:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (addSourceFeedWithFilePath != null)
|
|
|
|
{
|
|
|
|
return new ToolPackageObtainerMock(additionalFeeds:
|
|
|
|
new List<MockFeed>
|
|
|
|
{
|
|
|
|
new MockFeed
|
|
|
|
{
|
2018-02-06 21:38:06 +00:00
|
|
|
Type = MockFeedType.Source,
|
2018-01-24 18:16:27 +00:00
|
|
|
Uri = addSourceFeedWithFilePath,
|
|
|
|
Packages = new List<MockFeedPackage>
|
|
|
|
{
|
|
|
|
new MockFeedPackage
|
|
|
|
{
|
2018-01-18 03:26:52 +00:00
|
|
|
PackageId = TestPackageId,
|
2018-01-24 18:16:27 +00:00
|
|
|
Version = "1.0.4"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-06 21:38:06 +00:00
|
|
|
},
|
|
|
|
toolsPath: toolsPath);
|
2018-01-24 18:16:27 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 21:38:06 +00:00
|
|
|
return new ToolPackageObtainerMock(toolsPath: toolsPath);
|
2018-01-24 18:16:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 04:10:06 +00:00
|
|
|
return new ToolPackageObtainer(
|
|
|
|
new DirectoryPath(toolsPath),
|
2018-01-20 01:15:34 +00:00
|
|
|
new DirectoryPath("no such path"),
|
2017-11-22 04:10:06 +00:00
|
|
|
GetUniqueTempProjectPathEachTest,
|
|
|
|
new Lazy<string>(),
|
2018-01-18 03:26:52 +00:00
|
|
|
new ProjectRestorer(reporter));
|
2017-11-22 04:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static FilePath WriteNugetConfigFileToPointToTheFeed()
|
|
|
|
{
|
2017-12-04 22:13:24 +00:00
|
|
|
var nugetConfigName = "nuget.config";
|
|
|
|
|
|
|
|
var tempPathForNugetConfigWithWhiteSpace =
|
|
|
|
Path.Combine(Path.GetTempPath(),
|
2018-01-24 18:16:27 +00:00
|
|
|
Path.GetRandomFileName() + " " + Path.GetRandomFileName());
|
2017-12-04 22:13:24 +00:00
|
|
|
Directory.CreateDirectory(tempPathForNugetConfigWithWhiteSpace);
|
|
|
|
|
2017-11-22 04:10:06 +00:00
|
|
|
NuGetConfig.Write(
|
2017-12-04 22:13:24 +00:00
|
|
|
directory: tempPathForNugetConfigWithWhiteSpace,
|
2017-11-22 04:10:06 +00:00
|
|
|
configname: nugetConfigName,
|
2018-01-20 01:15:34 +00:00
|
|
|
localFeedPath: GetTestLocalFeedPath());
|
|
|
|
|
2017-12-04 22:13:24 +00:00
|
|
|
return new FilePath(Path.GetFullPath(Path.Combine(tempPathForNugetConfigWithWhiteSpace, nugetConfigName)));
|
2017-11-22 04:10:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 01:15:34 +00:00
|
|
|
private static string GetTestLocalFeedPath() => Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "TestAssetLocalNugetFeed");
|
2017-11-22 04:10:06 +00:00
|
|
|
private readonly string _testTargetframework = BundledTargetFramework.GetTargetFrameworkMoniker();
|
|
|
|
private const string TestPackageVersion = "1.0.4";
|
|
|
|
private const string TestPackageId = "global.tool.console.demo";
|
|
|
|
}
|
|
|
|
}
|