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 8 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
20 changes: 8 additions & 12 deletions src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,22 +545,18 @@ 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);

SettingsFile userSpecificSettings = ReadSettings(rootDirectory, defaultSettingsFilePath, settingsLoadingContext: settingsLoadingContext);
if (File.Exists(defaultSettingsFilePath) && userSpecificSettings.IsEmpty())
{
var trackFilePath = Path.Combine(Path.GetDirectoryName(defaultSettingsFilePath), NuGetConstants.AddV3TrackFile);
var defaultSettingsFilePathExistsPreviously = File.Exists(defaultSettingsFilePath);

if (!File.Exists(trackFilePath))
{
File.Create(trackFilePath).Dispose();
// If the default user config NuGet.Config does not exist, we need to create an empty one.
heng-liu marked this conversation as resolved.
Show resolved Hide resolved
SettingsFile userSpecificSettings = ReadSettings(rootDirectory, defaultSettingsFilePath, settingsLoadingContext: settingsLoadingContext);

var defaultSource = new SourceItem(NuGetConstants.FeedName, NuGetConstants.V3FeedUrl, protocolVersion: "3");
userSpecificSettings.AddOrUpdate(ConfigurationConstants.PackageSources, defaultSource);
userSpecificSettings.SaveToDisk();
}
if (!defaultSettingsFilePathExistsPreviously && File.Exists(defaultSettingsFilePath) && userSpecificSettings.IsEmpty())
{
heng-liu marked this conversation as resolved.
Show resolved Hide resolved
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
83 changes: 61 additions & 22 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_EmptyUserWideConfigFileAndNoTrackerFile_DoNotAddNuGetOrg()
{
using (var mockBaseDirectory = TestDirectory.Create())
{
Expand All @@ -2042,37 +2042,76 @@ 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""?>
var actual = SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, "TestingGlobalPath", "NuGet.Config")));
var expected = SettingsTestUtils.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?>
heng-liu marked this conversation as resolved.
Show resolved Hide resolved
<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);
}
}

var settingsFile = new SettingsFile(Path.Combine(mockBaseDirectory, "TestingGlobalPath"));
[Fact]
public void LoadSettings_EmptyUserWideConfigFileAndHasTrackerFile_DoNotAddNuGetOrg()
{
using (var mockBaseDirectory = TestDirectory.Create())
{
// Arrange
var config = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
</configuration>";

var nugetConfigPath = "NuGet.Config";
SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, Path.Combine(mockBaseDirectory, "TestingGlobalPath"), config);
var trackFilePath = Path.Combine(mockBaseDirectory, "TestingGlobalPath", "nugetorgadd.trk");
File.WriteAllText(trackFilePath, "");

// 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""?>
var actual = SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, "TestingGlobalPath", "NuGet.Config")));
var expected = SettingsTestUtils.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?>
heng-liu marked this conversation as resolved.
Show resolved Hide resolved
<configuration>
</configuration>");

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

[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 settings = Settings.LoadSettings(
root: mockBaseDirectory,
configFileName: null,
machineWideSettings: null,
loadUserWideSettings: true,
useTestingGlobalPath: true);

// Assert
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>");

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

Expand Down