Repository navigation
Support file-based apps in MTP dotnet test - #55701
Conversation
Co-authored-by: Copilot App <[email protected]> Copilot-Session: 213ea7a8-7f47-4187-a91f-3de3348d19f9
|
Azure Pipelines: Successfully started running 1 pipeline(s). 2 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
Adds support for running C# file-based test apps (e.g., dotnet test MyTests.cs) through the Microsoft.Testing.Platform (MTP) dotnet test flow. This extends the CLI’s MTP pipeline to recognize file-based entry points, build/evaluate a virtual project, and keep existing option/argument-forwarding behaviors consistent.
Changes:
- Add file-based entry point detection and virtual-project build/evaluation to the MTP MSBuild orchestration path.
- Update MTP argument parsing/validation so
--application arguments don’t interfere with positional project/file detection, and reject device selection for file-based apps. - Add end-to-end
dotnet.Testscoverage plus update help output snapshot and localization resources.
Show a summary per file
| File | Description |
|---|---|
| test/dotnet.Tests/CommandTests/Test/snapshots/MTPHelpSnapshotTests.VerifyMTPHelpOutput.verified.txt | Updates MTP dotnet test help snapshot for --project to mention file-based apps. |
| test/dotnet.Tests/CommandTests/Test/GivenDotnetTestRunsFileBasedApps.cs | Adds end-to-end tests covering file-based MTP execution, options, -- handling, multi-targeting, binlogs, and validation. |
| src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hant.xlf | Updates localized --project description string (marks translation for review). |
| src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hans.xlf | Updates localized --project description string (marks translation for review). |
| src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.tr.xlf | Updates localized --project description string (marks translation for review). |
| src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ru.xlf | Updates localized --project description string (marks translation for review). |
| src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pt-BR.xlf | Updates localized --project description string (marks translation for review). |
| src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pl.xlf | Updates localized --project description string (marks translation for review). |
| src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ko.xlf | Updates localized --project description string (marks translation for review). |
| src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ja.xlf | Updates localized --project description string (marks translation for review). |
| src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.it.xlf | Updates localized --project description string (marks translation for review). |
| src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.fr.xlf | Updates localized --project description string (marks translation for review). |
| src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.es.xlf | Updates localized --project description string (marks translation for review). |
| src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.de.xlf | Updates localized --project description string (marks translation for review). |
| src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.cs.xlf | Updates localized --project description string (marks translation for review). |
| src/Cli/Microsoft.DotNet.Cli.Definitions/CommandDefinitionStrings.resx | Updates the canonical --project description string to include file-based apps. |
| src/Cli/dotnet/Commands/xlf/CliCommandStrings.zh-Hant.xlf | Adds new localized resource entry for “device options not supported for file-based apps”. |
| src/Cli/dotnet/Commands/xlf/CliCommandStrings.zh-Hans.xlf | Adds new localized resource entry for “device options not supported for file-based apps”. |
| src/Cli/dotnet/Commands/xlf/CliCommandStrings.tr.xlf | Adds new localized resource entry for “device options not supported for file-based apps”. |
| src/Cli/dotnet/Commands/xlf/CliCommandStrings.ru.xlf | Adds new localized resource entry for “device options not supported for file-based apps”. |
| src/Cli/dotnet/Commands/xlf/CliCommandStrings.pt-BR.xlf | Adds new localized resource entry for “device options not supported for file-based apps”. |
| src/Cli/dotnet/Commands/xlf/CliCommandStrings.pl.xlf | Adds new localized resource entry for “device options not supported for file-based apps”. |
| src/Cli/dotnet/Commands/xlf/CliCommandStrings.ko.xlf | Adds new localized resource entry for “device options not supported for file-based apps”. |
| src/Cli/dotnet/Commands/xlf/CliCommandStrings.ja.xlf | Adds new localized resource entry for “device options not supported for file-based apps”. |
| src/Cli/dotnet/Commands/xlf/CliCommandStrings.it.xlf | Adds new localized resource entry for “device options not supported for file-based apps”. |
| src/Cli/dotnet/Commands/xlf/CliCommandStrings.fr.xlf | Adds new localized resource entry for “device options not supported for file-based apps”. |
| src/Cli/dotnet/Commands/xlf/CliCommandStrings.es.xlf | Adds new localized resource entry for “device options not supported for file-based apps”. |
| src/Cli/dotnet/Commands/xlf/CliCommandStrings.de.xlf | Adds new localized resource entry for “device options not supported for file-based apps”. |
| src/Cli/dotnet/Commands/xlf/CliCommandStrings.cs.xlf | Adds new localized resource entry for “device options not supported for file-based apps”. |
| src/Cli/dotnet/Commands/Test/MTP/ValidationUtility.cs | Extends MTP path validation to recognize file-based entry points as valid inputs. |
| src/Cli/dotnet/Commands/Test/MTP/SolutionAndProjectUtility.cs | Refactors project evaluation to support virtual-project evaluation via injected evaluator delegate. |
| src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs | Adds file-based project handling (virtual build + evaluation) and improves -- argument boundary handling for positional parsing. |
| src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs | Rejects --device / --list-devices when the input is a file-based app. |
| src/Cli/dotnet/Commands/CliCommandStrings.resx | Adds the new error message resource for device options + file-based apps. |
Review details
- Files reviewed: 34/34 changed files
- Comments generated: 0
- Review effort level: Lite
|
/ba-g #55258 |
JanKrivanek
left a comment
There was a problem hiding this comment.
Note
This review was generated with AI assistance.
Review of the file-based-app support in the MTP dotnet test flow. The overall approach looks sound - the GetProjectProperties refactor into a Func<string?, ProjectInstance> seam is clean, and TrySplitApplicationArgumentsAtDoubleDash has a safe fallback when the token and argument views disagree.
7 comments: 2 behavioral divergences from the project-based / dotnet run paths, 1 unflagged behavior change to existing project-based dotnet test, 3 test-coverage gaps, and 1 documentation sync.
| BuildOptions buildOptions, | ||
| MSBuildSession buildSession) | ||
| { | ||
| var msbuildArgs = SolutionAndProjectUtility.AnalyzeStandardTestMSBuildArgs(buildOptions.MSBuildArgs); |
There was a problem hiding this comment.
The project/solution path adds -verbosity:quiet when the user did not pass --verbosity (see BuildOrRestoreProjectOrSolution below), but this path does not, and VirtualProjectBuildingCommand falls back to MSBuildForwardingAppWithoutLogging.DefaultVerbosity (= minimal). dotnet run forces quiet for the same reason (RunCommand.SetupSilentBuildArgs).
Net effect: dotnet test app.cs prints MSBuild build output that dotnet test app.csproj suppresses.
Suggested fix:
var msbuildArgs = SolutionAndProjectUtility.AnalyzeStandardTestMSBuildArgs(buildOptions.MSBuildArgs);
if (buildOptions.Verbosity is null)
{
msbuildArgs = msbuildArgs.CloneWithVerbosity(VerbosityOptions.quiet);
}Note that PropagateBuildOptions and RepeatedFileAfterDoubleDashDoesNotHideTheTestInput currently assert on that leaked build output ("PassingTests.cs ->", \release\), so they would need to assert on MTP output instead.
| string fullEntryPointFilePath = Path.GetFullPath(entryPointFilePath); | ||
| var buildCommand = new VirtualProjectBuildingCommand( | ||
| fullEntryPointFilePath, | ||
| msbuildArgs) |
There was a problem hiding this comment.
MTPTargetOption forwards --target:_MTPBuild, so Builder.RequestedTargets is ["_MTPBuild"] and Execute runs only that target instead of the [Build, CoreCompile] default. dotnet run explicitly appends CoreCompile for exactly this reason (RunCommand.CreateProjectBuilder).
With no CoreCompile result in the BuildResult, CacheCscArguments logs "No CSC arguments found in targets" but MarkBuildSuccess still writes a cache entry with empty CscArguments and null BuildResultFile. It degrades safely, but it silently disables the csc fast path for a subsequent dotnet run app.cs on the same file.
Suggested fix - append CoreCompile to the build command only:
var buildCommand = new VirtualProjectBuildingCommand(
fullEntryPointFilePath,
msbuildArgs.CloneWithAdditionalTargets(Constants.CoreCompile))| ImmutableArray<string> loggerArgs; | ||
| ImmutableArray<string> otherArgs; | ||
| int positionalArgumentCount; | ||
| if (CommonRunHelpers.TrySplitApplicationArgumentsAtDoubleDash( |
There was a problem hiding this comment.
This is not file-based-specific - it changes dotnet test for projects and solutions too. Positional scanning for .sln/*proj/.dll now stops at --, and logger arguments after -- are no longer hoisted into loggerArgs.
So dotnet test -- foo.csproj no longer selects the project, and dotnet test -- -bl no longer produces a binlog.
Both look like improvements, but they are user-visible changes to an existing flow with no project-based regression test and no mention in the PR description. Suggest adding a project/solution case to the -- tests and calling the change out in the description.
| } | ||
|
|
||
| [TestMethod] | ||
| public void FileAfterDoubleDashIsNotUsedAsTheTestInput() |
There was a problem hiding this comment.
These -- tests only assert the negative (the file after -- is not consumed as the test input). Since GetBuildOptions now rewrites argument handling for every dotnet test invocation, the assertion that is missing is the positive one: that arguments after -- are actually forwarded to the test application.
Suggest adding a test that passes an MTP option after -- and asserts it took effect.
| } | ||
|
|
||
| [TestMethod] | ||
| public void RunWithNoBuild() |
There was a problem hiding this comment.
This test (and RunMultipleTargetFrameworks) builds first, so the never-built case is not covered.
With --no-build on a fresh file, GetProjectsFromFile skips buildCommand.Execute() entirely - so no restore either - and goes straight to CreateProjectInstance, which still has to resolve #:sdk MSTest.Sdk@.... Worth a test asserting a sensible error there rather than an evaluation failure.
| && VirtualProjectBuilder.IsValidEntryPointPath(projectOrSolutionPath) | ||
| && (buildOptions.ListDevices || !string.IsNullOrWhiteSpace(buildOptions.Device))) | ||
| { | ||
| throw new GracefulException(CliCommandStrings.CmdDeviceOptionsNotSupportedForFileBasedApps); |
There was a problem hiding this comment.
This catches --device/--list-devices but not -p:Device=....
For project-based runs SelectDevicesBeforeBuild treats an explicit Device global property as a device selection, but GetProjectsFromProject now dispatches to GetProjectsFromFile before device selection runs, so for a file-based app -p:Device=x is silently dropped with no diagnostic.
Suggest also checking the Device global property here so the same GracefulException is thrown.
|
|
||
| if (VirtualProjectBuilder.IsValidEntryPointPath(projectFilePath)) | ||
| { | ||
| return GetProjectsFromFile(projectFilePath, buildOptions, buildSession); |
There was a problem hiding this comment.
documentation/general/dotnet-run-file.md needs updating in this PR:
- the "Other commands" section enumerates the commands that support file-based programs (
restore,build,publish,pack,clean) -dotnet testshould be added; - the "Other possible commands" section still says additional commands "might never" be supported.
Per the repo's docs-in-sync guidance, this should land with the change rather than after it.
JanKrivanek
left a comment
There was a problem hiding this comment.
Other then the recommended fixes - the overall feature and directions LGTM
So feel free to use your judgement what to fix
Summary
dotnet testflow--projectfile inputs--application arguments, multi-targeting, no-build/no-restore, and binary loggingTests
build.cmddotnet.Testscoverage for file-based MTP apps, MTP help, parser, and validation (25 passed)