mirror of
				https://github.com/Tyrrrz/CliFx.git
				synced 2025-10-25 15:19:17 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			116 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			116 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System.Collections.Generic;
 | |
| using System.IO;
 | |
| using System.Threading.Tasks;
 | |
| using CliFx.Domain;
 | |
| using CliFx.Tests.Internal;
 | |
| using CliWrap;
 | |
| using CliWrap.Buffered;
 | |
| using FluentAssertions;
 | |
| using Xunit;
 | |
| 
 | |
| namespace CliFx.Tests
 | |
| {
 | |
|     public partial class EnvironmentVariablesSpecs
 | |
|     {
 | |
|         // This test uses a real application to make sure environment variables are actually read correctly
 | |
|         [Fact]
 | |
|         public async Task Option_can_use_a_specific_environment_variable_as_fallback()
 | |
|         {
 | |
|             // Arrange
 | |
|             var command = Cli.Wrap("dotnet")
 | |
|                 .WithArguments(a => a
 | |
|                     .Add(Dummy.Program.Location))
 | |
|                 .WithEnvironmentVariables(e => e
 | |
|                     .Set("ENV_TARGET", "Mars"));
 | |
| 
 | |
|             // Act
 | |
|             var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
 | |
| 
 | |
|             // Assert
 | |
|             stdOut.TrimEnd().Should().Be("Hello Mars!");
 | |
|         }
 | |
| 
 | |
|         // This test uses a real application to make sure environment variables are actually read correctly
 | |
|         [Fact]
 | |
|         public async Task Option_only_uses_environment_variable_as_fallback_if_the_value_was_not_directly_provided()
 | |
|         {
 | |
|             // Arrange
 | |
|             var command = Cli.Wrap("dotnet")
 | |
|                 .WithArguments(a => a
 | |
|                     .Add(Dummy.Program.Location)
 | |
|                     .Add("--target")
 | |
|                     .Add("Jupiter"))
 | |
|                 .WithEnvironmentVariables(e => e
 | |
|                     .Set("ENV_TARGET", "Mars"));
 | |
| 
 | |
|             // Act
 | |
|             var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
 | |
| 
 | |
|             // Assert
 | |
|             stdOut.TrimEnd().Should().Be("Hello Jupiter!");
 | |
|         }
 | |
| 
 | |
|         [Fact]
 | |
|         public void Option_of_non_scalar_type_can_take_multiple_separated_values_from_an_environment_variable()
 | |
|         {
 | |
|             // Arrange
 | |
|             var input = CommandInput.Empty;
 | |
|             var envVars = new Dictionary<string, string>
 | |
|             {
 | |
|                 ["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
 | |
|             };
 | |
| 
 | |
|             // Act
 | |
|             var instance = CommandHelper.ResolveCommand<EnvironmentVariableCollectionCommand>(input, envVars);
 | |
| 
 | |
|             // Assert
 | |
|             instance.Should().BeEquivalentTo(new EnvironmentVariableCollectionCommand
 | |
|             {
 | |
|                 Option = new[] {"foo", "bar"}
 | |
|             });
 | |
|         }
 | |
| 
 | |
|         [Fact]
 | |
|         public void Option_of_scalar_type_can_only_take_a_single_value_from_an_environment_variable_even_if_it_contains_separators()
 | |
|         {
 | |
|             // Arrange
 | |
|             var input = CommandInput.Empty;
 | |
|             var envVars = new Dictionary<string, string>
 | |
|             {
 | |
|                 ["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
 | |
|             };
 | |
| 
 | |
|             // Act
 | |
|             var instance = CommandHelper.ResolveCommand<EnvironmentVariableCommand>(input, envVars);
 | |
| 
 | |
|             // Assert
 | |
|             instance.Should().BeEquivalentTo(new EnvironmentVariableCommand
 | |
|             {
 | |
|                 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
 | |
|             });
 | |
|         }
 | |
|     }
 | |
| }
 |