-
Notifications
You must be signed in to change notification settings - Fork 401
Add Environment Variable directive parsing #965
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2bac5e3
Add environment variable directive parser
fredrikhr 047eed1
Add tests for special environment directive cases
fredrikhr 0f75261
Use FluentAssertions for Env-Directive unit-tests
fredrikhr b755924
Add brances in if-check for environment variables
fredrikhr 51bd07b
Construct test environment variable in test-constructor
fredrikhr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
185 changes: 185 additions & 0 deletions
185
src/System.CommandLine.Tests/EnvironmentVariableDirectiveTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
using FluentAssertions; | ||
|
||
using System.CommandLine.Builder; | ||
using System.CommandLine.Invocation; | ||
using System.CommandLine.Parsing; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
using Xunit; | ||
|
||
namespace System.CommandLine.Tests | ||
{ | ||
public class EnvironmentVariableDirectiveTests | ||
{ | ||
private static readonly Random randomizer = new Random(Seed: 456476756); | ||
private readonly string test_variable = $"TEST_ENVIRONMENT_VARIABLE{randomizer.Next()}"; | ||
|
||
[Fact] | ||
public async Task Sets_environment_variable_to_value() | ||
{ | ||
bool asserted = false; | ||
string variable = test_variable; | ||
const string value = "This is a test"; | ||
var rootCommand = new RootCommand | ||
{ | ||
Handler = CommandHandler.Create(() => | ||
{ | ||
asserted = true; | ||
Environment.GetEnvironmentVariable(variable).Should().Be(value); | ||
}) | ||
}; | ||
|
||
var parser = new CommandLineBuilder(rootCommand) | ||
.UseEnvironmentVariableDirective() | ||
.Build(); | ||
|
||
await parser.InvokeAsync(new[] { $"[env:{variable}={value}]" }); | ||
|
||
asserted.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public async Task Trims_environment_variable_name() | ||
{ | ||
bool asserted = false; | ||
string variable = test_variable; | ||
const string value = "This is a test"; | ||
var rootCommand = new RootCommand | ||
{ | ||
Handler = CommandHandler.Create(() => | ||
{ | ||
asserted = true; | ||
Environment.GetEnvironmentVariable(variable).Should().Be(value); | ||
}) | ||
}; | ||
|
||
var parser = new CommandLineBuilder(rootCommand) | ||
.UseEnvironmentVariableDirective() | ||
.Build(); | ||
|
||
await parser.InvokeAsync(new[] { $"[env: {variable} ={value}]" }); | ||
|
||
asserted.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public async Task Trims_environment_variable_value() | ||
{ | ||
bool asserted = false; | ||
string variable = test_variable; | ||
const string value = "This is a test"; | ||
var rootCommand = new RootCommand | ||
{ | ||
Handler = CommandHandler.Create(() => | ||
{ | ||
asserted = true; | ||
Environment.GetEnvironmentVariable(variable).Should().Be(value); | ||
}) | ||
}; | ||
|
||
var parser = new CommandLineBuilder(rootCommand) | ||
.UseEnvironmentVariableDirective() | ||
.Build(); | ||
|
||
await parser.InvokeAsync(new[] { $"[env:{variable}= {value} ]" }); | ||
|
||
asserted.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public async Task Sets_environment_variable_value_containing_equals_sign() | ||
{ | ||
bool asserted = false; | ||
string variable = test_variable; | ||
const string value = "This is = a test containing equals"; | ||
var rootCommand = new RootCommand | ||
{ | ||
Handler = CommandHandler.Create(() => | ||
{ | ||
asserted = true; | ||
Environment.GetEnvironmentVariable(variable).Should().Be(value); | ||
}) | ||
}; | ||
|
||
var parser = new CommandLineBuilder(rootCommand) | ||
.UseEnvironmentVariableDirective() | ||
.Build(); | ||
|
||
await parser.InvokeAsync(new[] { $"[env:{variable}={value}]" }); | ||
|
||
asserted.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public async Task Ignores_environment_directive_without_equals_sign() | ||
{ | ||
bool asserted = false; | ||
string variable = test_variable; | ||
var rootCommand = new RootCommand | ||
{ | ||
Handler = CommandHandler.Create(() => | ||
{ | ||
asserted = true; | ||
Environment.GetEnvironmentVariable(variable).Should().BeNull(); | ||
}) | ||
}; | ||
|
||
var parser = new CommandLineBuilder(rootCommand) | ||
.UseEnvironmentVariableDirective() | ||
.Build(); | ||
|
||
await parser.InvokeAsync(new[] { $"[env:{variable}]" }); | ||
|
||
asserted.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public static async Task Ignores_environment_directive_with_empty_variable_name() | ||
{ | ||
bool asserted = false; | ||
string value = $"This is a test, random: {randomizer.Next()}"; | ||
var rootCommand = new RootCommand | ||
{ | ||
Handler = CommandHandler.Create(() => | ||
{ | ||
asserted = true; | ||
var env = Environment.GetEnvironmentVariables(); | ||
env.Values.Cast<string>().Should().NotContain(value); | ||
}) | ||
}; | ||
|
||
var parser = new CommandLineBuilder(rootCommand) | ||
.UseEnvironmentVariableDirective() | ||
.Build(); | ||
|
||
await parser.InvokeAsync(new[] { $"[env:={value}]" }); | ||
|
||
asserted.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public static async Task Ignores_environment_directive_with_whitespace_variable_name() | ||
{ | ||
bool asserted = false; | ||
string value = $"This is a test, random: {randomizer.Next()}"; | ||
var rootCommand = new RootCommand | ||
{ | ||
Handler = CommandHandler.Create(() => | ||
{ | ||
asserted = true; | ||
var env = Environment.GetEnvironmentVariables(); | ||
env.Values.Cast<string>().Should().NotContain(value); | ||
}) | ||
}; | ||
|
||
var parser = new CommandLineBuilder(rootCommand) | ||
.UseEnvironmentVariableDirective() | ||
.Build(); | ||
|
||
await parser.InvokeAsync(new[] { $"[env: ={value}]" }); | ||
|
||
asserted.Should().BeTrue(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.