Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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 System.CommandLine;
using System.Diagnostics.CodeAnalysis;
using Microsoft.DotNet.Cli.Commands.Test.Terminal;
Expand Down Expand Up @@ -41,7 +42,10 @@ private int RunInternal(ParseResult parseResult, bool isHelp)
ValidationUtility.ValidateSolutionOrProjectOrDirectoryOrModulesArePassedCorrectly(parseResult);

int degreeOfParallelism = GetDegreeOfParallelism(parseResult);
var testOptions = new TestOptions(IsHelp: isHelp, IsDiscovery: parseResult.HasOption(MicrosoftTestingPlatformOptions.ListTestsOption));
var testOptions = new TestOptions(
IsHelp: isHelp,
IsDiscovery: parseResult.HasOption(MicrosoftTestingPlatformOptions.ListTestsOption),
EnvironmentVariables: parseResult.GetValue(CommonOptions.EnvOption) ?? ImmutableDictionary<string, string>.Empty);

InitializeOutput(degreeOfParallelism, parseResult, testOptions);

Expand Down
2 changes: 1 addition & 1 deletion src/Cli/dotnet/Commands/Test/MTP/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Microsoft.DotNet.Cli.Commands.Test;

internal record TestOptions(bool IsHelp, bool IsDiscovery);
internal record TestOptions(bool IsHelp, bool IsDiscovery, IReadOnlyDictionary<string, string> EnvironmentVariables);

internal record PathOptions(string? ProjectPath, string? SolutionPath, string? ResultsDirectoryPath, string? ConfigFilePath, string? DiagnosticOutputDirectoryPath);

Expand Down
6 changes: 6 additions & 0 deletions src/Cli/dotnet/Commands/Test/MTP/TestApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ private ProcessStartInfo CreateProcessStartInfo()
processStartInfo.Environment[entry.Key] = value;
}

// Env variables specified on command line override those specified in launch profile:
foreach (var (name, value) in TestOptions.EnvironmentVariables)
{
processStartInfo.Environment[name] = value;
}

if (!_buildOptions.NoLaunchProfileArguments &&
!string.IsNullOrEmpty(Module.LaunchSettings.CommandLineArgs))
{
Expand Down
1 change: 1 addition & 0 deletions src/Cli/dotnet/Commands/Test/TestCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ private static Command GetTestingPlatformCliCommand()
command.Options.Add(MicrosoftTestingPlatformOptions.MaxParallelTestModulesOption);
command.Options.Add(MicrosoftTestingPlatformOptions.MinimumExpectedTestsOption);
command.Options.Add(CommonOptions.ArchitectureOption);
command.Options.Add(CommonOptions.EnvOption);
command.Options.Add(CommonOptions.PropertiesOption);
command.Options.Add(MicrosoftTestingPlatformOptions.ConfigurationOption);
command.Options.Add(MicrosoftTestingPlatformOptions.FrameworkOption);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.Testing.Platform.Builder;
using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.TestFramework;

var testApplicationBuilder = await TestApplication.CreateBuilderAsync(args);

testApplicationBuilder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, __) => new DummyTestAdapter());

using var testApplication = await testApplicationBuilder.BuildAsync();
return await testApplication.RunAsync();

public class DummyTestAdapter : ITestFramework, IDataProducer
{
public string Uid => nameof(DummyTestAdapter);

public string Version => "2.0.0";

public string DisplayName => nameof(DummyTestAdapter);

public string Description => nameof(DummyTestAdapter);

public Task<bool> IsEnabledAsync() => Task.FromResult(true);

public Type[] DataTypesProduced => new[] {
typeof(TestNodeUpdateMessage)
};

public Task<CreateTestSessionResult> CreateTestSessionAsync(CreateTestSessionContext context)
=> Task.FromResult(new CreateTestSessionResult() { IsSuccess = true });

public Task<CloseTestSessionResult> CloseTestSessionAsync(CloseTestSessionContext context)
=> Task.FromResult(new CloseTestSessionResult() { IsSuccess = true });

public async Task ExecuteRequestAsync(ExecuteRequestContext context)
{
var envVariableValue = Environment.GetEnvironmentVariable("DUMMY_TEST_ENV_VAR") ?? "NOT SET";
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid, new TestNode()
{
Uid = "Test1",
DisplayName = "Test1",
Properties = new PropertyBag(new FailedTestNodeStateProperty($"DUMMY_TEST_ENV_VAR is '{envVariableValue}'")),
}));

context.Complete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"ConsoleApp25": {
"commandName": "Project",
"environmentVariables": {
"DUMMY_TEST_ENV_VAR": "FROM_LAUNCHSETTINGS"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), testAsset.props))\testAsset.props" />

<PropertyGroup>
<TargetFramework>$(CurrentTargetFramework)</TargetFramework>
<OutputType>Exe</OutputType>

<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
<IsTestingPlatformApplication>true</IsTestingPlatformApplication>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Testing.Platform" Version="$(MicrosoftTestingPlatformVersion)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"test": {
"runner": "Microsoft.Testing.Platform"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -476,5 +476,35 @@ at Microsoft.DotNet.Cli.Commands.Test.TestApplicationActionQueue.Read(BuildOptio
result.StdOut.Contains("Test run completed with non-success exit code: 1 (see: https://aka.ms/testingplatform/exitcodes)");
}
}

[Theory]
[InlineData(TestingConstants.Debug)]
[InlineData(TestingConstants.Release)]
public void RunTestProjectWithEnvVariable(string configuration)
{
TestAsset testInstance = _testAssetsManager.CopyTestAsset("TestProjectShowingEnvVariable", Guid.NewGuid().ToString())
.WithSource();

CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false)
.WithWorkingDirectory(testInstance.Path)
.Execute(
MicrosoftTestingPlatformOptions.ConfigurationOption.Name, configuration,
CommonOptions.EnvOption.Name, "DUMMY_TEST_ENV_VAR=ENV_VAR_CMD_LINE");

if (!TestContext.IsLocalized())
{
result.StdOut
.Should().Contain("Using launch settings from")
.And.Contain($"Properties{Path.DirectorySeparatorChar}launchSettings.json...")
.And.Contain("Test run summary: Failed!")
.And.Contain("total: 1")
.And.Contain("succeeded: 0")
.And.Contain("failed: 1")
.And.Contain("skipped: 0")
.And.Contain("DUMMY_TEST_ENV_VAR is 'ENV_VAR_CMD_LINE'");
}

result.ExitCode.Should().Be(ExitCodes.AtLeastOneTestFailed);
}
}
}