Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -e option to dotnet run #45795

Merged
merged 7 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 13 additions & 4 deletions src/Cli/dotnet/CommonOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.CommandLine;
using System.CommandLine.Completions;
using System.CommandLine.Parsing;
using System.ComponentModel.DataAnnotations;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools;
using Microsoft.DotNet.Tools.Common;
Expand Down Expand Up @@ -186,15 +187,23 @@ internal static string ArchOptionValue(ParseResult parseResult) =>

private static IReadOnlyDictionary<string, string> ParseEnvironmentVariables(ArgumentResult argumentResult)
{
Dictionary<string, string> result = [];
var result = new Dictionary<string, string>(
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal);

List<CliToken>? invalid = null;

foreach (var token in argumentResult.Tokens)
{
if (token.Value.IndexOf('=') is var index and > 0 &&
token.Value[0..index].Trim() is var name and not "")
var separator = token.Value.IndexOf('=');
var (name, value) = (separator >= 0)
? (token.Value[0..separator], token.Value[(separator + 1)..])
: (token.Value, "");

name = name.Trim();

if (name != "")
{
result[name] = token.Value[(index + 1)..];
result[name] = value;
}
else
{
Expand Down
45 changes: 43 additions & 2 deletions test/dotnet.Tests/ParserTests/CommonOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ public void Duplicates()
result.Errors.Should().BeEmpty();
}

[Fact]
public void Duplicates_CasingDifference()
{
var command = new CliRootCommand();
command.Options.Add(CommonOptions.EnvOption);

var result = command.Parse(["-e", "A=1", "-e", "a=2"]);

var expected = new Dictionary<string, string>();

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
expected.Add("A", "2");
}
else
{
expected.Add("A", "1");
expected.Add("a", "2");
}

result.GetValue(CommonOptions.EnvOption)
.Should()
.BeEquivalentTo(expected);

result.Errors.Should().BeEmpty();
}

[Fact]
public void MultiplePerToken()
{
Expand All @@ -44,6 +71,21 @@ public void MultiplePerToken()
result.Errors.Should().BeEmpty();
}

[Fact]
public void NoValue()
{
var command = new CliRootCommand();
command.Options.Add(CommonOptions.EnvOption);

var result = command.Parse(["-e", "A"]);

result.GetValue(CommonOptions.EnvOption)
.Should()
.BeEquivalentTo(new Dictionary<string, string> { ["A"] = "" });

result.Errors.Should().BeEmpty();
}

[Fact]
public void WhitespaceTrimming()
{
Expand All @@ -61,7 +103,6 @@ public void WhitespaceTrimming()

[Theory]
[InlineData("")]
[InlineData("X")]
[InlineData("=")]
[InlineData("= X")]
[InlineData(" \u2002 = X")]
Expand All @@ -74,7 +115,7 @@ public void Errors(string token)

result.Errors.Select(e => e.Message).Should().BeEquivalentTo(
[
string.Format(CommonLocalizableStrings.IncorrectlyFormattedEnvironmentVariables, token)
string.Format(CommonLocalizableStrings.IncorrectlyFormattedEnvironmentVariables, $"'{token}'")
]);
}
}
Loading