Skip to content

Commit

Permalink
Empty environment variables are allowed.
Browse files Browse the repository at this point in the history
  • Loading branch information
patriksvensson committed Aug 21, 2014
1 parent 7c6378a commit 2c7f4d3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 106 deletions.
174 changes: 77 additions & 97 deletions src/Cake.Common.Tests/Unit/EnvironmentExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICakeEnvironment>();
Expand All @@ -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<ICakeEnvironment>();
environment.GetEnvironmentVariable(TestVariableName)
.Returns(string.Empty);

var context = Substitute.For<ICakeContext>();
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<ICakeEnvironment>();
environment.GetEnvironmentVariable(TestVariableName)
.Returns((string)null);

var context = Substitute.For<ICakeContext>();
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<ICakeEnvironment>();
environment.GetEnvironmentVariable(TestVariableName)
.Returns(TestVariableValue);

var context = Substitute.For<ICakeContext>();
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<ICakeEnvironment>();
environment.GetEnvironmentVariable(TestVariableName)
.Returns("");

var context = Substitute.For<ICakeContext>();
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<ICakeEnvironment>();
environment.GetEnvironmentVariable(TestVariableName)
.Returns((string)null);

var context = Substitute.For<ICakeContext>();
context.Environment.Returns(environment);

// When
var result = EnvironmentExtensions.HasEnvironmentVariable(context, TestVariableName);

// Then
Assert.Equal(result, false);
}
// Given
var environment = Substitute.For<ICakeEnvironment>();
environment.GetEnvironmentVariable(TestVariableName)
.Returns(TestVariableValue);

var context = Substitute.For<ICakeContext>();
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<ICakeEnvironment>();
environment.GetEnvironmentVariable(TestVariableName)
.Returns(TestVariableValue);

var context = Substitute.For<ICakeContext>();
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<ICakeEnvironment>();
environment.GetEnvironmentVariable(TestVariableName)
.Returns("");

var context = Substitute.For<ICakeContext>();
context.Environment.Returns(environment);

// When
var result = EnvironmentExtensions.EnvironmentVariable(context, TestVariableName);

// Then
Assert.Equal(result, "");
}
// Given
var environment = Substitute.For<ICakeEnvironment>();
environment.GetEnvironmentVariable(TestVariableName)
.Returns((string)null);

var context = Substitute.For<ICakeContext>();
context.Environment.Returns(environment);

// When
var result = EnvironmentExtensions.EnvironmentVariable(context, TestVariableName);

// Then
Assert.Null(result);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
10 changes: 3 additions & 7 deletions src/Cake.Common/EnviromentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@ public static class EnvironmentExtensions
/// </summary>
/// <param name="context">The context.</param>
/// <param name="variable">The environment variable.</param>
/// <returns></returns>
/// <returns>The environment variable or <c>null</c> if variable did not exist.</returns>
[CakeMethodAlias]
public static string EnvironmentVariable(this ICakeContext context, string variable)
{
if (context == null)
{
throw new ArgumentNullException("context");
}

if (variable == null)
{
throw new ArgumentNullException("variable");
}

return context.Environment.GetEnvironmentVariable(variable);
}

Expand All @@ -37,21 +35,19 @@ public static string EnvironmentVariable(this ICakeContext context, string varia
/// </summary>
/// <param name="context">The context.</param>
/// <param name="variable">The environment variable.</param>
/// <returns></returns>
/// <returns><c>true</c> if environment variable exist, else <c>false</c>.</returns>
[CakeMethodAlias]
public static bool HasEnvironmentVariable(this ICakeContext context, string variable)
{
if (context == null)
{
throw new ArgumentNullException("context");
}

if (variable == null)
{
throw new ArgumentNullException("variable");
}

return !string.IsNullOrWhiteSpace(context.Environment.GetEnvironmentVariable(variable));
return context.Environment.GetEnvironmentVariable(variable) != null;
}
}
}

0 comments on commit 2c7f4d3

Please sign in to comment.