GivenThatThereAreImplicitPackageReferences.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. namespace Microsoft.NET.Pack.Tests
  4. {
  5. public class GivenThatThereAreImplicitPackageReferences : SdkTest
  6. {
  7. public GivenThatThereAreImplicitPackageReferences(ITestOutputHelper log) : base(log)
  8. {
  9. }
  10. [Fact]
  11. public void Packing_a_netstandard_1_x_library_includes_the_implicit_dependency()
  12. {
  13. TestProject testProject = new()
  14. {
  15. Name = "PackNetStandard1x",
  16. TargetFrameworks = "netstandard1.4",
  17. IsExe = false
  18. };
  19. var dependencies = PackAndGetDependencies(testProject);
  20. dependencies.Count().Should().Be(1);
  21. dependencies.Single().Attribute("id").Value
  22. .Should().Be("NETStandard.Library");
  23. dependencies.Single().Attribute("version").Value
  24. .Should().Be("1.6.1");
  25. }
  26. [Fact]
  27. public void Packing_a_netstandard_2_0_library_does_not_include_the_implicit_dependency()
  28. {
  29. TestProject testProject = new()
  30. {
  31. Name = "PackNetStandard20",
  32. TargetFrameworks = "netstandard2.0",
  33. IsExe = false
  34. };
  35. var dependencies = PackAndGetDependencies(testProject);
  36. dependencies.Should().BeEmpty();
  37. }
  38. [Fact]
  39. public void Packing_a_netcoreapp_1_1_library_includes_the_implicit_dependency()
  40. {
  41. TestProject testProject = new()
  42. {
  43. Name = "PackNetCoreApp11Library",
  44. TargetFrameworks = "netcoreapp1.1",
  45. IsExe = false
  46. };
  47. var dependencies = PackAndGetDependencies(testProject);
  48. dependencies.Count().Should().Be(1);
  49. dependencies.Single().Attribute("id").Value
  50. .Should().Be("Microsoft.NETCore.App");
  51. // Don't check the exact version so that the test doesn't break if we roll forward to new patch versions of the package
  52. dependencies.Single().Attribute("version").Value
  53. .Should().StartWith("1.1.");
  54. }
  55. [Fact]
  56. public void Packing_a_netcoreapp_2_0_library_does_not_include_the_implicit_dependency()
  57. {
  58. TestProject testProject = new()
  59. {
  60. Name = "PackNetCoreApp20Library",
  61. TargetFrameworks = "netcoreapp2.0",
  62. IsExe = false
  63. };
  64. var dependencies = PackAndGetDependencies(testProject);
  65. dependencies.Should().BeEmpty();
  66. }
  67. [Fact]
  68. public void Packing_a_netcoreapp_1_1_app_includes_the_implicit_dependency()
  69. {
  70. TestProject testProject = new()
  71. {
  72. Name = "PackNetCoreApp11App",
  73. TargetFrameworks = "netcoreapp1.1",
  74. IsExe = true
  75. };
  76. var dependencies = PackAndGetDependencies(testProject);
  77. dependencies.Count().Should().Be(1);
  78. dependencies.Single().Attribute("id").Value
  79. .Should().Be("Microsoft.NETCore.App");
  80. // Don't check the exact version so that the test doesn't break if we roll forward to new patch versions of the package
  81. dependencies.Single().Attribute("version").Value
  82. .Should().StartWith("1.1.");
  83. }
  84. [WindowsOnlyFact]
  85. public void Packing_an_app_exclude_dependencies_framework_assemblies_dependency()
  86. {
  87. TestProject testProject = new()
  88. {
  89. Name = "Packnet462App",
  90. TargetFrameworks = "net462",
  91. };
  92. testProject.PackageReferences.Add(
  93. new TestPackageReference(
  94. "System.IO.Compression",
  95. "4.3.0",
  96. null));
  97. testProject.References.Add("System.Web");
  98. var dependencies = GetFrameworkAssemblies(PackAndGetNuspec(testProject), out var _);
  99. dependencies.Count().Should().Be(1);
  100. dependencies.Single().Attribute("assemblyName").Value.Should().Be("System.Web");
  101. }
  102. [Theory]
  103. [InlineData("netcoreapp2.0")]
  104. [InlineData("netcoreapp3.0")]
  105. public void Packing_a_netcoreapp_2_0_app_includes_no_dependencies(string targetFramework)
  106. {
  107. TestProject testProject = new()
  108. {
  109. Name = "PackApp_" + targetFramework,
  110. TargetFrameworks = targetFramework,
  111. IsExe = true
  112. };
  113. var dependencies = PackAndGetDependencies(testProject);
  114. dependencies.Should().BeEmpty();
  115. }
  116. [Theory]
  117. [InlineData("Microsoft.AspNetCore.App")]
  118. [InlineData("Microsoft.AspNetCore.All")]
  119. public void Package_an_aspnetcore_2_1_app_does_not_include_the_implicit_dependency(string packageId)
  120. {
  121. TestProject testProject = new()
  122. {
  123. Name = "PackAspNetCoreApp21App",
  124. TargetFrameworks = "netcoreapp2.1",
  125. IsExe = true
  126. };
  127. testProject.PackageReferences.Add(new TestPackageReference(packageId, ""));
  128. var dependencies = PackAndGetDependencies(testProject, packageId);
  129. dependencies.Should().BeEmpty();
  130. }
  131. [Fact]
  132. public void Packing_a_netcoreapp_2_0_DotnetCliTool_app_includes_the_implicit_dependency()
  133. {
  134. TestProject testProject = new()
  135. {
  136. Name = "PackNetCoreApp20App",
  137. TargetFrameworks = "netcoreapp2.0",
  138. IsExe = true
  139. };
  140. testProject.AdditionalProperties.Add("PackageType", "DotnetCliTool");
  141. var dependencies = PackAndGetDependencies(testProject);
  142. dependencies.Count().Should().Be(1);
  143. dependencies.Single().Attribute("id").Value
  144. .Should().Be("Microsoft.NETCore.App");
  145. // Don't check the exact version so that the test doesn't break if we roll forward to new patch versions of the package
  146. dependencies.Single().Attribute("version").Value
  147. .Should().StartWith("2.0.");
  148. }
  149. [Fact]
  150. public void Packing_a_multitargeted_library_includes_implicit_dependencies_when_appropriate()
  151. {
  152. TestProject testProject = new()
  153. {
  154. Name = "PackMultiTargetedLibrary",
  155. TargetFrameworks = $"netstandard1.1;netstandard2.0;netcoreapp1.1;{ToolsetInfo.CurrentTargetFramework}",
  156. IsExe = false
  157. };
  158. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  159. {
  160. testProject.TargetFrameworks += ";net462";
  161. }
  162. var dependencyGroups = GetDependencyGroups(PackAndGetNuspec(testProject), out var ns);
  163. void ExpectDependencyGroup(string targetFramework, string dependencyId)
  164. {
  165. var matchingGroups = dependencyGroups.Where(dg => dg.Attribute("targetFramework").Value == targetFramework).ToList();
  166. matchingGroups.Count().Should().Be(1);
  167. var dependencies = matchingGroups.Single().Elements(ns + "dependency");
  168. if (dependencyId == null)
  169. {
  170. dependencies.Should().BeEmpty();
  171. }
  172. else
  173. {
  174. dependencies.Count().Should().Be(1);
  175. dependencies.Single().Attribute("id").Value
  176. .Should().Be(dependencyId);
  177. }
  178. }
  179. ExpectDependencyGroup(".NETStandard1.1", "NETStandard.Library");
  180. ExpectDependencyGroup(".NETStandard2.0", null);
  181. ExpectDependencyGroup(".NETCoreApp1.1", "Microsoft.NETCore.App");
  182. ExpectDependencyGroup(ToolsetInfo.CurrentTargetFramework, null);
  183. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  184. {
  185. ExpectDependencyGroup(".NETFramework4.6.2", null);
  186. }
  187. }
  188. private List<XElement> GetDependencyGroups(XDocument nuspec, out XNamespace ns)
  189. {
  190. ns = nuspec.Root.Name.Namespace;
  191. var dependencyGroups = nuspec.Root
  192. .Element(ns + "metadata")
  193. .Element(ns + "dependencies")
  194. .Elements()
  195. .ToList();
  196. return dependencyGroups;
  197. }
  198. private List<XElement> GetFrameworkAssemblies(XDocument nuspec, out XNamespace ns)
  199. {
  200. ns = nuspec.Root.Name.Namespace;
  201. return nuspec.Root
  202. .Element(ns + "metadata")
  203. .Element(ns + "frameworkAssemblies")
  204. .Elements()
  205. .ToList();
  206. }
  207. private XDocument PackAndGetNuspec(TestProject testProject, string identifier = null)
  208. {
  209. var testProjectInstance = _testAssetsManager.CreateTestProject(testProject, testProject.Name, identifier);
  210. var packCommand = new PackCommand(Log, testProjectInstance.TestRoot, testProject.Name);
  211. packCommand.Execute()
  212. .Should()
  213. .Pass();
  214. string nuspecPath = packCommand.GetIntermediateNuspecPath();
  215. var nuspec = XDocument.Load(nuspecPath);
  216. return nuspec;
  217. }
  218. private List<XElement> PackAndGetDependencies(TestProject testProject, string identifier = null)
  219. {
  220. var dependencyGroups = GetDependencyGroups(PackAndGetNuspec(testProject, identifier), out var ns);
  221. // There should be only one dependency group for these tests
  222. dependencyGroups.Count().Should().Be(1);
  223. // It should have the right element name
  224. dependencyGroups.Single().Name.Should().Be(ns + "group");
  225. var dependencies = dependencyGroups.Single().Elements(ns + "dependency").ToList();
  226. return dependencies;
  227. }
  228. private List<XElement> PackAndGetFrameworkAssemblies(TestProject testProject)
  229. {
  230. var frameworkAssemblies = GetFrameworkAssemblies(PackAndGetNuspec(testProject), out var ns);
  231. // There should be only one dependency group for these tests
  232. frameworkAssemblies.Count().Should().Be(1);
  233. frameworkAssemblies.Should().Contain(f => f.Name == "frameworkAssembly");
  234. return frameworkAssemblies.Elements(ns + "frameworkAssembly").ToList();
  235. }
  236. }
  237. }