mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Fix application crashes if there are two environment variables with same name, differing only in case (#67)
This commit is contained in:
@@ -206,7 +206,7 @@ namespace CliFx.Analyzers
|
||||
// Duplicate environment variable name
|
||||
var duplicateEnvironmentVariableNameOptions = options
|
||||
.Where(p => !string.IsNullOrWhiteSpace(p.EnvironmentVariableName))
|
||||
.GroupBy(p => p.EnvironmentVariableName, StringComparer.OrdinalIgnoreCase)
|
||||
.GroupBy(p => p.EnvironmentVariableName, StringComparer.Ordinal)
|
||||
.Where(g => g.Count() > 1)
|
||||
.SelectMany(g => g.AsEnumerable())
|
||||
.ToArray();
|
||||
|
||||
@@ -89,5 +89,27 @@ namespace CliFx.Tests
|
||||
Option = $"foo{Path.PathSeparator}bar"
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Option_can_use_a_specific_environment_variable_as_fallback_while_respecting_case()
|
||||
{
|
||||
// Arrange
|
||||
const string expected = "foobar";
|
||||
var input = CommandInput.Empty;
|
||||
var envVars = new Dictionary<string, string>
|
||||
{
|
||||
["ENV_OPT"] = expected,
|
||||
["env_opt"] = "2"
|
||||
};
|
||||
|
||||
// Act
|
||||
var instance = CommandHelper.ResolveCommand<EnvironmentVariableCommand>(input, envVars);
|
||||
|
||||
// Assert
|
||||
instance.Should().BeEquivalentTo(new EnvironmentVariableCommand
|
||||
{
|
||||
Option = expected
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,9 +218,10 @@ namespace CliFx
|
||||
/// </remarks>
|
||||
public async ValueTask<int> RunAsync(IReadOnlyList<string> commandLineArguments)
|
||||
{
|
||||
// Environment variable names are case-insensitive on Windows but are case-sensitive on Linux and macOS
|
||||
var environmentVariables = Environment.GetEnvironmentVariables()
|
||||
.Cast<DictionaryEntry>()
|
||||
.ToDictionary(e => (string) e.Key, e => (string) e.Value, StringComparer.OrdinalIgnoreCase);
|
||||
.ToDictionary(e => (string) e.Key, e => (string) e.Value, StringComparer.Ordinal);
|
||||
|
||||
return await RunAsync(commandLineArguments, environmentVariables);
|
||||
}
|
||||
@@ -264,4 +265,4 @@ namespace CliFx
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace CliFx.Domain
|
||||
|
||||
public bool MatchesEnvironmentVariableName(string environmentVariableName) =>
|
||||
!string.IsNullOrWhiteSpace(EnvironmentVariableName) &&
|
||||
string.Equals(EnvironmentVariableName, environmentVariableName, StringComparison.OrdinalIgnoreCase);
|
||||
string.Equals(EnvironmentVariableName, environmentVariableName, StringComparison.Ordinal);
|
||||
|
||||
public string GetUserFacingDisplayString()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user