dotnet-installer/test/Microsoft.DotNet.Cli.Utils.Tests/GivenAProjectToolsCommandResolver.cs
Livar Cunha 3a9525b5ac Merge branch 'rel/1.0.1' into merge_rel101_into_master
* rel/1.0.1: (66 commits)
  Update LZMA license with correct text
  Bump to 2.0.0-rc5-61427-04
  Remove duplicate installer suffix
  Add license text to LZMA SDK license notice
  Updating the SDK to 1.0.0-alpha-20170224-6
  Updating both platform abstractions and dependency model to 1.0.3.
  Bump Roslyn to 2.0.0-rc5-61424-02
  Update Stage0 to use the latest build.
  Update README with new distros.
  Back porting #5597 into rel/1.0.0
  Fixing the exclude pattern used by the Migration to exclude WEB SDK globs.
  Remove RID from test package creation
  Disable migrate and publish web app with content because CI does not have NPM
  Adding an E2E test for pack with content during migration.
  Fixing a failing test and adding a few more E2E tests around binplace content for migrated projects.
  Fix debian_config.json on ubuntu16.10
  Updating publish, pack and build of content to use None with Never/false/Never in their metadata for excluded items.
  Intermediate commit to get a WIP PR out. This adds the None Update with CopyToOutputDirectory set to Never.
  Switching the CopyToOutput for build and publish and the file for pack to use None Update instead of include. Also, fixed the exclude patterns for web apps.
  Do not migrate Content that is already included in the Web SDK for web apps.
  ...
2017-03-01 20:50:42 -08:00

352 lines
12 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.IO;
using System.Linq;
using FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.TestFramework;
using Microsoft.DotNet.Tools.Test.Utilities;
using NuGet.Frameworks;
using NuGet.ProjectModel;
using NuGet.Versioning;
using Xunit;
namespace Microsoft.DotNet.Tests
{
public class GivenAProjectToolsCommandResolver : TestBase
{
private static readonly NuGetFramework s_toolPackageFramework =
FrameworkConstants.CommonFrameworks.NetCoreApp11;
private const string TestProjectName = "AppWithToolDependency";
[Fact]
public void ItReturnsNullWhenCommandNameIsNull()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = null,
CommandArguments = new string[] { "" },
ProjectDirectory = "/some/directory"
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void ItReturnsNullWhenProjectDirectoryIsNull()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "command",
CommandArguments = new string[] { "" },
ProjectDirectory = null
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void ItReturnsNullWhenProjectDirectoryDoesNotContainAProjectFile()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var projectDirectory = TestAssets.CreateTestDirectory();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "command",
CommandArguments = new string[] { "" },
ProjectDirectory = projectDirectory.Root.FullName
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void ItReturnsNullWhenCommandNameDoesNotExistInProjectTools()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var testInstance = TestAssets.Get(TestProjectName)
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "nonexistent-command",
CommandArguments = null,
ProjectDirectory = testInstance.Root.FullName
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void ItReturnsACommandSpecWithDOTNETAsFileNameAndCommandNameInArgsWhenCommandNameExistsInProjectTools()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var testInstance = TestAssets.Get(TestProjectName)
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-portable",
CommandArguments = null,
ProjectDirectory = testInstance.Root.FullName
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileNameWithoutExtension(result.Path);
commandFile.Should().Be("dotnet");
result.Args.Should().Contain(commandResolverArguments.CommandName);
}
[Fact]
public void ItEscapesCommandArgumentsWhenReturningACommandSpec()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var testInstance = TestAssets.Get(TestProjectName)
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-portable",
CommandArguments = new[] { "arg with space" },
ProjectDirectory = testInstance.Root.FullName
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull("Because the command is a project tool dependency");
result.Args.Should().Contain("\"arg with space\"");
}
[Fact]
public void ItReturnsACommandSpecWithArgsContainingCommandPathWhenReturningACommandSpecAndCommandArgumentsAreNull()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var testInstance = TestAssets.Get(TestProjectName)
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-portable",
CommandArguments = null,
ProjectDirectory = testInstance.Root.FullName
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandPath = result.Args.Trim('"');
commandPath.Should().Contain("dotnet-portable.dll");
}
[Fact]
public void ItReturnsACommandSpecWithArgsContainingCommandPathWhenInvokingAToolReferencedWithADifferentCasing()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var testInstance = TestAssets.Get(TestProjectName)
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-prefercliruntime",
CommandArguments = null,
ProjectDirectory = testInstance.Root.FullName
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandPath = result.Args.Trim('"');
commandPath.Should().Contain("dotnet-prefercliruntime.dll");
}
[Fact]
public void ItWritesADepsJsonFileNextToTheLockfile()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var testInstance = TestAssets.Get(TestProjectName)
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-portable",
CommandArguments = null,
ProjectDirectory = testInstance.Root.FullName
};
var repoDirectoriesProvider = new RepoDirectoriesProvider();
var nugetPackagesRoot = repoDirectoriesProvider.NugetPackages;
var toolPathCalculator = new ToolPathCalculator(nugetPackagesRoot);
var lockFilePath = toolPathCalculator.GetLockFilePath(
"dotnet-portable",
new NuGetVersion("1.0.0"),
s_toolPackageFramework);
var directory = Path.GetDirectoryName(lockFilePath);
var depsJsonFile = Directory
.EnumerateFiles(directory)
.FirstOrDefault(p => Path.GetFileName(p).EndsWith(FileNameSuffixes.DepsJson));
if (depsJsonFile != null)
{
File.Delete(depsJsonFile);
}
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
new DirectoryInfo(directory)
.Should().HaveFilesMatching("*.deps.json", SearchOption.TopDirectoryOnly);
}
[Fact]
public void GenerateDepsJsonMethodDoesntOverwriteWhenDepsFileAlreadyExists()
{
var testInstance = TestAssets.Get(TestProjectName)
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles();
var repoDirectoriesProvider = new RepoDirectoriesProvider();
var nugetPackagesRoot = repoDirectoriesProvider.NugetPackages;
var toolPathCalculator = new ToolPathCalculator(nugetPackagesRoot);
var lockFilePath = toolPathCalculator.GetLockFilePath(
"dotnet-portable",
new NuGetVersion("1.0.0"),
s_toolPackageFramework);
var lockFile = new LockFileFormat().Read(lockFilePath);
var depsJsonFile = Path.Combine(
Path.GetDirectoryName(lockFilePath),
"dotnet-portable.deps.json");
if (File.Exists(depsJsonFile))
{
File.Delete(depsJsonFile);
}
File.WriteAllText(depsJsonFile, "temp");
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
projectToolsCommandResolver.GenerateDepsJsonFile(
lockFile,
s_toolPackageFramework,
depsJsonFile,
new SingleProjectInfo("dotnet-portable", "1.0.0", Enumerable.Empty<ResourceAssemblyInfo>()));
File.ReadAllText(depsJsonFile).Should().Be("temp");
File.Delete(depsJsonFile);
}
[Fact]
public void ItAddsFxVersionAsAParamWhenTheToolHasThePrefercliruntimeFile()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var testInstance = TestAssets.Get("MSBuildTestApp")
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-prefercliruntime",
CommandArguments = null,
ProjectDirectory = testInstance.Root.FullName
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Contain("--fx-version 2.0.0");
}
[Fact]
public void ItDoesNotAddFxVersionAsAParamWhenTheToolDoesNotHaveThePrefercliruntimeFile()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var testInstance = TestAssets.Get(TestProjectName)
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-portable",
CommandArguments = null,
ProjectDirectory = testInstance.Root.FullName
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().NotContain("--fx-version");
}
private ProjectToolsCommandResolver SetupProjectToolsCommandResolver()
{
Environment.SetEnvironmentVariable(
Constants.MSBUILD_EXE_PATH,
Path.Combine(new RepoDirectoriesProvider().Stage2Sdk, "MSBuild.dll"));
var packagedCommandSpecFactory = new PackagedCommandSpecFactoryWithCliRuntime();
var projectToolsCommandResolver =
new ProjectToolsCommandResolver(packagedCommandSpecFactory, new EnvironmentProvider());
return projectToolsCommandResolver;
}
}
}