GivenThatWeWantToPackAHelloWorldProject.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. using Microsoft.DotNet.Cli;
  4. namespace Microsoft.NET.Pack.Tests
  5. {
  6. public class GivenThatWeWantToPackAHelloWorldProject : SdkTest
  7. {
  8. public GivenThatWeWantToPackAHelloWorldProject(ITestOutputHelper log) : base(log)
  9. {
  10. }
  11. [Fact]
  12. public void It_packs_successfully()
  13. {
  14. var helloWorldAsset = _testAssetsManager
  15. .CopyTestAsset("HelloWorld", "PackHelloWorld")
  16. .WithSource();
  17. var packCommand = new PackCommand(Log, helloWorldAsset.TestRoot);
  18. packCommand
  19. .Execute()
  20. .Should()
  21. .Pass();
  22. // Validate the contents of the NuGet package by looking at the generated .nuspec file, as that's simpler
  23. // than unzipping and inspecting the .nupkg
  24. string nuspecPath = packCommand.GetIntermediateNuspecPath();
  25. var nuspec = XDocument.Load(nuspecPath);
  26. var ns = nuspec.Root.Name.Namespace;
  27. XElement filesSection = nuspec.Root.Element(ns + "files");
  28. var fileTargets = filesSection.Elements().Select(files => files.Attribute("target").Value).ToList();
  29. var expectedFileTargets = new[]
  30. {
  31. $@"lib\{ToolsetInfo.CurrentTargetFramework}\HelloWorld.runtimeconfig.json",
  32. $@"lib\{ToolsetInfo.CurrentTargetFramework}\HelloWorld.dll"
  33. }.Select(p => p.Replace('\\', Path.DirectorySeparatorChar));
  34. fileTargets.Should().BeEquivalentTo(expectedFileTargets);
  35. }
  36. [Fact]
  37. public void It_fails_if_nobuild_was_requested_but_build_was_invoked()
  38. {
  39. var testProject = new TestProject()
  40. {
  41. Name = "InvokeBuildOnPack",
  42. TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
  43. IsExe = true
  44. };
  45. var testAsset = _testAssetsManager.CreateTestProject(testProject, testProject.Name)
  46. .WithProjectChanges(project =>
  47. {
  48. project.Root.Add(XElement.Parse(@"<Target Name=""InvokeBuild"" DependsOnTargets=""Build"" BeforeTargets=""Pack"" />"));
  49. });
  50. new BuildCommand(testAsset)
  51. .Execute()
  52. .Should()
  53. .Pass();
  54. new PackCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name))
  55. .Execute("/p:NoBuild=true")
  56. .Should()
  57. .Fail()
  58. .And
  59. .HaveStdOutContaining("NETSDK1085");
  60. }
  61. [Theory]
  62. [InlineData(true)]
  63. [InlineData(false)]
  64. public void It_packs_with_release_if_PackRelease_property_set(bool optedOut)
  65. {
  66. var helloWorldAsset = _testAssetsManager
  67. .CopyTestAsset("HelloWorld", identifier: optedOut.ToString())
  68. .WithSource();
  69. File.WriteAllText(Path.Combine(helloWorldAsset.Path, "Directory.Build.props"), "<Project><PropertyGroup><PackRelease>true</PackRelease></PropertyGroup></Project>");
  70. var packCommand = new DotnetPackCommand(Log, helloWorldAsset.TestRoot);
  71. packCommand
  72. .WithEnvironmentVariable(EnvironmentVariableNames.DISABLE_PUBLISH_AND_PACK_RELEASE, optedOut.ToString())
  73. .Execute()
  74. .Should()
  75. .Pass();
  76. var expectedAssetPath = Path.Combine(helloWorldAsset.Path, "bin", optedOut ? "Debug" : "Release", "HelloWorld.1.0.0.nupkg");
  77. Assert.True(File.Exists(expectedAssetPath));
  78. }
  79. [Theory]
  80. [InlineData("true")]
  81. [InlineData("false")]
  82. public void It_packs_with_release_if_PackRelease_property_set_in_csproj(string valueOfPackRelease)
  83. {
  84. var helloWorldAsset = _testAssetsManager
  85. .CopyTestAsset("HelloWorld")
  86. .WithSource()
  87. .WithProjectChanges(project =>
  88. {
  89. var ns = project.Root.Name.Namespace;
  90. var propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
  91. propertyGroup.Add(new XElement(ns + "PackRelease", valueOfPackRelease));
  92. });
  93. var packCommand = new DotnetPackCommand(Log, helloWorldAsset.TestRoot);
  94. packCommand
  95. .Execute()
  96. .Should()
  97. .Pass();
  98. var expectedAssetPath = Path.Combine(helloWorldAsset.Path, "bin", valueOfPackRelease == "true" ? "Release" : "Debug", "HelloWorld.1.0.0.nupkg");
  99. new FileInfo(expectedAssetPath).Should().Exist();
  100. }
  101. [InlineData("")]
  102. [InlineData("false")]
  103. [Theory]
  104. public void It_packs_successfully_with_Multitargeting_where_net_8_and_net_7_project_defines_PackRelease_or_not(string packReleaseValue)
  105. {
  106. var helloWorldAsset = _testAssetsManager
  107. .CopyTestAsset("HelloWorld", identifier: packReleaseValue)
  108. .WithSource()
  109. .WithTargetFrameworks("net8.0;net7.0")
  110. .WithProjectChanges(project =>
  111. {
  112. var ns = project.Root.Name.Namespace;
  113. var propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
  114. if (packReleaseValue != "")
  115. {
  116. propertyGroup
  117. .Add(new XElement(ns + "PackRelease", packReleaseValue));
  118. };
  119. });
  120. var packCommand = new DotnetPackCommand(Log, helloWorldAsset.TestRoot);
  121. packCommand
  122. .Execute()
  123. .Should()
  124. .Pass();
  125. string expectedConfiguration = packReleaseValue == "false" ? "Debug" : "Release";
  126. var expectedAssetPath = Path.Combine(helloWorldAsset.Path, "bin", expectedConfiguration, "HelloWorld.1.0.0.nupkg");
  127. new FileInfo(expectedAssetPath).Should().Exist();
  128. }
  129. [Fact]
  130. public void A_PackRelease_property_does_not_affect_other_commands_besides_pack()
  131. {
  132. var tfm = "net8.0";
  133. var helloWorldAsset = _testAssetsManager
  134. .CopyTestAsset("HelloWorld")
  135. .WithSource()
  136. .WithTargetFramework(tfm);
  137. File.WriteAllText(helloWorldAsset.Path + "/Directory.Build.props", "<Project><PropertyGroup><PackRelease>false</PackRelease></PropertyGroup></Project>");
  138. var publishCommand = new DotnetPublishCommand(Log, helloWorldAsset.TestRoot);
  139. publishCommand
  140. .Execute()
  141. .Should()
  142. .Pass();
  143. var unexpectedAssetPath = Path.Combine(helloWorldAsset.Path, "bin", "Debug", tfm, "HelloWorld.dll");
  144. Assert.False(File.Exists(unexpectedAssetPath));
  145. var expectedAssetPath = Path.Combine(helloWorldAsset.Path, "bin", "Release", tfm, "HelloWorld.dll");
  146. Assert.True(File.Exists(expectedAssetPath));
  147. }
  148. }
  149. }