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 c8cc60a commit d5702bd
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public static class NuGetConstants

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

public static readonly string AddV3TrackFile = "AddV3Track.config";

public static readonly string DefaultConfigContent = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
Expand Down
46 changes: 46 additions & 0 deletions src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ 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 (appDataSettings.GetorCreateAddV3TrackFileValue(trackFilePath))
{
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 Expand Up @@ -1192,5 +1205,38 @@ private void CheckConfigRoot()
string.Format(Resources.ShowError_ConfigRootInvalid, ConfigFilePath));
}
}

private bool GetorCreateAddV3TrackFileValue(string path)
{
bool FileNotExist = false;
if (!File.Exists(path))
{
FileNotExist = true;
}
var content = new XDocument(new XElement(ConfigurationConstants.AddV3Track, "false"));
XDocument doc = null;
ExecuteSynchronized(() => doc = XmlUtility.GetOrCreateDocument(content, path));

if (FileNotExist)
{
return true;
}
else
{
if (doc != null)
{
try
{
return Convert.ToBoolean(doc.Elements(ConfigurationConstants.AddV3Track).FirstOrDefault().Value);
}
catch (FormatException)
{
return false;
}
}

return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ public static class ConfigurationConstants

public static readonly string BeginIgnoreMarker = "NUGET: BEGIN LICENSE TEXT";
public static readonly string EndIgnoreMarker = "NUGET: END LICENSE TEXT";

public static string AddV3Track = "AddV3Track";
}
}
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 @@ -2235,6 +2235,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 d5702bd

Please sign in to comment.