Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -201,7 +201,12 @@ public static Option<bool> CreateUseCurrentRuntimeOption(string description) =>
{
Description = description,
HelpName = CommandDefinitionStrings.ConfigurationArgumentName,
IsDynamic = true
IsDynamic = true,
DefaultValueFactory = _ =>
{
string? configuration = Environment.GetEnvironmentVariable("Configuration");
return string.IsNullOrWhiteSpace(configuration) ? null : configuration;
}
}.ForwardAsSingle(o => $"--property:Configuration={o}");

public static Option<string> CreateVersionSuffixOption() =>
Expand Down Expand Up @@ -380,4 +385,3 @@ public static void ValidateSelfContainedOptions(bool hasSelfContainedOption, boo
Arity = ArgumentArity.Zero
};
}

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ public void RunWithSolutionPathWithFailingTests_ShouldReturnExitCodeAtLeastOneTe
result.ExitCode.Should().Be(ExitCodes.AtLeastOneTestFailed);
}

[TestMethod]
public void RunWithSolutionPath_ShouldUseConfigurationEnvironmentVariable()
{
TestAsset testInstance = TestAssetsManager.CopyTestAsset("MultiTestProjectSolutionWithTests", Guid.NewGuid().ToString()).WithSource();

CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false)
.WithWorkingDirectory(testInstance.Path)
.WithEnvironmentVariable("Configuration", TestingConstants.Release)
.Execute("--solution", "MultiTestProjectSolutionWithTests.sln");

Assert.MatchesRegex(RegexPatternHelper.GenerateProjectRegexPattern("TestProject", TestingConstants.Failed, true, TestingConstants.Release), result.StdOut);
Assert.MatchesRegex(RegexPatternHelper.GenerateProjectRegexPattern("OtherTestProject", TestingConstants.Passed, true, TestingConstants.Release), result.StdOut);
result.ExitCode.Should().Be(ExitCodes.AtLeastOneTestFailed);
}

[TestMethod, CombinatorialData]
public void RunWithSolutionFilterPathWithFailingTests_ShouldReturnExitCodeGenericFailure(
[CombinatorialValues(TestingConstants.Debug, TestingConstants.Release)] string configuration,
Expand Down
75 changes: 75 additions & 0 deletions test/dotnet.Tests/ParserTests/CommonOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,87 @@

using System.CommandLine;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;

namespace Microsoft.DotNet.Tests.ParserTests;

[TestClass]
public class CommonOptionsTests
{
[TestMethod]
public void ConfigurationDefaultsToEnvironmentVariable()
{
string? originalConfiguration = Environment.GetEnvironmentVariable("Configuration");

try
{
Environment.SetEnvironmentVariable("Configuration", "EnvironmentConfiguration");
var command = new RootCommand();
var option = CommonOptions.CreateConfigurationOption("Configuration");
command.Options.Add(option);

var result = command.Parse([]);

result.GetValue(option).Should().Be("EnvironmentConfiguration");
result.OptionValuesToBeForwarded(command).Should().ContainSingle()
.Which.Should().Be("--property:Configuration=EnvironmentConfiguration");
}
finally
{
Environment.SetEnvironmentVariable("Configuration", originalConfiguration);
}
}

[TestMethod]
public void ExplicitConfigurationOverridesEnvironmentVariable()
{
string? originalConfiguration = Environment.GetEnvironmentVariable("Configuration");

try
{
Environment.SetEnvironmentVariable("Configuration", "EnvironmentConfiguration");
var command = new RootCommand();
var option = CommonOptions.CreateConfigurationOption("Configuration");
command.Options.Add(option);

var result = command.Parse(["--configuration", "ExplicitConfiguration"]);

result.GetValue(option).Should().Be("ExplicitConfiguration");
result.OptionValuesToBeForwarded(command).Should().ContainSingle()
.Which.Should().Be("--property:Configuration=ExplicitConfiguration");
}
finally
{
Environment.SetEnvironmentVariable("Configuration", originalConfiguration);
}
}

[TestMethod]
[DataRow("")]
[DataRow(" ")]
[DataRow("\t")]
public void EmptyOrWhitespaceConfigurationEnvironmentVariableIsIgnored(string configuration)
{
string? originalConfiguration = Environment.GetEnvironmentVariable("Configuration");

try
{
Environment.SetEnvironmentVariable("Configuration", configuration);
var command = new RootCommand();
var option = CommonOptions.CreateConfigurationOption("Configuration");
command.Options.Add(option);

var result = command.Parse([]);

result.GetValue(option).Should().BeNull();
result.OptionValuesToBeForwarded(command).Should().BeEmpty();
}
finally
{
Environment.SetEnvironmentVariable("Configuration", originalConfiguration);
}
}

[TestMethod]
public void Duplicates()
{
Expand Down
Loading