|
| 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 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using AwesomeAssertions; |
| 7 | +using Microsoft.Arcade.Test.Common; |
| 8 | +using Microsoft.Build.Framework; |
| 9 | +using Microsoft.Build.Utilities; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +#nullable enable |
| 13 | +namespace Microsoft.DotNet.Helix.Sdk.Tests |
| 14 | +{ |
| 15 | + public class CreateMTPWorkItemsTests |
| 16 | + { |
| 17 | + private static CreateMTPWorkItems CreateTask() => |
| 18 | + new CreateMTPWorkItems |
| 19 | + { |
| 20 | + BuildEngine = new MockBuildEngine(), |
| 21 | + IsPosixShell = false, |
| 22 | + }; |
| 23 | + |
| 24 | + private static ITaskItem CreateProject( |
| 25 | + string itemSpec, |
| 26 | + string? publishDirectory = "/publish", |
| 27 | + string? targetPath = "/publish/MyApp.Tests.dll", |
| 28 | + string? arguments = null, |
| 29 | + string? additionalProperties = null) |
| 30 | + { |
| 31 | + var metadata = new Dictionary<string, string>(); |
| 32 | + if (publishDirectory != null) metadata["PublishDirectory"] = publishDirectory; |
| 33 | + if (targetPath != null) metadata["TargetPath"] = targetPath; |
| 34 | + if (arguments != null) metadata["Arguments"] = arguments; |
| 35 | + if (additionalProperties != null) metadata["AdditionalProperties"] = additionalProperties; |
| 36 | + return new TaskItem(itemSpec, metadata); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void GeneratedCommandHasExpectedShape() |
| 41 | + { |
| 42 | + var task = CreateTask(); |
| 43 | + task.MTPProjects = new[] { CreateProject("MyApp.Tests.csproj") }; |
| 44 | + |
| 45 | + task.Execute().Should().BeTrue(); |
| 46 | + |
| 47 | + task.MTPWorkItems.Should().HaveCount(1); |
| 48 | + var workItem = task.MTPWorkItems.Single(); |
| 49 | + workItem.GetMetadata("Identity").Should().Be("MyApp.Tests.dll"); |
| 50 | + workItem.GetMetadata("PayloadDirectory").Should().Be("/publish"); |
| 51 | + workItem.GetMetadata("Timeout").Should().Be("00:05:00"); |
| 52 | + |
| 53 | + var command = workItem.GetMetadata("Command"); |
| 54 | + command.Should().StartWith("dotnet exec --roll-forward Major "); |
| 55 | + command.Should().Contain("--runtimeconfig MyApp.Tests.runtimeconfig.json"); |
| 56 | + command.Should().Contain("--depsfile MyApp.Tests.deps.json"); |
| 57 | + command.Should().Contain("MyApp.Tests.dll"); |
| 58 | + command.Should().EndWith(" --results-directory ."); |
| 59 | + command.Should().NotContain("--report-trx"); |
| 60 | + command.Should().NotContain("--auto-reporters"); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void ArgumentsMetadataIsAppendedAfterReporterArgs() |
| 65 | + { |
| 66 | + var task = CreateTask(); |
| 67 | + task.MTPProjects = new[] { CreateProject("MyApp.Tests.csproj", arguments: "--filter Category=Smoke") }; |
| 68 | + |
| 69 | + task.Execute().Should().BeTrue(); |
| 70 | + |
| 71 | + var command = task.MTPWorkItems.Single().GetMetadata("Command"); |
| 72 | + command.Should().EndWith("--results-directory . --filter Category=Smoke"); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void MTPAdditionalArgumentsIsInsertedBeforePerProjectArguments() |
| 77 | + { |
| 78 | + var task = CreateTask(); |
| 79 | + task.MTPAdditionalArguments = "--report-trx --report-trx-filename testResults.trx --auto-reporters off"; |
| 80 | + task.MTPProjects = new[] { CreateProject("MyApp.Tests.csproj", arguments: "--filter Category=Smoke") }; |
| 81 | + |
| 82 | + task.Execute().Should().BeTrue(); |
| 83 | + |
| 84 | + var command = task.MTPWorkItems.Single().GetMetadata("Command"); |
| 85 | + command.Should().EndWith("--results-directory . --report-trx --report-trx-filename testResults.trx --auto-reporters off --filter Category=Smoke"); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void MTPAdditionalArgumentsAloneIsAppendedAfterReporterArgs() |
| 90 | + { |
| 91 | + var task = CreateTask(); |
| 92 | + task.MTPAdditionalArguments = "--auto-reporters off"; |
| 93 | + task.MTPProjects = new[] { CreateProject("MyApp.Tests.csproj") }; |
| 94 | + |
| 95 | + task.Execute().Should().BeTrue(); |
| 96 | + |
| 97 | + var command = task.MTPWorkItems.Single().GetMetadata("Command"); |
| 98 | + command.Should().EndWith("--results-directory . --auto-reporters off"); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void MissingPublishDirectoryReturnsNoWorkItem() |
| 103 | + { |
| 104 | + var task = CreateTask(); |
| 105 | + task.MTPProjects = new[] { CreateProject("MyApp.Tests.csproj", publishDirectory: null) }; |
| 106 | + |
| 107 | + task.Execute().Should().BeFalse(); |
| 108 | + task.MTPWorkItems.Should().BeEmpty(); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public void MissingTargetPathReturnsNoWorkItem() |
| 113 | + { |
| 114 | + var task = CreateTask(); |
| 115 | + task.MTPProjects = new[] { CreateProject("MyApp.Tests.csproj", targetPath: null) }; |
| 116 | + |
| 117 | + task.Execute().Should().BeFalse(); |
| 118 | + task.MTPWorkItems.Should().BeEmpty(); |
| 119 | + } |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public void ValidTimeoutIsApplied() |
| 123 | + { |
| 124 | + var task = CreateTask(); |
| 125 | + task.MTPWorkItemTimeout = "00:12:34"; |
| 126 | + task.MTPProjects = new[] { CreateProject("MyApp.Tests.csproj") }; |
| 127 | + |
| 128 | + task.Execute().Should().BeTrue(); |
| 129 | + task.MTPWorkItems.Single().GetMetadata("Timeout").Should().Be("00:12:34"); |
| 130 | + } |
| 131 | + |
| 132 | + [Fact] |
| 133 | + public void InvalidTimeoutFallsBackToDefaultWithWarning() |
| 134 | + { |
| 135 | + var task = CreateTask(); |
| 136 | + var buildEngine = (MockBuildEngine)task.BuildEngine; |
| 137 | + task.MTPWorkItemTimeout = "not-a-timespan"; |
| 138 | + task.MTPProjects = new[] { CreateProject("MyApp.Tests.csproj") }; |
| 139 | + |
| 140 | + task.Execute().Should().BeTrue(); |
| 141 | + task.MTPWorkItems.Single().GetMetadata("Timeout").Should().Be("00:05:00"); |
| 142 | + buildEngine.BuildWarningEvents.Should().Contain(e => e.Message != null && e.Message.Contains("not-a-timespan")); |
| 143 | + } |
| 144 | + |
| 145 | + [Fact] |
| 146 | + public void DuplicateInputsArePassedThroughAsSeparateWorkItems() |
| 147 | + { |
| 148 | + // The task does not deduplicate - it mirrors the old CreateXUnitV3WorkItems behavior |
| 149 | + // and trusts the caller to provide a clean item list (MSBuild's RemoveDuplicates can |
| 150 | + // be used upstream if needed). |
| 151 | + var task = CreateTask(); |
| 152 | + task.MTPProjects = new[] |
| 153 | + { |
| 154 | + CreateProject("MyApp.Tests.csproj"), |
| 155 | + CreateProject("MyApp.Tests.csproj"), |
| 156 | + }; |
| 157 | + |
| 158 | + task.Execute().Should().BeTrue(); |
| 159 | + task.MTPWorkItems.Should().HaveCount(2); |
| 160 | + } |
| 161 | + |
| 162 | + [Fact] |
| 163 | + public void SameIdentityWithDifferentAdditionalPropertiesProducesSeparateWorkItems() |
| 164 | + { |
| 165 | + var task = CreateTask(); |
| 166 | + task.MTPProjects = new[] |
| 167 | + { |
| 168 | + CreateProject( |
| 169 | + "MyApp.Tests.csproj", |
| 170 | + publishDirectory: "/publish/net8.0", |
| 171 | + targetPath: "/publish/net8.0/MyApp.Tests.dll", |
| 172 | + additionalProperties: "TargetFramework=net8.0"), |
| 173 | + CreateProject( |
| 174 | + "MyApp.Tests.csproj", |
| 175 | + publishDirectory: "/publish/net9.0", |
| 176 | + targetPath: "/publish/net9.0/MyApp.Tests.dll", |
| 177 | + additionalProperties: "TargetFramework=net9.0"), |
| 178 | + }; |
| 179 | + |
| 180 | + task.Execute().Should().BeTrue(); |
| 181 | + task.MTPWorkItems.Should().HaveCount(2); |
| 182 | + task.MTPWorkItems.Select(w => w.GetMetadata("PayloadDirectory")) |
| 183 | + .Should().BeEquivalentTo(new[] { "/publish/net8.0", "/publish/net9.0" }); |
| 184 | + } |
| 185 | + |
| 186 | + [Fact] |
| 187 | + public void PathToDotnetIsHonored() |
| 188 | + { |
| 189 | + var task = CreateTask(); |
| 190 | + task.PathToDotnet = "/custom/dotnet"; |
| 191 | + task.MTPProjects = new[] { CreateProject("MyApp.Tests.csproj") }; |
| 192 | + |
| 193 | + task.Execute().Should().BeTrue(); |
| 194 | + task.MTPWorkItems.Single().GetMetadata("Command").Should().StartWith("/custom/dotnet exec "); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments