PublishIntegrationTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 System.Reflection;
  4. namespace Microsoft.NET.Sdk.Razor.Tests
  5. {
  6. public class PublishIntegrationTest : AspNetSdkTest
  7. {
  8. public PublishIntegrationTest(ITestOutputHelper log) : base(log) { }
  9. [Fact]
  10. public void Publish_RazorCompileOnPublish_IsDefault()
  11. {
  12. var testAsset = "RazorSimpleMvc";
  13. var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
  14. var publish = new PublishCommand(projectDirectory);
  15. publish.Execute().Should().Pass();
  16. var outputPath = new BuildCommand(projectDirectory).GetOutputDirectory().FullName;
  17. var publishOutputPath = publish.GetOutputDirectory(DefaultTfm, "Debug").FullName;
  18. new FileInfo(Path.Combine(publishOutputPath, "SimpleMvc.dll")).Should().Exist();
  19. new FileInfo(Path.Combine(publishOutputPath, "SimpleMvc.pdb")).Should().Exist();
  20. new FileInfo(Path.Combine(publishOutputPath, "appsettings.json")).Should().Exist();
  21. new FileInfo(Path.Combine(publishOutputPath, "appsettings.Development.json")).Should().Exist();
  22. new FileInfo(Path.Combine(outputPath, "SimpleMvc.dll")).Should().Exist();
  23. new FileInfo(Path.Combine(outputPath, "SimpleMvc.pdb")).Should().Exist();
  24. new FileInfo(Path.Combine(outputPath, "appsettings.json")).Should().Exist();
  25. new FileInfo(Path.Combine(outputPath, "appsettings.Development.json")).Should().Exist();
  26. // Verify assets get published
  27. new FileInfo(Path.Combine(publishOutputPath, "wwwroot", "js", "SimpleMvc.js")).Should().Exist();
  28. new FileInfo(Path.Combine(publishOutputPath, "wwwroot", "css", "site.css")).Should().Exist();
  29. new FileInfo(Path.Combine(publishOutputPath, "wwwroot", ".well-known", "security.txt")).Should().Exist();
  30. // By default refs and .cshtml files will not be copied on publish
  31. new DirectoryInfo(Path.Combine(publishOutputPath, "refs")).Should().NotExist();
  32. new DirectoryInfo(Path.Combine(publishOutputPath, "Views")).Should().NotExist();
  33. new FileInfo(Path.Combine(publishOutputPath, "wwwroot", ".not-copied", "test.txt")).Should().NotExist();
  34. }
  35. [Fact]
  36. public void Publish_WithRazorCompileOnBuildFalse_PublishesAssembly()
  37. {
  38. var testAsset = "RazorSimpleMvc";
  39. var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
  40. var publish = new PublishCommand(Log, projectDirectory.TestRoot);
  41. publish.Execute("/p:RazorCompileOnBuild=false").Should().Pass();
  42. var outputPath = Path.Combine(projectDirectory.Path, "bin", "Debug", DefaultTfm);
  43. var publishOutputPath = publish.GetOutputDirectory(DefaultTfm, "Debug").ToString();
  44. new FileInfo(Path.Combine(publishOutputPath, "SimpleMvc.dll")).Should().Exist();
  45. new FileInfo(Path.Combine(publishOutputPath, "SimpleMvc.pdb")).Should().Exist();
  46. // By default refs and .cshtml files will not be copied on publish
  47. new DirectoryInfo(Path.Combine(publishOutputPath, "refs")).Should().NotExist();
  48. new DirectoryInfo(Path.Combine(publishOutputPath, "Views")).Should().NotExist();
  49. }
  50. [Fact]
  51. public void Publish_NoopsWith_RazorCompileOnPublishFalse()
  52. {
  53. var testAsset = "RazorSimpleMvc";
  54. var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
  55. Directory.Delete(Path.Combine(projectDirectory.Path, "Views"), recursive: true);
  56. var publish = new PublishCommand(projectDirectory);
  57. publish.Execute("/p:RazorCompileOnPublish=false").Should().Pass();
  58. var publishOutputPath = publish.GetOutputDirectory(DefaultTfm, "Debug").ToString();
  59. // Everything we do should noop - including building the app.
  60. new FileInfo(Path.Combine(publishOutputPath, "SimpleMvc.dll")).Should().Exist();
  61. new FileInfo(Path.Combine(publishOutputPath, "SimpleMvc.pdb")).Should().Exist();
  62. }
  63. [Fact]
  64. public void Publish_IncludeCshtmlAndRefAssemblies_CopiesFiles()
  65. {
  66. var testAsset = "RazorSimpleMvc";
  67. var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
  68. var publish = new PublishCommand(projectDirectory);
  69. publish.Execute("/p:CopyRazorGenerateFilesToPublishDirectory=true", "/p:CopyRefAssembliesToPublishDirectory=true").Should().Pass();
  70. var publishOutputPath = publish.GetOutputDirectory(DefaultTfm, "Debug").ToString();
  71. var intermediateOutputPath = Path.Combine(publish.GetBaseIntermediateDirectory().ToString(), "Debug", DefaultTfm);
  72. new FileInfo(Path.Combine(publishOutputPath, "SimpleMvc.dll")).Should().Exist();
  73. new FileInfo(Path.Combine(publishOutputPath, "SimpleMvc.pdb")).Should().Exist();
  74. // By default refs and .cshtml files will not be copied on publish
  75. new FileInfo(Path.Combine(publishOutputPath, "refs", "mscorlib.dll")).Should().Exist();
  76. new DirectoryInfo(Path.Combine(publishOutputPath, "Views")).Should().NotBeEmpty();
  77. }
  78. [Fact]
  79. public void Publish_WithPreserveCompilationReferencesSetInProjectFile_CopiesRefs()
  80. {
  81. var testAsset = "RazorSimpleMvc";
  82. var projectDirectory = CreateAspNetSdkTestAsset(testAsset)
  83. .WithProjectChanges(project =>
  84. {
  85. var ns = project.Root.Name.Namespace;
  86. var itemGroup = new XElement(ns + "PropertyGroup");
  87. itemGroup.Add(new XElement("PreserveCompilationReferences", true));
  88. project.Root.Add(itemGroup);
  89. });
  90. var publish = new PublishCommand(projectDirectory);
  91. publish.Execute().Should().Pass();
  92. var publishOutputPath = publish.GetOutputDirectory(DefaultTfm, "Debug").ToString();
  93. new FileInfo(Path.Combine(publishOutputPath, "SimpleMvc.dll")).Should().Exist();
  94. new FileInfo(Path.Combine(publishOutputPath, "SimpleMvc.pdb")).Should().Exist();
  95. // By default refs and .cshtml files will not be copied on publish
  96. new FileInfo(Path.Combine(publishOutputPath, "refs", "mscorlib.dll")).Should().Exist();
  97. }
  98. [Fact]
  99. public void Publish_WithP2P_AndRazorCompileOnBuild_CopiesRazorAssembly()
  100. {
  101. var testAsset = "RazorAppWithP2PReference";
  102. var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
  103. var publish = new PublishCommand(projectDirectory, "AppWithP2PReference");
  104. publish.Execute().Should().Pass();
  105. var publishOutputPath = publish.GetOutputDirectory(DefaultTfm, "Debug").ToString();
  106. new FileInfo(Path.Combine(publishOutputPath, "AppWithP2PReference.dll")).Should().Exist();
  107. new FileInfo(Path.Combine(publishOutputPath, "AppWithP2PReference.pdb")).Should().Exist();
  108. new FileInfo(Path.Combine(publishOutputPath, "ClassLibrary.dll")).Should().Exist();
  109. new FileInfo(Path.Combine(publishOutputPath, "ClassLibrary.pdb")).Should().Exist();
  110. // Verify fix for https://github.com/aspnet/Razor/issues/2295. No cshtml files should be published from the app
  111. // or the ClassLibrary.
  112. new DirectoryInfo(Path.Combine(publishOutputPath, "refs")).Should().NotExist();
  113. new DirectoryInfo(Path.Combine(publishOutputPath, "Views")).Should().NotExist();
  114. }
  115. [Fact]
  116. public void Publish_WithP2P_WorksWhenBuildProjectReferencesIsDisabled()
  117. {
  118. // Simulates publishing the same way VS does by setting BuildProjectReferences=false.
  119. // With this flag, P2P references aren't resolved during GetCopyToPublishDirectoryItems which would cause
  120. // any target that uses References as inputs to not be incremental. This test verifies no Razor Sdk work
  121. // is performed at this time.
  122. var testAsset = "RazorAppWithP2PReference";
  123. var projectDirectory = CreateAspNetSdkTestAsset(testAsset)
  124. .WithProjectChanges((path, project) =>
  125. {
  126. if (path.Contains("AppWithP2PReference"))
  127. {
  128. var ns = project.Root.Name.Namespace;
  129. var itemGroup = new XElement(ns + "ItemGroup");
  130. itemGroup.Add(new XElement("ProjectReference", new XAttribute("Include", "..\\AnotherClassLib\\AnotherClassLib.csproj")));
  131. project.Root.Add(itemGroup);
  132. }
  133. });
  134. var build = new BuildCommand(projectDirectory, "AppWithP2PReference");
  135. build.Execute().Should().Pass();
  136. var outputPath = build.GetOutputDirectory(DefaultTfm, "Debug").ToString();
  137. new FileInfo(Path.Combine(outputPath, "AppWithP2PReference.dll")).Should().Exist();
  138. new FileInfo(Path.Combine(outputPath, "ClassLibrary.dll")).Should().Exist();
  139. new FileInfo(Path.Combine(outputPath, "AnotherClassLib.dll")).Should().Exist();
  140. // dotnet msbuild /t:Publish /p:BuildProjectReferences=false
  141. var publish = new PublishCommand(projectDirectory, "AppWithP2PReference");
  142. publish.WithWorkingDirectory(projectDirectory.TestRoot);
  143. publish.Execute("/p:BuildProjectReferences=false", "/p:ErrorOnDuplicatePublishOutputFiles=false", "/bl").Should().Pass();
  144. var publishOutputPath = publish.GetOutputDirectory(DefaultTfm, "Debug").ToString();
  145. new FileInfo(Path.Combine(publishOutputPath, "AppWithP2PReference.dll")).Should().Exist();
  146. new FileInfo(Path.Combine(publishOutputPath, "AppWithP2PReference.pdb")).Should().Exist();
  147. new FileInfo(Path.Combine(publishOutputPath, "ClassLibrary.dll")).Should().Exist();
  148. new FileInfo(Path.Combine(publishOutputPath, "ClassLibrary.pdb")).Should().Exist();
  149. new FileInfo(Path.Combine(publishOutputPath, "AnotherClassLib.dll")).Should().Exist();
  150. new FileInfo(Path.Combine(publishOutputPath, "AnotherClassLib.pdb")).Should().Exist();
  151. }
  152. [Fact]
  153. public void Publish_WithNoBuild_CopiesAlreadyCompiledViews()
  154. {
  155. var testAsset = "RazorSimpleMvc";
  156. var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
  157. // Build
  158. var build = new BuildCommand(projectDirectory);
  159. build.Execute("/p:AssemblyVersion=1.1.1.1").Should().Pass();
  160. var outputPath = build.GetOutputDirectory(DefaultTfm, "Debug").ToString();
  161. var assemblyPath = Path.Combine(outputPath, "SimpleMvc.dll");
  162. new FileInfo(assemblyPath).Should().Exist();
  163. var assemblyVersion = AssemblyName.GetAssemblyName(assemblyPath).Version;
  164. // Publish should copy dlls from OutputPath
  165. var publish = new PublishCommand(projectDirectory);
  166. publish.Execute("/p:NoBuild=true").Should().Pass();
  167. var publishOutputPath = publish.GetOutputDirectory(DefaultTfm, "Debug").ToString();
  168. var publishAssemblyPath = Path.Combine(publishOutputPath, "SimpleMvc.dll");
  169. new FileInfo(publishAssemblyPath).Should().Exist();
  170. var publishAssemblyVersion = AssemblyName.GetAssemblyName(publishAssemblyPath).Version;
  171. Assert.Equal(assemblyVersion, publishAssemblyVersion);
  172. }
  173. }
  174. }