2018-05-11 11:38:51 -04:00
|
|
|
|
using System;
|
2017-08-25 16:28:43 -06:00
|
|
|
|
using Cake.Core;
|
|
|
|
|
using Moq;
|
2018-05-11 11:38:51 -04:00
|
|
|
|
using Xunit;
|
2017-08-25 16:28:43 -06:00
|
|
|
|
|
2018-05-11 11:38:51 -04:00
|
|
|
|
namespace Cake.ArgumentHelpers.Tests
|
|
|
|
|
{
|
|
|
|
|
public class ArgumentOrEnvironmentVariableAlias_StringTests : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private Mock<ICakeContext> cakeContextMock;
|
|
|
|
|
private Mock<ICakeArguments> cakeArgumentsMock;
|
|
|
|
|
private Mock<ICakeEnvironment> cakeEnvironmentMock;
|
2017-08-25 16:28:43 -06:00
|
|
|
|
|
2018-05-11 11:38:51 -04:00
|
|
|
|
public ArgumentOrEnvironmentVariableAlias_StringTests()
|
|
|
|
|
{
|
2017-08-25 16:28:43 -06:00
|
|
|
|
cakeContextMock = new Mock<ICakeContext>();
|
|
|
|
|
cakeArgumentsMock = new Mock<ICakeArguments>();
|
|
|
|
|
cakeEnvironmentMock = new Mock<ICakeEnvironment>();
|
|
|
|
|
cakeContextMock.Setup(cakeContext => cakeContext.Arguments).Returns(cakeArgumentsMock.Object);
|
|
|
|
|
cakeContextMock.Setup(cakeContext => cakeContext.Environment).Returns(cakeEnvironmentMock.Object);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 11:38:51 -04:00
|
|
|
|
private void SetupVariables(string key, string environmentPrefix, string argumentValue, string environmentValue)
|
|
|
|
|
{
|
2017-08-25 16:28:43 -06:00
|
|
|
|
bool hasArgument = argumentValue != null;
|
|
|
|
|
cakeArgumentsMock.Setup(x => x.HasArgument(key)).Returns(hasArgument);
|
2018-05-11 11:38:51 -04:00
|
|
|
|
if (hasArgument)
|
|
|
|
|
{
|
2017-08-25 16:28:43 -06:00
|
|
|
|
cakeArgumentsMock.Setup(x => x.GetArgument(key)).Returns(argumentValue.ToString());
|
|
|
|
|
}
|
|
|
|
|
bool hasEnvironmentVariable = environmentValue != null;
|
|
|
|
|
if (hasEnvironmentVariable)
|
|
|
|
|
{
|
|
|
|
|
cakeEnvironmentMock.Setup(x => x.GetEnvironmentVariable(environmentPrefix + key)).Returns(environmentValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 11:38:51 -04:00
|
|
|
|
[Fact]
|
|
|
|
|
public void SomeArgumentAndNullEnvironment_ReturnsSome()
|
|
|
|
|
{
|
2017-08-25 16:28:43 -06:00
|
|
|
|
var testKey = "someVariable";
|
|
|
|
|
var testKeyEnvironmentPrefix = "somePrefix_";
|
|
|
|
|
string testArgumentValue = "Some";
|
|
|
|
|
string testEnvironmentValue = null;
|
|
|
|
|
|
|
|
|
|
SetupVariables(testKey, testKeyEnvironmentPrefix, testArgumentValue, testEnvironmentValue);
|
|
|
|
|
|
|
|
|
|
var expected = testArgumentValue;
|
|
|
|
|
var actual = cakeContextMock.Object.ArgumentOrEnvironmentVariable(testKey, testKeyEnvironmentPrefix, (string)null);
|
|
|
|
|
|
2018-05-11 11:38:51 -04:00
|
|
|
|
Assert.Equal(expected, actual);
|
2017-08-25 16:28:43 -06:00
|
|
|
|
}
|
2018-05-11 11:38:51 -04:00
|
|
|
|
[Fact]
|
|
|
|
|
public void NullArgumentAndNullEnvironmentAndNullDefault_ReturnsNull()
|
|
|
|
|
{
|
2017-08-25 16:28:43 -06:00
|
|
|
|
var testKey = "someVariable";
|
|
|
|
|
var testKeyEnvironmentPrefix = "somePrefix_";
|
|
|
|
|
string testArgumentValue = null;
|
|
|
|
|
string testEnvironmentValue = null;
|
|
|
|
|
|
|
|
|
|
SetupVariables(testKey, testKeyEnvironmentPrefix, testArgumentValue, testEnvironmentValue);
|
|
|
|
|
|
|
|
|
|
var expected = (string)null;
|
|
|
|
|
var actual = cakeContextMock.Object.ArgumentOrEnvironmentVariable(testKey, testKeyEnvironmentPrefix, (string)null);
|
|
|
|
|
|
2018-05-11 11:38:51 -04:00
|
|
|
|
Assert.Equal(expected, actual);
|
2017-08-25 16:28:43 -06:00
|
|
|
|
}
|
2018-05-11 11:38:51 -04:00
|
|
|
|
[Fact]
|
|
|
|
|
public void NullArgumentAndSomeEnvironment_ReturnsSome()
|
|
|
|
|
{
|
2017-08-25 16:28:43 -06:00
|
|
|
|
var testKey = "someVariable";
|
|
|
|
|
var testKeyEnvironmentPrefix = "somePrefix_";
|
|
|
|
|
string testArgumentValue = null;
|
|
|
|
|
string testEnvironmentValue = "Some";
|
|
|
|
|
|
|
|
|
|
SetupVariables(testKey, testKeyEnvironmentPrefix, testArgumentValue, testEnvironmentValue);
|
|
|
|
|
|
|
|
|
|
var expected = testEnvironmentValue;
|
|
|
|
|
var actual = cakeContextMock.Object.ArgumentOrEnvironmentVariable(testKey, testKeyEnvironmentPrefix, (string)null);
|
|
|
|
|
|
2018-05-11 11:38:51 -04:00
|
|
|
|
Assert.Equal(expected, actual);
|
2017-08-25 16:28:43 -06:00
|
|
|
|
}
|
2018-05-11 11:38:51 -04:00
|
|
|
|
[Fact]
|
|
|
|
|
public void NullArgumentAndSomeEnvironmentWithoutPrefix_ReturnsSome()
|
|
|
|
|
{
|
2017-08-25 16:28:43 -06:00
|
|
|
|
var testKey = "someVariable";
|
|
|
|
|
var testKeyEnvironmentPrefix = (string)null;
|
|
|
|
|
string testArgumentValue = null;
|
|
|
|
|
string testEnvironmentValue = "Some";
|
|
|
|
|
|
|
|
|
|
SetupVariables(testKey, testKeyEnvironmentPrefix, testArgumentValue, testEnvironmentValue);
|
|
|
|
|
|
|
|
|
|
var expected = testEnvironmentValue;
|
|
|
|
|
var actual = cakeContextMock.Object.ArgumentOrEnvironmentVariable(testKey, testKeyEnvironmentPrefix, (string)null);
|
|
|
|
|
|
2018-05-11 11:38:51 -04:00
|
|
|
|
Assert.Equal(expected, actual);
|
2017-08-25 16:28:43 -06:00
|
|
|
|
}
|
2018-05-11 11:38:51 -04:00
|
|
|
|
[Fact]
|
|
|
|
|
public void SomeArgumentAndOtherEnvironment_ReturnsSome()
|
|
|
|
|
{
|
2017-08-25 16:28:43 -06:00
|
|
|
|
var testKey = "someVariable";
|
|
|
|
|
var testKeyEnvironmentPrefix = "somePrefix_";
|
|
|
|
|
string testArgumentValue = "Some";
|
|
|
|
|
string testEnvironmentValue = "Other";
|
|
|
|
|
|
|
|
|
|
SetupVariables(testKey, testKeyEnvironmentPrefix, testArgumentValue, testEnvironmentValue);
|
|
|
|
|
|
|
|
|
|
var expected = testArgumentValue;
|
|
|
|
|
var actual = cakeContextMock.Object.ArgumentOrEnvironmentVariable(testKey, testKeyEnvironmentPrefix, (string)null);
|
|
|
|
|
|
2018-05-11 11:38:51 -04:00
|
|
|
|
Assert.Equal(expected, actual);
|
2017-08-25 16:28:43 -06:00
|
|
|
|
}
|
2018-05-11 11:38:51 -04:00
|
|
|
|
[Fact]
|
2017-08-25 16:28:43 -06:00
|
|
|
|
public void NullArgumentAndNullEnvironment_ReturnsDefault()
|
|
|
|
|
{
|
|
|
|
|
var testKey = "someVariable";
|
|
|
|
|
var testKeyEnvironmentPrefix = "somePrefix_";
|
|
|
|
|
var defaultValue = "Default";
|
|
|
|
|
string testArgumentValue = null;
|
|
|
|
|
string testEnvironmentValue = null;
|
|
|
|
|
|
|
|
|
|
SetupVariables(testKey, testKeyEnvironmentPrefix, testArgumentValue, testEnvironmentValue);
|
|
|
|
|
|
|
|
|
|
var expected = defaultValue;
|
|
|
|
|
var actual = cakeContextMock.Object.ArgumentOrEnvironmentVariable(testKey, testKeyEnvironmentPrefix, defaultValue);
|
|
|
|
|
|
2018-05-11 11:38:51 -04:00
|
|
|
|
Assert.Equal(expected, actual);
|
2017-08-25 16:28:43 -06:00
|
|
|
|
}
|
2018-05-11 11:38:51 -04:00
|
|
|
|
[Fact]
|
2017-08-25 16:28:43 -06:00
|
|
|
|
public void NullArgumentAndNullEnvironmentWithoutDefault_ReturnsNull()
|
|
|
|
|
{
|
|
|
|
|
var testKey = "someVariable";
|
|
|
|
|
var testKeyEnvironmentPrefix = "somePrefix_";
|
|
|
|
|
string testArgumentValue = null;
|
|
|
|
|
string testEnvironmentValue = null;
|
|
|
|
|
|
|
|
|
|
SetupVariables(testKey, testKeyEnvironmentPrefix, testArgumentValue, testEnvironmentValue);
|
|
|
|
|
|
|
|
|
|
var expected = (string)null;
|
|
|
|
|
var actual = cakeContextMock.Object.ArgumentOrEnvironmentVariable(testKey, testKeyEnvironmentPrefix);
|
|
|
|
|
|
2018-05-11 11:38:51 -04:00
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2017-08-25 16:28:43 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|