GivenThatWeWantToPackANetFrameworkLibrary.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 GivenThatWeWantToPackANetFrameworkLibrary : SdkTest
  6. {
  7. public GivenThatWeWantToPackANetFrameworkLibrary(ITestOutputHelper log) : base(log)
  8. {
  9. }
  10. [WindowsOnlyFact]
  11. public void ImplicitReferencesAreNotIncludedAsFrameworkReferences()
  12. {
  13. TestProject testProject = new()
  14. {
  15. Name = "PackImplicitReferences",
  16. TargetFrameworks = "net462",
  17. IsExe = false
  18. };
  19. var nuspecPath = PackAndGetNuspecPath(testProject);
  20. var nuspec = XDocument.Load(nuspecPath);
  21. var ns = nuspec.Root.Name.Namespace;
  22. var frameworkAssemblies = nuspec.Root
  23. .Element(ns + "metadata")
  24. .Element(ns + "frameworkAssemblies");
  25. frameworkAssemblies.Should().BeNull();
  26. }
  27. [WindowsOnlyFact]
  28. public void ExplicitReferencesAreIncludedAsFrameworkReferences()
  29. {
  30. TestProject testProject = new()
  31. {
  32. Name = "PackImplicitReferences",
  33. TargetFrameworks = "net462",
  34. IsExe = false
  35. };
  36. var nuspecPath = PackAndGetNuspecPath(testProject,
  37. "PackImplicitRefs",
  38. p =>
  39. {
  40. var pns = p.Root.Name.Namespace;
  41. p.Root.Add(new XElement(pns + "ItemGroup",
  42. new XElement(pns + "Reference", new XAttribute("Include", "System")),
  43. new XElement(pns + "Reference", new XAttribute("Include", "System.Xml.Linq"))));
  44. });
  45. var nuspec = XDocument.Load(nuspecPath);
  46. var ns = nuspec.Root.Name.Namespace;
  47. var frameworkAssemblies = nuspec.Root
  48. .Element(ns + "metadata")
  49. .Element(ns + "frameworkAssemblies")
  50. .Elements(ns + "frameworkAssembly");
  51. frameworkAssemblies.Count().Should().Be(2);
  52. frameworkAssemblies.Should().Contain(i => i.Attribute("assemblyName").Value == "System");
  53. frameworkAssemblies.Should().Contain(i => i.Attribute("assemblyName").Value == "System.Xml.Linq");
  54. }
  55. private string PackAndGetNuspecPath(TestProject testProject, string identifier = null, Action<XDocument> xmlAction = null)
  56. {
  57. var testProjectInstance = _testAssetsManager.CreateTestProject(testProject, testProject.Name, identifier);
  58. if (xmlAction != null)
  59. {
  60. testProjectInstance = testProjectInstance.WithProjectChanges(xmlAction);
  61. }
  62. var packCommand = new PackCommand(Log, testProjectInstance.TestRoot, testProject.Name);
  63. packCommand.Execute()
  64. .Should()
  65. .Pass();
  66. string nuspecPath = packCommand.GetIntermediateNuspecPath();
  67. return nuspecPath;
  68. }
  69. }
  70. }