GiventThatWeWantDesignerSupport.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.Extensions.DependencyModel;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. namespace Microsoft.NET.Build.Tests
  7. {
  8. public class GivenThatWeWantDesignerSupport : SdkTest
  9. {
  10. public GivenThatWeWantDesignerSupport(ITestOutputHelper log) : base(log)
  11. {
  12. }
  13. [Theory]
  14. [InlineData("net46", "false")]
  15. [InlineData("netcoreapp3.0", "true")]
  16. [InlineData("netcoreapp3.0", "false")]
  17. [InlineData("net5.0-windows", "true")]
  18. [InlineData("net5.0-windows", "false")]
  19. [InlineData("net7.0-windows10.0.17763", "true")]
  20. [InlineData("net7.0-windows10.0.17763", "false")]
  21. public void It_provides_runtime_configuration_and_shadow_copy_files_via_outputgroup(string targetFramework, string isSelfContained)
  22. {
  23. if ((targetFramework == "net5.0-windows" || targetFramework == "net7.0-windows10.0.17763")
  24. && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  25. {
  26. // net5.0-windows is windows only scenario
  27. return;
  28. }
  29. var projectRef = new TestProject
  30. {
  31. Name = "ReferencedProject",
  32. TargetFrameworks = targetFramework,
  33. };
  34. var project = new TestProject
  35. {
  36. Name = "DesignerTest",
  37. IsExe = true,
  38. TargetFrameworks = targetFramework,
  39. PackageReferences = { new TestPackageReference("NewtonSoft.Json", ToolsetInfo.GetNewtonsoftJsonPackageVersion()) },
  40. ReferencedProjects = { projectRef },
  41. SelfContained = isSelfContained
  42. };
  43. var asset = _testAssetsManager
  44. .CreateTestProject(project, identifier: targetFramework);
  45. var command = new GetValuesCommand(
  46. Log,
  47. Path.Combine(asset.Path, project.Name),
  48. targetFramework,
  49. "DesignerRuntimeImplementationProjectOutputGroupOutput",
  50. GetValuesCommand.ValueType.Item)
  51. {
  52. DependsOnTargets = "DesignerRuntimeImplementationProjectOutputGroup",
  53. MetadataNames = { "TargetPath" },
  54. };
  55. command.Execute().Should().Pass();
  56. var items =
  57. from item in command.GetValuesWithMetadata()
  58. select new
  59. {
  60. Identity = item.value,
  61. TargetPath = item.metadata["TargetPath"]
  62. };
  63. string depsFile = null;
  64. string runtimeConfig = null;
  65. var otherFiles = new List<string>();
  66. foreach (var item in items)
  67. {
  68. Path.IsPathFullyQualified(item.Identity).Should().BeTrue();
  69. Path.GetFileName(item.Identity).Should().Be(item.TargetPath);
  70. switch (item.TargetPath)
  71. {
  72. case "DesignerTest.designer.deps.json":
  73. depsFile = item.Identity;
  74. break;
  75. case "DesignerTest.designer.runtimeconfig.json":
  76. runtimeConfig = item.Identity;
  77. break;
  78. default:
  79. otherFiles.Add(item.TargetPath);
  80. break;
  81. }
  82. }
  83. switch (targetFramework)
  84. {
  85. case "netcoreapp3.0":
  86. case "net5.0-windows":
  87. case "net7.0-windows10.0.17763":
  88. var depsFileLibraries = GetRuntimeLibraryFileNames(depsFile);
  89. depsFileLibraries.Should().BeEquivalentTo(new[] { "Newtonsoft.Json.dll" });
  90. var options = GetRuntimeOptions(runtimeConfig);
  91. options["configProperties"]["Microsoft.NETCore.DotNetHostPolicy.SetAppPaths"].Value<bool>().Should().BeTrue();
  92. // runtimeconfiguration should not have platform.
  93. // it should be net5.0 instead of net5.0-windows
  94. options["tfm"].Value<string>().Should().Be(targetFramework.Split('-')[0]);
  95. options["additionalProbingPaths"].Value<JArray>().Should().NotBeEmpty();
  96. if (targetFramework == "net7.0-windows10.0.17763")
  97. {
  98. otherFiles.Should().BeEquivalentTo(["ReferencedProject.dll", "ReferencedProject.pdb", "Microsoft.Windows.SDK.NET.dll", "WinRT.Runtime.dll"]);
  99. }
  100. else
  101. {
  102. otherFiles.Should().BeEquivalentTo(["ReferencedProject.dll", "ReferencedProject.pdb"]);
  103. }
  104. break;
  105. case "net46":
  106. depsFile.Should().BeNull();
  107. runtimeConfig.Should().BeNull();
  108. otherFiles.Should().BeEquivalentTo(["Newtonsoft.Json.dll", "ReferencedProject.dll", "ReferencedProject.pdb"]);
  109. break;
  110. }
  111. }
  112. private static JToken GetRuntimeOptions(string runtimeConfigFilePath)
  113. {
  114. var config = ParseRuntimeConfig(runtimeConfigFilePath);
  115. return config["runtimeOptions"];
  116. }
  117. private static IEnumerable<string> GetRuntimeLibraryFileNames(string depsFilePath)
  118. {
  119. var deps = ParseDepsFile(depsFilePath);
  120. return deps.RuntimeLibraries
  121. .SelectMany(r => r.RuntimeAssemblyGroups)
  122. .SelectMany(a => a.AssetPaths)
  123. .Select(p => Path.GetFileName(p));
  124. }
  125. private static JToken ParseRuntimeConfig(string path)
  126. {
  127. using (var streamReader = File.OpenText(path))
  128. using (var jsonReader = new JsonTextReader(streamReader))
  129. {
  130. return JObject.Load(jsonReader);
  131. }
  132. }
  133. private static DependencyContext ParseDepsFile(string path)
  134. {
  135. using (var stream = File.OpenRead(path))
  136. using (var reader = new DependencyContextJsonReader())
  137. {
  138. return reader.Read(stream);
  139. }
  140. }
  141. }
  142. }