Skip to content

Commit c6f8b8d

Browse files
committed
Do not include forwarded --target option in build arguments.
1 parent 330bcff commit c6f8b8d

File tree

7 files changed

+95
-4
lines changed

7 files changed

+95
-4
lines changed

eng/Versions.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
<AwesomeAssertionsJsonVersion>8.0.0</AwesomeAssertionsJsonVersion>
132132
<MoqPackageVersion>4.18.4</MoqPackageVersion>
133133
<XunitCombinatorialVersion>1.3.2</XunitCombinatorialVersion>
134+
<XUnitRunnerVisualStudioPackageVersion>3.1.4</XUnitRunnerVisualStudioPackageVersion>
134135
<MicrosoftDotNetInstallerWindowsSecurityTestDataPackageVersion>8.0.0-beta.23607.1</MicrosoftDotNetInstallerWindowsSecurityTestDataPackageVersion>
135136
<BenchmarkDotNetPackageVersion>0.14.0</BenchmarkDotNetPackageVersion>
136137
</PropertyGroup>

src/Dotnet.Watch/dotnet-watch/CommandLine/CommandLineOptions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.DotNet.Cli.CommandLine;
1111
using Microsoft.DotNet.Cli.Commands;
1212
using Microsoft.DotNet.Cli.Commands.Build;
13+
using Microsoft.DotNet.Cli.Commands.MSBuild;
1314
using Microsoft.DotNet.Cli.Commands.Test;
1415
using Microsoft.Extensions.Logging;
1516

@@ -116,10 +117,14 @@ internal sealed class CommandLineOptions
116117
out var binLogToken,
117118
out var binLogPath);
118119

119-
// We assume that forwarded options, if any, are intended for dotnet build.
120+
// We assume that forwarded options, if any, are intended for `dotnet build`.
121+
// Exclude --target option since we need to control the targets being built.
122+
var msbuildCommandDefinition = new MSBuildCommandDefinition();
123+
120124
var buildArguments = buildOptions
121125
.Select(option => option.ForwardingFunction!(parseResult))
122126
.SelectMany(args => args)
127+
.Where(arg => !msbuildCommandDefinition.Parse(arg).HasOption(msbuildCommandDefinition.TargetOption))
123128
.ToList();
124129

125130
if (binLogToken != null)

test/Microsoft.NET.TestFramework/Microsoft.NET.TestFramework.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@
4141
<_Parameter1>MicrosoftExtensionsServiceDiscoveryPackageVersion</_Parameter1>
4242
<_Parameter2>$(MicrosoftExtensionsServiceDiscoveryPackageVersion)</_Parameter2>
4343
</AssemblyAttribute>
44+
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
45+
<_Parameter1>MicrosoftNETTestSdkPackageVersion</_Parameter1>
46+
<_Parameter2>$(MicrosoftNETTestSdkPackageVersion)</_Parameter2>
47+
</AssemblyAttribute>
48+
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
49+
<_Parameter1>XUnitPackageVersion</_Parameter1>
50+
<_Parameter2>$(XUnitVersion)</_Parameter2>
51+
</AssemblyAttribute>
52+
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
53+
<_Parameter1>XUnitRunnerVisualStudioPackageVersion</_Parameter1>
54+
<_Parameter2>$(XUnitRunnerVisualStudioPackageVersion)</_Parameter2>
55+
</AssemblyAttribute>
4456
</ItemGroup>
4557

4658
<ItemGroup>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using Xunit;
3+
4+
namespace XUnitTestProject1
5+
{
6+
public class UnitTest1
7+
{
8+
[Fact]
9+
public void Test1()
10+
{
11+
12+
}
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>$(CurrentTargetFramework)</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" />
9+
<PackageReference Include="xunit" Version="$(XUnitPackageVersion)" />
10+
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVisualStudioPackageVersion)" />
11+
</ItemGroup>
12+
13+
</Project>

test/dotnet-watch.Tests/CommandLine/CommandLineOptionsTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,10 @@ public void ForwardedOptionsAndArguments_Test(string[] args, string[] buildArgs,
514514
var runOptions = VerifyOptions(["test", .. args]);
515515

516516
var isShortProperty = args[0].Contains("-p") || args[0].Contains("/p");
517-
string[] expectedBuildArgs = isShortProperty
518-
? ["--property:VSTestNoLogo=true", "--property:NuGetInteractive=false", .. buildArgs, "--target:VSTest"]
519-
: ["--property:VSTestNoLogo=true", "--property:NuGetInteractive=false", "--target:VSTest", .. buildArgs];
517+
518+
// `test` subcommand forwards "--target:VSTest" to build, but BuildArguments are used to invoke
519+
// `dotnet build` and design-time build and should not specify build targets:
520+
string[] expectedBuildArgs = ["--property:VSTestNoLogo=true", "--property:NuGetInteractive=false", .. buildArgs];
520521
AssertEx.SequenceEqual(expectedBuildArgs, runOptions.BuildArguments);
521522

522523
AssertEx.SequenceEqual(commandArgs ?? [], runOptions.CommandArguments);

test/dotnet-watch.Tests/Watch/FileUpdateTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,49 @@ public async Task RestartProcessThatTerminatesAfterFileChange()
4848
Assert.NotEqual(processIdentifier, processIdentifier2);
4949
await App.WaitUntilOutputContains("Exiting"); // process should exit after run
5050
}
51+
52+
/// <summary>
53+
/// Validates `dotnet watch test` scenario: https://github.com/dotnet/sdk/issues/52528
54+
/// </summary>
55+
[Fact]
56+
public async Task TestCommand()
57+
{
58+
var testAsset = TestAssets.CopyTestAsset("WatchXUnit")
59+
.WithSource();
60+
61+
var testFile = Path.Combine(testAsset.Path, "UnitTest1.cs");
62+
File.WriteAllText(testFile, """
63+
using Xunit;
64+
65+
public class UnitTest1
66+
{
67+
[Fact]
68+
public void Test1()
69+
{
70+
Assert.True(false);
71+
}
72+
}
73+
""");
74+
75+
App.Start(testAsset, ["test"]);
76+
77+
await App.WaitUntilOutputContains(MessageDescriptor.WaitingForChanges);
78+
await App.WaitUntilOutputContains("Failed!");
79+
App.Process.ClearOutput();
80+
81+
UpdateSourceFile(testFile, """
82+
using Xunit;
83+
84+
public class UnitTest1
85+
{
86+
[Fact]
87+
public void Test1()
88+
{
89+
Assert.True(true);
90+
}
91+
}
92+
""");
93+
94+
await App.WaitUntilOutputContains("Passed!");
95+
}
5196
}

0 commit comments

Comments
 (0)