2018-03-28 03:26:00 +00:00
using System ;
using System.IO ;
using System.Linq ;
2020-04-21 20:54:31 +00:00
using System.Runtime.InteropServices ;
2018-03-28 03:26:00 +00:00
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 17:40:44 +00:00
public partial class GivenSelfContainedAppsRollForward : TestBase
2018-03-28 03:26:00 +00:00
{
2018-10-23 02:59:58 +00:00
internal void ItRollsForwardToTheLatestVersion ( string packageName , string minorVersion )
2018-09-27 01:54:17 +00:00
{
2018-11-08 20:11:30 +00:00
var testProjectCreator = new TestProjectCreator ( )
2018-09-27 01:54:17 +00:00
{
2018-11-08 20:11:30 +00:00
PackageName = packageName ,
MinorVersion = minorVersion ,
// Set RuntimeIdentifier to opt in to roll-forward behavior
2020-04-21 20:54:31 +00:00
RuntimeIdentifier = RuntimeInformation . RuntimeIdentifier
2018-11-08 20:11:30 +00:00
} ;
2018-09-27 01:54:17 +00:00
2018-11-08 20:11:30 +00:00
var testInstance = testProjectCreator . Create ( ) ;
string projectDirectory = testInstance . Root . FullName ;
2018-03-28 03:26:00 +00:00
// 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-27 01:54:17 +00:00
var rolledForwardVersion = GetPackageVersion ( assetsFile , packageName ) ;
2018-03-28 03:26:00 +00: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 ;
}
2018-11-08 20:11:30 +00:00
testProjectCreator . Identifier = "floating" ;
var floatingProjectInstance = testProjectCreator . Create ( ) ;
var floatingProjectPath = Path . Combine ( floatingProjectInstance . Root . FullName , "TestAppSimple.csproj" ) ;
var floatingProject = XDocument . Load ( floatingProjectPath ) ;
var ns = floatingProject . Root . Name . Namespace ;
if ( packageName = = TestProjectCreator . NETCorePackageName )
2018-09-27 01:54:17 +00:00
{
// Float the RuntimeFrameworkVersion to get the latest version of the runtime available from feeds
2018-11-08 20:11:30 +00:00
floatingProject . Root . Element ( ns + "PropertyGroup" )
2018-09-27 01:54:17 +00:00
. Add ( new XElement ( ns + "RuntimeFrameworkVersion" , $"{minorVersion}.*" ) ) ;
}
else
{
2018-11-08 20:11:30 +00:00
floatingProject . Root . Element ( ns + "ItemGroup" )
2018-09-27 01:54:17 +00:00
. Element ( ns + "PackageReference" )
. Add ( new XAttribute ( "Version" , $"{minorVersion}.*" ) ,
new XAttribute ( "AllowExplicitVersion" , "true" ) ) ;
}
2018-11-08 20:11:30 +00:00
floatingProject . Save ( floatingProjectPath ) ;
2018-03-28 03:26:00 +00:00
new RestoreCommand ( )
2018-11-08 20:11:30 +00:00
. WithWorkingDirectory ( floatingProjectInstance . Root . FullName )
2018-03-28 03:26:00 +00:00
. Execute ( )
. Should ( ) . Pass ( ) ;
2018-11-08 20:11:30 +00:00
string floatingAssetsFilePath = Path . Combine ( floatingProjectInstance . Root . FullName , "obj" , "project.assets.json" ) ;
var floatedAssetsFile = new LockFileFormat ( ) . Read ( floatingAssetsFilePath ) ;
2018-03-28 03:26:00 +00:00
2018-09-27 01:54:17 +00:00
var floatedVersion = GetPackageVersion ( floatedAssetsFile , packageName ) ;
2018-03-28 03:26:00 +00:00
floatedVersion . Should ( ) . NotBeNull ( ) ;
rolledForwardVersion . ToNormalizedString ( ) . Should ( ) . BeEquivalentTo ( floatedVersion . ToNormalizedString ( ) ,
2018-09-27 01:54:17 +00: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-28 03:26:00 +00:00
}
2018-09-27 01:54:17 +00:00
private static NuGetVersion GetPackageVersion ( LockFile lockFile , string packageName )
2018-03-28 03:26:00 +00:00
{
return lockFile ? . Targets ? . SingleOrDefault ( t = > t . RuntimeIdentifier ! = null )
? . Libraries ? . SingleOrDefault ( l = >
2018-09-27 01:54:17 +00:00
string . Compare ( l . Name , packageName , StringComparison . CurrentCultureIgnoreCase ) = = 0 )
2018-03-28 03:26:00 +00:00
? . Version ;
}
2018-11-12 23:24:05 +00:00
[Fact]
2018-03-28 03:26:00 +00:00
public void WeCoverLatestNetCoreAppRollForward ( )
{
// Run "dotnet new console", get TargetFramework property, and make sure it's covered in SupportedNetCoreAppVersions
2018-10-29 18:26:53 +00:00
var directory = TestAssets . CreateTestDirectory ( ) ;
string projectDirectory = directory . FullName ;
2018-03-28 03:26:00 +00:00
2018-10-29 18:26:53 +00:00
new NewCommandShim ( )
. WithWorkingDirectory ( projectDirectory )
. Execute ( "console --no-restore" )
. Should ( ) . Pass ( ) ;
2018-03-28 03:26:00 +00:00
2018-10-29 18:26:53 +00:00
string projectPath = Path . Combine ( projectDirectory , Path . GetFileName ( projectDirectory ) + ".csproj" ) ;
2018-03-28 03:26:00 +00:00
2018-10-29 18:26:53 +00:00
var project = XDocument . Load ( projectPath ) ;
var ns = project . Root . Name . Namespace ;
2018-03-28 03:26:00 +00:00
2018-10-29 18:26:53 +00:00
string targetFramework = project . Root . Element ( ns + "PropertyGroup" )
. Element ( ns + "TargetFramework" )
. Value ;
2018-03-28 03:26:00 +00:00
2020-03-20 12:34:30 +00:00
SupportedNetCoreAppVersions . TargetFrameworkShortFolderVersion
2018-10-29 18:26:53 +00:00
. Should ( ) . Contain ( targetFramework , $"the {nameof(SupportedNetCoreAppVersions)}.{nameof(SupportedNetCoreAppVersions.Versions)} property should include the default version " +
"of .NET Core created by \"dotnet new\"" ) ;
2018-03-28 03:26:00 +00:00
}
2018-09-27 01:54:17 +00:00
[Fact]
public void WeCoverLatestAspNetCoreAppRollForward ( )
{
2018-10-29 18:26:53 +00:00
var directory = TestAssets . CreateTestDirectory ( ) ;
string projectDirectory = directory . FullName ;
2018-09-27 01:54:17 +00:00
// 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-09-27 01:54:17 +00:00
2018-10-29 18:26:53 +00:00
string projectPath = Path . Combine ( projectDirectory , Path . GetFileName ( projectDirectory ) + ".csproj" ) ;
2018-09-27 01:54:17 +00:00
2018-10-29 18:26:53 +00:00
var project = XDocument . Load ( projectPath ) ;
var ns = project . Root . Name . Namespace ;
2018-09-27 01:54:17 +00:00
2018-10-29 18:26:53 +00:00
string targetFramework = project . Root . Element ( ns + "PropertyGroup" )
. Element ( ns + "TargetFramework" )
. Value ;
2018-09-27 01:54:17 +00:00
2020-03-15 13:14:26 +00:00
TargetFrameworkHelper . GetNetAppTargetFrameworks ( SupportedAspNetCoreVersions . Versions )
2018-10-29 18:26:53 +00:00
. Should ( ) . Contain ( targetFramework , $"the {nameof(SupportedAspNetCoreVersions)} should include the default version " +
2020-03-15 13:14:26 +00:00
"of Microsoft.AspNetCore.App used by the templates created by \"dotnet new web\"" ) ;
2018-09-27 01:54:17 +00:00
}
2018-03-28 03:26:00 +00:00
}
}