2018-01-24 16:09:08 -08:00
|
|
|
// Copyright (c) .NET Foundation. All rights reserved.
|
|
|
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
|
|
2025-11-20 21:13:26 +10:30
|
|
|
#nullable disable
|
|
|
|
|
|
2018-01-24 16:09:08 -08:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2022-01-26 08:11:18 -08:00
|
|
|
using System.Xml;
|
2018-01-24 16:09:08 -08:00
|
|
|
using System.Xml.Linq;
|
|
|
|
|
using NuGet.Common;
|
2022-11-23 11:01:34 +10:30
|
|
|
using NuGet.Configuration;
|
2018-01-24 16:09:08 -08:00
|
|
|
|
|
|
|
|
namespace NuGet.Test.Utility
|
|
|
|
|
{
|
|
|
|
|
public class SimpleTestSettingsContext
|
|
|
|
|
{
|
2025-06-02 08:41:14 -07:00
|
|
|
public const string DefaultPackageSourceName = "source";
|
|
|
|
|
|
2018-01-24 16:09:08 -08:00
|
|
|
/// <summary>
|
|
|
|
|
/// NuGet.Config path on disk
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string ConfigPath { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// XML
|
|
|
|
|
/// </summary>
|
|
|
|
|
public XDocument XML { get; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SimpleTestSettingsContext(string nugetConfigPath, string userPackagesFolder, string packagesV2, string fallbackFolder, string packageSource)
|
|
|
|
|
: this(nugetConfigPath, GetDefault(userPackagesFolder, packagesV2, fallbackFolder, packageSource))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SimpleTestSettingsContext(string path, XDocument xml)
|
|
|
|
|
{
|
|
|
|
|
XML = xml ?? throw new ArgumentNullException(nameof(xml));
|
|
|
|
|
ConfigPath = path ?? throw new ArgumentNullException(nameof(path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Save to disk
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Save()
|
|
|
|
|
{
|
|
|
|
|
FileUtility.Replace((outputPath) =>
|
|
|
|
|
{
|
|
|
|
|
using (var output = new FileStream(outputPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
|
|
|
|
|
{
|
|
|
|
|
XML.Save(output);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
ConfigPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disable automatic package restore and save the file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void DisableAutoRestore()
|
|
|
|
|
{
|
|
|
|
|
var section = GetOrAddSection(XML, "packageRestore");
|
|
|
|
|
|
|
|
|
|
AddEntry(section, "enabled", "false");
|
|
|
|
|
AddEntry(section, "automatic", "false");
|
|
|
|
|
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-19 01:44:16 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Just disable “Automatically check for missing packages during build in Visual Studio” and save the file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void DisableAutomaticInPackageRestoreSection()
|
|
|
|
|
{
|
|
|
|
|
var section = GetOrAddSection(XML, "packageRestore");
|
|
|
|
|
|
|
|
|
|
AddEntry(section, "enabled", "True");
|
|
|
|
|
AddEntry(section, "automatic", "false");
|
|
|
|
|
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 02:24:29 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Set default package management format to PackageReference.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SetPackageFormatToPackageReference()
|
|
|
|
|
{
|
|
|
|
|
var section = GetOrAddSection(XML, "packageManagement");
|
|
|
|
|
|
|
|
|
|
AddEntry(section, "format", "1");
|
|
|
|
|
AddEntry(section, "disabled", "False");
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-29 11:19:36 +09:30
|
|
|
/// <summary>
|
|
|
|
|
/// Set default package management format to packages.config.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SetPackageFormatToPackagesConfig()
|
|
|
|
|
{
|
|
|
|
|
var section = GetOrAddSection(XML, "packageManagement");
|
|
|
|
|
|
|
|
|
|
AddEntry(section, "format", "0");
|
|
|
|
|
AddEntry(section, "disabled", "False");
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetDependencyVersion(string dependencyVersion)
|
|
|
|
|
{
|
|
|
|
|
var section = GetOrAddSection(XML, "config");
|
|
|
|
|
|
|
|
|
|
AddEntry(section, "dependencyVersion", dependencyVersion);
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 16:09:08 -08:00
|
|
|
private static XDocument GetDefault(string userPackagesFolder, string packagesV2, string fallbackFolder, string packageSource)
|
|
|
|
|
{
|
|
|
|
|
var doc = GetEmptyConfig();
|
|
|
|
|
|
|
|
|
|
var packageSources = GetOrAddSection(doc, "packageSources");
|
2025-06-02 08:41:14 -07:00
|
|
|
AddEntry(packageSources, DefaultPackageSourceName, packageSource);
|
2018-01-24 16:09:08 -08:00
|
|
|
|
|
|
|
|
var fallbackFolders = GetOrAddSection(doc, "fallbackPackageFolders");
|
|
|
|
|
AddEntry(fallbackFolders, "shared", fallbackFolder);
|
|
|
|
|
|
|
|
|
|
AddSetting(doc, "globalPackagesFolder", userPackagesFolder);
|
|
|
|
|
AddSetting(doc, "repositoryPath", packagesV2);
|
|
|
|
|
|
|
|
|
|
return doc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static XDocument GetEmptyConfig()
|
|
|
|
|
{
|
|
|
|
|
var doc = new XDocument();
|
|
|
|
|
var configuration = new XElement(XName.Get("configuration"));
|
|
|
|
|
doc.Add(configuration);
|
|
|
|
|
|
|
|
|
|
var config = GetOrAddSection(doc, "config");
|
|
|
|
|
var packageSources = GetOrAddSection(doc, "packageSources");
|
2024-10-02 07:28:45 +09:30
|
|
|
var auditSources = GetOrAddSection(doc, "auditSources");
|
2018-01-24 16:09:08 -08:00
|
|
|
var disabledSources = GetOrAddSection(doc, "disabledPackageSources");
|
|
|
|
|
var fallbackFolders = GetOrAddSection(doc, "fallbackPackageFolders");
|
2022-01-10 11:25:23 -08:00
|
|
|
var packageSourceMapping = GetOrAddSection(doc, "packageSourceMapping");
|
2018-01-24 16:09:08 -08:00
|
|
|
|
|
|
|
|
packageSources.Add(new XElement(XName.Get("clear")));
|
2024-10-02 07:28:45 +09:30
|
|
|
auditSources.Add(new XElement(XName.Get("clear")));
|
2018-01-24 16:09:08 -08:00
|
|
|
disabledSources.Add(new XElement(XName.Get("clear")));
|
2022-01-10 11:25:23 -08:00
|
|
|
packageSourceMapping.Add(new XElement(XName.Get("clear")));
|
2018-01-24 16:09:08 -08:00
|
|
|
|
|
|
|
|
return doc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static XElement GetOrAddSection(XDocument doc, string name)
|
|
|
|
|
{
|
|
|
|
|
var root = doc.Element(XName.Get("configuration"));
|
|
|
|
|
|
|
|
|
|
var node = root.Element(XName.Get(name));
|
|
|
|
|
|
|
|
|
|
if (node == null)
|
|
|
|
|
{
|
|
|
|
|
node = new XElement(XName.Get(name));
|
|
|
|
|
root.Add(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 09:55:26 -08:00
|
|
|
public static void AddPackageSourceCredentialsSection(XDocument doc, string sourceName, string userName, string password, bool clearTextPassword)
|
|
|
|
|
{
|
|
|
|
|
var root = doc.Element(XName.Get("configuration"));
|
|
|
|
|
|
|
|
|
|
var sourceNode = new XElement(XName.Get(sourceName));
|
|
|
|
|
AddEntry(sourceNode, "Username", userName);
|
|
|
|
|
if (clearTextPassword)
|
|
|
|
|
{
|
|
|
|
|
AddEntry(sourceNode, "ClearTextPassword", password);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
AddEntry(sourceNode, "Password", password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var packageSourceCredentialsNode = new XElement(XName.Get("packageSourceCredentials"));
|
|
|
|
|
packageSourceCredentialsNode.Add(sourceNode);
|
|
|
|
|
|
|
|
|
|
root.Add(packageSourceCredentialsNode);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 16:09:08 -08:00
|
|
|
public static void AddEntry(XElement section, string key, string value)
|
|
|
|
|
{
|
|
|
|
|
var setting = new XElement(XName.Get("add"));
|
|
|
|
|
setting.Add(new XAttribute(XName.Get("key"), key));
|
|
|
|
|
setting.Add(new XAttribute(XName.Get("value"), value));
|
|
|
|
|
section.Add(setting);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 09:55:26 -08:00
|
|
|
public static void AddEntry(XElement section, string key, string value, string additionalAtrributeName, string additionalAttributeValue)
|
|
|
|
|
{
|
|
|
|
|
var setting = new XElement(XName.Get("add"));
|
|
|
|
|
setting.Add(new XAttribute(XName.Get("key"), key));
|
|
|
|
|
setting.Add(new XAttribute(XName.Get("value"), value));
|
|
|
|
|
setting.Add(new XAttribute(XName.Get(additionalAtrributeName), additionalAttributeValue));
|
|
|
|
|
section.Add(setting);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 10:10:28 -07:00
|
|
|
public static void AddEntry(XElement section, string key, string value, string additionalAtrributeName, string additionalAttributeValue, string additionalAtrributeName2, string additionalAttributeValue2)
|
|
|
|
|
{
|
|
|
|
|
var setting = new XElement(XName.Get("add"));
|
|
|
|
|
setting.Add(new XAttribute(XName.Get("key"), key));
|
|
|
|
|
setting.Add(new XAttribute(XName.Get("value"), value));
|
|
|
|
|
setting.Add(new XAttribute(XName.Get(additionalAtrributeName), additionalAttributeValue));
|
|
|
|
|
setting.Add(new XAttribute(XName.Get(additionalAtrributeName2), additionalAttributeValue2));
|
|
|
|
|
section.Add(setting);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 16:09:08 -08:00
|
|
|
public static void AddSetting(XDocument doc, string key, string value)
|
|
|
|
|
{
|
|
|
|
|
RemoveSetting(doc, key);
|
|
|
|
|
var config = GetOrAddSection(doc, "config");
|
|
|
|
|
|
|
|
|
|
var setting = new XElement(XName.Get("add"));
|
|
|
|
|
setting.Add(new XAttribute(XName.Get("key"), key));
|
|
|
|
|
setting.Add(new XAttribute(XName.Get("value"), value));
|
|
|
|
|
config.Add(setting);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RemoveSetting(XDocument doc, string key)
|
|
|
|
|
{
|
|
|
|
|
var config = GetOrAddSection(doc, "config");
|
|
|
|
|
|
2025-04-11 09:12:08 -07:00
|
|
|
foreach (var item in config.Elements(XName.Get("add")).Where(e => e.FirstAttribute.Value.Equals(key, StringComparison.OrdinalIgnoreCase)).ToArray())
|
2018-01-24 16:09:08 -08:00
|
|
|
{
|
|
|
|
|
item.Remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-11 23:11:07 -08:00
|
|
|
|
2020-12-14 23:07:25 +00:00
|
|
|
public static void RemoveSource(XDocument doc, string key)
|
|
|
|
|
{
|
|
|
|
|
var packageSources = GetOrAddSection(doc, "packageSources");
|
|
|
|
|
|
|
|
|
|
foreach (var item in packageSources.Elements(XName.Get("add")).Where(e => e.FirstAttribute.Value.Equals(key, StringComparison.OrdinalIgnoreCase)).ToArray())
|
|
|
|
|
{
|
|
|
|
|
item.Remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-07 10:30:09 -07:00
|
|
|
// Add nuget.org as package source when NetStandard.Library and NetCorePlatforms packages are needed and save the file.
|
2019-12-11 23:11:07 -08:00
|
|
|
public void AddNetStandardFeeds()
|
|
|
|
|
{
|
2021-10-07 10:30:09 -07:00
|
|
|
const string nuget = "https://api.nuget.org/v3/index.json";
|
2019-12-11 23:11:07 -08:00
|
|
|
|
|
|
|
|
var section = GetOrAddSection(XML, "packageSources");
|
2021-10-07 10:30:09 -07:00
|
|
|
AddEntry(section, "nuget", nuget);
|
2019-12-11 23:11:07 -08:00
|
|
|
|
|
|
|
|
Save();
|
|
|
|
|
}
|
2022-01-26 08:11:18 -08:00
|
|
|
|
2024-02-26 09:49:46 -08:00
|
|
|
public void RemoveSource(string key)
|
|
|
|
|
{
|
|
|
|
|
RemoveSource(XML, key);
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 11:54:19 -08:00
|
|
|
public void AddSource(string sourceName, string sourceUri)
|
|
|
|
|
{
|
|
|
|
|
var section = GetOrAddSection(XML, "packageSources");
|
|
|
|
|
AddEntry(section, sourceName, sourceUri);
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 13:12:32 -07:00
|
|
|
public void AddSource(string sourceName, string sourceUri, string allowInsecureConnectionsValue)
|
|
|
|
|
{
|
|
|
|
|
var section = GetOrAddSection(XML, "packageSources");
|
|
|
|
|
AddEntry(section, sourceName, sourceUri, "allowInsecureConnections", allowInsecureConnectionsValue);
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-04 08:54:48 -07:00
|
|
|
public void AddSource(string sourceName, string sourceUri, string attributeName, string attributeValue)
|
|
|
|
|
{
|
|
|
|
|
var section = GetOrAddSection(XML, "packageSources");
|
|
|
|
|
AddEntry(section, sourceName, sourceUri, attributeName, attributeValue);
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 00:41:41 +01:00
|
|
|
public void AddAuditSource(string sourceName, string sourceUri, string allowInsecureConnectionsValue)
|
|
|
|
|
{
|
|
|
|
|
var section = GetOrAddSection(XML, "auditSources");
|
|
|
|
|
AddEntry(section, sourceName, sourceUri, "allowInsecureConnections", allowInsecureConnectionsValue);
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-16 16:41:12 -08:00
|
|
|
public void AddPackageSourceMapping(string sourceName, params string[] patterns)
|
|
|
|
|
{
|
|
|
|
|
XElement packageSourceMappingSection = GetOrAddSection(XML, "packageSourceMapping");
|
|
|
|
|
|
|
|
|
|
packageSourceMappingSection.Add(
|
|
|
|
|
new XElement(
|
|
|
|
|
XName.Get("packageSource"),
|
|
|
|
|
new XAttribute(XName.Get("key"), sourceName),
|
|
|
|
|
patterns.Select(i => new XElement(
|
|
|
|
|
XName.Get("package"),
|
|
|
|
|
new XAttribute(
|
|
|
|
|
XName.Get("pattern"),
|
|
|
|
|
i)))));
|
|
|
|
|
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 08:11:18 -08:00
|
|
|
// Simply add any text as section into nuget.config file, adding large child node into nuget.config via api is tedious.
|
|
|
|
|
public static void AddSectionIntoNuGetConfig(string path, string content, string parentNode)
|
|
|
|
|
{
|
|
|
|
|
FileAttributes attr = File.GetAttributes(path);
|
|
|
|
|
// if path is directory then add section to default nuget.config, else add to file.
|
|
|
|
|
string nugetConfigPath = (attr & FileAttributes.Directory) == FileAttributes.Directory ?
|
|
|
|
|
Path.Combine(path, NuGet.Configuration.Settings.DefaultSettingsFileName) : path;
|
|
|
|
|
|
|
|
|
|
XmlDocument doc = new XmlDocument();
|
|
|
|
|
doc.Load(nugetConfigPath);
|
|
|
|
|
XmlNode docParentNode = doc.SelectSingleNode(parentNode);
|
|
|
|
|
|
|
|
|
|
XmlDocument tempDoc = new XmlDocument();
|
|
|
|
|
tempDoc.LoadXml(content);
|
|
|
|
|
|
|
|
|
|
foreach (XmlNode child in tempDoc.ChildNodes)
|
|
|
|
|
{
|
|
|
|
|
XmlNode existingNode = docParentNode.SelectSingleNode(child.Name);
|
|
|
|
|
|
|
|
|
|
if (existingNode != null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"Element node {existingNode.Name} already exist inside {parentNode} element.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//necessary for crossing XmlDocument contexts
|
|
|
|
|
XmlNode importNode = docParentNode.OwnerDocument.ImportNode(node: child, deep: true);
|
|
|
|
|
docParentNode.AppendChild(importNode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
doc.Save(nugetConfigPath);
|
|
|
|
|
}
|
2022-11-23 11:01:34 +10:30
|
|
|
|
|
|
|
|
public void SetDefaultPushSource(string packageSource)
|
|
|
|
|
{
|
|
|
|
|
XElement config = GetOrAddSection(XML, ConfigurationConstants.Config);
|
|
|
|
|
AddEntry(config, ConfigurationConstants.DefaultPushSource, packageSource);
|
|
|
|
|
Save();
|
|
|
|
|
}
|
2018-01-24 16:09:08 -08:00
|
|
|
}
|
|
|
|
|
}
|