Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zhili1208 committed Apr 5, 2016
1 parent 0bbe461 commit 8bab7a6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
14 changes: 14 additions & 0 deletions src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
Expand Up @@ -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()
{
Expand Down

0 comments on commit 8bab7a6

Please sign in to comment.