mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-05-31 02:05:39 +00:00
ChecksClient constructor should take interface arguments (#2194)
* ChecksClient ctor takes IApiConnection interface Instead of requiring the concrete implementation type * Ensure all clients nested under GitHubClient have a concrete implementation with a matching ctor. An api client ctor should take either an IConnection or IApiConnection interface as argument.
This commit is contained in:
committed by
GitHub
parent
b9d1f448d4
commit
449faaefea
@@ -4,6 +4,8 @@ using NSubstitute;
|
||||
using Octokit.Internal;
|
||||
using Xunit;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Linq;
|
||||
|
||||
namespace Octokit.Tests
|
||||
{
|
||||
@@ -211,5 +213,54 @@ namespace Octokit.Tests
|
||||
httpClient.Received(1).SetRequestTimeout(TimeSpan.FromSeconds(15));
|
||||
}
|
||||
}
|
||||
|
||||
public class TheNestedClients
|
||||
{
|
||||
private static void VisitAllClientTypes(Type rootType, HashSet<Type> result)
|
||||
{
|
||||
const BindingFlags ifPropBinding = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
if (!result.Add(rootType))
|
||||
return;
|
||||
|
||||
foreach (var pi in rootType.GetProperties(ifPropBinding).Where(pi => pi.CanRead && pi.PropertyType.Name.EndsWith("Client", StringComparison.Ordinal)))
|
||||
{
|
||||
VisitAllClientTypes(pi.PropertyType, result);
|
||||
}
|
||||
}
|
||||
|
||||
internal static IEnumerable<Type> GetGitHubClientNestedInterfaces()
|
||||
{
|
||||
var visitedTypes = new HashSet<Type>();
|
||||
var rootType = typeof(GitHubClient);
|
||||
VisitAllClientTypes(rootType, visitedTypes);
|
||||
visitedTypes.Remove(rootType);
|
||||
return visitedTypes;
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> GetGitHubClientNestedInterfacesMemberData() =>
|
||||
GetGitHubClientNestedInterfaces().Select(t => new[] { t });
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetGitHubClientNestedInterfacesMemberData))]
|
||||
public void HasImplementationClassWithIApiConnectionCtor(Type clientInterface)
|
||||
{
|
||||
var octokitAssembly = typeof(GitHubClient).Assembly;
|
||||
|
||||
var implTypes = octokitAssembly.GetTypes()
|
||||
.Where(t => t.IsClass && t.IsPublic)
|
||||
.Where(t => t.GetInterfaces().Contains(clientInterface))
|
||||
.ToList();
|
||||
|
||||
Assert.Single(implTypes, t =>
|
||||
{
|
||||
const BindingFlags ctorBinding = BindingFlags.Instance | BindingFlags.Public;
|
||||
var ctor = t.GetConstructor(ctorBinding, Type.DefaultBinder,
|
||||
new[] { typeof(IApiConnection) }, null)
|
||||
?? t.GetConstructor(ctorBinding, Type.DefaultBinder,
|
||||
new[] { typeof(IConnection) }, null);
|
||||
return !(ctor is null);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
/// Initializes a new GitHub Checks API client.
|
||||
/// </summary>
|
||||
/// <param name="apiConnection">An API connection</param>
|
||||
public ChecksClient(ApiConnection apiConnection)
|
||||
public ChecksClient(IApiConnection apiConnection)
|
||||
{
|
||||
Run = new CheckRunsClient(apiConnection);
|
||||
Suite = new CheckSuitesClient(apiConnection);
|
||||
|
||||
Reference in New Issue
Block a user