From 2c7f4d3ca4291405a8c37bcf6d66c058dcf1c7b3 Mon Sep 17 00:00:00 2001 From: Patrik Svensson Date: Thu, 21 Aug 2014 20:53:57 +0200 Subject: [PATCH] Empty environment variables are allowed. --- .../Unit/EnvironmentExtensionsTests.cs | 174 ++++++++---------- .../Unit/Tools/MSBuild/MSBuildRunnerTests.cs | 3 +- src/Cake.Common/EnviromentExtensions.cs | 10 +- 3 files changed, 81 insertions(+), 106 deletions(-) diff --git a/src/Cake.Common.Tests/Unit/EnvironmentExtensionsTests.cs b/src/Cake.Common.Tests/Unit/EnvironmentExtensionsTests.cs index 9b9d0cc9ec..7cdabb5d87 100644 --- a/src/Cake.Common.Tests/Unit/EnvironmentExtensionsTests.cs +++ b/src/Cake.Common.Tests/Unit/EnvironmentExtensionsTests.cs @@ -6,13 +6,13 @@ namespace Cake.Common.Tests.Unit { public sealed class EnvironmentExtensionsTests { - public sealed class EnvMethods - { - private const string TestVariableName = "Test"; - private const string TestVariableValue = "Value"; + private const string TestVariableName = "Test"; + private const string TestVariableValue = "Value"; + public sealed class TheHasEnvironmentVariableMethod + { [Fact] - public void Does_Call_Context_Check1186147061() + public void Should_Return_True_If_Variable_Exist() { // Given var environment = Substitute.For(); @@ -23,105 +23,85 @@ public void Does_Call_Context_Check1186147061() context.Environment.Returns(environment); // When - EnvironmentExtensions.HasEnvironmentVariable(context, TestVariableName); + var result = EnvironmentExtensions.HasEnvironmentVariable(context, TestVariableName); + + // Then + Assert.True(result); + } + + [Fact] + public void Should_Return_True_If_Value_Is_Empty() + { + // Given + var environment = Substitute.For(); + environment.GetEnvironmentVariable(TestVariableName) + .Returns(string.Empty); + + var context = Substitute.For(); + context.Environment.Returns(environment); + + // When + var result = EnvironmentExtensions.HasEnvironmentVariable(context, TestVariableName); + + // Then + Assert.True(result); + } + + [Fact] + public void Should_Return_False_If_Variable_Was_Null() + { + // Given + var environment = Substitute.For(); + environment.GetEnvironmentVariable(TestVariableName) + .Returns((string)null); + + var context = Substitute.For(); + context.Environment.Returns(environment); + + // When + var result = EnvironmentExtensions.HasEnvironmentVariable(context, TestVariableName); // Then - environment.Received().GetEnvironmentVariable(TestVariableName); + Assert.False(result); } + } - public sealed class HasEnv + public sealed class TheGetEnvironmentVariableVariable + { + [Fact] + public void Should_Return_Value() { - [Fact] - public void Does_Check_For_Value() - { - // Given - var environment = Substitute.For(); - environment.GetEnvironmentVariable(TestVariableName) - .Returns(TestVariableValue); - - var context = Substitute.For(); - context.Environment.Returns(environment); - - // When - var result = EnvironmentExtensions.HasEnvironmentVariable(context, TestVariableName); - - // Then - Assert.Equal(result, true); - } - - [Fact] - public void Returns_False_On_Empty_Variable() - { - // Given - var environment = Substitute.For(); - environment.GetEnvironmentVariable(TestVariableName) - .Returns(""); - - var context = Substitute.For(); - context.Environment.Returns(environment); - - // When - var result = EnvironmentExtensions.HasEnvironmentVariable(context, TestVariableName); - - // Then - Assert.Equal(result, false); - } - - [Fact] - public void Returns_False_On_Null_Variable() - { - // Given - var environment = Substitute.For(); - environment.GetEnvironmentVariable(TestVariableName) - .Returns((string)null); - - var context = Substitute.For(); - context.Environment.Returns(environment); - - // When - var result = EnvironmentExtensions.HasEnvironmentVariable(context, TestVariableName); - - // Then - Assert.Equal(result, false); - } + // Given + var environment = Substitute.For(); + environment.GetEnvironmentVariable(TestVariableName) + .Returns(TestVariableValue); + + var context = Substitute.For(); + context.Environment.Returns(environment); + + // When + var result = EnvironmentExtensions.EnvironmentVariable(context, TestVariableName); + + // Then + Assert.Equal(result, TestVariableValue); } - public sealed class GetEnv + + [Fact] + public void Should_Return_Null_If_Value_Do_Not_Exist() { - [Fact] - public void Retrieves_Value() - { - // Given - var environment = Substitute.For(); - environment.GetEnvironmentVariable(TestVariableName) - .Returns(TestVariableValue); - - var context = Substitute.For(); - context.Environment.Returns(environment); - - // When - var result = EnvironmentExtensions.EnvironmentVariable(context, TestVariableName); - - // Then - Assert.Equal(result, TestVariableValue); - } - - [Fact] - public void Returns_Empty_Value() - { - // Given - var environment = Substitute.For(); - environment.GetEnvironmentVariable(TestVariableName) - .Returns(""); - - var context = Substitute.For(); - context.Environment.Returns(environment); - - // When - var result = EnvironmentExtensions.EnvironmentVariable(context, TestVariableName); - - // Then - Assert.Equal(result, ""); - } + // Given + var environment = Substitute.For(); + environment.GetEnvironmentVariable(TestVariableName) + .Returns((string)null); + + var context = Substitute.For(); + context.Environment.Returns(environment); + + // When + var result = EnvironmentExtensions.EnvironmentVariable(context, TestVariableName); + + // Then + Assert.Null(result); } } } diff --git a/src/Cake.Common.Tests/Unit/Tools/MSBuild/MSBuildRunnerTests.cs b/src/Cake.Common.Tests/Unit/Tools/MSBuild/MSBuildRunnerTests.cs index eb6e9cc26f..85e50cf3e2 100644 --- a/src/Cake.Common.Tests/Unit/Tools/MSBuild/MSBuildRunnerTests.cs +++ b/src/Cake.Common.Tests/Unit/Tools/MSBuild/MSBuildRunnerTests.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using Cake.Common.Tests.Fixtures; using Cake.Common.Tools.MSBuild; using Cake.Core; diff --git a/src/Cake.Common/EnviromentExtensions.cs b/src/Cake.Common/EnviromentExtensions.cs index 26e212c0b4..9b8eb246f0 100644 --- a/src/Cake.Common/EnviromentExtensions.cs +++ b/src/Cake.Common/EnviromentExtensions.cs @@ -15,7 +15,7 @@ public static class EnvironmentExtensions /// /// The context. /// The environment variable. - /// + /// The environment variable or null if variable did not exist. [CakeMethodAlias] public static string EnvironmentVariable(this ICakeContext context, string variable) { @@ -23,12 +23,10 @@ public static string EnvironmentVariable(this ICakeContext context, string varia { throw new ArgumentNullException("context"); } - if (variable == null) { throw new ArgumentNullException("variable"); } - return context.Environment.GetEnvironmentVariable(variable); } @@ -37,7 +35,7 @@ public static string EnvironmentVariable(this ICakeContext context, string varia /// /// The context. /// The environment variable. - /// + /// true if environment variable exist, else false. [CakeMethodAlias] public static bool HasEnvironmentVariable(this ICakeContext context, string variable) { @@ -45,13 +43,11 @@ public static bool HasEnvironmentVariable(this ICakeContext context, string vari { throw new ArgumentNullException("context"); } - if (variable == null) { throw new ArgumentNullException("variable"); } - - return !string.IsNullOrWhiteSpace(context.Environment.GetEnvironmentVariable(variable)); + return context.Environment.GetEnvironmentVariable(variable) != null; } } }