PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Tests/Microsoft.NET.Publish.Tests/GivenThatAPublishedDepsJsonShouldContainVersionInformation.cs

https://gitlab.com/dotnetfoundation/sdk
C# | 268 lines | 208 code | 50 blank | 10 comment | 9 complexity | 8b9570d5769ebd7eaa1e18fb5e40c959 MD5 | raw file
  1. // Copyright (c) .NET Foundation and contributors. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. using FluentAssertions;
  10. using Microsoft.DotNet.Cli.Utils;
  11. using Microsoft.Extensions.DependencyModel;
  12. using Microsoft.NET.TestFramework;
  13. using Microsoft.NET.TestFramework.Assertions;
  14. using Microsoft.NET.TestFramework.Commands;
  15. using Microsoft.NET.TestFramework.ProjectConstruction;
  16. using Newtonsoft.Json.Linq;
  17. using NuGet.Common;
  18. using NuGet.Frameworks;
  19. using NuGet.ProjectModel;
  20. using Xunit;
  21. using Xunit.Abstractions;
  22. namespace Microsoft.NET.Publish.Tests
  23. {
  24. public class GivenThatAPublishedDepsJsonShouldContainVersionInformation : SdkTest
  25. {
  26. public GivenThatAPublishedDepsJsonShouldContainVersionInformation(ITestOutputHelper log) : base(log)
  27. {
  28. }
  29. private TestProject GetTestProject()
  30. {
  31. var testProject = new TestProject()
  32. {
  33. Name = "DepsJsonVersions",
  34. TargetFrameworks = "netcoreapp2.0",
  35. IsExe = true,
  36. };
  37. testProject.PackageReferences.Add(new TestPackageReference("System.Collections.Immutable", "1.5.0-preview1-26216-02"));
  38. testProject.PackageReferences.Add(new TestPackageReference("Libuv", "1.10.0"));
  39. return testProject;
  40. }
  41. [Fact]
  42. public void Versions_are_included_in_deps_json()
  43. {
  44. var testProject = GetTestProject();
  45. var testAsset = _testAssetsManager.CreateTestProject(testProject);
  46. var publishCommand = new PublishCommand(testAsset);
  47. publishCommand.Execute()
  48. .Should()
  49. .Pass();
  50. var publishDirectory = publishCommand.GetOutputDirectory(testProject.TargetFrameworks, runtimeIdentifier: testProject.RuntimeIdentifier);
  51. publishDirectory.Should().HaveFile(testProject.Name + ".deps.json");
  52. var depsFilePath = Path.Combine(publishDirectory.FullName, $"{testProject.Name}.deps.json");
  53. CheckVersionsInDepsFile(depsFilePath);
  54. }
  55. void CheckVersionsInDepsFile(string depsFilePath)
  56. {
  57. DependencyContext dependencyContext;
  58. using (var depsJsonFileStream = File.OpenRead(depsFilePath))
  59. {
  60. dependencyContext = new DependencyContextJsonReader().Read(depsJsonFileStream);
  61. }
  62. var libuvRuntimeLibrary = dependencyContext.RuntimeLibraries.Single(l => l.Name == "Libuv");
  63. var libuvRuntimeFiles = libuvRuntimeLibrary.NativeLibraryGroups.SelectMany(rag => rag.RuntimeFiles).ToList();
  64. libuvRuntimeFiles.Should().NotBeEmpty();
  65. foreach (var runtimeFile in libuvRuntimeFiles)
  66. {
  67. runtimeFile.AssemblyVersion.Should().BeNull();
  68. runtimeFile.FileVersion.Should().Be("0.0.0.0");
  69. }
  70. var immutableRuntimeLibrary = dependencyContext.RuntimeLibraries.Single(l => l.Name == "System.Collections.Immutable");
  71. var immutableRuntimeFiles = immutableRuntimeLibrary.RuntimeAssemblyGroups.SelectMany(rag => rag.RuntimeFiles).ToList();
  72. immutableRuntimeFiles.Should().NotBeEmpty();
  73. foreach (var runtimeFile in immutableRuntimeFiles)
  74. {
  75. runtimeFile.AssemblyVersion.Should().Be("1.2.3.0");
  76. runtimeFile.FileVersion.Should().Be("4.6.26216.2");
  77. }
  78. }
  79. [Fact]
  80. public void Versions_are_included_for_self_contained_apps()
  81. {
  82. Versions_are_included(build: false);
  83. }
  84. [Fact]
  85. public void Versions_are_included_for_build()
  86. {
  87. Versions_are_included(build: true);
  88. }
  89. private void Versions_are_included(bool build, [CallerMemberName] string callingMethod = "")
  90. {
  91. var testProject = GetTestProject();
  92. if (!EnvironmentInfo.SupportsTargetFramework(testProject.TargetFrameworks))
  93. {
  94. return;
  95. }
  96. testProject.RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid(testProject.TargetFrameworks);
  97. var testAsset = _testAssetsManager.CreateTestProject(testProject, callingMethod);
  98. MSBuildCommand command;
  99. if (build)
  100. {
  101. command = new BuildCommand(testAsset);
  102. }
  103. else
  104. {
  105. command = new PublishCommand(testAsset);
  106. }
  107. command.Execute()
  108. .Should()
  109. .Pass();
  110. var outputDirectory = command.GetOutputDirectory(testProject.TargetFrameworks, runtimeIdentifier: testProject.RuntimeIdentifier);
  111. outputDirectory.Should().HaveFile(testProject.Name + ".deps.json");
  112. var depsFilePath = Path.Combine(outputDirectory.FullName, $"{testProject.Name}.deps.json");
  113. CheckVersionsInDepsFile(depsFilePath);
  114. }
  115. [Fact(Skip = "Host deps.json doesn't have runtime file version info yet: https://github.com/dotnet/sdk/issues/2124")]
  116. public void Inbox_version_of_assembly_is_loaded_over_applocal_version()
  117. {
  118. var (coreDir, publishDir, immutableDir) = TestConflictResult();
  119. immutableDir.Should().BeEquivalentTo(coreDir, "immutable collections library from Framework should win");
  120. }
  121. [Fact]
  122. public void Inbox_version_is_loaded_if_runtime_file_versions_arent_in_deps()
  123. {
  124. void testProjectChanges(TestProject testProject)
  125. {
  126. testProject.AdditionalProperties["IncludeFileVersionsInDependencyFile"] = "false";
  127. }
  128. var (coreDir, publishDir, immutableDir) = TestConflictResult(testProjectChanges);
  129. immutableDir.Should().BeEquivalentTo(coreDir, "inbox immutable collections library from should win");
  130. }
  131. [Fact]
  132. public void Local_version_of_assembly_with_higher_version_is_loaded_over_inbox_version()
  133. {
  134. void publishFolderChanges(string publishFolder)
  135. {
  136. var depsJsonPath = Path.Combine(publishFolder, "DepsJsonVersions.deps.json");
  137. var depsJson = JObject.Parse(File.ReadAllText(depsJsonPath));
  138. var target = ((JProperty)depsJson["targets"].First).Value;
  139. var file = target["System.Collections.Immutable/1.5.0-preview1-26216-02"]["runtime"]["lib/netstandard2.0/System.Collections.Immutable.dll"];
  140. // Set fileVersion in deps.json to 4.7.0.0, which should be bigger than in box 4.6.x version
  141. file["fileVersion"] = "4.7.0.0";
  142. File.WriteAllText(depsJsonPath, depsJson.ToString());
  143. }
  144. var (coreDir, publishDir, immutableDir) = TestConflictResult(publishFolderChanges: publishFolderChanges);
  145. immutableDir.Should().BeEquivalentTo(publishDir, "published immutable collections library from should win");
  146. }
  147. private (string coreDir, string publishDir, string immutableDir) TestConflictResult(
  148. Action<TestProject> testProjectChanges = null,
  149. Action<string> publishFolderChanges = null,
  150. [CallerMemberName] string callingMethod = "")
  151. {
  152. var testProject = GetTestProject();
  153. testProject.SourceFiles["Program.cs"] = @"
  154. using System;
  155. static class Program
  156. {
  157. public static void Main()
  158. {
  159. Console.WriteLine(typeof(object).Assembly.Location);
  160. Console.WriteLine(typeof(System.Collections.Immutable.ImmutableList).Assembly.Location);
  161. }
  162. }
  163. ";
  164. if (testProjectChanges != null)
  165. {
  166. testProjectChanges(testProject);
  167. }
  168. var testAsset = _testAssetsManager.CreateTestProject(testProject, callingMethod: callingMethod);
  169. var publishCommand = new PublishCommand(testAsset);
  170. publishCommand.Execute()
  171. .Should()
  172. .Pass();
  173. var publishDirectory = publishCommand.GetOutputDirectory(testProject.TargetFrameworks, runtimeIdentifier: testProject.RuntimeIdentifier);
  174. if (publishFolderChanges != null)
  175. {
  176. publishFolderChanges(publishDirectory.FullName);
  177. }
  178. // Assembly from package should be deployed, as it is newer than the in-box version for netcoreapp2.0,
  179. // which is what the app targets
  180. publishDirectory.Should().HaveFile("System.Collections.Immutable.dll");
  181. var exePath = Path.Combine(publishDirectory.FullName, testProject.Name + ".dll");
  182. // We want to test a .NET Core 2.0 app rolling forward to .NET Core 2.2.
  183. // This wouldn't happen in our test environment as we also have the .NET Core 2.0 shared
  184. // framework installed. So we get the RuntimeFrameworkVersion of an app
  185. // that targets .NET Core 2.1, and then use the --fx-version parameter to the host
  186. // to force the .NET Core 2.0 app to run on that version
  187. string rollForwardVersion = GetRollForwardNetCoreAppVersion(callingMethod);
  188. var runAppCommand = new DotnetCommand(Log, "exec", "--fx-version", rollForwardVersion, exePath );
  189. var runAppResult = runAppCommand
  190. .Execute();
  191. runAppResult
  192. .Should()
  193. .Pass();
  194. var stdOutLines = runAppResult.StdOut.Split(Environment.NewLine);
  195. string coreDir = Path.GetDirectoryName(stdOutLines[0]);
  196. string immutableDir = Path.GetDirectoryName(stdOutLines[1]);
  197. return (coreDir, publishDirectory.FullName, immutableDir);
  198. }
  199. string GetRollForwardNetCoreAppVersion([CallerMemberName] string callingMethod = "", string identifier = null)
  200. {
  201. var testProject = new TestProject()
  202. {
  203. Name = nameof(GetRollForwardNetCoreAppVersion),
  204. TargetFrameworks = "netcoreapp2.2",
  205. IsExe = true
  206. };
  207. testProject.AdditionalProperties.Add("TargetLatestRuntimePatch", "true");
  208. var testAsset = _testAssetsManager.CreateTestProject(testProject, callingMethod, identifier)
  209. .Restore(Log, testProject.Name);
  210. LockFile lockFile = LockFileUtilities.GetLockFile(Path.Combine(testAsset.TestRoot, testProject.Name,
  211. "obj", "project.assets.json"), NullLogger.Instance);
  212. var target = lockFile.GetTarget(NuGetFramework.Parse(testProject.TargetFrameworks), null);
  213. var netCoreAppLibrary = target.Libraries.Single(l => l.Name == "Microsoft.NETCore.App");
  214. return netCoreAppLibrary.Version.ToString();
  215. }
  216. }
  217. }