2018-03-27 20:26:00 -07:00
using System ;
2018-07-27 15:01:22 -07:00
using System.Collections.Generic ;
2018-03-27 20:26:00 -07:00
using System.IO ;
using System.Linq ;
2018-07-09 10:25:13 -07:00
using System.Runtime.InteropServices ;
2018-03-27 20:26:00 -07:00
using System.Text ;
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
{
2018-06-21 10:40:44 -07:00
public partial class GivenSelfContainedAppsRollForward : TestBase
2018-03-27 20:26:00 -07:00
{
2018-09-26 18:54:17 -07:00
public const string NETCorePackageName = "Microsoft.NETCore.App" ;
public const string AspNetCoreAppPackageName = "Microsoft.AspNetCore.App" ;
public const string AspNetCoreAllPackageName = "Microsoft.AspNetCore.All" ;
2018-03-27 20:26:00 -07:00
2018-10-08 18:29:15 -07:00
[Theory(Skip = "https://github.com/dotnet/cli/issues/10123")]
2018-03-27 20:26:00 -07:00
// MemberData is used instead of InlineData here so we can access it in another test to
// verify that we are covering the latest release of .NET Core
2018-06-21 10:40:44 -07:00
[ClassData(typeof(SupportedNetCoreAppVersions))]
2018-09-26 18:54:17 -07:00
public void ItRollsForwardToTheLatestNetCoreVersion ( string minorVersion )
2018-03-27 20:26:00 -07:00
{
2018-09-26 18:54:17 -07:00
ItRollsForwardToTheLatestVersion ( NETCorePackageName , minorVersion ) ;
}
2018-10-08 18:29:15 -07:00
[Theory(Skip = "https://github.com/dotnet/cli/issues/10123")]
2018-09-26 18:54:17 -07:00
[ClassData(typeof(SupportedAspNetCoreVersions))]
public void ItRollsForwardToTheLatestAspNetCoreAppVersion ( string minorVersion )
{
ItRollsForwardToTheLatestVersion ( AspNetCoreAppPackageName , minorVersion ) ;
}
2018-10-08 18:29:15 -07:00
[Theory(Skip = "https://github.com/dotnet/cli/issues/10123")]
2018-09-26 18:54:17 -07:00
[ClassData(typeof(SupportedAspNetCoreVersions))]
public void ItRollsForwardToTheLatestAspNetCoreAllVersion ( string minorVersion )
{
ItRollsForwardToTheLatestVersion ( AspNetCoreAllPackageName , minorVersion ) ;
}
2018-07-13 16:55:23 -07:00
2018-09-26 18:54:17 -07:00
public void ItRollsForwardToTheLatestVersion ( string packageName , string minorVersion )
{
2018-03-27 20:26:00 -07:00
var _testInstance = TestAssets . Get ( "TestAppSimple" )
2018-09-26 18:54:17 -07:00
. CreateInstance ( identifier : packageName + "_" + minorVersion )
2018-03-27 20:26:00 -07:00
. WithSourceFiles ( ) ;
string projectDirectory = _testInstance . Root . FullName ;
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 ) ) ;
2018-09-26 18:54:17 -07:00
if ( packageName ! = NETCorePackageName )
{
// Add implicit ASP.NET reference
project . Root . Add ( new XElement ( ns + "ItemGroup" ,
new XElement ( ns + "PackageReference" , new XAttribute ( "Include" , packageName ) ) ) ) ;
}
2018-03-27 20:26:00 -07:00
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 ) ;
2018-09-26 18:54:17 -07:00
var rolledForwardVersion = GetPackageVersion ( assetsFile , packageName ) ;
2018-03-27 20:26:00 -07:00
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 ;
}
Directory . Delete ( Path . Combine ( projectDirectory , "obj" ) , true ) ;
2018-09-26 18:54:17 -07:00
if ( packageName = = NETCorePackageName )
{
// Float the RuntimeFrameworkVersion to get the latest version of the runtime available from feeds
project . Root . Element ( ns + "PropertyGroup" )
. Add ( new XElement ( ns + "RuntimeFrameworkVersion" , $"{minorVersion}.*" ) ) ;
}
else
{
project . Root . Element ( ns + "ItemGroup" )
. Element ( ns + "PackageReference" )
. Add ( new XAttribute ( "Version" , $"{minorVersion}.*" ) ,
new XAttribute ( "AllowExplicitVersion" , "true" ) ) ;
}
2018-03-27 20:26:00 -07:00
project . Save ( projectPath ) ;
new RestoreCommand ( )
. WithWorkingDirectory ( projectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
var floatedAssetsFile = new LockFileFormat ( ) . Read ( assetsFilePath ) ;
2018-09-26 18:54:17 -07:00
var floatedVersion = GetPackageVersion ( floatedAssetsFile , packageName ) ;
2018-03-27 20:26:00 -07:00
floatedVersion . Should ( ) . NotBeNull ( ) ;
rolledForwardVersion . ToNormalizedString ( ) . Should ( ) . BeEquivalentTo ( floatedVersion . ToNormalizedString ( ) ,
2018-09-26 18:54:17 -07:00
$"the latest patch version for {packageName} {minorVersion} in Microsoft.NETCoreSdk.BundledVersions.props " +
"needs to be updated (see the ImplicitPackageVariable items in MSBuildExtensions.targets in this repo)" ) ;
2018-03-27 20:26:00 -07:00
}
2018-09-26 18:54:17 -07:00
private static NuGetVersion GetPackageVersion ( LockFile lockFile , string packageName )
2018-03-27 20:26:00 -07:00
{
return lockFile ? . Targets ? . SingleOrDefault ( t = > t . RuntimeIdentifier ! = null )
? . Libraries ? . SingleOrDefault ( l = >
2018-09-26 18:54:17 -07:00
string . Compare ( l . Name , packageName , StringComparison . CurrentCultureIgnoreCase ) = = 0 )
2018-03-27 20:26:00 -07:00
? . Version ;
}
2018-09-09 21:56:57 -07:00
[Fact(Skip = "https://github.com/dotnet/cli/issues/9968")]
2018-03-27 20:26:00 -07:00
public void WeCoverLatestNetCoreAppRollForward ( )
{
// Run "dotnet new console", get TargetFramework property, and make sure it's covered in SupportedNetCoreAppVersions
using ( DisposableDirectory directory = Temp . CreateDirectory ( ) )
{
string projectDirectory = directory . Path ;
new NewCommandShim ( )
. WithWorkingDirectory ( projectDirectory )
. Execute ( "console --no-restore" )
. Should ( ) . Pass ( ) ;
string projectPath = Path . Combine ( projectDirectory , Path . GetFileName ( projectDirectory ) + ".csproj" ) ;
var project = XDocument . Load ( projectPath ) ;
var ns = project . Root . Name . Namespace ;
string targetFramework = project . Root . Element ( ns + "PropertyGroup" )
. Element ( ns + "TargetFramework" )
. Value ;
2018-09-26 18:54:17 -07:00
SupportedNetCoreAppVersions . Versions . Select ( v = > $"netcoreapp{v}" )
2018-06-21 10:40:44 -07:00
. Should ( ) . Contain ( targetFramework , $"the {nameof(SupportedNetCoreAppVersions)}.{nameof(SupportedNetCoreAppVersions.Versions)} property should include the default version " +
2018-03-27 20:26:00 -07:00
"of .NET Core created by \"dotnet new\"" ) ;
}
}
2018-09-26 18:54:17 -07:00
[Fact]
public void WeCoverLatestAspNetCoreAppRollForward ( )
{
// Run "dotnet new web", get TargetFramework property, and make sure it's covered in SupportedAspNetCoreAppVersions
using ( DisposableDirectory directory = Temp . CreateDirectory ( ) )
{
string projectDirectory = directory . Path ;
new NewCommandShim ( )
. WithWorkingDirectory ( projectDirectory )
. Execute ( "web --no-restore" )
. Should ( ) . Pass ( ) ;
string projectPath = Path . Combine ( projectDirectory , Path . GetFileName ( projectDirectory ) + ".csproj" ) ;
var project = XDocument . Load ( projectPath ) ;
var ns = project . Root . Name . Namespace ;
string targetFramework = project . Root . Element ( ns + "PropertyGroup" )
. Element ( ns + "TargetFramework" )
. Value ;
SupportedAspNetCoreVersions . Versions . Select ( v = > $"netcoreapp{v}" )
. Should ( ) . Contain ( targetFramework , $"the {nameof(SupportedAspNetCoreVersions)} should include the default version " +
"of Microsoft.AspNetCore.App used by the templates created by \"dotnet new web\"" ) ;
}
}
2018-10-05 18:33:02 -07:00
2018-03-27 20:26:00 -07:00
}
}