From d73fa64583c7f65956041f89d0bcf8f876b66509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Mon, 27 Jul 2026 13:32:49 +0200 Subject: [PATCH 1/2] Add dotnet test --results-directory-layout for MTP runs When 'dotnet test' runs a solution, every test application resolves the same shared TestResults directory, so reports configured with a relative file name (--coverage-output, --report-trx-filename, ...) overwrite each other and coverage data is silently lost (microsoft/codecoverage#226). Add an SDK-side layout policy for the Microsoft.Testing.Platform path: dotnet test --results-directory-layout 'flat' is the default and keeps today's behavior. 'per-module' gives every test application its own directory, modeled on the artifacts output layout: //_ Microsoft.Testing.Platform gains no new option; the SDK simply passes an explicit --results-directory per child process, so the whole module set is resolved up front. That also lets the layout append an identity hash to the project folder only when two distinct projects in the run share a name. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 0cb64c89-128b-4f8f-88cc-2741ffac3767 --- .../CommandDefinitionStrings.resx | 8 + ...mandDefinition.MicrosoftTestingPlatform.cs | 8 + .../xlf/CommandDefinitionStrings.cs.xlf | 14 + .../xlf/CommandDefinitionStrings.de.xlf | 14 + .../xlf/CommandDefinitionStrings.es.xlf | 14 + .../xlf/CommandDefinitionStrings.fr.xlf | 14 + .../xlf/CommandDefinitionStrings.it.xlf | 14 + .../xlf/CommandDefinitionStrings.ja.xlf | 14 + .../xlf/CommandDefinitionStrings.ko.xlf | 14 + .../xlf/CommandDefinitionStrings.pl.xlf | 14 + .../xlf/CommandDefinitionStrings.pt-BR.xlf | 14 + .../xlf/CommandDefinitionStrings.ru.xlf | 14 + .../xlf/CommandDefinitionStrings.tr.xlf | 14 + .../xlf/CommandDefinitionStrings.zh-Hans.xlf | 14 + .../xlf/CommandDefinitionStrings.zh-Hant.xlf | 14 + .../Test/MTP/ArtifactPostProcessingManager.cs | 3 + .../dotnet/Commands/Test/MTP/ITestHandler.cs | 6 + .../Commands/Test/MTP/MSBuildHandler.cs | 3 + .../Commands/Test/MTP/MSBuildUtility.cs | 3 + .../MicrosoftTestingPlatformTestCommand.cs | 7 + src/Cli/dotnet/Commands/Test/MTP/Options.cs | 15 +- .../Commands/Test/MTP/TestApplication.cs | 4 +- .../Test/MTP/TestApplicationActionQueue.cs | 4 + .../Test/MTP/TestModulesFilterHandler.cs | 26 +- .../Test/MTP/TestResultsDirectoryResolver.cs | 308 ++++++++++++++++++ ...jectSolutionWithDuplicateProjectNames.slnx | 8 + .../global.json | 5 + .../samples/Tests/Program.cs | 59 ++++ .../samples/Tests/Tests.csproj | 19 ++ .../src/Tests/Program.cs | 59 ++++ .../src/Tests/Tests.csproj | 19 ++ ...estProjectSolutionWithSharedReportName.sln | 28 ++ .../TestProjectA/Program.cs | 59 ++++ .../TestProjectA/TestProjectA.csproj | 19 ++ .../TestProjectB/Program.cs | 59 ++++ .../TestProjectB/TestProjectB.csproj | 19 ++ .../global.json | 5 + .../Test/GivenDotnetTestBuildsAndRunsTests.cs | 97 ++++++ .../Test/TestCommandParserTests.cs | 24 ++ .../Test/TestResultsDirectoryResolverTests.cs | 211 ++++++++++++ ...shotTests.VerifyMTPHelpOutput.verified.txt | 3 + 41 files changed, 1263 insertions(+), 7 deletions(-) create mode 100644 src/Cli/dotnet/Commands/Test/MTP/TestResultsDirectoryResolver.cs create mode 100644 test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/MultiTestProjectSolutionWithDuplicateProjectNames.slnx create mode 100644 test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/global.json create mode 100644 test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/samples/Tests/Program.cs create mode 100644 test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/samples/Tests/Tests.csproj create mode 100644 test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/src/Tests/Program.cs create mode 100644 test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/src/Tests/Tests.csproj create mode 100644 test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/MultiTestProjectSolutionWithSharedReportName.sln create mode 100644 test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectA/Program.cs create mode 100644 test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectA/TestProjectA.csproj create mode 100644 test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectB/Program.cs create mode 100644 test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectB/TestProjectB.csproj create mode 100644 test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/global.json create mode 100644 test/dotnet.Tests/CommandTests/Test/TestResultsDirectoryResolverTests.cs diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/CommandDefinitionStrings.resx b/src/Cli/Microsoft.DotNet.Cli.Definitions/CommandDefinitionStrings.resx index 9645faf1db54..ecff9c0f2f11 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/CommandDefinitionStrings.resx +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/CommandDefinitionStrings.resx @@ -555,6 +555,14 @@ This is equivalent to deleting project.assets.json. The directory where the test results will be placed. The specified directory will be created if it does not exist. + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + LAYOUT + Specifies a testconfig.json file. diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/Commands/Test/TestCommandDefinition.MicrosoftTestingPlatform.cs b/src/Cli/Microsoft.DotNet.Cli.Definitions/Commands/Test/TestCommandDefinition.MicrosoftTestingPlatform.cs index 8862525d8418..395186d9bab2 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/Commands/Test/TestCommandDefinition.MicrosoftTestingPlatform.cs +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/Commands/Test/TestCommandDefinition.MicrosoftTestingPlatform.cs @@ -52,6 +52,13 @@ public sealed partial class MicrosoftTestingPlatform : TestCommandDefinition, IC Arity = ArgumentArity.ExactlyOne }; + public readonly Option ResultsDirectoryLayoutOption = new Option("--results-directory-layout") + { + Description = CommandDefinitionStrings.CmdResultsDirectoryLayoutDescription, + HelpName = CommandDefinitionStrings.CmdResultsDirectoryLayoutName, + Arity = ArgumentArity.ExactlyOne + }.AcceptOnlyFromAmong("flat", "per-module"); + public const string ConfigFileOptionName = "--config-file"; public readonly Option ConfigFileOption = new(ConfigFileOptionName) @@ -194,6 +201,7 @@ public MicrosoftTestingPlatform() Options.Add(TestModulesFilterOption); Options.Add(TestModulesRootDirectoryOption); Options.Add(ResultsDirectoryOption); + Options.Add(ResultsDirectoryLayoutOption); Options.Add(ConfigFileOption); Options.Add(DiagnosticOutputDirectoryOption); Options.Add(MaxParallelTestModulesOption); diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.cs.xlf b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.cs.xlf index 06000cb8f219..8e7de285ab9d 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.cs.xlf +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.cs.xlf @@ -571,6 +571,20 @@ The specified directory will be created if it does not exist. Pokud zadaný adresář neexistuje, bude vytvořen. + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + + LAYOUT + LAYOUT + + ROOT_PATH ROOT_PATH diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.de.xlf b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.de.xlf index 83979a8c32b6..cb66e0551156 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.de.xlf +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.de.xlf @@ -571,6 +571,20 @@ The specified directory will be created if it does not exist. Das angegebene Verzeichnis wird erstellt, wenn es nicht vorhanden ist. + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + + LAYOUT + LAYOUT + + ROOT_PATH ROOT_PATH diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.es.xlf b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.es.xlf index e85ecdec78c8..82d9970a3062 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.es.xlf +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.es.xlf @@ -571,6 +571,20 @@ The specified directory will be created if it does not exist. Si no existe, se creará el directorio especificado. + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + + LAYOUT + LAYOUT + + ROOT_PATH ROOT_PATH diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.fr.xlf b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.fr.xlf index 48890e4a1dbf..63714bf06898 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.fr.xlf +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.fr.xlf @@ -571,6 +571,20 @@ The specified directory will be created if it does not exist. Le répertoire spécifié est créé, s'il n'existe pas déjà. + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + + LAYOUT + LAYOUT + + ROOT_PATH ROOT_PATH diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.it.xlf b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.it.xlf index b48974079e2b..ddd11e9fa44c 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.it.xlf +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.it.xlf @@ -571,6 +571,20 @@ The specified directory will be created if it does not exist. Se non esiste, la directory specificata verrà creata. + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + + LAYOUT + LAYOUT + + ROOT_PATH ROOT_PATH diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ja.xlf b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ja.xlf index 789645e7aeff..7ec38c84fe79 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ja.xlf +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ja.xlf @@ -571,6 +571,20 @@ The specified directory will be created if it does not exist. 指定したディレクトリが存在しない場合は、作成されます。 + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + + LAYOUT + LAYOUT + + ROOT_PATH ROOT_PATH diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ko.xlf b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ko.xlf index 39a7e14d7881..ee0f2a36917a 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ko.xlf +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ko.xlf @@ -571,6 +571,20 @@ The specified directory will be created if it does not exist. 지정한 디렉터리가 존재하지 않는 경우 생성됩니다. + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + + LAYOUT + LAYOUT + + ROOT_PATH ROOT_PATH diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pl.xlf b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pl.xlf index 68aac5510e09..59ef993895b1 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pl.xlf +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pl.xlf @@ -571,6 +571,20 @@ The specified directory will be created if it does not exist. Jeśli określony katalog nie istnieje, zostanie utworzony. + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + + LAYOUT + LAYOUT + + ROOT_PATH ROOT_PATH diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pt-BR.xlf b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pt-BR.xlf index 7b285cd2bd81..5065462dd575 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pt-BR.xlf +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pt-BR.xlf @@ -571,6 +571,20 @@ The specified directory will be created if it does not exist. O diretório especificado será criado se ele ainda não existir. + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + + LAYOUT + LAYOUT + + ROOT_PATH ROOT_PATH diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ru.xlf b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ru.xlf index 1ef32468a5e6..ce487ab1cd4e 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ru.xlf +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ru.xlf @@ -571,6 +571,20 @@ The specified directory will be created if it does not exist. Если указанного каталога не существует, он будет создан. + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + + LAYOUT + LAYOUT + + ROOT_PATH ROOT_PATH diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.tr.xlf b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.tr.xlf index bce3188757f2..8add6d3fa2ed 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.tr.xlf +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.tr.xlf @@ -571,6 +571,20 @@ The specified directory will be created if it does not exist. Belirtilen dizin yoksa oluşturulur. + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + + LAYOUT + LAYOUT + + ROOT_PATH ROOT_PATH diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hans.xlf b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hans.xlf index 15b4af011561..331ad8323028 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hans.xlf +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hans.xlf @@ -571,6 +571,20 @@ The specified directory will be created if it does not exist. 若不存在,将创建指定目录。 + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + + LAYOUT + LAYOUT + + ROOT_PATH ROOT_PATH diff --git a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hant.xlf b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hant.xlf index 702ac70b0e3d..e0c86362d965 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hant.xlf +++ b/src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hant.xlf @@ -571,6 +571,20 @@ The specified directory will be created if it does not exist. 若指定的目錄不存在,則會建立該目錄。 + + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + Specifies how test results are organized within the results directory. +'flat' (the default) places the results of every test project directly in the results directory. +'per-module' gives every test project its own '<project>/<target-framework>_<runtime>' subdirectory, so reports with the same file name cannot overwrite each other. + + + + LAYOUT + LAYOUT + + ROOT_PATH ROOT_PATH diff --git a/src/Cli/dotnet/Commands/Test/MTP/ArtifactPostProcessingManager.cs b/src/Cli/dotnet/Commands/Test/MTP/ArtifactPostProcessingManager.cs index 45029f9aed6b..ef7cd3e4eaa3 100644 --- a/src/Cli/dotnet/Commands/Test/MTP/ArtifactPostProcessingManager.cs +++ b/src/Cli/dotnet/Commands/Test/MTP/ArtifactPostProcessingManager.cs @@ -102,6 +102,9 @@ public async Task ExecuteAsync( job.Application.Module, buildOptions, toolOptions, + // Post-processing merges artifacts across modules, so it keeps writing to the + // shared results directory even when the run uses a per-module layout. + TestResultsDirectoryResolver.CreateShared(buildOptions.PathOptions, Directory.GetCurrentDirectory()), output, onHelpRequested: _ => { }, artifactPostProcessingManager: this, diff --git a/src/Cli/dotnet/Commands/Test/MTP/ITestHandler.cs b/src/Cli/dotnet/Commands/Test/MTP/ITestHandler.cs index 1837797db18b..387da776adaa 100644 --- a/src/Cli/dotnet/Commands/Test/MTP/ITestHandler.cs +++ b/src/Cli/dotnet/Commands/Test/MTP/ITestHandler.cs @@ -7,5 +7,11 @@ internal interface ITestHandler { bool Initialize(); + /// + /// All modules that will be run. Available after a successful so the + /// results directory layout can be computed with knowledge of the whole run. + /// + IEnumerable EnumerateTestModules(); + int RunTestApplications(TestApplicationActionQueue actionQueue); } diff --git a/src/Cli/dotnet/Commands/Test/MTP/MSBuildHandler.cs b/src/Cli/dotnet/Commands/Test/MTP/MSBuildHandler.cs index 6e3d54ae2e8c..66a5fe86ad39 100644 --- a/src/Cli/dotnet/Commands/Test/MTP/MSBuildHandler.cs +++ b/src/Cli/dotnet/Commands/Test/MTP/MSBuildHandler.cs @@ -80,6 +80,9 @@ public int RunTestApplications(TestApplicationActionQueue actionQueue) return actionQueue.CompleteEnqueueAndWait(); } + public IEnumerable EnumerateTestModules() + => _testApplications.SelectMany(static moduleGroup => moduleGroup); + private static void LogProjectProperties(IEnumerable moduleGroups) { if (!Logger.TraceEnabled) diff --git a/src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs b/src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs index 833e8ed5c9e7..a33f9d78ebbb 100644 --- a/src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs +++ b/src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs @@ -252,6 +252,9 @@ public static BuildOptions GetBuildOptions(ParseResult parseResult) parseResult.GetValue(definition.SolutionOption), positionalTestModules ?? parseResult.GetValue(definition.TestModulesFilterOption), resultsDirectory, + parseResult.GetValue(definition.ResultsDirectoryLayoutOption) == "per-module" + ? ResultsDirectoryLayout.PerModule + : ResultsDirectoryLayout.Flat, configFile, diagnosticOutputDirectory); diff --git a/src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs b/src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs index b0fa329d6aa8..42e0cccdab3b 100644 --- a/src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs +++ b/src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs @@ -80,6 +80,11 @@ public int Run(ParseResult parseResult, bool isHelp) ListTestsFormat: GetListTestsFormat(parseResult, definition)); var output = InitializeOutput(degreeOfParallelism, parseResult, testOptions); + var resultsDirectoryResolver = TestResultsDirectoryResolver.Create( + buildOptions.PathOptions, + testHandler.EnumerateTestModules(), + Directory.GetCurrentDirectory()); + using var testRunPolicy = new TestRunPolicy( testOptions.IsDiscovery || testOptions.IsHelp ? null @@ -88,6 +93,7 @@ public int Run(ParseResult parseResult, bool isHelp) ? null : parseResult.GetValue(definition.TimeoutOption), onCancellation: _ => output.MarkCancelled()); + using var ctrlC = new CtrlCCancellationManager(output.StartCancelling); using var queueCancellation = CancellationTokenSource.CreateLinkedTokenSource(ctrlC.Token, testRunPolicy.Token); var artifactPostProcessingManager = new ArtifactPostProcessingManager(); @@ -98,6 +104,7 @@ public int Run(ParseResult parseResult, bool isHelp) degreeOfParallelism, buildOptions, testOptions, + resultsDirectoryResolver, output, OnHelpRequested, ctrlC, diff --git a/src/Cli/dotnet/Commands/Test/MTP/Options.cs b/src/Cli/dotnet/Commands/Test/MTP/Options.cs index 3c9425ea8639..43beb6081e8b 100644 --- a/src/Cli/dotnet/Commands/Test/MTP/Options.cs +++ b/src/Cli/dotnet/Commands/Test/MTP/Options.cs @@ -18,9 +18,22 @@ internal enum TestListFormat Json, } +internal enum ResultsDirectoryLayout +{ + Flat, + PerModule, +} + internal record TestOptions(bool IsHelp, bool IsDiscovery, TestListFormat ListTestsFormat, bool IsArtifactPostProcessing = false); -internal record PathOptions(string? ProjectOrSolutionPath, string? SolutionPath, string? TestModules, string? ResultsDirectoryPath, string? ConfigFilePath, string? DiagnosticOutputDirectoryPath); +internal record PathOptions( + string? ProjectOrSolutionPath, + string? SolutionPath, + string? TestModules, + string? ResultsDirectoryPath, + ResultsDirectoryLayout ResultsDirectoryLayout, + string? ConfigFilePath, + string? DiagnosticOutputDirectoryPath); internal record BuildOptions( PathOptions PathOptions, diff --git a/src/Cli/dotnet/Commands/Test/MTP/TestApplication.cs b/src/Cli/dotnet/Commands/Test/MTP/TestApplication.cs index 6196dcce2edd..7b89fff4c8e7 100644 --- a/src/Cli/dotnet/Commands/Test/MTP/TestApplication.cs +++ b/src/Cli/dotnet/Commands/Test/MTP/TestApplication.cs @@ -19,6 +19,7 @@ internal sealed class TestApplication( TestModule module, BuildOptions buildOptions, TestOptions testOptions, + TestResultsDirectoryResolver resultsDirectoryResolver, TerminalTestReporter output, Action onHelpRequested, ArtifactPostProcessingManager? artifactPostProcessingManager = null, @@ -33,6 +34,7 @@ internal sealed class TestApplication( private readonly Lock _controlRequestLock = new(); private readonly Lock _pipeConnectionsLock = new(); private readonly BuildOptions _buildOptions = buildOptions; + private readonly TestResultsDirectoryResolver _resultsDirectoryResolver = resultsDirectoryResolver; private readonly Action _onHelpRequested = onHelpRequested; private readonly TestApplicationHandler _handler = new( output, @@ -331,7 +333,7 @@ _artifactPostProcessingInvocation is null builder.Append($" {TestCommandDefinition.MicrosoftTestingPlatform.ListTestsOptionName}"); } - if (_buildOptions.PathOptions.ResultsDirectoryPath is { } resultsDirectoryPath) + if (_resultsDirectoryResolver.Resolve(Module) is { } resultsDirectoryPath) { builder.Append($" {TestCommandDefinition.MicrosoftTestingPlatform.ResultsDirectoryOptionName} {ArgumentEscaper.EscapeSingleArg(resultsDirectoryPath)}"); } diff --git a/src/Cli/dotnet/Commands/Test/MTP/TestApplicationActionQueue.cs b/src/Cli/dotnet/Commands/Test/MTP/TestApplicationActionQueue.cs index 0ed3be3295dc..3bbc60edac5b 100644 --- a/src/Cli/dotnet/Commands/Test/MTP/TestApplicationActionQueue.cs +++ b/src/Cli/dotnet/Commands/Test/MTP/TestApplicationActionQueue.cs @@ -22,6 +22,7 @@ public TestApplicationActionQueue( int degreeOfParallelism, BuildOptions buildOptions, TestOptions testOptions, + TestResultsDirectoryResolver resultsDirectoryResolver, TerminalTestReporter output, Action onHelpRequested, CtrlCCancellationManager ctrlC, @@ -38,6 +39,7 @@ public TestApplicationActionQueue( _readers[i] = Task.Run(async () => await Read( buildOptions, testOptions, + resultsDirectoryResolver, output, onHelpRequested, ctrlC, @@ -74,6 +76,7 @@ public int CompleteEnqueueAndWait() private async Task Read( BuildOptions buildOptions, TestOptions testOptions, + TestResultsDirectoryResolver resultsDirectoryResolver, TerminalTestReporter output, Action onHelpRequested, CtrlCCancellationManager ctrlC, @@ -93,6 +96,7 @@ private async Task Read( module, buildOptions, testOptions, + resultsDirectoryResolver, output, onHelpRequested, artifactPostProcessingManager, diff --git a/src/Cli/dotnet/Commands/Test/MTP/TestModulesFilterHandler.cs b/src/Cli/dotnet/Commands/Test/MTP/TestModulesFilterHandler.cs index b2b5944dca57..10a5417b87aa 100644 --- a/src/Cli/dotnet/Commands/Test/MTP/TestModulesFilterHandler.cs +++ b/src/Cli/dotnet/Commands/Test/MTP/TestModulesFilterHandler.cs @@ -17,6 +17,7 @@ internal sealed class TestModulesFilterHandler : ITestHandler private readonly string? _testModulesRoot; private readonly List _testModulePaths; private readonly IReadOnlyDictionary _environmentVariables; + private List _testApplications = []; public TestModulesFilterHandler(string testModules, ParseResult parseResult) { @@ -55,12 +56,29 @@ public bool Initialize() return false; } + _testApplications = BuildTestApplications(); return true; } + public IEnumerable EnumerateTestModules() + => _testApplications.SelectMany(static moduleGroup => moduleGroup); + public int RunTestApplications(TestApplicationActionQueue actionQueue) + { + foreach (var testApp in _testApplications) + { + // Write the test application to the channel + actionQueue.Enqueue(testApp); + } + + return actionQueue.CompleteEnqueueAndWait(); + } + + private List BuildTestApplications() { var muxerPath = new Muxer().MuxerPath; + var testApplications = new List(_testModulePaths.Count); + foreach (string testModule in _testModulePaths) { // We want to produce the right RunCommand and RunArguments for TestApplication implementation to consume directly. @@ -70,7 +88,7 @@ public int RunTestApplications(TestApplicationActionQueue actionQueue) ? new RunProperties(muxerPath, $@"exec ""{testModule}""", null) : new RunProperties(testModule, null, null); - var testApp = new ParallelizableTestModuleGroupWithSequentialInnerModules(new TestModule( + testApplications.Add(new ParallelizableTestModuleGroupWithSequentialInnerModules(new TestModule( runProperties, null, null, @@ -78,12 +96,10 @@ public int RunTestApplications(TestApplicationActionQueue actionQueue) null, testModule, DotnetRootArchVariableName: null, - EnvironmentVariables: _environmentVariables)); - // Write the test application to the channel - actionQueue.Enqueue(testApp); + EnvironmentVariables: _environmentVariables))); } - return actionQueue.CompleteEnqueueAndWait(); + return testApplications; } internal static List GetMatchedModulePaths(string testModules, string? rootDirectory) diff --git a/src/Cli/dotnet/Commands/Test/MTP/TestResultsDirectoryResolver.cs b/src/Cli/dotnet/Commands/Test/MTP/TestResultsDirectoryResolver.cs new file mode 100644 index 000000000000..9b0c653a40f1 --- /dev/null +++ b/src/Cli/dotnet/Commands/Test/MTP/TestResultsDirectoryResolver.cs @@ -0,0 +1,308 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Security.Cryptography; +using System.Text; + +namespace Microsoft.DotNet.Cli.Commands.Test; + +/// +/// Computes the results directory handed to each test application. +/// The per-module layout mirrors the SDK artifacts output layout +/// (https://learn.microsoft.com/dotnet/core/sdk/artifacts-output): a project folder containing a +/// pivot folder, where pivot elements are joined by an underscore. +/// +/// Project names are not guaranteed to be unique within a run, so the whole module set is inspected +/// up front. Only when two distinct projects would land in the same project folder is a short +/// identity hash appended to disambiguate them, keeping the common case clean. +/// +/// +internal sealed class TestResultsDirectoryResolver +{ + private const string DefaultResultsDirectoryName = "TestResults"; + private const string UnknownComponent = "unknown"; + private const int MaxPathComponentLength = 255; + + private static readonly HashSet s_invalidPathComponentCharacters = + [.. Path.GetInvalidFileNameChars(), Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar]; + + private readonly PathOptions _pathOptions; + private readonly string _workingDirectory; + private readonly string _identityRoot; + private readonly HashSet _ambiguousProjectNames; + + private TestResultsDirectoryResolver(PathOptions pathOptions, string workingDirectory, string identityRoot, HashSet ambiguousProjectNames) + { + _pathOptions = pathOptions; + _workingDirectory = workingDirectory; + _identityRoot = identityRoot; + _ambiguousProjectNames = ambiguousProjectNames; + } + + public static TestResultsDirectoryResolver Create(PathOptions pathOptions, IEnumerable modules, string workingDirectory) + { + if (pathOptions.ResultsDirectoryLayout == ResultsDirectoryLayout.Flat) + { + return new TestResultsDirectoryResolver(pathOptions, workingDirectory, workingDirectory, []); + } + // Anchor identities to the directory shared by every module rather than the current + // directory, so the same solution produces the same folder names no matter where + // 'dotnet test' was invoked from. + List materializedModules = [.. modules]; + string identityRoot = GetCommonRootDirectory(materializedModules, workingDirectory); + + Dictionary> identitiesByProjectName = new(StringComparer.OrdinalIgnoreCase); + foreach (TestModule module in materializedModules) + { + string projectName = GetProjectName(module); + if (!identitiesByProjectName.TryGetValue(projectName, out HashSet? identities)) + { + identities = new HashSet(StringComparer.Ordinal); + identitiesByProjectName.Add(projectName, identities); + } + + identities.Add(GetProjectIdentity(module, identityRoot)); + } + + HashSet ambiguousProjectNames = new(StringComparer.OrdinalIgnoreCase); + foreach ((string projectName, HashSet identities) in identitiesByProjectName) + { + if (identities.Count > 1) + { + ambiguousProjectNames.Add(projectName); + } + } + + return new TestResultsDirectoryResolver(pathOptions, workingDirectory, identityRoot, ambiguousProjectNames); + } + + /// + /// A resolver that always yields the configured results directory, whatever the requested + /// layout. Used by internal invocations such as artifact post-processing, which merge results + /// across modules and so must not be scoped to a single module's directory. + /// + public static TestResultsDirectoryResolver CreateShared(PathOptions pathOptions, string workingDirectory) + => new(pathOptions with { ResultsDirectoryLayout = ResultsDirectoryLayout.Flat }, workingDirectory, workingDirectory, []); + + public string? Resolve(TestModule module) + { + if (_pathOptions.ResultsDirectoryLayout == ResultsDirectoryLayout.Flat) + { + return _pathOptions.ResultsDirectoryPath; + } + + string resultsDirectory = _pathOptions.ResultsDirectoryPath + ?? Path.Combine(_workingDirectory, DefaultResultsDirectoryName); + + string resolved = Path.GetFullPath( + Path.Combine(resultsDirectory, GetProjectDirectoryName(module), GetPivotDirectoryName(module))); + + // Sanitization strips separators and dot-only components, so a module can never steer its + // results out of the requested root. Asserted rather than thrown because it is unreachable + // by design and only a future change to the component rules could break it. + Debug.Assert(IsUnderRoot(resolved, resultsDirectory), $"'{resolved}' escaped the results directory '{resultsDirectory}'."); + + return resolved; + } + + private static bool IsUnderRoot(string candidate, string root) + { + string normalizedRoot = Path.TrimEndingDirectorySeparator(Path.GetFullPath(root)); + string relative = Path.GetRelativePath(normalizedRoot, candidate); + + return relative != ".." + && !relative.StartsWith(".." + Path.DirectorySeparatorChar, StringComparison.Ordinal) + && !relative.StartsWith(".." + Path.AltDirectorySeparatorChar, StringComparison.Ordinal) + && !Path.IsPathRooted(relative); + } + + /// + /// The deepest directory that contains every module, used as a stable anchor for identities. + /// Falls back to the working directory when the modules share nothing (for example, modules on + /// different drives). + /// + private static string GetCommonRootDirectory(List modules, string workingDirectory) + { + string? commonRoot = null; + foreach (TestModule module in modules) + { + string? moduleDirectory = Path.GetDirectoryName(GetProjectPath(module, workingDirectory)); + if (string.IsNullOrEmpty(moduleDirectory)) + { + continue; + } + + commonRoot = commonRoot is null ? moduleDirectory : GetCommonPrefixDirectory(commonRoot, moduleDirectory); + if (string.IsNullOrEmpty(commonRoot)) + { + return workingDirectory; + } + } + + return string.IsNullOrEmpty(commonRoot) ? workingDirectory : commonRoot; + } + + private static string GetCommonPrefixDirectory(string first, string second) + { + StringComparison comparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + + // Keep the filesystem root ('C:\', '/', '\\server\share\') attached. Joining bare segments + // would turn 'C:\foo' and 'C:\bar' into the drive-relative 'C:', whose meaning depends on + // the process working directory. + string firstRoot = Path.GetPathRoot(first) ?? string.Empty; + string secondRoot = Path.GetPathRoot(second) ?? string.Empty; + if (firstRoot.Length == 0 || !string.Equals(firstRoot, secondRoot, comparison)) + { + return string.Empty; + } + + char[] separators = [Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar]; + string[] firstSegments = first[firstRoot.Length..].Split(separators, StringSplitOptions.RemoveEmptyEntries); + string[] secondSegments = second[secondRoot.Length..].Split(separators, StringSplitOptions.RemoveEmptyEntries); + + int shared = 0; + while (shared < firstSegments.Length + && shared < secondSegments.Length + && string.Equals(firstSegments[shared], secondSegments[shared], comparison)) + { + shared++; + } + + return Path.Combine(firstRoot, string.Join(Path.DirectorySeparatorChar, firstSegments, 0, shared)); + } + + /// + /// The project folder, defaulting to the project file name and falling back to the assembly name + /// when the module was discovered through --test-modules instead of a project. A short hash + /// is appended only when another distinct project in the same run shares the name. + /// + private string GetProjectDirectoryName(TestModule module) + { + string projectName = GetProjectName(module); + + return LimitComponentLength(_ambiguousProjectNames.Contains(projectName) + ? $"{projectName}_{GetShortHash(GetProjectIdentity(module, _identityRoot))}" + : projectName); + } + + /// + /// The pivot folder distinguishing runs of the same project across target frameworks and + /// runtimes. Multiple elements are joined by an underscore, following the artifacts layout. + /// The configuration is deliberately not part of the pivot: a single test run targets one + /// configuration, so it would only ever add a constant level to every path. + /// + private static string GetPivotDirectoryName(TestModule module) + { + string targetFramework = SanitizePathComponent(module.TargetFramework); + string runtime = SanitizePathComponent(GetRuntimeComponent(module)); + + return LimitComponentLength($"{targetFramework}_{runtime}".ToLowerInvariant()); + } + + /// + /// Prefers the runtime identifier the module was actually built for, so that runs differing + /// only by RID stay separate, and falls back to the architecture for the common case where no + /// runtime identifier was requested. + /// + private static string GetRuntimeComponent(TestModule module) + { + if (!string.IsNullOrEmpty(module.RunProperties.RuntimeIdentifier)) + { + return module.RunProperties.RuntimeIdentifier; + } + + return GetTargetArchitecture(module).ToString(); + } + + private static string GetProjectName(TestModule module) + { + string? projectName = string.IsNullOrEmpty(module.ProjectFullPath) + ? Path.GetFileNameWithoutExtension(module.TargetPath) + : Path.GetFileNameWithoutExtension(module.ProjectFullPath); + + return SanitizePathComponent(projectName); + } + + /// + /// Identifies the project a module belongs to. Modules of a multi-targeted project share an + /// identity so they nest under a single project folder and are separated only by their pivot. + /// + private static string GetProjectIdentity(TestModule module, string identityRoot) + { + string path = GetProjectPath(module, identityRoot); + if (string.IsNullOrEmpty(path)) + { + return string.Empty; + } + + string relativePath = Path.GetRelativePath(identityRoot, path) + .Replace(Path.DirectorySeparatorChar, '/'); + + return OperatingSystem.IsWindows() ? relativePath.ToLowerInvariant() : relativePath; + } + + private static string GetProjectPath(TestModule module, string basePath) + { + string path = string.IsNullOrEmpty(module.ProjectFullPath) ? module.TargetPath : module.ProjectFullPath; + + return string.IsNullOrEmpty(path) ? string.Empty : Path.GetFullPath(path, basePath); + } + + private static string GetShortHash(string value) + { + byte[] hash = SHA256.HashData(Encoding.UTF8.GetBytes(value)); + return Convert.ToHexString(hash.AsSpan(0, 8)).ToLowerInvariant(); + } + + private static Architecture GetTargetArchitecture(TestModule module) + { + if (EnvironmentVariableNames.TryParseArchitecture(module.RunProperties.RuntimeIdentifier, out Architecture architecture) + || EnvironmentVariableNames.TryParseArchitecture(module.RunProperties.DefaultAppHostRuntimeIdentifier, out architecture)) + { + return architecture; + } + + return RuntimeInformation.ProcessArchitecture; + } + + private static string SanitizePathComponent(string? value) + { + if (string.IsNullOrEmpty(value)) + { + return UnknownComponent; + } + + StringBuilder builder = new(value.Length); + foreach (char character in value) + { + builder.Append(s_invalidPathComponentCharacters.Contains(character) ? '_' : character); + } + + string sanitized = builder.ToString(); + + // A project named '...csproj' yields '..', which would otherwise walk out of the results + // directory. Trailing dots and spaces are also not addressable on Windows. + string trimmed = sanitized.TrimEnd('.', ' '); + + return trimmed.Length == 0 ? UnknownComponent : trimmed; + } + + /// + /// Keeps a single directory component within the limit common to Windows and Linux + /// filesystems, so that a long project name (or a long name plus its disambiguating suffix) + /// cannot make the test application fail to create its results directory. + /// + private static string LimitComponentLength(string component) + { + if (component.Length <= MaxPathComponentLength) + { + return component; + } + + // The appended hash is computed over the full component, so truncated names stay unique. + string hash = GetShortHash(component); + return string.Concat(component.AsSpan(0, MaxPathComponentLength - hash.Length - 1), "_", hash); + } +} diff --git a/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/MultiTestProjectSolutionWithDuplicateProjectNames.slnx b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/MultiTestProjectSolutionWithDuplicateProjectNames.slnx new file mode 100644 index 000000000000..aeccfd9a07ee --- /dev/null +++ b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/MultiTestProjectSolutionWithDuplicateProjectNames.slnx @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/global.json b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/global.json new file mode 100644 index 000000000000..9009caf0ba8f --- /dev/null +++ b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/global.json @@ -0,0 +1,5 @@ +{ + "test": { + "runner": "Microsoft.Testing.Platform" + } +} diff --git a/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/samples/Tests/Program.cs b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/samples/Tests/Program.cs new file mode 100644 index 000000000000..05bfea68b3f1 --- /dev/null +++ b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/samples/Tests/Program.cs @@ -0,0 +1,59 @@ +using Microsoft.Testing.Platform.Builder; +using Microsoft.Testing.Platform.Capabilities.TestFramework; +using Microsoft.Testing.Platform.Configurations; +using Microsoft.Testing.Platform.Extensions.Messages; +using Microsoft.Testing.Platform.Extensions.TestFramework; +using Microsoft.Testing.Platform.Services; + +var testApplicationBuilder = await TestApplication.CreateBuilderAsync(args); + +testApplicationBuilder.RegisterTestFramework( + _ => new TestFrameworkCapabilities(), + (_, serviceProvider) => new DummyTestAdapter(serviceProvider)); + +using var testApplication = await testApplicationBuilder.BuildAsync(); +return await testApplication.RunAsync(); + +public class DummyTestAdapter(IServiceProvider serviceProvider) : ITestFramework, IDataProducer +{ + // Every project in this solution writes this same relative file name into its test results + // directory, the way a coverage or TRX report with a relative path does. With a shared results + // directory the projects overwrite each other; with a per-module layout both reports survive. + private const string ReportFileName = "report.txt"; + + public string Uid => nameof(DummyTestAdapter); + + public string Version => "2.0.0"; + + public string DisplayName => nameof(DummyTestAdapter); + + public string Description => nameof(DummyTestAdapter); + + public Task IsEnabledAsync() => Task.FromResult(true); + + public Type[] DataTypesProduced => new[] { + typeof(TestNodeUpdateMessage) + }; + + public Task CreateTestSessionAsync(CreateTestSessionContext context) + => Task.FromResult(new CreateTestSessionResult() { IsSuccess = true }); + + public Task CloseTestSessionAsync(CloseTestSessionContext context) + => Task.FromResult(new CloseTestSessionResult() { IsSuccess = true }); + + public async Task ExecuteRequestAsync(ExecuteRequestContext context) + { + string resultDirectory = serviceProvider.GetConfiguration().GetTestResultDirectory(); + Directory.CreateDirectory(resultDirectory); + File.WriteAllText(Path.Combine(resultDirectory, ReportFileName), "samples"); + + await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid, new TestNode() + { + Uid = "Test1", + DisplayName = "Test1", + Properties = new PropertyBag(new PassedTestNodeStateProperty("OK")), + })); + + context.Complete(); + } +} diff --git a/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/samples/Tests/Tests.csproj b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/samples/Tests/Tests.csproj new file mode 100644 index 000000000000..d02beca2c7e8 --- /dev/null +++ b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/samples/Tests/Tests.csproj @@ -0,0 +1,19 @@ + + + + + $(CurrentTargetFramework) + Exe + + enable + enable + + false + false + true + + + + + + diff --git a/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/src/Tests/Program.cs b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/src/Tests/Program.cs new file mode 100644 index 000000000000..7ebb681458f8 --- /dev/null +++ b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/src/Tests/Program.cs @@ -0,0 +1,59 @@ +using Microsoft.Testing.Platform.Builder; +using Microsoft.Testing.Platform.Capabilities.TestFramework; +using Microsoft.Testing.Platform.Configurations; +using Microsoft.Testing.Platform.Extensions.Messages; +using Microsoft.Testing.Platform.Extensions.TestFramework; +using Microsoft.Testing.Platform.Services; + +var testApplicationBuilder = await TestApplication.CreateBuilderAsync(args); + +testApplicationBuilder.RegisterTestFramework( + _ => new TestFrameworkCapabilities(), + (_, serviceProvider) => new DummyTestAdapter(serviceProvider)); + +using var testApplication = await testApplicationBuilder.BuildAsync(); +return await testApplication.RunAsync(); + +public class DummyTestAdapter(IServiceProvider serviceProvider) : ITestFramework, IDataProducer +{ + // Every project in this solution writes this same relative file name into its test results + // directory, the way a coverage or TRX report with a relative path does. With a shared results + // directory the projects overwrite each other; with a per-module layout both reports survive. + private const string ReportFileName = "report.txt"; + + public string Uid => nameof(DummyTestAdapter); + + public string Version => "2.0.0"; + + public string DisplayName => nameof(DummyTestAdapter); + + public string Description => nameof(DummyTestAdapter); + + public Task IsEnabledAsync() => Task.FromResult(true); + + public Type[] DataTypesProduced => new[] { + typeof(TestNodeUpdateMessage) + }; + + public Task CreateTestSessionAsync(CreateTestSessionContext context) + => Task.FromResult(new CreateTestSessionResult() { IsSuccess = true }); + + public Task CloseTestSessionAsync(CloseTestSessionContext context) + => Task.FromResult(new CloseTestSessionResult() { IsSuccess = true }); + + public async Task ExecuteRequestAsync(ExecuteRequestContext context) + { + string resultDirectory = serviceProvider.GetConfiguration().GetTestResultDirectory(); + Directory.CreateDirectory(resultDirectory); + File.WriteAllText(Path.Combine(resultDirectory, ReportFileName), "src"); + + await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid, new TestNode() + { + Uid = "Test1", + DisplayName = "Test1", + Properties = new PropertyBag(new PassedTestNodeStateProperty("OK")), + })); + + context.Complete(); + } +} diff --git a/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/src/Tests/Tests.csproj b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/src/Tests/Tests.csproj new file mode 100644 index 000000000000..d02beca2c7e8 --- /dev/null +++ b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/src/Tests/Tests.csproj @@ -0,0 +1,19 @@ + + + + + $(CurrentTargetFramework) + Exe + + enable + enable + + false + false + true + + + + + + diff --git a/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/MultiTestProjectSolutionWithSharedReportName.sln b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/MultiTestProjectSolutionWithSharedReportName.sln new file mode 100644 index 000000000000..754d4f06e2d7 --- /dev/null +++ b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/MultiTestProjectSolutionWithSharedReportName.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35322.30 main +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProjectA", "TestProjectA\TestProjectA.csproj", "{2B7B0D3C-6C36-4C5E-9E3E-9C0F2D5A1A01}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProjectB", "TestProjectB\TestProjectB.csproj", "{2B7B0D3C-6C36-4C5E-9E3E-9C0F2D5A1A02}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2B7B0D3C-6C36-4C5E-9E3E-9C0F2D5A1A01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2B7B0D3C-6C36-4C5E-9E3E-9C0F2D5A1A01}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2B7B0D3C-6C36-4C5E-9E3E-9C0F2D5A1A01}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2B7B0D3C-6C36-4C5E-9E3E-9C0F2D5A1A01}.Release|Any CPU.Build.0 = Release|Any CPU + {2B7B0D3C-6C36-4C5E-9E3E-9C0F2D5A1A02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2B7B0D3C-6C36-4C5E-9E3E-9C0F2D5A1A02}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2B7B0D3C-6C36-4C5E-9E3E-9C0F2D5A1A02}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2B7B0D3C-6C36-4C5E-9E3E-9C0F2D5A1A02}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectA/Program.cs b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectA/Program.cs new file mode 100644 index 000000000000..6785889b0991 --- /dev/null +++ b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectA/Program.cs @@ -0,0 +1,59 @@ +using Microsoft.Testing.Platform.Builder; +using Microsoft.Testing.Platform.Capabilities.TestFramework; +using Microsoft.Testing.Platform.Configurations; +using Microsoft.Testing.Platform.Extensions.Messages; +using Microsoft.Testing.Platform.Extensions.TestFramework; +using Microsoft.Testing.Platform.Services; + +var testApplicationBuilder = await TestApplication.CreateBuilderAsync(args); + +testApplicationBuilder.RegisterTestFramework( + _ => new TestFrameworkCapabilities(), + (_, serviceProvider) => new DummyTestAdapter(serviceProvider)); + +using var testApplication = await testApplicationBuilder.BuildAsync(); +return await testApplication.RunAsync(); + +public class DummyTestAdapter(IServiceProvider serviceProvider) : ITestFramework, IDataProducer +{ + // Every project in this solution writes this same relative file name into its test results + // directory, the way a coverage or TRX report with a relative path does. With a shared results + // directory the projects overwrite each other; with a per-module layout both reports survive. + private const string ReportFileName = "report.txt"; + + public string Uid => nameof(DummyTestAdapter); + + public string Version => "2.0.0"; + + public string DisplayName => nameof(DummyTestAdapter); + + public string Description => nameof(DummyTestAdapter); + + public Task IsEnabledAsync() => Task.FromResult(true); + + public Type[] DataTypesProduced => new[] { + typeof(TestNodeUpdateMessage) + }; + + public Task CreateTestSessionAsync(CreateTestSessionContext context) + => Task.FromResult(new CreateTestSessionResult() { IsSuccess = true }); + + public Task CloseTestSessionAsync(CloseTestSessionContext context) + => Task.FromResult(new CloseTestSessionResult() { IsSuccess = true }); + + public async Task ExecuteRequestAsync(ExecuteRequestContext context) + { + string resultDirectory = serviceProvider.GetConfiguration().GetTestResultDirectory(); + Directory.CreateDirectory(resultDirectory); + File.WriteAllText(Path.Combine(resultDirectory, ReportFileName), "TestProjectA"); + + await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid, new TestNode() + { + Uid = "Test1", + DisplayName = "Test1", + Properties = new PropertyBag(new PassedTestNodeStateProperty("OK")), + })); + + context.Complete(); + } +} diff --git a/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectA/TestProjectA.csproj b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectA/TestProjectA.csproj new file mode 100644 index 000000000000..d02beca2c7e8 --- /dev/null +++ b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectA/TestProjectA.csproj @@ -0,0 +1,19 @@ + + + + + $(CurrentTargetFramework) + Exe + + enable + enable + + false + false + true + + + + + + diff --git a/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectB/Program.cs b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectB/Program.cs new file mode 100644 index 000000000000..e70774a00bf9 --- /dev/null +++ b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectB/Program.cs @@ -0,0 +1,59 @@ +using Microsoft.Testing.Platform.Builder; +using Microsoft.Testing.Platform.Capabilities.TestFramework; +using Microsoft.Testing.Platform.Configurations; +using Microsoft.Testing.Platform.Extensions.Messages; +using Microsoft.Testing.Platform.Extensions.TestFramework; +using Microsoft.Testing.Platform.Services; + +var testApplicationBuilder = await TestApplication.CreateBuilderAsync(args); + +testApplicationBuilder.RegisterTestFramework( + _ => new TestFrameworkCapabilities(), + (_, serviceProvider) => new DummyTestAdapter(serviceProvider)); + +using var testApplication = await testApplicationBuilder.BuildAsync(); +return await testApplication.RunAsync(); + +public class DummyTestAdapter(IServiceProvider serviceProvider) : ITestFramework, IDataProducer +{ + // Every project in this solution writes this same relative file name into its test results + // directory, the way a coverage or TRX report with a relative path does. With a shared results + // directory the projects overwrite each other; with a per-module layout both reports survive. + private const string ReportFileName = "report.txt"; + + public string Uid => nameof(DummyTestAdapter); + + public string Version => "2.0.0"; + + public string DisplayName => nameof(DummyTestAdapter); + + public string Description => nameof(DummyTestAdapter); + + public Task IsEnabledAsync() => Task.FromResult(true); + + public Type[] DataTypesProduced => new[] { + typeof(TestNodeUpdateMessage) + }; + + public Task CreateTestSessionAsync(CreateTestSessionContext context) + => Task.FromResult(new CreateTestSessionResult() { IsSuccess = true }); + + public Task CloseTestSessionAsync(CloseTestSessionContext context) + => Task.FromResult(new CloseTestSessionResult() { IsSuccess = true }); + + public async Task ExecuteRequestAsync(ExecuteRequestContext context) + { + string resultDirectory = serviceProvider.GetConfiguration().GetTestResultDirectory(); + Directory.CreateDirectory(resultDirectory); + File.WriteAllText(Path.Combine(resultDirectory, ReportFileName), "TestProjectB"); + + await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid, new TestNode() + { + Uid = "Test1", + DisplayName = "Test1", + Properties = new PropertyBag(new PassedTestNodeStateProperty("OK")), + })); + + context.Complete(); + } +} diff --git a/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectB/TestProjectB.csproj b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectB/TestProjectB.csproj new file mode 100644 index 000000000000..d02beca2c7e8 --- /dev/null +++ b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectB/TestProjectB.csproj @@ -0,0 +1,19 @@ + + + + + $(CurrentTargetFramework) + Exe + + enable + enable + + false + false + true + + + + + + diff --git a/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/global.json b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/global.json new file mode 100644 index 000000000000..9009caf0ba8f --- /dev/null +++ b/test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/global.json @@ -0,0 +1,5 @@ +{ + "test": { + "runner": "Microsoft.Testing.Platform" + } +} diff --git a/test/dotnet.Tests/CommandTests/Test/GivenDotnetTestBuildsAndRunsTests.cs b/test/dotnet.Tests/CommandTests/Test/GivenDotnetTestBuildsAndRunsTests.cs index b686ffdc246d..4e1771f6ed8b 100644 --- a/test/dotnet.Tests/CommandTests/Test/GivenDotnetTestBuildsAndRunsTests.cs +++ b/test/dotnet.Tests/CommandTests/Test/GivenDotnetTestBuildsAndRunsTests.cs @@ -287,6 +287,103 @@ public void RunMultipleTestProjectsWithFailingTests_ShouldReturnExitCodeAtLeastO result.ExitCode.Should().Be(ExitCodes.AtLeastOneTestFailed); } + [TestMethod] + public void RunMultipleTestProjectsWithPerModuleResultsDirectoryLayout_ShouldCreateSeparateDirectories() + { + TestAsset testInstance = TestAssetsManager.CopyTestAsset("MultiTestProjectSolutionWithTests", Guid.NewGuid().ToString()) + .WithSource(); + string resultsDirectory = Path.Combine(testInstance.Path, "TestResults"); + + CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false) + .WithWorkingDirectory(testInstance.Path) + .Execute( + "-c", TestingConstants.Debug, + "--results-directory", resultsDirectory, + "--results-directory-layout", "per-module"); + + result.ExitCode.Should().Be(ExitCodes.AtLeastOneTestFailed); + + // Mirrors the artifacts output layout: //. + Directory.GetDirectories(resultsDirectory).Select(Path.GetFileName) + .Should().BeEquivalentTo(["TestProject", "OtherTestProject"]); + foreach (string projectDirectory in Directory.GetDirectories(resultsDirectory)) + { + Directory.GetDirectories(projectDirectory).Select(Path.GetFileName) + .Should().ContainSingle().Which.Should().MatchRegex(@"^net\d+\.\d+_[a-z0-9\-\.]+$"); + } + } + + [TestMethod] + public void RunMultipleTestProjectsWritingTheSameReportName_ShouldOverwriteWithFlatLayout() + { + // Regression coverage for https://github.com/microsoft/codecoverage/issues/226: both + // projects write the same relative report file name into the shared results directory, + // so only one report survives. This documents the behavior 'per-module' exists to fix. + TestAsset testInstance = TestAssetsManager.CopyTestAsset("MultiTestProjectSolutionWithSharedReportName", Guid.NewGuid().ToString()) + .WithSource(); + string resultsDirectory = Path.Combine(testInstance.Path, "TestResults"); + + CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false) + .WithWorkingDirectory(testInstance.Path) + .Execute( + "-c", TestingConstants.Debug, + "--results-directory", resultsDirectory, + // Serialize the modules so the two processes cannot race on the same file: + // the point of this test is which file survives, not concurrent write behavior. + "--max-parallel-test-modules", "1"); + + result.ExitCode.Should().Be(ExitCodes.Success); + Directory.GetFiles(resultsDirectory, "report.txt", SearchOption.AllDirectories) + .Should().ContainSingle("both projects write into the same directory with the flat layout"); + } + + [TestMethod] + public void RunMultipleTestProjectsWritingTheSameReportName_ShouldKeepBothWithPerModuleLayout() + { + TestAsset testInstance = TestAssetsManager.CopyTestAsset("MultiTestProjectSolutionWithSharedReportName", Guid.NewGuid().ToString()) + .WithSource(); + string resultsDirectory = Path.Combine(testInstance.Path, "TestResults"); + + CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false) + .WithWorkingDirectory(testInstance.Path) + .Execute( + "-c", TestingConstants.Debug, + "--results-directory", resultsDirectory, + "--results-directory-layout", "per-module"); + + result.ExitCode.Should().Be(ExitCodes.Success); + + string[] reports = Directory.GetFiles(resultsDirectory, "report.txt", SearchOption.AllDirectories); + reports.Should().HaveCount(2, "each project writes its report into its own directory"); + reports.Select(File.ReadAllText).Should().BeEquivalentTo(["TestProjectA", "TestProjectB"]); + } + + [TestMethod] + public void RunTestProjectsWithTheSameNameAndPerModuleLayout_ShouldDisambiguateAndKeepBothReports() + { + // Two distinct projects both named 'Tests' would share a project folder, so the layout + // appends an identity hash. Also covers the default results directory (no + // --results-directory), which is the shape most users will hit first. + TestAsset testInstance = TestAssetsManager.CopyTestAsset("MultiTestProjectSolutionWithDuplicateProjectNames", Guid.NewGuid().ToString()) + .WithSource(); + string resultsDirectory = Path.Combine(testInstance.Path, "TestResults"); + + CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false) + .WithWorkingDirectory(testInstance.Path) + .Execute( + "-c", TestingConstants.Debug, + "--results-directory-layout", "per-module"); + + result.ExitCode.Should().Be(ExitCodes.Success); + + Directory.GetDirectories(resultsDirectory).Select(Path.GetFileName) + .Should().HaveCount(2).And.AllSatisfy(name => name.Should().MatchRegex("^Tests_[0-9a-f]{16}$")); + + string[] reports = Directory.GetFiles(resultsDirectory, "report.txt", SearchOption.AllDirectories); + reports.Should().HaveCount(2); + reports.Select(File.ReadAllText).Should().BeEquivalentTo(["src", "samples"]); + } + [DataRow(TestingConstants.Debug)] [DataRow(TestingConstants.Release)] [TestMethod] diff --git a/test/dotnet.Tests/CommandTests/Test/TestCommandParserTests.cs b/test/dotnet.Tests/CommandTests/Test/TestCommandParserTests.cs index 887b6349de9a..182bc78d3b90 100644 --- a/test/dotnet.Tests/CommandTests/Test/TestCommandParserTests.cs +++ b/test/dotnet.Tests/CommandTests/Test/TestCommandParserTests.cs @@ -290,6 +290,30 @@ public void MTPCommandRejectsInvalidListTestsFormatValue(string format) parseResult.Errors.Should().NotBeEmpty(); } + [TestMethod] + [DataRow(null, nameof(ResultsDirectoryLayout.Flat))] + [DataRow("flat", nameof(ResultsDirectoryLayout.Flat))] + [DataRow("per-module", nameof(ResultsDirectoryLayout.PerModule))] + public void MTPCommandParsesResultsDirectoryLayout(string? value, string expected) + { + var command = new TestCommandDefinition.MicrosoftTestingPlatform(); + var parseResult = value is null + ? command.Parse([]) + : command.Parse(["--results-directory-layout", value]); + + parseResult.Errors.Should().BeEmpty(); + MSBuildUtility.GetBuildOptions(parseResult).PathOptions.ResultsDirectoryLayout.ToString().Should().Be(expected); + } + + [TestMethod] + public void MTPCommandRejectsInvalidResultsDirectoryLayout() + { + var command = new TestCommandDefinition.MicrosoftTestingPlatform(); + var parseResult = command.Parse(["--results-directory-layout", "invalid"]); + + parseResult.Errors.Should().NotBeEmpty(); + } + [TestMethod] public void DllDetectionShouldExcludeRunArgumentsAndGlobalProperties() { diff --git a/test/dotnet.Tests/CommandTests/Test/TestResultsDirectoryResolverTests.cs b/test/dotnet.Tests/CommandTests/Test/TestResultsDirectoryResolverTests.cs new file mode 100644 index 000000000000..496e540cd1c6 --- /dev/null +++ b/test/dotnet.Tests/CommandTests/Test/TestResultsDirectoryResolverTests.cs @@ -0,0 +1,211 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Immutable; +using Microsoft.DotNet.Cli.Commands.Run; +using Microsoft.DotNet.Cli.Commands.Test; + +namespace dotnet.Tests.CommandTests.Test; + +[TestClass] +public class TestResultsDirectoryResolverTests +{ + [TestMethod] + public void ResolveReturnsConfiguredDirectoryForFlatLayout() + { + string resultsDirectory = Path.GetFullPath("results"); + TestModule module = CreateModule(); + TestResultsDirectoryResolver resolver = CreateResolver(resultsDirectory, ResultsDirectoryLayout.Flat, module); + + resolver.Resolve(module).Should().Be(resultsDirectory); + } + + [TestMethod] + public void ResolveReturnsNullForFlatLayoutWithoutConfiguredDirectory() + { + TestModule module = CreateModule(); + TestResultsDirectoryResolver resolver = CreateResolver(null, ResultsDirectoryLayout.Flat, module); + + resolver.Resolve(module).Should().BeNull(); + } + + [TestMethod] + public void ResolveCreatesProjectAndPivotDirectoriesUnderConfiguredRoot() + { + string resultsDirectory = Path.Combine(WorkingDirectory, "artifacts"); + TestModule module = CreateModule("ProjectA"); + TestResultsDirectoryResolver resolver = CreateResolver(resultsDirectory, ResultsDirectoryLayout.PerModule, module); + + string first = resolver.Resolve(module)!; + string second = resolver.Resolve(module)!; + + first.Should().Be(second); + first.Should().Be(Path.Combine(resultsDirectory, "ProjectA", "net10.0_x64")); + } + + [TestMethod] + public void ResolveUsesDefaultRootForPerModuleLayout() + { + TestModule module = CreateModule("ProjectA"); + TestResultsDirectoryResolver resolver = CreateResolver(null, ResultsDirectoryLayout.PerModule, module); + + resolver.Resolve(module).Should().Be(Path.Combine(WorkingDirectory, "TestResults", "ProjectA", "net10.0_x64")); + } + + [TestMethod] + public void ResolveNestsTargetFrameworksOfTheSameProjectUnderOneProjectDirectory() + { + TestModule net10 = CreateModule("ProjectA"); + TestModule net9 = CreateModule("ProjectA") with { TargetFramework = "net9.0" }; + TestResultsDirectoryResolver resolver = CreateResolver(null, ResultsDirectoryLayout.PerModule, net10, net9); + + string first = resolver.Resolve(net10)!; + string second = resolver.Resolve(net9)!; + + Path.GetDirectoryName(first).Should().Be(Path.Combine(WorkingDirectory, "TestResults", "ProjectA")); + Path.GetDirectoryName(first).Should().Be(Path.GetDirectoryName(second)); + Path.GetFileName(first).Should().Be("net10.0_x64"); + Path.GetFileName(second).Should().Be("net9.0_x64"); + } + + [TestMethod] + public void ResolveKeepsProjectDirectoryCleanWhenProjectNamesAreUnique() + { + TestModule projectA = CreateModule("ProjectA"); + TestModule projectB = CreateModule("ProjectB"); + TestResultsDirectoryResolver resolver = CreateResolver(null, ResultsDirectoryLayout.PerModule, projectA, projectB); + + resolver.Resolve(projectA).Should().Be(Path.Combine(WorkingDirectory, "TestResults", "ProjectA", "net10.0_x64")); + resolver.Resolve(projectB).Should().Be(Path.Combine(WorkingDirectory, "TestResults", "ProjectB", "net10.0_x64")); + } + + [TestMethod] + public void ResolveDisambiguatesDistinctProjectsThatShareAName() + { + // Two different 'Tests.csproj' files in one run would otherwise clobber each other. + TestModule first = CreateModule("Tests", parentDirectory: "src"); + TestModule second = CreateModule("Tests", parentDirectory: "samples"); + TestResultsDirectoryResolver resolver = CreateResolver(null, ResultsDirectoryLayout.PerModule, first, second); + + string firstPath = resolver.Resolve(first)!; + string secondPath = resolver.Resolve(second)!; + + firstPath.Should().NotBe(secondPath); + Path.GetFileName(Path.GetDirectoryName(firstPath)).Should().MatchRegex("^Tests_[0-9a-f]{16}$"); + Path.GetFileName(Path.GetDirectoryName(secondPath)).Should().MatchRegex("^Tests_[0-9a-f]{16}$"); + Path.GetFileName(firstPath).Should().Be("net10.0_x64"); + } + + [TestMethod] + public void ResolveIsStableAcrossResolverInstancesForTheSameModuleSet() + { + TestModule first = CreateModule("Tests", parentDirectory: "src"); + TestModule second = CreateModule("Tests", parentDirectory: "samples"); + + string firstRun = CreateResolver(null, ResultsDirectoryLayout.PerModule, first, second).Resolve(first)!; + string secondRun = CreateResolver(null, ResultsDirectoryLayout.PerModule, first, second).Resolve(first)!; + + firstRun.Should().Be(secondRun); + } + + [TestMethod] + public void ResolveKeepsProjectsWithDottedNamesInsideTheResultsRoot() + { + // Path.GetFileNameWithoutExtension("...csproj") is "..", which must never be used as a + // path component or the results would be written outside the results directory. + TestModule module = CreateModule(".."); + TestResultsDirectoryResolver resolver = CreateResolver(null, ResultsDirectoryLayout.PerModule, module); + + string actual = resolver.Resolve(module)!; + + string root = Path.Combine(WorkingDirectory, "TestResults"); + actual.Should().StartWith(root + Path.DirectorySeparatorChar); + Path.GetFileName(Path.GetDirectoryName(actual)).Should().NotBe(".."); + } + + [TestMethod] + public void ResolveIsIndependentOfTheCurrentDirectory() + { + // The same solution must produce the same folder names no matter where dotnet test ran. + TestModule first = CreateModule("Tests", parentDirectory: "src"); + TestModule second = CreateModule("Tests", parentDirectory: "samples"); + + string fromRepoRoot = CreateResolver(null, ResultsDirectoryLayout.PerModule, WorkingDirectory, first, second).Resolve(first)!; + string fromElsewhere = CreateResolver(null, ResultsDirectoryLayout.PerModule, Path.Combine(WorkingDirectory, "src"), first, second).Resolve(first)!; + + Path.GetFileName(Path.GetDirectoryName(fromRepoRoot)) + .Should().Be(Path.GetFileName(Path.GetDirectoryName(fromElsewhere))); + } + + [TestMethod] + public void ResolveUsesRuntimeIdentifierInPivotWhenOneWasRequested() + { + TestModule module = CreateModule("ProjectA", runtimeIdentifier: "linux-musl-arm64"); + TestResultsDirectoryResolver resolver = CreateResolver(null, ResultsDirectoryLayout.PerModule, module); + + Path.GetFileName(resolver.Resolve(module)).Should().Be("net10.0_linux-musl-arm64"); + } + + [TestMethod] + public void ResolveFallsBackToAssemblyNameWhenModuleHasNoProjectMetadata() + { + string targetPath = Path.Combine(WorkingDirectory, "bin", "DirectTests.dll"); + TestModule module = new( + new RunProperties("dotnet", $"exec \"{targetPath}\"", null), + ProjectFullPath: null, + TargetFramework: null, + IsTestingPlatformApplication: true, + LaunchSettings: null, + TargetPath: targetPath, + DotnetRootArchVariableName: null, + EnvironmentVariables: ImmutableDictionary.Empty); + TestResultsDirectoryResolver resolver = CreateResolver(null, ResultsDirectoryLayout.PerModule, module); + + string actual = resolver.Resolve(module)!; + + Path.GetDirectoryName(actual).Should().Be(Path.Combine(WorkingDirectory, "TestResults", "DirectTests")); + Path.GetFileName(actual).Should().MatchRegex("^unknown_[a-z0-9]+$"); + } + + private static string WorkingDirectory => Path.GetFullPath("repo"); + + private static TestResultsDirectoryResolver CreateResolver(string? resultsDirectory, ResultsDirectoryLayout layout, params TestModule[] modules) + => CreateResolver(resultsDirectory, layout, WorkingDirectory, modules); + + private static TestResultsDirectoryResolver CreateResolver(string? resultsDirectory, ResultsDirectoryLayout layout, string workingDirectory, params TestModule[] modules) + => TestResultsDirectoryResolver.Create( + new PathOptions( + ProjectOrSolutionPath: null, + SolutionPath: null, + TestModules: null, + ResultsDirectoryPath: resultsDirectory, + ResultsDirectoryLayout: layout, + ConfigFilePath: null, + DiagnosticOutputDirectoryPath: null), + modules, + workingDirectory); + + private static TestModule CreateModule(string projectName = "ProjectA", string? parentDirectory = null, string runtimeIdentifier = "") + { + string projectDirectory = parentDirectory is null + ? Path.Combine(WorkingDirectory, projectName) + : Path.Combine(WorkingDirectory, parentDirectory, projectName); + string targetPath = Path.Combine(projectDirectory, "bin", "Debug", "net10.0", "MyTests.dll"); + + return new TestModule( + new RunProperties( + Command: "dotnet", + Arguments: targetPath, + WorkingDirectory: projectDirectory, + RuntimeIdentifier: runtimeIdentifier, + DefaultAppHostRuntimeIdentifier: "win-x64", + TargetFrameworkVersion: "v10.0"), + ProjectFullPath: Path.Combine(projectDirectory, $"{projectName}.csproj"), + TargetFramework: "net10.0", + IsTestingPlatformApplication: true, + LaunchSettings: null, + TargetPath: targetPath, + DotnetRootArchVariableName: null, + EnvironmentVariables: ImmutableDictionary.Empty); + } +} diff --git a/test/dotnet.Tests/CommandTests/Test/snapshots/MTPHelpSnapshotTests.VerifyMTPHelpOutput.verified.txt b/test/dotnet.Tests/CommandTests/Test/snapshots/MTPHelpSnapshotTests.VerifyMTPHelpOutput.verified.txt index 5e3f844bf320..6c57081d52f4 100644 --- a/test/dotnet.Tests/CommandTests/Test/snapshots/MTPHelpSnapshotTests.VerifyMTPHelpOutput.verified.txt +++ b/test/dotnet.Tests/CommandTests/Test/snapshots/MTPHelpSnapshotTests.VerifyMTPHelpOutput.verified.txt @@ -11,6 +11,9 @@ Options: --root-directory The test modules have the specified root directory. --results-directory The directory where the test results will be placed. The specified directory will be created if it does not exist. + --results-directory-layout Specifies how test results are organized within the results directory. + 'flat' (the default) places the results of every test project directly in the results directory. + 'per-module' gives every test project its own '/_' subdirectory, so reports with the same file name cannot overwrite each other. --config-file Specifies a testconfig.json file. --diagnostic-output-directory Output directory of the diagnostic logging. If not specified the file will be generated inside the default 'TestResults' directory. From 79a63d2554566b815a7dc7d4a1a11e13ab160ff2 Mon Sep 17 00:00:00 2001 From: Amaury Leveugle Date: Mon, 3 Aug 2026 20:27:14 +0200 Subject: [PATCH 2/2] Fix PathOptions construction in artifact tests Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 00868f6a-f41f-422e-aaf7-0cc9f56142a1 --- .../Test/ArtifactPostProcessingManagerTests.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/dotnet.Tests/CommandTests/Test/ArtifactPostProcessingManagerTests.cs b/test/dotnet.Tests/CommandTests/Test/ArtifactPostProcessingManagerTests.cs index 6b3945f294d5..28e6f5594515 100644 --- a/test/dotnet.Tests/CommandTests/Test/ArtifactPostProcessingManagerTests.cs +++ b/test/dotnet.Tests/CommandTests/Test/ArtifactPostProcessingManagerTests.cs @@ -140,6 +140,7 @@ public void BuildArtifactPostProcessingArguments_ForwardsOptionsThatGovernExtens SolutionPath: null, TestModules: null, ResultsDirectoryPath: "/results", + ResultsDirectoryLayout: ResultsDirectoryLayout.Flat, ConfigFilePath: "/config/testconfig.json", DiagnosticOutputDirectoryPath: "/diagnostics"); @@ -166,6 +167,7 @@ public void BuildArtifactPostProcessingArguments_DoesNotForwardResultsDirectory( SolutionPath: null, TestModules: null, ResultsDirectoryPath: "/results", + ResultsDirectoryLayout: ResultsDirectoryLayout.Flat, ConfigFilePath: null, DiagnosticOutputDirectoryPath: null); @@ -466,7 +468,14 @@ private static CtrlCCancellationManager CreateCancellationManager() private static BuildOptions CreateBuildOptions(string? resultsDirectory = null) => new( - new PathOptions(null, null, null, ResultsDirectoryPath: resultsDirectory, null, null), + new PathOptions( + ProjectOrSolutionPath: null, + SolutionPath: null, + TestModules: null, + ResultsDirectoryPath: resultsDirectory, + ResultsDirectoryLayout: ResultsDirectoryLayout.Flat, + ConfigFilePath: null, + DiagnosticOutputDirectoryPath: null), HasNoRestore: false, HasNoBuild: false, Verbosity: null,