|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using FluentAssertions; |
| 7 | +using Microsoft.NET.TestFramework; |
| 8 | +using Microsoft.NET.TestFramework.Assertions; |
| 9 | +using Microsoft.NET.TestFramework.Commands; |
| 10 | +using Microsoft.NET.TestFramework.ProjectConstruction; |
| 11 | +using NuGet.Common; |
| 12 | +using NuGet.Frameworks; |
| 13 | +using NuGet.ProjectModel; |
| 14 | +using NuGet.Versioning; |
| 15 | +using Xunit; |
| 16 | +using Xunit.Abstractions; |
| 17 | + |
| 18 | +namespace Microsoft.NET.Build.Tests |
| 19 | +{ |
| 20 | + public class ImplicitAspNetVersions : SdkTest |
| 21 | + { |
| 22 | + public ImplicitAspNetVersions(ITestOutputHelper log) : base(log) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + [Theory] |
| 27 | + [InlineData("Microsoft.AspNetCore.App")] |
| 28 | + [InlineData("Microsoft.AspNetCore.All")] |
| 29 | + public void AspNetCoreVersionIsSetImplicitly(string aspnetPackageName) |
| 30 | + { |
| 31 | + var testProject = new TestProject() |
| 32 | + { |
| 33 | + Name = "AspNetImplicitVersion", |
| 34 | + TargetFrameworks = "netcoreapp2.1", |
| 35 | + IsSdkProject = true, |
| 36 | + IsExe = true |
| 37 | + }; |
| 38 | + |
| 39 | + // Add versionless PackageReference |
| 40 | + testProject.PackageReferences.Add(new TestPackageReference(aspnetPackageName, null)); |
| 41 | + |
| 42 | + var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: aspnetPackageName) |
| 43 | + .Restore(Log, testProject.Name); |
| 44 | + |
| 45 | + var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); |
| 46 | + |
| 47 | + buildCommand |
| 48 | + .Execute() |
| 49 | + .Should() |
| 50 | + .Pass(); |
| 51 | + |
| 52 | + var aspnetVersion = GetLibraryVersion(testProject, buildCommand, aspnetPackageName); |
| 53 | + |
| 54 | + // Version of AspNetCore packages is 2.1.1 because 2.1.0 packages had exact version constraints, which was broken |
| 55 | + aspnetVersion.ToString().Should().Be("2.1.1"); |
| 56 | + } |
| 57 | + |
| 58 | + [Theory] |
| 59 | + [InlineData("Microsoft.AspNetCore.App")] |
| 60 | + [InlineData("Microsoft.AspNetCore.All")] |
| 61 | + public void AspNetCoreVersionRollsForward(string aspnetPackageName) |
| 62 | + { |
| 63 | + var testProject = new TestProject() |
| 64 | + { |
| 65 | + Name = "AspNetImplicitVersion", |
| 66 | + TargetFrameworks = "netcoreapp2.1", |
| 67 | + IsSdkProject = true, |
| 68 | + IsExe = true, |
| 69 | + |
| 70 | + }; |
| 71 | + |
| 72 | + testProject.RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid(testProject.TargetFrameworks); |
| 73 | + |
| 74 | + // Add versionless PackageReference |
| 75 | + testProject.PackageReferences.Add(new TestPackageReference(aspnetPackageName, null)); |
| 76 | + |
| 77 | + var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: aspnetPackageName) |
| 78 | + .Restore(Log, testProject.Name); |
| 79 | + |
| 80 | + var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); |
| 81 | + |
| 82 | + buildCommand |
| 83 | + .Execute() |
| 84 | + .Should() |
| 85 | + .Pass(); |
| 86 | + |
| 87 | + var aspnetVersion = GetLibraryVersion(testProject, buildCommand, aspnetPackageName); |
| 88 | + |
| 89 | + // Self-contained app (because RID is specified) should roll forward to later patch |
| 90 | + aspnetVersion.CompareTo(new SemanticVersion(2, 1, 1)).Should().BeGreaterThan(0); |
| 91 | + } |
| 92 | + |
| 93 | + [Theory] |
| 94 | + [InlineData("Microsoft.AspNetCore.App")] |
| 95 | + [InlineData("Microsoft.AspNetCore.All")] |
| 96 | + public void ExplicitVersionsOfAspNetCoreWarn(string aspnetPackageName) |
| 97 | + { |
| 98 | + var testProject = new TestProject() |
| 99 | + { |
| 100 | + Name = "AspNetExplicitVersion", |
| 101 | + TargetFrameworks = "netcoreapp2.1", |
| 102 | + IsSdkProject = true, |
| 103 | + IsExe = true |
| 104 | + }; |
| 105 | + |
| 106 | + string explicitVersion = "2.1.0"; |
| 107 | + |
| 108 | + testProject.PackageReferences.Add(new TestPackageReference(aspnetPackageName, explicitVersion)); |
| 109 | + |
| 110 | + var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: aspnetPackageName) |
| 111 | + .Restore(Log, testProject.Name); |
| 112 | + |
| 113 | + var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); |
| 114 | + |
| 115 | + buildCommand |
| 116 | + .Execute() |
| 117 | + .Should() |
| 118 | + .Pass() |
| 119 | + .And |
| 120 | + .HaveStdOutContaining("NETSDK1071"); |
| 121 | + |
| 122 | + var aspnetVersion = GetLibraryVersion(testProject, buildCommand, aspnetPackageName); |
| 123 | + |
| 124 | + aspnetVersion.ToString().Should().Be(explicitVersion); |
| 125 | + } |
| 126 | + |
| 127 | + [Theory] |
| 128 | + [InlineData("netcoreapp2.0", "Microsoft.AspNetCore.All", "2.0.9")] |
| 129 | + [InlineData("netcoreapp1.1", "Microsoft.AspNetCore", "1.1.7")] |
| 130 | + public void ExplicitVersionsDontWarnForOlderVersions(string targetFramework, string packageName, string packageVersion) |
| 131 | + { |
| 132 | + var testProject = new TestProject() |
| 133 | + { |
| 134 | + Name = "AspNetPreviousVersion", |
| 135 | + TargetFrameworks = targetFramework, |
| 136 | + IsSdkProject = true, |
| 137 | + IsExe = true |
| 138 | + }; |
| 139 | + |
| 140 | + testProject.PackageReferences.Add(new TestPackageReference(packageName, packageVersion)); |
| 141 | + |
| 142 | + var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework) |
| 143 | + .Restore(Log, testProject.Name); |
| 144 | + |
| 145 | + var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); |
| 146 | + |
| 147 | + buildCommand |
| 148 | + .Execute() |
| 149 | + .Should() |
| 150 | + .Pass() |
| 151 | + .And |
| 152 | + .NotHaveStdOutContaining("warning"); |
| 153 | + |
| 154 | + var aspnetVersion = GetLibraryVersion(testProject, buildCommand, packageName); |
| 155 | + |
| 156 | + aspnetVersion.ToString().Should().Be(packageVersion); |
| 157 | + } |
| 158 | + |
| 159 | + [Fact] |
| 160 | + public void MultipleWarningsAreGeneratedForMultipleExplicitReferences() |
| 161 | + { |
| 162 | + var testProject = new TestProject() |
| 163 | + { |
| 164 | + Name = "MultipleExplicitReferences", |
| 165 | + TargetFrameworks = "netcoreapp2.1", |
| 166 | + IsSdkProject = true, |
| 167 | + IsExe = true |
| 168 | + }; |
| 169 | + |
| 170 | + testProject.PackageReferences.Add(new TestPackageReference("Microsoft.NETCore.App", "2.1.0")); |
| 171 | + testProject.PackageReferences.Add(new TestPackageReference("Microsoft.AspNetCore.App", "2.1.0")); |
| 172 | + |
| 173 | + var testAsset = _testAssetsManager.CreateTestProject(testProject); |
| 174 | + |
| 175 | + var restoreCommand = new RestoreCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); |
| 176 | + restoreCommand |
| 177 | + .Execute() |
| 178 | + .Should() |
| 179 | + .Pass() |
| 180 | + .And |
| 181 | + .NotHaveStdOutContaining("NETSDK1071"); |
| 182 | + |
| 183 | + |
| 184 | + var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); |
| 185 | + |
| 186 | + buildCommand |
| 187 | + .Execute() |
| 188 | + .Should() |
| 189 | + .Pass() |
| 190 | + .And |
| 191 | + .HaveStdOutContaining("NETSDK1071") |
| 192 | + .And |
| 193 | + .HaveStdOutContaining("NETSDK1023"); |
| 194 | + } |
| 195 | + |
| 196 | + static NuGetVersion GetLibraryVersion(TestProject testProject, BuildCommand buildCommand, string libraryName) |
| 197 | + { |
| 198 | + LockFile lockFile = LockFileUtilities.GetLockFile( |
| 199 | + Path.Combine(buildCommand.GetBaseIntermediateDirectory().FullName, "project.assets.json"), |
| 200 | + NullLogger.Instance); |
| 201 | + |
| 202 | + var target = lockFile.GetTarget(NuGetFramework.Parse(testProject.TargetFrameworks), testProject.RuntimeIdentifier); |
| 203 | + var lockFileLibrary = target.Libraries.Single(l => l.Name == libraryName); |
| 204 | + |
| 205 | + return lockFileLibrary.Version; |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments