Skip to content

Commit

Permalink
Treat empty variables as not being set (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
digimangos authored Apr 4, 2022
1 parent 1d88468 commit 76944fd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
28 changes: 26 additions & 2 deletions src/OctoshiftCLI.Tests/ado2gh/EnvironmentVariableProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ public void GithubPersonalAccessToken_Throws_If_Github_Pat_Is_Not_Set()
.Should().Throw<OctoshiftCliException>();
}

[Fact]
public void GithubPersonalAccessToken_Throws_If_Github_Pat_Is_Empty_String()
{
// Arrange
ResetEnvs();
var altEnvironmentVariableProvider = new EnvironmentVariableProvider(_mockLogger.Object, v => string.Empty);

// Act, Assert
altEnvironmentVariableProvider.Invoking(env => env.GithubPersonalAccessToken())
.Should().Throw<OctoshiftCliException>();
}

[Fact]
public void AdoPersonalAccessToken_Should_Return_Ado_Pat()
{
Expand Down Expand Up @@ -95,10 +107,22 @@ public void AdoPersonalAccessToken_Throws_If_Ado_Pat_Is_Not_Set()
.Should().Throw<OctoshiftCliException>();
}

[Fact]
public void AdoPersonalAccessToken_Throws_If_Ado_Pat_Is_Empty_String()
{
// Arrange
ResetEnvs();
var altEnvironmentVariableProvider = new EnvironmentVariableProvider(_mockLogger.Object, x => string.Empty);

// Act, Assert
altEnvironmentVariableProvider.Invoking(env => env.AdoPersonalAccessToken())
.Should().Throw<OctoshiftCliException>();
}

private void ResetEnvs(string githubPat = null, string adoPat = null)
{
Environment.SetEnvironmentVariable("GH_PAT", githubPat);
Environment.SetEnvironmentVariable("ADO_PAT", adoPat);
Environment.SetEnvironmentVariable(GH_PAT, githubPat);
Environment.SetEnvironmentVariable(ADO_PAT, adoPat);
}
}
}
28 changes: 23 additions & 5 deletions src/ado2gh/EnvironmentVariableProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleToAttribute("OctoshiftCLI.Tests")]

namespace OctoshiftCLI.AdoToGithub;

Expand All @@ -9,19 +12,34 @@ public class EnvironmentVariableProvider

private readonly OctoLogger _logger;

public EnvironmentVariableProvider(OctoLogger logger)
private readonly Func<string, string> _getEnvironmentVariable;

public EnvironmentVariableProvider(OctoLogger logger) : this(logger, v => Environment.GetEnvironmentVariable(v))
{
}

internal EnvironmentVariableProvider(OctoLogger logger, Func<string, string> getEnvironmentVariable)
{
_logger = logger;
_getEnvironmentVariable = getEnvironmentVariable;
}

public virtual string GithubPersonalAccessToken() => GetSecret(GH_PAT);
public virtual string GithubPersonalAccessToken() =>
GetSecret(GH_PAT)
?? throw new OctoshiftCliException($"{GH_PAT} environment variable is not set.");

public virtual string AdoPersonalAccessToken() => GetSecret(ADO_PAT);
public virtual string AdoPersonalAccessToken() =>
GetSecret(ADO_PAT)
?? throw new OctoshiftCliException($"{ADO_PAT} environment variable is not set.");

private string GetSecret(string secretName)
{
var secret = Environment.GetEnvironmentVariable(secretName) ??
throw new OctoshiftCliException($"{secretName} environment variable is not set.");
var secret = _getEnvironmentVariable(secretName);

if (string.IsNullOrEmpty(secret))
{
return null;
}

_logger?.RegisterSecret(secret);

Expand Down
2 changes: 1 addition & 1 deletion src/gei/EnvironmentVariableProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private string GetSecret(string secretName)
{
var secret = Environment.GetEnvironmentVariable(secretName);

if (secret is null)
if (string.IsNullOrEmpty(secret))
{
return null;
}
Expand Down

0 comments on commit 76944fd

Please sign in to comment.