Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files Browse the repository at this point in the history
zhili1208 committed Apr 5, 2016

Verified

This commit was signed with the committer’s verified signature.
ViBiOh Vincent Boutour
1 parent 4fb2db0 commit ad4394d
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -23,6 +23,8 @@ public static class NuGetConstants

public static readonly string FeedName = "nuget.org";

public static readonly string AddV3TrackFile = "nugetorgadd.trk";

public static readonly string DefaultConfigContent = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
14 changes: 14 additions & 0 deletions src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs
Original file line number Diff line number Diff line change
@@ -291,6 +291,20 @@ bool useTestingGlobalPath
else
{
appDataSettings = ReadSettings(root, defaultSettingsFilePath);
bool IsEmptyConfig = !appDataSettings.GetSettingValues(ConfigurationConstants.PackageSources).Any();

if (IsEmptyConfig)
{
var trackFilePath = Path.Combine(Path.GetDirectoryName(defaultSettingsFilePath), NuGetConstants.AddV3TrackFile);

if (!File.Exists(trackFilePath))
{
File.Create(trackFilePath).Dispose();
var defaultPackageSource = new SettingValue(NuGetConstants.FeedName, NuGetConstants.V3FeedUrl, isMachineWide: false);
defaultPackageSource.AdditionalData.Add(ConfigurationConstants.ProtocolVersionAttribute, "3");
appDataSettings.UpdateSections(ConfigurationConstants.PackageSources, new List<SettingValue> { defaultPackageSource });
}
}
}
}
else
40 changes: 40 additions & 0 deletions test/NuGet.Core.Tests/NuGet.Configuration.Test/SettingsTests.cs
Original file line number Diff line number Diff line change
@@ -2236,6 +2236,46 @@ public void CreateNewConfigFileIfNoConfig()
}
}

[Fact]
public void AddV3ToEmptyConfigFile()
{
using (var mockBaseDirectory = TestFileSystemUtility.CreateRandomTestFolder())
{
// Arrange
var config = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
</configuration>";

var nugetConfigPath = "NuGet.Config";
ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath,
Path.Combine(mockBaseDirectory, "TestingGlobalPath"), config);

// Act
var settings = Settings.LoadDefaultSettings(mockBaseDirectory, null, null, true, true);

// Assert
var text = File.ReadAllText(Path.Combine(mockBaseDirectory, "TestingGlobalPath", "NuGet.Config")).Replace("\r\n", "\n");
var result = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""nuget.org"" value=""https://api.nuget.org/v3/index.json"" protocolVersion=""3"" />
</packageSources>
</configuration>".Replace("\r\n", "\n");
Assert.Equal(result, text);

// Act
settings.DeleteSection("packageSources");
settings = Settings.LoadDefaultSettings(mockBaseDirectory, null, null, true, true);

// Assert
text = File.ReadAllText(Path.Combine(mockBaseDirectory, "TestingGlobalPath" , "NuGet.Config")).Replace("\r\n", "\n");
result = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
</configuration>".Replace("\r\n", "\n");
Assert.Equal(result, text);
}
}

[Fact]
public void LoadNuGetConfig_InvalidXmlThrowException()
{

0 comments on commit ad4394d

Please sign in to comment.