Support runsettings environment variables (#3918)

This commit is contained in:
Marco Rossignoli 2024-10-07 17:47:44 +02:00 коммит произвёл GitHub
Родитель 9ff577c878
Коммит 7ff261bdc1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
5 изменённых файлов: 107 добавлений и 3 удалений

Просмотреть файл

@ -20,6 +20,7 @@ public static class TestApplicationBuilderExtensions
testApplicationBuilder.AddRunSettingsService(extension);
testApplicationBuilder.AddTestCaseFilterService(extension);
testApplicationBuilder.AddTestRunParametersService(extension);
testApplicationBuilder.AddRunSettingsEnvironmentVariableProvider(extension);
testApplicationBuilder.RegisterTestFramework(
serviceProvider => new TestFrameworkCapabilities(
new VSTestBridgeExtensionBaseCapabilities(),

Просмотреть файл

@ -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 class TestApplicationBuilderExtensions
/// <param name="extension">The extension that will be used as the source of registration for this helper service.</param>
public static void AddTestRunParametersService(this ITestApplicationBuilder builder, IExtension extension)
=> builder.CommandLine.AddProvider(() => new TestRunParametersCommandLineOptionsProvider(extension));
/// <summary>
/// Register the environment variable provider.
/// </summary>
/// <param name="builder">The test application builder.</param>
/// <param name="extension">The extension that will be used as the source of registration for this helper service.</param>
public static void AddRunSettingsEnvironmentVariableProvider(this ITestApplicationBuilder builder, IExtension extension)
=> builder.TestHostControllers.AddEnvironmentVariableProvider(serviceProvider
=> new RunSettingsEnvironmentVariableProvider(extension, serviceProvider.GetCommandLineOptions(), serviceProvider.GetFileSystem()));
}

Просмотреть файл

@ -1 +1,2 @@
#nullable enable
static Microsoft.Testing.Extensions.VSTestBridge.Helpers.TestApplicationBuilderExtensions.AddRunSettingsEnvironmentVariableProvider(this Microsoft.Testing.Platform.Builder.ITestApplicationBuilder! builder, Microsoft.Testing.Platform.Extensions.IExtension! extension) -> void

Просмотреть файл

@ -0,0 +1,70 @@
// 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 sealed class RunSettingsEnvironmentVariableProvider : ITestHostEnvironmentVariableProvider
{
private readonly IExtension _extension;
private readonly ICommandLineOptions _commandLineOptions;
private readonly IFileSystem _fileSystem;
private XDocument? _runSettings;
public RunSettingsEnvironmentVariableProvider(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<bool> IsEnabledAsync()
{
if (!_commandLineOptions.TryGetOptionArgumentList(RunSettingsCommandLineOptionsProvider.RunSettingsOptionName, out string[]? runsettings))
{
return false;
}
if (!_fileSystem.Exists(runsettings[0]))
{
return false;
}
using IFileStream fileStream = _fileSystem.NewFileStream(runsettings[0], FileMode.Open, FileAccess.Read);
#if NETCOREAPP
_runSettings = await XDocument.LoadAsync(fileStream.Stream, LoadOptions.None, CancellationToken.None);
#else
using StreamReader streamReader = new(fileStream.Stream);
_runSettings = XDocument.Parse(await streamReader.ReadToEndAsync());
#endif
return _runSettings.Element("RunSettings")?.Element("RunConfiguration")?.Element("EnvironmentVariables") is not null;
}
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;
}
public Task<ValidationResult> ValidateTestHostEnvironmentVariablesAsync(IReadOnlyEnvironmentVariables environmentVariables)
=> ValidationResult.ValidTask;
}

Просмотреть файл

@ -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<TestArgumentsEntry<string>> TfmList()
{
var testHost = TestHost.LocateFrom(_testAssetFixture.ProjectPath, TestAssetFixture.ProjectName, TargetFrameworks.NetCurrent.Arguments);
yield return TargetFrameworks.NetCurrent;
yield return TargetFrameworks.NetFramework.First();
}
[ArgumentsProvider(nameof(TfmList))]
public async Task UnsupportedRunSettingsEntriesAreFlagged(string tfm)
{
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 RunSettingsTests : AcceptanceTestBase
{
yield return (ProjectName, ProjectName,
SourceCode
.PatchTargetFrameworks(TargetFrameworks.NetCurrent)
.PatchTargetFrameworks(TargetFrameworks.NetCurrent, TargetFrameworks.NetFramework.First())
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
}
@ -57,6 +71,7 @@ public sealed class RunSettingsTests : AcceptanceTestBase
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
<LangVersion>Preview</LangVersion>
</PropertyGroup>
<ItemGroup>
@ -105,6 +120,11 @@ public sealed class RunSettingsTests : AcceptanceTestBase
<!-- true or false -->
<!-- Value that specifies the exit code when no tests are discovered -->
<TreatNoTestsAsError>true</TreatNoTestsAsError>
<!-- List of environment variables we want to set-->
<EnvironmentVariables>
<SAMPLEKEY>SAMPLEVALUE</SAMPLEKEY>
</EnvironmentVariables>
</RunConfiguration>
<!-- Configurations for data collectors -->
@ -200,6 +220,7 @@ public class UnitTest1
[TestMethod]
public void TestMethod()
{
Assert.AreEqual("SAMPLEVALUE", System.Environment.GetEnvironmentVariable("SAMPLEKEY")!);
}
}
""";