|
| 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 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics.Contracts; |
| 8 | +using System.IO; |
| 9 | +using System.Linq; |
| 10 | +using System.Runtime.CompilerServices; |
| 11 | +using System.Text; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using FluentAssertions; |
| 14 | +using Microsoft.NET.TestFramework; |
| 15 | +using Microsoft.NET.TestFramework.Assertions; |
| 16 | +using Microsoft.NET.TestFramework.Commands; |
| 17 | +using Microsoft.NET.TestFramework.ProjectConstruction; |
| 18 | +using Xunit; |
| 19 | +using Xunit.Abstractions; |
| 20 | +using Xunit.Sdk; |
| 21 | + |
| 22 | + |
| 23 | +namespace Microsoft.NET.Build.Tests |
| 24 | +{ |
| 25 | + public class RootOutputPathTests : SdkTest |
| 26 | + { |
| 27 | + public RootOutputPathTests(ITestOutputHelper log) : base(log) |
| 28 | + { |
| 29 | + } |
| 30 | + |
| 31 | + (List<TestProject> testProjects, TestAsset testAsset) GetTestProjects(bool setRootOutputInProject, [CallerMemberName] string callingMethod = "") |
| 32 | + { |
| 33 | + var testProject1 = new TestProject() |
| 34 | + { |
| 35 | + Name = "App1", |
| 36 | + IsExe = true |
| 37 | + }; |
| 38 | + |
| 39 | + var testProject2 = new TestProject() |
| 40 | + { |
| 41 | + Name = "App2", |
| 42 | + IsExe = true |
| 43 | + }; |
| 44 | + |
| 45 | + var testLibraryProject = new TestProject() |
| 46 | + { |
| 47 | + Name = "Library", |
| 48 | + }; |
| 49 | + |
| 50 | + testProject1.ReferencedProjects.Add(testLibraryProject); |
| 51 | + testProject2.ReferencedProjects.Add(testLibraryProject); |
| 52 | + |
| 53 | + List<TestProject> testProjects = new() { testProject1, testProject2, testLibraryProject }; |
| 54 | + |
| 55 | + if (setRootOutputInProject) |
| 56 | + { |
| 57 | + foreach (var testProject in testProjects) |
| 58 | + { |
| 59 | + testProject.AdditionalProperties["RootOutputPath"] = "..\\artifacts"; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + var testAsset = _testAssetsManager.CreateTestProjects(testProjects, callingMethod: callingMethod, identifier: setRootOutputInProject.ToString()); |
| 64 | + |
| 65 | + if (!setRootOutputInProject) |
| 66 | + { |
| 67 | + File.WriteAllText(Path.Combine(testAsset.Path, "Directory.Build.props"), |
| 68 | + """ |
| 69 | + <Project> |
| 70 | + <PropertyGroup> |
| 71 | + <RootOutputPath>$(MSBuildThisFileDirectory)\artifacts</RootOutputPath> |
| 72 | + </PropertyGroup> |
| 73 | + </Project> |
| 74 | + """); |
| 75 | + } |
| 76 | + |
| 77 | + return (testProjects, testAsset); |
| 78 | + } |
| 79 | + |
| 80 | + [Theory] |
| 81 | + [InlineData(true)] |
| 82 | + [InlineData(false)] |
| 83 | + public void ItUsesRootOutputPathForBuild(bool setRootOutputInProject) |
| 84 | + { |
| 85 | + var (testProjects, testAsset) = GetTestProjects(setRootOutputInProject); |
| 86 | + |
| 87 | + new DotnetCommand(Log, "build") |
| 88 | + .WithWorkingDirectory(testAsset.Path) |
| 89 | + .Execute() |
| 90 | + .Should() |
| 91 | + .Pass(); |
| 92 | + |
| 93 | + ValidateIntermediatePaths(testAsset, testProjects, setRootOutputInProject); |
| 94 | + |
| 95 | + foreach (var testProject in testProjects) |
| 96 | + { |
| 97 | + new FileInfo(Path.Combine(testAsset.TestRoot, "artifacts", "build", testProject.Name, "debug", testProject.Name + ".dll")) |
| 98 | + .Should() |
| 99 | + .Exist(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [Theory] |
| 104 | + [InlineData(true)] |
| 105 | + [InlineData(false)] |
| 106 | + public void ItUsesRootOutputPathForPublish(bool setRootOutputInProject) |
| 107 | + { |
| 108 | + var (testProjects, testAsset) = GetTestProjects(setRootOutputInProject); |
| 109 | + |
| 110 | + new DotnetCommand(Log, "publish") |
| 111 | + .WithWorkingDirectory(testAsset.Path) |
| 112 | + .Execute() |
| 113 | + .Should() |
| 114 | + .Pass(); |
| 115 | + |
| 116 | + ValidateIntermediatePaths(testAsset, testProjects, setRootOutputInProject); |
| 117 | + |
| 118 | + foreach (var testProject in testProjects) |
| 119 | + { |
| 120 | + new FileInfo(Path.Combine(testAsset.TestRoot, "artifacts", "build", testProject.Name, "debug", testProject.Name + ".dll")) |
| 121 | + .Should() |
| 122 | + .Exist(); |
| 123 | + |
| 124 | + new FileInfo(Path.Combine(testAsset.TestRoot, "artifacts", "publish", testProject.Name, "debug", testProject.Name + ".dll")) |
| 125 | + .Should() |
| 126 | + .Exist(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + [Theory] |
| 131 | + [InlineData(true)] |
| 132 | + [InlineData(false)] |
| 133 | + public void ItUsesRootOutputPathForPack(bool setRootOutputInProject) |
| 134 | + { |
| 135 | + var (testProjects, testAsset) = GetTestProjects(setRootOutputInProject); |
| 136 | + |
| 137 | + new DotnetCommand(Log, "pack") |
| 138 | + .WithWorkingDirectory(testAsset.Path) |
| 139 | + .Execute() |
| 140 | + .Should() |
| 141 | + .Pass(); |
| 142 | + |
| 143 | + ValidateIntermediatePaths(testAsset, testProjects, setRootOutputInProject); |
| 144 | + |
| 145 | + foreach (var testProject in testProjects) |
| 146 | + { |
| 147 | + new FileInfo(Path.Combine(testAsset.TestRoot, "artifacts", "build", testProject.Name, "debug", testProject.Name + ".dll")) |
| 148 | + .Should() |
| 149 | + .Exist(); |
| 150 | + |
| 151 | + new FileInfo(Path.Combine(testAsset.TestRoot, "artifacts", "package", "debug", testProject.Name + ".1.0.0.nupkg")) |
| 152 | + .Should() |
| 153 | + .Exist(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + void ValidateIntermediatePaths(TestAsset testAsset, IEnumerable<TestProject> testProjects, bool setRootOutputInProject) |
| 158 | + { |
| 159 | + foreach (var testProject in testProjects) |
| 160 | + { |
| 161 | + if (setRootOutputInProject) |
| 162 | + { |
| 163 | + new DirectoryInfo(Path.Combine(testAsset.TestRoot, testProject.Name)) |
| 164 | + .Should() |
| 165 | + .HaveDirectory("obj"); |
| 166 | + |
| 167 | + new DirectoryInfo(Path.Combine(testAsset.TestRoot, testProject.Name, "bin")) |
| 168 | + .Should() |
| 169 | + .NotExist(); |
| 170 | + |
| 171 | + new DirectoryInfo(Path.Combine(testAsset.TestRoot, "artifacts", "obj")) |
| 172 | + .Should() |
| 173 | + .NotExist(); |
| 174 | + } |
| 175 | + else |
| 176 | + { |
| 177 | + new DirectoryInfo(Path.Combine(testAsset.TestRoot, testProject.Name)) |
| 178 | + .Should() |
| 179 | + .NotHaveSubDirectories(); |
| 180 | + |
| 181 | + new DirectoryInfo(Path.Combine(testAsset.TestRoot, "artifacts", "obj", testProject.Name, "debug")) |
| 182 | + .Should() |
| 183 | + .Exist(); |
| 184 | + }; |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments