2017-02-01 01:31:37 +00:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
2016-12-13 22:15:35 +00:00
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2017-02-10 20:14:04 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Xml.Linq;
|
2016-12-13 22:15:35 +00:00
|
|
|
|
using FluentAssertions;
|
2017-02-10 20:14:04 +00:00
|
|
|
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
2017-03-23 18:21:39 +00:00
|
|
|
|
using Microsoft.Extensions.DependencyModel;
|
2017-01-06 22:48:27 +00:00
|
|
|
|
using Xunit;
|
2016-12-13 22:15:35 +00:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.New.Tests
|
|
|
|
|
{
|
|
|
|
|
public class GivenThatIWantANewApp : TestBase
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void When_dotnet_new_is_invoked_mupliple_times_it_should_fail()
|
|
|
|
|
{
|
2017-02-15 23:35:03 +00:00
|
|
|
|
var rootPath = TestAssets.CreateTestDirectory().FullName;
|
2016-12-13 22:15:35 +00:00
|
|
|
|
|
2017-02-10 20:14:04 +00:00
|
|
|
|
new NewCommand()
|
|
|
|
|
.WithWorkingDirectory(rootPath)
|
|
|
|
|
.Execute($"console --debug:ephemeral-hive");
|
2016-12-13 22:15:35 +00:00
|
|
|
|
|
|
|
|
|
DateTime expectedState = Directory.GetLastWriteTime(rootPath);
|
|
|
|
|
|
2017-02-10 20:14:04 +00:00
|
|
|
|
var result = new NewCommand()
|
|
|
|
|
.WithWorkingDirectory(rootPath)
|
|
|
|
|
.ExecuteWithCapturedOutput($"console --debug:ephemeral-hive");
|
2016-12-13 22:15:35 +00:00
|
|
|
|
|
|
|
|
|
DateTime actualState = Directory.GetLastWriteTime(rootPath);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(expectedState, actualState);
|
|
|
|
|
|
2017-02-01 01:31:37 +00:00
|
|
|
|
result.Should().Fail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RestoreDoesNotUseAnyCliProducedPackagesOnItsTemplates()
|
|
|
|
|
{
|
|
|
|
|
string[] cSharpTemplates = new[] { "console", "classlib", "mstest", "xunit", "web", "mvc", "webapi" };
|
|
|
|
|
|
2017-02-15 23:35:03 +00:00
|
|
|
|
var rootPath = TestAssets.CreateTestDirectory().FullName;
|
2017-02-01 01:31:37 +00:00
|
|
|
|
var packagesDirectory = Path.Combine(rootPath, "packages");
|
|
|
|
|
|
|
|
|
|
foreach (string cSharpTemplate in cSharpTemplates)
|
|
|
|
|
{
|
|
|
|
|
var projectFolder = Path.Combine(rootPath, cSharpTemplate + "1");
|
|
|
|
|
Directory.CreateDirectory(projectFolder);
|
|
|
|
|
CreateAndRestoreNewProject(cSharpTemplate, projectFolder, packagesDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Directory.EnumerateFiles(packagesDirectory, $"*.nupkg", SearchOption.AllDirectories)
|
|
|
|
|
.Should().NotContain(p => p.Contains("Microsoft.DotNet.Cli.Utils"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateAndRestoreNewProject(
|
|
|
|
|
string projectType,
|
|
|
|
|
string projectFolder,
|
|
|
|
|
string packagesDirectory)
|
|
|
|
|
{
|
2017-02-10 20:14:04 +00:00
|
|
|
|
new NewCommand()
|
|
|
|
|
.WithWorkingDirectory(projectFolder)
|
|
|
|
|
.Execute($"{projectType} --debug:ephemeral-hive")
|
2017-02-01 01:31:37 +00:00
|
|
|
|
.Should().Pass();
|
|
|
|
|
|
|
|
|
|
new RestoreCommand()
|
|
|
|
|
.WithWorkingDirectory(projectFolder)
|
2017-02-10 20:14:04 +00:00
|
|
|
|
.Execute($"--packages {packagesDirectory}")
|
2017-02-01 01:31:37 +00:00
|
|
|
|
.Should().Pass();
|
2016-12-13 22:15:35 +00:00
|
|
|
|
}
|
2017-02-10 20:14:04 +00:00
|
|
|
|
|
2017-03-23 18:21:39 +00:00
|
|
|
|
[Theory]
|
2017-03-23 19:54:07 +00:00
|
|
|
|
[InlineData("console", "RuntimeFrameworkVersion", "microsoft.netcore.app")]
|
|
|
|
|
[InlineData("classlib", "NetStandardImplicitPackageVersion", "netstandard.library")]
|
2017-03-23 18:21:39 +00:00
|
|
|
|
public void NewProjectRestoresCorrectPackageVersion(string type, string propertyName, string packageName)
|
2017-02-10 20:14:04 +00:00
|
|
|
|
{
|
2017-03-23 18:21:39 +00:00
|
|
|
|
// These will fail when templates stop including explicit version.
|
|
|
|
|
// Collapse back to one method and remove the explicit version handling when that happens.
|
|
|
|
|
NewProjectRestoresCorrectPackageVersion(type, propertyName, packageName, deleteExplicitVersion: true);
|
|
|
|
|
NewProjectRestoresCorrectPackageVersion(type, propertyName, packageName, deleteExplicitVersion: false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void NewProjectRestoresCorrectPackageVersion(string type, string propertyName, string packageName, bool deleteExplicitVersion)
|
|
|
|
|
{
|
|
|
|
|
var rootPath = TestAssets.CreateTestDirectory(identifier: $"_{type}_{deleteExplicitVersion}").FullName;
|
2017-02-10 20:14:04 +00:00
|
|
|
|
var packagesDirectory = Path.Combine(rootPath, "packages");
|
2017-03-23 18:21:39 +00:00
|
|
|
|
var projectName = "Project";
|
|
|
|
|
var expectedVersion = GetFrameworkPackageVersion();
|
2017-02-10 20:14:04 +00:00
|
|
|
|
|
|
|
|
|
new NewCommand()
|
|
|
|
|
.WithWorkingDirectory(rootPath)
|
2017-03-23 18:21:39 +00:00
|
|
|
|
.Execute($"{type} --name {projectName} -o .")
|
2017-02-10 20:14:04 +00:00
|
|
|
|
.Should().Pass();
|
|
|
|
|
|
2017-03-23 18:21:39 +00:00
|
|
|
|
ValidateAndRemoveExplicitVersion();
|
|
|
|
|
|
2017-02-10 20:14:04 +00:00
|
|
|
|
new RestoreCommand()
|
|
|
|
|
.WithWorkingDirectory(rootPath)
|
|
|
|
|
.Execute($"--packages {packagesDirectory}")
|
|
|
|
|
.Should().Pass();
|
|
|
|
|
|
2017-03-23 18:21:39 +00:00
|
|
|
|
new DirectoryInfo(Path.Combine(packagesDirectory, packageName))
|
2017-02-10 20:14:04 +00:00
|
|
|
|
.Should().Exist()
|
|
|
|
|
.And.HaveDirectory(expectedVersion);
|
2017-03-23 18:21:39 +00:00
|
|
|
|
|
|
|
|
|
string GetFrameworkPackageVersion()
|
|
|
|
|
{
|
|
|
|
|
var dotnetDir = new FileInfo(DotnetUnderTest.FullName).Directory;
|
|
|
|
|
var sharedFxDir = dotnetDir
|
|
|
|
|
.GetDirectory("shared", "Microsoft.NETCore.App")
|
|
|
|
|
.EnumerateDirectories()
|
|
|
|
|
.Single(d => d.Name.StartsWith("2.0.0"));
|
|
|
|
|
|
2017-03-23 19:54:07 +00:00
|
|
|
|
if (packageName == "microsoft.netcore.app")
|
2017-03-23 18:21:39 +00:00
|
|
|
|
{
|
|
|
|
|
return sharedFxDir.Name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var depsFile = Path.Combine(sharedFxDir.FullName, "Microsoft.NETCore.App.deps.json");
|
|
|
|
|
using (var stream = File.OpenRead(depsFile))
|
|
|
|
|
using (var reader = new DependencyContextJsonReader())
|
|
|
|
|
{
|
|
|
|
|
var context = reader.Read(stream);
|
|
|
|
|
var dependency = context
|
|
|
|
|
.RuntimeLibraries
|
2017-03-23 19:54:07 +00:00
|
|
|
|
.Single(library => library.Name == packageName);
|
2017-03-23 18:21:39 +00:00
|
|
|
|
|
|
|
|
|
return dependency.Version;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove when templates stop putting an explicit version
|
|
|
|
|
void ValidateAndRemoveExplicitVersion()
|
|
|
|
|
{
|
|
|
|
|
var projectFileName = $"{projectName}.csproj";
|
|
|
|
|
var projectPath = Path.Combine(rootPath, projectFileName);
|
|
|
|
|
var projectDocument = XDocument.Load(projectPath);
|
|
|
|
|
var explicitVersionNode = projectDocument
|
|
|
|
|
.Elements("Project")
|
|
|
|
|
.Elements("PropertyGroup")
|
|
|
|
|
.Elements(propertyName)
|
|
|
|
|
.SingleOrDefault();
|
|
|
|
|
|
|
|
|
|
explicitVersionNode.Should().NotBeNull();
|
2017-03-23 20:16:54 +00:00
|
|
|
|
explicitVersionNode.Value.Should().Be(expectedVersion);
|
|
|
|
|
|
2017-03-23 18:21:39 +00:00
|
|
|
|
if (deleteExplicitVersion)
|
|
|
|
|
{
|
|
|
|
|
explicitVersionNode.Remove();
|
|
|
|
|
projectDocument.Save(projectPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-10 20:14:04 +00:00
|
|
|
|
}
|
2016-12-13 22:15:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|