dotnet-installer/test/EndToEnd/GivenAspNetAppsResolveImplicitVersions.cs

229 lines
9.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using FluentAssertions;
using Microsoft.DotNet.TestFramework;
using Microsoft.DotNet.Tools.Test.Utilities;
using NuGet.ProjectModel;
using NuGet.Versioning;
using Xunit;
namespace EndToEnd
{
public class GivenAspNetAppsResolveImplicitVersions : TestBase
{
private const string AspNetTestProject = "TestWebAppSimple";
2018-07-09 17:25:13 +00:00
[Fact(Skip ="https://github.com/dotnet/core-sdk/issues/21")]
public void PortablePublishWithLatestTFMUsesBundledAspNetCoreAppVersion()
{
var _testInstance = TestAssets.Get(AspNetTestProject)
.CreateInstance(identifier: LatestSupportedAspNetCoreAppVersion)
.WithSourceFiles();
string projectDirectory = _testInstance.Root.FullName;
string projectPath = Path.Combine(projectDirectory, $"{AspNetTestProject}.csproj");
var project = XDocument.Load(projectPath);
var ns = project.Root.Name.Namespace;
// Update TargetFramework to the right version of .NET Core
project.Root.Element(ns + "PropertyGroup")
.Element(ns + "TargetFramework")
.Value = "netcoreapp" + LatestSupportedAspNetCoreAppVersion;
project.Save(projectPath);
// Get the implicit version
new RestoreCommand()
.WithWorkingDirectory(projectDirectory)
2018-11-12 23:24:05 +00:00
.Execute("/bl:restore.binlog")
.Should().Pass();
var assetsFilePath = Path.Combine(projectDirectory, "obj", "project.assets.json");
var assetsFile = new LockFileFormat().Read(assetsFilePath);
var restoredVersion = GetAspNetCoreAppVersion(assetsFile, portable: true);
restoredVersion.Should().NotBeNull();
var bundledVersionPath = Path.Combine(projectDirectory, ".BundledAspNetCoreVersion");
var bundledVersion = File.ReadAllText(bundledVersionPath).Trim();
restoredVersion.ToNormalizedString().Should().BeEquivalentTo(bundledVersion,
2018-06-29 20:10:19 +00:00
"The bundled aspnetcore versions set in Microsoft.NETCoreSdk.BundledVersions.props should be identical to the versions generated." +
"Please update MSBuildExtensions.targets in this repo so these versions match.");
}
2018-07-09 17:25:13 +00:00
[Fact(Skip = "https://github.com/dotnet/core-sdk/issues/21")]
public void StandalonePublishWithLatestTFMUsesBundledAspNetCoreAppVersion()
{
var _testInstance = TestAssets.Get(AspNetTestProject)
.CreateInstance(identifier: LatestSupportedAspNetCoreAppVersion)
.WithSourceFiles();
string projectDirectory = _testInstance.Root.FullName;
string projectPath = Path.Combine(projectDirectory, $"{AspNetTestProject}.csproj");
var project = XDocument.Load(projectPath);
var ns = project.Root.Name.Namespace;
// Update TargetFramework to the right version of .NET Core
project.Root.Element(ns + "PropertyGroup")
.Element(ns + "TargetFramework")
.Value = "netcoreapp" + LatestSupportedAspNetCoreAppVersion;
var rid = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.GetRuntimeIdentifier();
// Set RuntimeIdentifier to simulate standalone publish
project.Root.Element(ns + "PropertyGroup")
.Add(new XElement(ns + "RuntimeIdentifier", rid));
project.Save(projectPath);
// Get the implicit version
new RestoreCommand()
.WithWorkingDirectory(projectDirectory)
.Execute()
.Should().Pass();
var assetsFilePath = Path.Combine(projectDirectory, "obj", "project.assets.json");
var assetsFile = new LockFileFormat().Read(assetsFilePath);
var restoredVersion = GetAspNetCoreAppVersion(assetsFile);
restoredVersion.Should().NotBeNull();
var bundledVersionPath = Path.Combine(projectDirectory, ".BundledAspNetCoreVersion");
var bundledVersion = File.ReadAllText(bundledVersionPath).Trim();
restoredVersion.ToNormalizedString().Should().BeEquivalentTo(bundledVersion,
2018-06-29 20:10:19 +00:00
"The bundled aspnetcore versions set in Microsoft.NETCoreSdk.BundledVersions.props should be identical to the versions set in DependencyVersions.props." +
"Please update MSBuildExtensions.targets in this repo so these versions match.");
}
2018-11-12 23:24:05 +00:00
[Theory]
[MemberData(nameof(SupportedAspNetCoreAppVersions))]
public void ItRollsForwardToTheLatestVersion(string minorVersion)
{
2018-11-12 23:24:05 +00:00
var testProjectCreator = new TestProjectCreator(identifier: minorVersion)
{
PackageName = TestProjectCreator.AspNetCoreAppPackageName,
MinorVersion = minorVersion ,
};
var _testInstance = testProjectCreator.Create()
.WithSourceFiles();
string projectDirectory = _testInstance.Root.FullName;
2018-11-12 23:24:05 +00:00
string projectPath = Path.Combine(projectDirectory, $"TestAppSimple.csproj");
var project = XDocument.Load(projectPath);
var ns = project.Root.Name.Namespace;
// Update TargetFramework to the right version of .NET Core
project.Root.Element(ns + "PropertyGroup")
.Element(ns + "TargetFramework")
.Value = "netcoreapp" + minorVersion;
var rid = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.GetRuntimeIdentifier();
// Set RuntimeIdentifier to opt in to roll-forward behavior
project.Root.Element(ns + "PropertyGroup")
.Add(new XElement(ns + "RuntimeIdentifier", rid));
project.Save(projectPath);
// Get the version rolled forward to
new RestoreCommand()
.WithWorkingDirectory(projectDirectory)
.Execute()
.Should().Pass();
string assetsFilePath = Path.Combine(projectDirectory, "obj", "project.assets.json");
var assetsFile = new LockFileFormat().Read(assetsFilePath);
var rolledForwardVersion = GetAspNetCoreAppVersion(assetsFile);
rolledForwardVersion.Should().NotBeNull();
if (rolledForwardVersion.IsPrerelease)
{
// If this version of .NET Core is still prerelease, then:
// - Floating the patch by adding ".*" to the major.minor version won't work, but
// - There aren't any patches to roll-forward to, so we skip testing this until the version
// leaves prerelease.
return;
}
// Float the RuntimeFrameworkVersion to get the latest version of the runtime available from feeds
Directory.Delete(Path.Combine(projectDirectory, "obj"), true);
project.Root.Element(ns + "PropertyGroup")
.Add(new XElement(ns + "RuntimeFrameworkVersion", $"{minorVersion}.*"));
project.Save(projectPath);
new RestoreCommand()
.WithWorkingDirectory(projectDirectory)
.Execute()
.Should().Pass();
var floatedAssetsFile = new LockFileFormat().Read(assetsFilePath);
var floatedVersion = GetAspNetCoreAppVersion(floatedAssetsFile);
floatedVersion.Should().NotBeNull();
rolledForwardVersion.ToNormalizedString().Should().BeEquivalentTo(floatedVersion.ToNormalizedString(),
"the latest patch version properties in Microsoft.NETCoreSdk.BundledVersions.props need to be updated " +
"(see MSBuildExtensions.targets in this repo)");
}
2018-11-12 23:24:05 +00:00
[Fact]
public void WeCoverLatestAspNetCoreAppRollForward()
{
2018-10-29 18:26:53 +00:00
var directory = TestAssets.CreateTestDirectory();
string projectDirectory = directory.FullName;
// Run "dotnet new web", get TargetFramework property, and make sure it's covered in SupportedAspNetCoreAppVersions
2018-10-29 18:26:53 +00:00
new NewCommandShim()
.WithWorkingDirectory(projectDirectory)
.Execute("web --no-restore")
.Should().Pass();
2018-10-29 18:26:53 +00:00
string projectPath = Path.Combine(projectDirectory, Path.GetFileName(projectDirectory) + ".csproj");
2018-10-29 18:26:53 +00:00
var project = XDocument.Load(projectPath);
var ns = project.Root.Name.Namespace;
2018-10-29 18:26:53 +00:00
string targetFramework = project.Root.Element(ns + "PropertyGroup")
.Element(ns + "TargetFramework")
.Value;
2018-10-29 18:26:53 +00:00
SupportedAspNetCoreAppVersions.Select(v => $"netcoreapp{v[0]}")
.Should().Contain(targetFramework, $"the {nameof(SupportedAspNetCoreAppVersions)} property should include the default version " +
"of Microsoft.AspNetCore.App used by the templates created by \"dotnet new web\"");
}
private NuGetVersion GetAspNetCoreAppVersion(LockFile lockFile, bool portable = false)
{
2018-11-12 23:24:05 +00:00
return lockFile?.Targets?.SingleOrDefault(t => portable == (t.RuntimeIdentifier == null))
?.Libraries?.SingleOrDefault(l =>
string.Compare(l.Name, "Microsoft.AspNetCore.App", StringComparison.CurrentCultureIgnoreCase) == 0)
?.Version;
}
2018-07-09 17:25:13 +00:00
public static string LatestSupportedAspNetCoreAppVersion = "3.0";
public static IEnumerable<object[]> SupportedAspNetCoreAppVersions
{
get
{
2018-07-09 17:25:13 +00:00
yield return new object[] { "2.1" };
2018-11-27 02:58:37 +00:00
yield return new object[] { "2.2" };
yield return new object[] { LatestSupportedAspNetCoreAppVersion };
}
}
}
}