2018-09-17 17:18:08 -07:00
|
|
|
// Copyright (c) .NET Foundation. All rights reserved.
|
2015-05-07 15:18:29 -07:00
|
|
|
// 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
|
|
|
|
|
|
2015-01-08 23:38:14 -08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2018-09-17 17:18:08 -07:00
|
|
|
using System.Linq;
|
2015-05-07 15:18:29 -07:00
|
|
|
using NuGet.Configuration;
|
2015-03-04 11:47:13 -08:00
|
|
|
using NuGet.Protocol.Core.Types;
|
2015-03-10 20:52:10 -07:00
|
|
|
using NuGet.Protocol.VisualStudio;
|
2015-12-07 17:40:52 -08:00
|
|
|
using NuGet.Test.Utility;
|
2015-01-08 23:38:14 -08:00
|
|
|
|
|
|
|
|
namespace Test.Utility
|
|
|
|
|
{
|
|
|
|
|
public class TestSourceRepositoryUtility
|
|
|
|
|
{
|
2015-01-25 20:11:10 -08:00
|
|
|
public static PackageSource V2PackageSource = new PackageSource("https://www.nuget.org/api/v2/", "v2");
|
|
|
|
|
public static PackageSource V3PackageSource = new PackageSource("https://api.nuget.org/v3/index.json", "v3");
|
2015-01-08 23:38:14 -08:00
|
|
|
|
2015-03-10 20:52:10 -07:00
|
|
|
public IEnumerable<Lazy<INuGetResourceProvider>> ResourceProviders { get; private set; }
|
2015-01-08 23:38:14 -08:00
|
|
|
|
2015-03-10 20:52:10 -07:00
|
|
|
private void Initialize()
|
2015-01-08 23:38:14 -08:00
|
|
|
{
|
2015-03-10 20:52:10 -07:00
|
|
|
ResourceProviders = Repository.Provider.GetVisualStudio();
|
|
|
|
|
//var aggregateCatalog = new AggregateCatalog();
|
|
|
|
|
//{
|
|
|
|
|
// aggregateCatalog.Catalogs.Add(new DirectoryCatalog(Environment.CurrentDirectory, "*.dll"));
|
|
|
|
|
// var container = new CompositionContainer(aggregateCatalog);
|
|
|
|
|
// container.ComposeParts(this);
|
|
|
|
|
// return container;
|
|
|
|
|
//}
|
2015-01-08 23:38:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static SourceRepositoryProvider CreateV3OnlySourceRepositoryProvider()
|
2015-01-19 00:56:29 -08:00
|
|
|
{
|
2015-05-07 15:18:29 -07:00
|
|
|
return CreateSourceRepositoryProvider(new List<PackageSource> { V3PackageSource });
|
2015-01-19 00:56:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static SourceRepositoryProvider CreateV2OnlySourceRepositoryProvider()
|
|
|
|
|
{
|
2015-05-07 15:18:29 -07:00
|
|
|
return CreateSourceRepositoryProvider(new List<PackageSource> { V2PackageSource });
|
2015-01-19 00:56:29 -08:00
|
|
|
}
|
|
|
|
|
|
2022-04-12 19:49:42 -07:00
|
|
|
public static SourceRepositoryProvider CreateSourceRepositoryProvider(PackageSource packageSource)
|
|
|
|
|
{
|
|
|
|
|
if (packageSource == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(packageSource));
|
|
|
|
|
}
|
|
|
|
|
return CreateSourceRepositoryProvider(new[] { packageSource });
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-19 00:56:29 -08:00
|
|
|
public static SourceRepositoryProvider CreateSourceRepositoryProvider(IEnumerable<PackageSource> packageSources)
|
2015-01-08 23:38:14 -08:00
|
|
|
{
|
|
|
|
|
var thisUtility = new TestSourceRepositoryUtility();
|
2015-03-10 20:52:10 -07:00
|
|
|
thisUtility.Initialize();
|
2015-01-19 00:56:29 -08:00
|
|
|
var packageSourceProvider = new TestPackageSourceProvider(packageSources);
|
2015-01-08 23:38:14 -08:00
|
|
|
|
|
|
|
|
var sourceRepositoryProvider = new SourceRepositoryProvider(packageSourceProvider, thisUtility.ResourceProviders);
|
|
|
|
|
return sourceRepositoryProvider;
|
|
|
|
|
}
|
2015-01-25 20:11:10 -08:00
|
|
|
|
2019-07-30 13:43:14 -07:00
|
|
|
public static SourceRepositoryProvider CreateSourceRepositoryProvider(IEnumerable<PackageSource> packageSources, IEnumerable<Lazy<INuGetResourceProvider>> mockNuGetResourceProviders)
|
|
|
|
|
{
|
|
|
|
|
var thisUtility = new TestSourceRepositoryUtility();
|
|
|
|
|
thisUtility.Initialize();
|
|
|
|
|
var packageSourceProvider = new TestPackageSourceProvider(packageSources);
|
|
|
|
|
|
|
|
|
|
var sourceRepositoryProvider = new SourceRepositoryProvider(packageSourceProvider, mockNuGetResourceProviders.Concat(thisUtility.ResourceProviders));
|
|
|
|
|
return sourceRepositoryProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-01-25 20:11:10 -08:00
|
|
|
public static SourceRepositoryProvider CreateSourceRepositoryProvider(IPackageSourceProvider packageSourceProvider)
|
|
|
|
|
{
|
|
|
|
|
var thisUtility = new TestSourceRepositoryUtility();
|
2015-03-10 20:52:10 -07:00
|
|
|
thisUtility.Initialize();
|
2015-01-25 20:11:10 -08:00
|
|
|
|
|
|
|
|
var sourceRepositoryProvider = new SourceRepositoryProvider(packageSourceProvider, thisUtility.ResourceProviders);
|
|
|
|
|
return sourceRepositoryProvider;
|
|
|
|
|
}
|
2025-08-12 16:16:02 -07:00
|
|
|
|
|
|
|
|
public static ISettings PopulateSettingsWithSources(SourceRepositoryProvider sourceRepositoryProvider, TestDirectory settingsDirectory)
|
|
|
|
|
{
|
|
|
|
|
var settings = new Settings(settingsDirectory);
|
|
|
|
|
|
|
|
|
|
foreach (var source in sourceRepositoryProvider.GetRepositories())
|
|
|
|
|
{
|
|
|
|
|
settings.AddOrUpdate(ConfigurationConstants.PackageSources, source.PackageSource.AsSourceItem());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
2015-01-08 23:38:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provider that only returns V3 as a source
|
|
|
|
|
/// </summary>
|
2015-01-25 20:11:10 -08:00
|
|
|
public class TestPackageSourceProvider : IPackageSourceProvider
|
2015-01-08 23:38:14 -08:00
|
|
|
{
|
2025-08-15 03:29:41 +10:00
|
|
|
private IEnumerable<PackageSource> _packageSources;
|
2015-05-07 15:18:29 -07:00
|
|
|
|
2015-01-19 00:56:29 -08:00
|
|
|
public TestPackageSourceProvider(IEnumerable<PackageSource> packageSources)
|
|
|
|
|
{
|
2025-08-15 03:29:41 +10:00
|
|
|
_packageSources = packageSources;
|
2015-01-19 00:56:29 -08:00
|
|
|
}
|
2015-05-07 15:18:29 -07:00
|
|
|
|
2025-08-15 03:29:41 +10:00
|
|
|
public IEnumerable<PackageSource> LoadPackageSources() => _packageSources;
|
2015-01-08 23:38:14 -08:00
|
|
|
|
2024-02-07 06:59:36 +10:30
|
|
|
public IReadOnlyList<PackageSource> LoadAuditSources() => Array.Empty<PackageSource>();
|
|
|
|
|
|
2015-04-15 14:03:39 -07:00
|
|
|
public event EventHandler PackageSourcesChanged;
|
2015-01-08 23:38:14 -08:00
|
|
|
|
|
|
|
|
public void SavePackageSources(IEnumerable<PackageSource> sources)
|
|
|
|
|
{
|
2025-08-15 03:29:41 +10:00
|
|
|
_packageSources = sources;
|
2018-09-17 17:18:08 -07:00
|
|
|
PackageSourcesChanged?.Invoke(this, null);
|
2015-01-08 23:38:14 -08:00
|
|
|
}
|
2015-02-06 14:29:39 -08:00
|
|
|
|
2018-09-17 17:18:08 -07:00
|
|
|
public string ActivePackageSourceName => throw new NotImplementedException();
|
2015-02-06 14:29:39 -08:00
|
|
|
|
2018-09-17 17:18:08 -07:00
|
|
|
public string DefaultPushSource => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public void SaveActivePackageSource(PackageSource source) => throw new NotImplementedException();
|
|
|
|
|
|
2025-08-15 03:29:41 +10:00
|
|
|
public PackageSource GetPackageSource(string name) => _packageSources.FirstOrDefault(s => string.Equals(s.Name, name, StringComparison.OrdinalIgnoreCase));
|
2018-09-17 17:18:08 -07:00
|
|
|
|
|
|
|
|
public void RemovePackageSource(string name) => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public void EnablePackageSource(string name) => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public void DisablePackageSource(string name) => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public PackageSource GetPackageSourceByName(string name) => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public PackageSource GetPackageSourceBySource(string source) => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public void UpdatePackageSource(PackageSource source, bool updateCredentials, bool updateEnabled) => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public void AddPackageSource(PackageSource source) => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public bool IsPackageSourceEnabled(string name) => throw new NotImplementedException();
|
2025-12-05 16:31:48 -05:00
|
|
|
|
|
|
|
|
public void SaveAuditSources(IEnumerable<PackageSource> sources) => throw new NotImplementedException();
|
2015-01-08 23:38:14 -08:00
|
|
|
}
|
|
|
|
|
|
2015-01-25 20:11:10 -08:00
|
|
|
public static class TestPackageSourceSettings
|
|
|
|
|
{
|
|
|
|
|
public static string TempPackageSourceContents =
|
|
|
|
|
@"<?xml version='1.0' encoding='utf-8'?>
|
|
|
|
|
<configuration>
|
|
|
|
|
<packageSources>
|
|
|
|
|
<add key='nuget.org' value='https://www.nuget.org/api/v2/' />
|
|
|
|
|
</packageSources>
|
|
|
|
|
</configuration>";
|
|
|
|
|
|
2015-12-07 17:40:52 -08:00
|
|
|
public static TestDirectory CreateAndGetSettingFilePath()
|
2015-01-25 20:11:10 -08:00
|
|
|
{
|
2016-10-06 22:07:45 -07:00
|
|
|
var tempFolder = TestDirectory.Create();
|
2015-01-25 20:11:10 -08:00
|
|
|
var fileName = "nuget.config";
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(Path.Combine(tempFolder, fileName), TempPackageSourceContents);
|
|
|
|
|
return tempFolder;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-08 23:38:14 -08:00
|
|
|
}
|