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:
Fredrik Høisæther Rasch
2020-06-07 17:53:26 +02:00
committed by GitHub
parent b9d1f448d4
commit 449faaefea
2 changed files with 52 additions and 1 deletions
+51
View File
@@ -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);
});
}
}
}
}
+1 -1
View File
@@ -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);