|
| 1 | +// Copyright (c) .NET Foundation and contributors. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Text.Json.Nodes; |
| 7 | +using FluentAssertions; |
| 8 | +using Microsoft.NET.TestFramework; |
| 9 | +using Microsoft.NET.TestFramework.Assertions; |
| 10 | +using Microsoft.NET.TestFramework.Commands; |
| 11 | +using Microsoft.NET.TestFramework.ProjectConstruction; |
| 12 | +using Xunit; |
| 13 | +using Xunit.Abstractions; |
| 14 | + |
| 15 | +namespace Microsoft.NET.Sdk.Web.Tests |
| 16 | +{ |
| 17 | + public class PublishTests : SdkTest |
| 18 | + { |
| 19 | + public PublishTests(ITestOutputHelper log) : base(log) |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + [Theory()] |
| 24 | + [MemberData(nameof(SupportedTfms))] |
| 25 | + public void TrimmingOptions_Are_Defaulted_Correctly_On_Trimmed_Apps(string targetFramework) |
| 26 | + { |
| 27 | + var projectName = "HelloWorld"; |
| 28 | + var rid = EnvironmentInfo.GetCompatibleRid(targetFramework); |
| 29 | + |
| 30 | + var testProject = CreateTestProjectForILLinkTesting(targetFramework, projectName); |
| 31 | + testProject.AdditionalProperties["PublishTrimmed"] = "true"; |
| 32 | + |
| 33 | + var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: projectName + targetFramework); |
| 34 | + |
| 35 | + var publishCommand = new PublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); |
| 36 | + publishCommand.Execute($"/p:RuntimeIdentifier={rid}").Should().Pass(); |
| 37 | + |
| 38 | + string outputDirectory = publishCommand.GetOutputDirectory(targetFramework: targetFramework, runtimeIdentifier: rid).FullName; |
| 39 | + string runtimeConfigFile = Path.Combine(outputDirectory, $"{projectName}.runtimeconfig.json"); |
| 40 | + string runtimeConfigContents = File.ReadAllText(runtimeConfigFile); |
| 41 | + |
| 42 | + JsonNode runtimeConfig = JsonNode.Parse(runtimeConfigContents); |
| 43 | + JsonNode configProperties = runtimeConfig["runtimeOptions"]["configProperties"]; |
| 44 | + |
| 45 | + configProperties["Microsoft.AspNetCore.EnsureJsonTrimmability"].GetValue<bool>() |
| 46 | + .Should().BeTrue(); |
| 47 | + } |
| 48 | + |
| 49 | + [Theory()] |
| 50 | + [MemberData(nameof(SupportedTfms))] |
| 51 | + public void TrimmingOptions_Are_Defaulted_Correctly_On_Aot_Apps(string targetFramework) |
| 52 | + { |
| 53 | + var projectName = "HelloWorld"; |
| 54 | + var rid = EnvironmentInfo.GetCompatibleRid(targetFramework); |
| 55 | + |
| 56 | + var testProject = CreateTestProjectForILLinkTesting(targetFramework, projectName); |
| 57 | + testProject.AdditionalProperties["PublishAOT"] = "true"; |
| 58 | + |
| 59 | + var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: projectName + targetFramework); |
| 60 | + var publishCommand = new PublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); |
| 61 | + publishCommand.Execute($"/p:RuntimeIdentifier={rid}").Should().Pass(); |
| 62 | + |
| 63 | + string outputDirectory = publishCommand.GetIntermediateDirectory(targetFramework: targetFramework, runtimeIdentifier: rid).FullName; |
| 64 | + string responseFile = Path.Combine(outputDirectory, "native", $"{projectName}.ilc.rsp"); |
| 65 | + var responseFileContents = File.ReadLines(responseFile); |
| 66 | + |
| 67 | + responseFileContents.Should().Contain("--feature:Microsoft.AspNetCore.EnsureJsonTrimmability=true"); |
| 68 | + } |
| 69 | + |
| 70 | + public static IEnumerable<object[]> SupportedTfms { get; } = new List<object[]> |
| 71 | + { |
| 72 | +#if NET8_0 |
| 73 | + new object[] { ToolsetInfo.CurrentTargetFramework } |
| 74 | +#else |
| 75 | +#error If building for a newer TFM, please update the values above |
| 76 | +#endif |
| 77 | + }; |
| 78 | + |
| 79 | + private TestProject CreateTestProjectForILLinkTesting( |
| 80 | + string targetFramework, |
| 81 | + string projectName) |
| 82 | + { |
| 83 | + var testProject = new TestProject() |
| 84 | + { |
| 85 | + Name = projectName, |
| 86 | + TargetFrameworks = targetFramework, |
| 87 | + IsExe = true, |
| 88 | + ProjectSdk = "Microsoft.NET.Sdk.Web" |
| 89 | + }; |
| 90 | + |
| 91 | + testProject.SourceFiles[$"Program.cs"] = """ |
| 92 | + using Microsoft.AspNetCore.Builder; |
| 93 | + using Microsoft.Extensions.Hosting; |
| 94 | +
|
| 95 | + var builder = WebApplication.CreateBuilder(); |
| 96 | + var app = builder.Build(); |
| 97 | + app.Start(); |
| 98 | + """; |
| 99 | + |
| 100 | + return testProject; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments