Skip to content

Commit

Permalink
Settings.GetValue supports env expansion
Browse files Browse the repository at this point in the history
* Added tests for `Settings.GetValue()` and
`Settings.GetSettingValues()`
* Added simple implementation for `Settings.GetValue()`
* `Settings.GetSettingValues()` still needs an implementation, or an
implementation that overlaps both.
  • Loading branch information
TravisTheTechie authored and deepakaravindr committed Feb 4, 2016
1 parent cf8f76c commit 603907b
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs
Expand Up @@ -372,7 +372,17 @@ public string GetValue(string section, string key, bool isPath = false)
curr = curr._next;
}

return ret;
return ApplyEnvironmentTransform(ret);
}

private string ApplyEnvironmentTransform(string configValue)
{
if (string.IsNullOrEmpty(configValue))
{
return configValue;
}

return Environment.ExpandEnvironmentVariables(configValue);
}

public IList<SettingValue> GetSettingValues(string section, bool isPath = false)
Expand All @@ -390,6 +400,8 @@ public IList<SettingValue> GetSettingValues(string section, bool isPath = false)
curr = curr._next;
}

settingValues.ForEach(settingValue => settingValue.Value = ApplyEnvironmentTransform(settingValue.Value));

return settingValues.AsReadOnly();
}

Expand Down
Expand Up @@ -201,6 +201,50 @@ public void TestVerbosityQuiet_ShowsErrorsAndWarnings()
Assert.True(result.Item3.Contains("Key 'nonExistentKey' not found."));
}

[Fact]
public void ConfigCommand_EvaluatesEnvironmentalVariables()
{
using (var testFolder = TestFileSystemUtility.CreateRandomTestFolder())
{
// Arrange
var nugetexe = Util.GetNuGetExePath();
var configFile = Path.Combine(testFolder, "NuGet.config");
var envValue = Guid.NewGuid().ToString();
var expectedValue = envValue + @"\two" + Environment.NewLine;

Util.CreateFile(Path.GetDirectoryName(configFile),
Path.GetFileName(configFile),
@"
<configuration>
<config>
<add key='repositoryPath' value='%RP_ENV_VAR%\two' />
</config>
</configuration>
");

string[] args = new string[] {
"config",
"repositoryPath"
};

// Act
Environment.SetEnvironmentVariable("RP_ENV_VAR", envValue);
var result = CommandRunner.Run(
nugetexe,
testFolder,
string.Join(" ", args),
waitForExit: true);

var output = result.Item2;
Environment.SetEnvironmentVariable("RP_ENV_VAR", string.Empty);


// Assert
Assert.Equal(0, result.Item1);
Assert.Equal(expectedValue, output);
}
}

private void AssertEqualCollections(IList<Configuration.SettingValue> actual, string[] expected)
{
Assert.Equal(actual.Count, expected.Length / 2);
Expand Down
@@ -0,0 +1,66 @@
using NuGet.Test.Utility;
using System;
using System.IO;
using System.Linq;
using Xunit;

namespace NuGet.Configuration.Test
{
public class EnvironmentSupportTests
{
private readonly string DefaultNuGetConfigurationWithEnvironmentVariable = @"
<configuration>
<config>
<add key='repositoryPath' value='%RP_ENV_VAR%\two' />
</config>
</configuration>";

[Fact]
public void GetValueEvaluatesEnvironmentVariable()
{
//Arrange
var expectedRepositoryPath = @"ONE\two";

using (var nugetConfigFileFolder = TestFileSystemUtility.CreateRandomTestFolder())
{
var nugetConfigFile = "NuGet.config";
var nugetConfigFilePath = Path.Combine(nugetConfigFileFolder, nugetConfigFile);

File.WriteAllText(nugetConfigFilePath, DefaultNuGetConfigurationWithEnvironmentVariable);

Environment.SetEnvironmentVariable("RP_ENV_VAR", "ONE");

//Act
ISettings settings = new Settings(nugetConfigFileFolder, nugetConfigFile);

//Assert
Assert.Equal(settings.GetValue("config", "repositoryPath"), expectedRepositoryPath);
}
}

[Fact]
public void GetSettingValuesEvaluatesEnvironmentVariable()
{
//Arrange
var expectedRepositoryPath = @"ONE\two";

using (var nugetConfigFileFolder = TestFileSystemUtility.CreateRandomTestFolder())
{
var nugetConfigFile = "NuGet.config";
var nugetConfigFilePath = Path.Combine(nugetConfigFileFolder, nugetConfigFile);

File.WriteAllText(nugetConfigFilePath, DefaultNuGetConfigurationWithEnvironmentVariable);

Environment.SetEnvironmentVariable("RP_ENV_VAR", "ONE");

//Act
ISettings settings = new Settings(nugetConfigFileFolder, nugetConfigFile);

//Assert
var settingsForConfig = settings.GetSettingValues("config");
Assert.Single(settingsForConfig);
Assert.Equal(settingsForConfig.Single().Value, expectedRepositoryPath);
}
}
}
}

0 comments on commit 603907b

Please sign in to comment.