From 571d3e60e666b9b0d4658899126ee9fa33422527 Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Fri, 4 Oct 2024 16:32:14 +0200 Subject: [PATCH 1/8] Save work --- NuGet.config | 1 + eng/Build.props | 20 ++++++ global.json | 4 +- samples/Playground/Program.cs | 6 +- samples/Playground/config.runsettings | 7 +- .../TestApplicationBuilderExtensions.cs | 1 + .../TestApplicationBuilderExtensions.cs | 11 +++ .../PublicAPI.Unshipped.txt | 1 + .../VSBridgeEnvironmentVariableProvider.cs | 68 +++++++++++++++++++ 9 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/VSBridgeEnvironmentVariableProvider.cs diff --git a/NuGet.config b/NuGet.config index b5ce5f5511..7860a3c455 100644 --- a/NuGet.config +++ b/NuGet.config @@ -10,6 +10,7 @@ + diff --git a/eng/Build.props b/eng/Build.props index 57aac3a459..5424ab6dfa 100644 --- a/eng/Build.props +++ b/eng/Build.props @@ -38,4 +38,24 @@ + + + + 6.0.33 + 6.0.33 + + + 6.0.33 + + + + + 8.0.8 + 8.0.8 + + + 8.0.8 + + + diff --git a/global.json b/global.json index abb5de4aae..4c67350ffa 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "tools": { - "dotnet": "9.0.100-rc.1.24452.12", + "dotnet": "8.0.400", "runtimes": { "dotnet": [ "3.1.32", @@ -20,7 +20,7 @@ } }, "sdk": { - "version": "9.0.100-rc.1.24452.12", + "version": "8.0.400", "allowPrerelease": true, "rollForward": "latestFeature" }, diff --git a/samples/Playground/Program.cs b/samples/Playground/Program.cs index ad40e696f6..982ff3c7ac 100644 --- a/samples/Playground/Program.cs +++ b/samples/Playground/Program.cs @@ -3,6 +3,7 @@ using System.Reflection; +using Microsoft.Testing.Extensions; using Microsoft.Testing.Platform.Builder; using Microsoft.Testing.Platform.Extensions.Messages; using Microsoft.Testing.Platform.Extensions.TestHostControllers; @@ -25,15 +26,16 @@ public static async Task Main(string[] args) if (Environment.GetEnvironmentVariable("TESTSERVERMODE") != "1") { // To attach to the children - Microsoft.Testing.TestInfrastructure.DebuggerUtility.AttachCurrentProcessToParentVSProcess(); + // Microsoft.Testing.TestInfrastructure.DebuggerUtility.AttachCurrentProcessToParentVSProcess(); ITestApplicationBuilder testApplicationBuilder = await TestApplication.CreateBuilderAsync(args); testApplicationBuilder.AddMSTest(() => [Assembly.GetEntryAssembly()!]); // Custom test host controller extension - testApplicationBuilder.TestHostControllers.AddProcessLifetimeHandler(s => new OutOfProc(s.GetMessageBus())); + // testApplicationBuilder.TestHostControllers.AddProcessLifetimeHandler(s => new OutOfProc(s.GetMessageBus())); // Enable Trx // testApplicationBuilder.AddTrxReportProvider(); + testApplicationBuilder.AddAppInsightsTelemetryProvider(); // Enable Telemetry // testApplicationBuilder.AddAppInsightsTelemetryProvider(); diff --git a/samples/Playground/config.runsettings b/samples/Playground/config.runsettings index ad8d6a8826..967e0d7a56 100644 --- a/samples/Playground/config.runsettings +++ b/samples/Playground/config.runsettings @@ -2,8 +2,13 @@ + + + C:\ProgramFiles\dotnet + C:\Codebase\Sdk + - \ No newline at end of file + diff --git a/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/TestApplicationBuilderExtensions.cs b/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/TestApplicationBuilderExtensions.cs index d1b31310d5..e0eb75e08f 100644 --- a/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/TestApplicationBuilderExtensions.cs +++ b/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/TestApplicationBuilderExtensions.cs @@ -20,6 +20,7 @@ public static void AddMSTest(this ITestApplicationBuilder testApplicationBuilder testApplicationBuilder.AddRunSettingsService(extension); testApplicationBuilder.AddTestCaseFilterService(extension); testApplicationBuilder.AddTestRunParametersService(extension); + testApplicationBuilder.AddEnvironmentVariableProvider(extension); testApplicationBuilder.RegisterTestFramework( serviceProvider => new TestFrameworkCapabilities( new VSTestBridgeExtensionBaseCapabilities(), diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Helpers/TestApplicationBuilderExtensions.cs b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Helpers/TestApplicationBuilderExtensions.cs index e788253efe..99d8f10ed6 100644 --- a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Helpers/TestApplicationBuilderExtensions.cs +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Helpers/TestApplicationBuilderExtensions.cs @@ -3,9 +3,11 @@ using Microsoft.Testing.Extensions.VSTestBridge.CommandLine; using Microsoft.Testing.Extensions.VSTestBridge.Configurations; +using Microsoft.Testing.Extensions.VSTestBridge.TestHostControllers; using Microsoft.Testing.Platform.Builder; using Microsoft.Testing.Platform.Extensions; using Microsoft.Testing.Platform.Helpers; +using Microsoft.Testing.Platform.Services; namespace Microsoft.Testing.Extensions.VSTestBridge.Helpers; @@ -44,4 +46,13 @@ public static void AddRunSettingsService(this ITestApplicationBuilder builder, I /// The extension that will be used as the source of registration for this helper service. public static void AddTestRunParametersService(this ITestApplicationBuilder builder, IExtension extension) => builder.CommandLine.AddProvider(() => new TestRunParametersCommandLineOptionsProvider(extension)); + + /// + /// Register the environment variable provider + /// + /// The test application builder. + /// The extension that will be used as the source of registration for this helper service. + public static void AddEnvironmentVariableProvider(this ITestApplicationBuilder builder, IExtension extension) + => builder.TestHostControllers.AddEnvironmentVariableProvider(serviceProvider + => new VSBridgeEnvironmentVariableProvider(extension, serviceProvider.GetCommandLineOptions(), serviceProvider.GetFileSystem())); } diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/PublicAPI.Unshipped.txt b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/PublicAPI.Unshipped.txt index 7dc5c58110..da60dfd378 100644 --- a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/PublicAPI.Unshipped.txt +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/PublicAPI.Unshipped.txt @@ -1 +1,2 @@ #nullable enable +static Microsoft.Testing.Extensions.VSTestBridge.Helpers.TestApplicationBuilderExtensions.AddEnvironmentVariableProvider(this Microsoft.Testing.Platform.Builder.ITestApplicationBuilder! builder, Microsoft.Testing.Platform.Extensions.IExtension! extension) -> void diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/VSBridgeEnvironmentVariableProvider.cs b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/VSBridgeEnvironmentVariableProvider.cs new file mode 100644 index 0000000000..81f32d4831 --- /dev/null +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/VSBridgeEnvironmentVariableProvider.cs @@ -0,0 +1,68 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Xml.Linq; + +using Microsoft.Testing.Extensions.VSTestBridge.CommandLine; +using Microsoft.Testing.Platform.CommandLine; +using Microsoft.Testing.Platform.Extensions; +using Microsoft.Testing.Platform.Extensions.TestHostControllers; +using Microsoft.Testing.Platform.Helpers; + +namespace Microsoft.Testing.Extensions.VSTestBridge.TestHostControllers; + +internal class VSBridgeEnvironmentVariableProvider : ITestHostEnvironmentVariableProvider +{ + private readonly IExtension _extension; + private readonly ICommandLineOptions _commandLineOptions; + private readonly IFileSystem _fileSystem; + private XDocument? _runSettings; + + public VSBridgeEnvironmentVariableProvider(IExtension extension, ICommandLineOptions commandLineOptions, IFileSystem fileSystem) + { + _extension = extension; + _commandLineOptions = commandLineOptions; + _fileSystem = fileSystem; + } + + public string Uid => _extension.Uid; + + public string Version => _extension.Version; + + public string DisplayName => _extension.DisplayName; + + public string Description => _extension.Description; + + public async Task IsEnabledAsync() + { + if (_commandLineOptions.TryGetOptionArgumentList(RunSettingsCommandLineOptionsProvider.RunSettingsOptionName, out string[]? runsettings)) + { + if (_fileSystem.Exists(runsettings[0])) + { +#if NETCOREAPP + using IFileStream fileStream = _fileSystem.NewFileStream(runsettings[0], FileMode.Open); + _runSettings = await XDocument.LoadAsync(fileStream.Stream, LoadOptions.None, CancellationToken.None); +#else + _runSettings = XDocument.Load(runsettings[0]); +#endif + return _runSettings.Element("RunSettings")?.Element("RunConfiguration")?.Element("EnvironmentVariables") is not null; + } + } + + return false; + } + + public Task UpdateAsync(IEnvironmentVariables environmentVariables) + { + + foreach (XElement element in _runSettings!.Element("RunSettings")!.Element("RunConfiguration")!.Element("EnvironmentVariables")!.Elements()) + { + + } + + return Task.CompletedTask; + } + + public Task ValidateTestHostEnvironmentVariablesAsync(IReadOnlyEnvironmentVariables environmentVariables) + => ValidationResult.ValidTask; +} From 04f83f644a455bb60f8d3b7d5bbba6fd09534fb9 Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Mon, 7 Oct 2024 16:13:37 +0200 Subject: [PATCH 2/8] add tests --- .../TestApplicationBuilderExtensions.cs | 2 +- .../VSBridgeEnvironmentVariableProvider.cs | 8 +++--- .../RunsettingsTests.cs | 27 ++++++++++++++++--- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Helpers/TestApplicationBuilderExtensions.cs b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Helpers/TestApplicationBuilderExtensions.cs index 99d8f10ed6..1d4ec7f1b0 100644 --- a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Helpers/TestApplicationBuilderExtensions.cs +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Helpers/TestApplicationBuilderExtensions.cs @@ -48,7 +48,7 @@ public static void AddTestRunParametersService(this ITestApplicationBuilder buil => builder.CommandLine.AddProvider(() => new TestRunParametersCommandLineOptionsProvider(extension)); /// - /// Register the environment variable provider + /// Register the environment variable provider. /// /// The test application builder. /// The extension that will be used as the source of registration for this helper service. diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/VSBridgeEnvironmentVariableProvider.cs b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/VSBridgeEnvironmentVariableProvider.cs index 81f32d4831..0adbac100c 100644 --- a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/VSBridgeEnvironmentVariableProvider.cs +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/VSBridgeEnvironmentVariableProvider.cs @@ -39,11 +39,12 @@ public async Task IsEnabledAsync() { if (_fileSystem.Exists(runsettings[0])) { -#if NETCOREAPP using IFileStream fileStream = _fileSystem.NewFileStream(runsettings[0], FileMode.Open); +#if NETCOREAPP _runSettings = await XDocument.LoadAsync(fileStream.Stream, LoadOptions.None, CancellationToken.None); #else - _runSettings = XDocument.Load(runsettings[0]); + using StreamReader streamReader = new(fileStream.Stream); + _runSettings = XDocument.Parse(await streamReader.ReadToEndAsync()); #endif return _runSettings.Element("RunSettings")?.Element("RunConfiguration")?.Element("EnvironmentVariables") is not null; } @@ -54,10 +55,9 @@ public async Task IsEnabledAsync() public Task UpdateAsync(IEnvironmentVariables environmentVariables) { - foreach (XElement element in _runSettings!.Element("RunSettings")!.Element("RunConfiguration")!.Element("EnvironmentVariables")!.Elements()) { - + environmentVariables.SetVariable(new(element.Name.ToString(), element.Value, true, true)); } return Task.CompletedTask; diff --git a/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/RunsettingsTests.cs b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/RunsettingsTests.cs index b4cb113c85..8b9ec37177 100644 --- a/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/RunsettingsTests.cs +++ b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/RunsettingsTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.Runtime.InteropServices; + using Microsoft.Testing.Platform.Acceptance.IntegrationTests; using Microsoft.Testing.Platform.Acceptance.IntegrationTests.Helpers; @@ -14,9 +16,21 @@ public sealed class RunSettingsTests : AcceptanceTestBase public RunSettingsTests(ITestExecutionContext testExecutionContext, TestAssetFixture testAssetFixture) : base(testExecutionContext) => _testAssetFixture = testAssetFixture; - public async Task UnsupportedRunSettingsEntriesAreFlagged() + internal static IEnumerable> TfmList() + { + yield return TargetFrameworks.NetCurrent; + yield return TargetFrameworks.NetFramework.First(); + } + + [ArgumentsProvider(nameof(TfmList))] + public async Task UnsupportedRunSettingsEntriesAreFlagged(string tfm) { - var testHost = TestHost.LocateFrom(_testAssetFixture.ProjectPath, TestAssetFixture.ProjectName, TargetFrameworks.NetCurrent.Arguments); + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && tfm == TargetFrameworks.NetFramework.First().Arguments) + { + return; + } + + var testHost = TestHost.LocateFrom(_testAssetFixture.ProjectPath, TestAssetFixture.ProjectName, tfm); TestHostResult testHostResult = await testHost.ExecuteAsync("--settings my.runsettings"); // Assert @@ -45,7 +59,7 @@ public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture) : Test { yield return (ProjectName, ProjectName, SourceCode - .PatchTargetFrameworks(TargetFrameworks.NetCurrent) + .PatchTargetFrameworks(TargetFrameworks.NetCurrent, TargetFrameworks.NetFramework.First()) .PatchCodeWithReplace("$MSTestVersion$", MSTestVersion)); } @@ -57,6 +71,7 @@ public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture) : Test Exe true $TargetFrameworks$ + Preview @@ -105,6 +120,11 @@ public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture) : Test true + + + + SAMPLEVALUE + @@ -200,6 +220,7 @@ public class UnitTest1 [TestMethod] public void TestMethod() { + Assert.AreEqual("SAMPLEVALUE", System.Environment.GetEnvironmentVariable("SAMPLEKEY")!); } } """; From ebc2fba2a61750c4b07ca150bf5a469307a8cacb Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Mon, 7 Oct 2024 16:15:01 +0200 Subject: [PATCH 3/8] revert some updates --- NuGet.config | 1 - eng/Build.props | 20 ------------------- global.json | 6 +++--- samples/Playground/Program.cs | 6 ++---- samples/Playground/Tests.cs | 1 + samples/Playground/config.runsettings | 7 +------ .../Properties/launchSettings.json | 2 +- 7 files changed, 8 insertions(+), 35 deletions(-) diff --git a/NuGet.config b/NuGet.config index 7860a3c455..b5ce5f5511 100644 --- a/NuGet.config +++ b/NuGet.config @@ -10,7 +10,6 @@ - diff --git a/eng/Build.props b/eng/Build.props index 5424ab6dfa..57aac3a459 100644 --- a/eng/Build.props +++ b/eng/Build.props @@ -38,24 +38,4 @@ - - - - 6.0.33 - 6.0.33 - - - 6.0.33 - - - - - 8.0.8 - 8.0.8 - - - 8.0.8 - - - diff --git a/global.json b/global.json index 4c67350ffa..1db4da5449 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "tools": { - "dotnet": "8.0.400", + "dotnet": "9.0.100-rc.1.24452.12", "runtimes": { "dotnet": [ "3.1.32", @@ -20,12 +20,12 @@ } }, "sdk": { - "version": "8.0.400", + "version": "9.0.100-rc.1.24452.12", "allowPrerelease": true, "rollForward": "latestFeature" }, "msbuild-sdks": { - "Microsoft.DotNet.Arcade.Sdk": "10.0.0-beta.24501.6", + "Microsoft.DotNet.Arcade.Sdk": "10.0.0-beta.24503.1", "MSBuild.Sdk.Extras": "3.0.44" } } diff --git a/samples/Playground/Program.cs b/samples/Playground/Program.cs index 982ff3c7ac..ad40e696f6 100644 --- a/samples/Playground/Program.cs +++ b/samples/Playground/Program.cs @@ -3,7 +3,6 @@ using System.Reflection; -using Microsoft.Testing.Extensions; using Microsoft.Testing.Platform.Builder; using Microsoft.Testing.Platform.Extensions.Messages; using Microsoft.Testing.Platform.Extensions.TestHostControllers; @@ -26,16 +25,15 @@ public static async Task Main(string[] args) if (Environment.GetEnvironmentVariable("TESTSERVERMODE") != "1") { // To attach to the children - // Microsoft.Testing.TestInfrastructure.DebuggerUtility.AttachCurrentProcessToParentVSProcess(); + Microsoft.Testing.TestInfrastructure.DebuggerUtility.AttachCurrentProcessToParentVSProcess(); ITestApplicationBuilder testApplicationBuilder = await TestApplication.CreateBuilderAsync(args); testApplicationBuilder.AddMSTest(() => [Assembly.GetEntryAssembly()!]); // Custom test host controller extension - // testApplicationBuilder.TestHostControllers.AddProcessLifetimeHandler(s => new OutOfProc(s.GetMessageBus())); + testApplicationBuilder.TestHostControllers.AddProcessLifetimeHandler(s => new OutOfProc(s.GetMessageBus())); // Enable Trx // testApplicationBuilder.AddTrxReportProvider(); - testApplicationBuilder.AddAppInsightsTelemetryProvider(); // Enable Telemetry // testApplicationBuilder.AddAppInsightsTelemetryProvider(); diff --git a/samples/Playground/Tests.cs b/samples/Playground/Tests.cs index 4ea420bd9c..2746ea7990 100644 --- a/samples/Playground/Tests.cs +++ b/samples/Playground/Tests.cs @@ -20,6 +20,7 @@ public class TestClass [TestMethod] public void Test2() { + Assert.AreEqual("SAMPLEVALUE", System.Environment.GetEnvironmentVariable("SAMPLEKEY")!); } [TestMethod] diff --git a/samples/Playground/config.runsettings b/samples/Playground/config.runsettings index 967e0d7a56..ad8d6a8826 100644 --- a/samples/Playground/config.runsettings +++ b/samples/Playground/config.runsettings @@ -2,13 +2,8 @@ - - - C:\ProgramFiles\dotnet - C:\Codebase\Sdk - - + \ No newline at end of file diff --git a/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/Properties/launchSettings.json b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/Properties/launchSettings.json index f422322857..ebbcbdd579 100644 --- a/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/Properties/launchSettings.json +++ b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "MSTest.Acceptance.IntegrationTests": { "commandName": "Project", - "commandLineArgs": "--treenode-filter /*/*/*/**" + "commandLineArgs": "--treenode-filter /*/*/RunSettingsTests/**" } } } From b78876e0ebe2cb434f726d9e1310ef95e8b51ba4 Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Mon, 7 Oct 2024 16:16:01 +0200 Subject: [PATCH 4/8] revert update --- global.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.json b/global.json index 1db4da5449..abb5de4aae 100644 --- a/global.json +++ b/global.json @@ -25,7 +25,7 @@ "rollForward": "latestFeature" }, "msbuild-sdks": { - "Microsoft.DotNet.Arcade.Sdk": "10.0.0-beta.24503.1", + "Microsoft.DotNet.Arcade.Sdk": "10.0.0-beta.24501.6", "MSBuild.Sdk.Extras": "3.0.44" } } From eaa81c2de9c481a8f694924a6e3ad8af19f54109 Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Mon, 7 Oct 2024 16:17:58 +0200 Subject: [PATCH 5/8] revert some updates --- .../Properties/launchSettings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/Properties/launchSettings.json b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/Properties/launchSettings.json index ebbcbdd579..f422322857 100644 --- a/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/Properties/launchSettings.json +++ b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "MSTest.Acceptance.IntegrationTests": { "commandName": "Project", - "commandLineArgs": "--treenode-filter /*/*/RunSettingsTests/**" + "commandLineArgs": "--treenode-filter /*/*/*/**" } } } From 20343c4734abdadc24633e745cea300c1b7b0962 Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Mon, 7 Oct 2024 16:34:42 +0200 Subject: [PATCH 6/8] fix build --- samples/Playground/Tests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/Playground/Tests.cs b/samples/Playground/Tests.cs index 2746ea7990..4ea420bd9c 100644 --- a/samples/Playground/Tests.cs +++ b/samples/Playground/Tests.cs @@ -20,7 +20,6 @@ public class TestClass [TestMethod] public void Test2() { - Assert.AreEqual("SAMPLEVALUE", System.Environment.GetEnvironmentVariable("SAMPLEKEY")!); } [TestMethod] From 2e30f2f80b022a49bd19778eb98028139e44cd2f Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Mon, 7 Oct 2024 16:40:42 +0200 Subject: [PATCH 7/8] address PR feedback --- .../TestApplicationBuilderExtensions.cs | 2 +- .../TestApplicationBuilderExtensions.cs | 4 +-- .../PublicAPI.Unshipped.txt | 2 +- ...unSettingsEnvironmentVariableProvider .cs} | 30 ++++++++++--------- 4 files changed, 20 insertions(+), 18 deletions(-) rename src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/{VSBridgeEnvironmentVariableProvider.cs => RunSettingsEnvironmentVariableProvider .cs} (63%) diff --git a/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/TestApplicationBuilderExtensions.cs b/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/TestApplicationBuilderExtensions.cs index e0eb75e08f..2a86adaf09 100644 --- a/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/TestApplicationBuilderExtensions.cs +++ b/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/TestApplicationBuilderExtensions.cs @@ -20,7 +20,7 @@ public static void AddMSTest(this ITestApplicationBuilder testApplicationBuilder testApplicationBuilder.AddRunSettingsService(extension); testApplicationBuilder.AddTestCaseFilterService(extension); testApplicationBuilder.AddTestRunParametersService(extension); - testApplicationBuilder.AddEnvironmentVariableProvider(extension); + testApplicationBuilder.AddRunSettingsEnvironmentVariableProvider(extension); testApplicationBuilder.RegisterTestFramework( serviceProvider => new TestFrameworkCapabilities( new VSTestBridgeExtensionBaseCapabilities(), diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Helpers/TestApplicationBuilderExtensions.cs b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Helpers/TestApplicationBuilderExtensions.cs index 1d4ec7f1b0..9ec3739da9 100644 --- a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Helpers/TestApplicationBuilderExtensions.cs +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Helpers/TestApplicationBuilderExtensions.cs @@ -52,7 +52,7 @@ public static void AddTestRunParametersService(this ITestApplicationBuilder buil /// /// The test application builder. /// The extension that will be used as the source of registration for this helper service. - public static void AddEnvironmentVariableProvider(this ITestApplicationBuilder builder, IExtension extension) + public static void AddRunSettingsEnvironmentVariableProvider(this ITestApplicationBuilder builder, IExtension extension) => builder.TestHostControllers.AddEnvironmentVariableProvider(serviceProvider - => new VSBridgeEnvironmentVariableProvider(extension, serviceProvider.GetCommandLineOptions(), serviceProvider.GetFileSystem())); + => new RunSettingsEnvironmentVariableProvider(extension, serviceProvider.GetCommandLineOptions(), serviceProvider.GetFileSystem())); } diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/PublicAPI.Unshipped.txt b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/PublicAPI.Unshipped.txt index da60dfd378..c27c99f4e5 100644 --- a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/PublicAPI.Unshipped.txt +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/PublicAPI.Unshipped.txt @@ -1,2 +1,2 @@ #nullable enable -static Microsoft.Testing.Extensions.VSTestBridge.Helpers.TestApplicationBuilderExtensions.AddEnvironmentVariableProvider(this Microsoft.Testing.Platform.Builder.ITestApplicationBuilder! builder, Microsoft.Testing.Platform.Extensions.IExtension! extension) -> void +static Microsoft.Testing.Extensions.VSTestBridge.Helpers.TestApplicationBuilderExtensions.AddRunSettingsEnvironmentVariableProvider(this Microsoft.Testing.Platform.Builder.ITestApplicationBuilder! builder, Microsoft.Testing.Platform.Extensions.IExtension! extension) -> void diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/VSBridgeEnvironmentVariableProvider.cs b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/RunSettingsEnvironmentVariableProvider .cs similarity index 63% rename from src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/VSBridgeEnvironmentVariableProvider.cs rename to src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/RunSettingsEnvironmentVariableProvider .cs index 0adbac100c..9fafabeae7 100644 --- a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/VSBridgeEnvironmentVariableProvider.cs +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/RunSettingsEnvironmentVariableProvider .cs @@ -11,14 +11,14 @@ namespace Microsoft.Testing.Extensions.VSTestBridge.TestHostControllers; -internal class VSBridgeEnvironmentVariableProvider : ITestHostEnvironmentVariableProvider +internal sealed class RunSettingsEnvironmentVariableProvider : ITestHostEnvironmentVariableProvider { private readonly IExtension _extension; private readonly ICommandLineOptions _commandLineOptions; private readonly IFileSystem _fileSystem; private XDocument? _runSettings; - public VSBridgeEnvironmentVariableProvider(IExtension extension, ICommandLineOptions commandLineOptions, IFileSystem fileSystem) + public RunSettingsEnvironmentVariableProvider(IExtension extension, ICommandLineOptions commandLineOptions, IFileSystem fileSystem) { _extension = extension; _commandLineOptions = commandLineOptions; @@ -35,22 +35,24 @@ public VSBridgeEnvironmentVariableProvider(IExtension extension, ICommandLineOpt public async Task IsEnabledAsync() { - if (_commandLineOptions.TryGetOptionArgumentList(RunSettingsCommandLineOptionsProvider.RunSettingsOptionName, out string[]? runsettings)) + if (!_commandLineOptions.TryGetOptionArgumentList(RunSettingsCommandLineOptionsProvider.RunSettingsOptionName, out string[]? runsettings)) { - if (_fileSystem.Exists(runsettings[0])) - { - using IFileStream fileStream = _fileSystem.NewFileStream(runsettings[0], FileMode.Open); + return false; + } + + if (!_fileSystem.Exists(runsettings[0])) + { + return false; + } + + using IFileStream fileStream = _fileSystem.NewFileStream(runsettings[0], FileMode.Open); #if NETCOREAPP - _runSettings = await XDocument.LoadAsync(fileStream.Stream, LoadOptions.None, CancellationToken.None); + _runSettings = await XDocument.LoadAsync(fileStream.Stream, LoadOptions.None, CancellationToken.None); #else - using StreamReader streamReader = new(fileStream.Stream); - _runSettings = XDocument.Parse(await streamReader.ReadToEndAsync()); + using StreamReader streamReader = new(fileStream.Stream); + _runSettings = XDocument.Parse(await streamReader.ReadToEndAsync()); #endif - return _runSettings.Element("RunSettings")?.Element("RunConfiguration")?.Element("EnvironmentVariables") is not null; - } - } - - return false; + return _runSettings.Element("RunSettings")?.Element("RunConfiguration")?.Element("EnvironmentVariables") is not null; } public Task UpdateAsync(IEnvironmentVariables environmentVariables) From 852f002e518e7e44dda02b9f6b21b4ed5e117557 Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Mon, 7 Oct 2024 17:12:37 +0200 Subject: [PATCH 8/8] open in read --- .../RunSettingsEnvironmentVariableProvider .cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/RunSettingsEnvironmentVariableProvider .cs b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/RunSettingsEnvironmentVariableProvider .cs index 9fafabeae7..23a6896af9 100644 --- a/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/RunSettingsEnvironmentVariableProvider .cs +++ b/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/TestHostControllers/RunSettingsEnvironmentVariableProvider .cs @@ -45,7 +45,7 @@ public async Task IsEnabledAsync() return false; } - using IFileStream fileStream = _fileSystem.NewFileStream(runsettings[0], FileMode.Open); + using IFileStream fileStream = _fileSystem.NewFileStream(runsettings[0], FileMode.Open, FileAccess.Read); #if NETCOREAPP _runSettings = await XDocument.LoadAsync(fileStream.Stream, LoadOptions.None, CancellationToken.None); #else