Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect empty user-wide NuGet.Config file, no matter if there is a track file or not #3907

Merged
merged 10 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ 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
1 change: 0 additions & 1 deletion src/NuGet.Core/NuGet.Configuration/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ static readonly NuGet.Configuration.ConfigurationConstants.UserKey -> string
static readonly NuGet.Configuration.ConfigurationConstants.UsernameToken -> string
static readonly NuGet.Configuration.ConfigurationConstants.ValidAuthenticationTypesToken -> string
static readonly NuGet.Configuration.ConfigurationConstants.ValueAttribute -> string
static readonly NuGet.Configuration.NuGetConstants.AddV3TrackFile -> string
heng-liu marked this conversation as resolved.
Show resolved Hide resolved
static readonly NuGet.Configuration.NuGetConstants.DefaultConfigContent -> string
static readonly NuGet.Configuration.NuGetConstants.DefaultGalleryServerUrl -> string
static readonly NuGet.Configuration.NuGetConstants.DefaultSymbolServerUrl -> string
Expand Down
15 changes: 1 addition & 14 deletions src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,23 +545,10 @@ internal static IEnumerable<SettingsFile> LoadUserSpecificSettings(
yield break;
}

// If the default user config NuGet.Config does not exist, we need to create it.
var defaultSettingsFilePath = Path.Combine(userSettingsDir, DefaultSettingsFileName);

// ReadSettings will try to create the default config file if it doesn't exist
SettingsFile userSpecificSettings = ReadSettings(rootDirectory, defaultSettingsFilePath, settingsLoadingContext: settingsLoadingContext);
if (File.Exists(defaultSettingsFilePath) && userSpecificSettings.IsEmpty())
{
var trackFilePath = Path.Combine(Path.GetDirectoryName(defaultSettingsFilePath), NuGetConstants.AddV3TrackFile);

if (!File.Exists(trackFilePath))
{
File.Create(trackFilePath).Dispose();

var defaultSource = new SourceItem(NuGetConstants.FeedName, NuGetConstants.V3FeedUrl, protocolVersion: "3");
userSpecificSettings.AddOrUpdate(ConfigurationConstants.PackageSources, defaultSource);
userSpecificSettings.SaveToDisk();
}
}

yield return userSpecificSettings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1814,7 +1814,7 @@ public void DisabledMachineWideSourceByDefaultWithNull(bool useStaticMethod)
List<PackageSource> sources = LoadPackageSources(useStaticMethod, settings);

// Assert
Assert.Equal(1, sources.Count);
Assert.Equal(0, sources.Count);
}
}

Expand Down Expand Up @@ -2253,7 +2253,7 @@ private void AssertCredentials(PackageSourceCredential actual, string source, st

private static void CreateSettingsFileInTestingGlobalDirectory(TestDirectory directory)
{
var settingsFile = new FileInfo(Path.Combine(directory.Path, "TestingGlobalPath", "NuGet.config"));
var settingsFile = new FileInfo(Path.Combine(directory.Path, "TestingGlobalPath", "NuGet.Config"));

settingsFile.Directory.Create();

Expand Down
52 changes: 28 additions & 24 deletions test/NuGet.Core.Tests/NuGet.Configuration.Test/SettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,7 @@ public void LoadDefaultSettings_WithUserSpecifiedConfigFile_IgnoresOtherSettings
}

[Fact]
public void LoadSettings_AddsV3ToEmptyConfigFile_OnlyFirstTime()
public void LoadSettings_EmptyUserWideConfigFile_DoNotAddNuGetOrg()
{
using (var mockBaseDirectory = TestDirectory.Create())
{
Expand All @@ -2042,37 +2042,41 @@ public void LoadSettings_AddsV3ToEmptyConfigFile_OnlyFirstTime()
useTestingGlobalPath: true);

// Assert
var text = SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, "TestingGlobalPath", "NuGet.Config")));
var result = SettingsTestUtils.RemoveWhitespace(@"<?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>");
var actual = SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, "TestingGlobalPath", "NuGet.Config")));
var expected = SettingsTestUtils.RemoveWhitespace(config);

text.Should().Be(result);
actual.Should().Be(expected);
}
}

var settingsFile = new SettingsFile(Path.Combine(mockBaseDirectory, "TestingGlobalPath"));
[Fact]
public void LoadSettings_NonExistingUserWideConfigFile_CreateUserWideConfigFileWithNuGetOrg()
{
using (var mockBaseDirectory = TestDirectory.Create())
{
// Arrange
var nugetConfigPath = Path.Combine(mockBaseDirectory, "TestingGlobalPath", "NuGet.Config");
File.Exists(nugetConfigPath).Should().BeFalse();

// Act
var section = settingsFile.GetSection("packageSources");
section.Should().NotBeNull();
settingsFile.Remove("packageSources", section.Items.First());
settingsFile.SaveToDisk();

settings = Settings.LoadSettings(
root: mockBaseDirectory,
configFileName: null,
machineWideSettings: null,
loadUserWideSettings: true,
useTestingGlobalPath: true);
var settings = Settings.LoadSettings(
root: mockBaseDirectory,
configFileName: null,
machineWideSettings: null,
loadUserWideSettings: true,
useTestingGlobalPath: true);

// Assert
text = SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, "TestingGlobalPath", "NuGet.Config")));
result = SettingsTestUtils.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?>
File.Exists(nugetConfigPath).Should().BeTrue();
var actual = SettingsTestUtils.RemoveWhitespace(File.ReadAllText(nugetConfigPath));
var expected = SettingsTestUtils.RemoveWhitespace(@"<?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>");

text.Should().Be(result);
actual.Should().Be(expected);
}
}

Expand Down