Skip to content

Commit efcd62a

Browse files
committed
Fix test for non-virtual methods in types with public ctor
* Exclude delegates from types checked * Missing virtual modifiers
1 parent f5fe1ee commit efcd62a

3 files changed

Lines changed: 26 additions & 24 deletions

File tree

LibGit2Sharp.Tests/MetaFixture.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ namespace LibGit2Sharp.Tests
1010
{
1111
public class MetaFixture
1212
{
13-
private static readonly Type[] excludedTypes = new[] { typeof(Repository) };
13+
private static readonly Type[] excludedTypes = new[]
14+
{
15+
typeof(Credentials),
16+
typeof(Filter),
17+
typeof(ObjectId),
18+
typeof(Repository),
19+
typeof(RepositoryOptions),
20+
typeof(Signature),
21+
};
1422

1523
// Related to https://github.com/libgit2/libgit2sharp/pull/251
1624
[Fact]
@@ -59,27 +67,21 @@ public void TypesInLibGit2SharpMustBeExtensibleInATestingContext()
5967
var nonTestableTypes = new Dictionary<Type, IEnumerable<string>>();
6068

6169
IEnumerable<Type> libGit2SharpTypes = Assembly.GetAssembly(typeof(Repository)).GetExportedTypes()
62-
.Where(t => !excludedTypes.Contains(t) && t.Namespace == typeof(Repository).Namespace);
70+
.Where(t => !excludedTypes.Contains(t) && t.Namespace == typeof(Repository).Namespace && !t.IsSubclassOf(typeof(Delegate)));
6371

6472
foreach (Type type in libGit2SharpTypes)
6573
{
6674
if (type.IsInterface || type.IsEnum || IsStatic(type))
6775
continue;
6876

69-
ConstructorInfo[] publicConstructor = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
70-
if (publicConstructor.Any())
71-
{
72-
continue;
73-
}
74-
7577
var nonVirtualMethodNamesForType = GetNonVirtualPublicMethodsNames(type).ToList();
7678
if (nonVirtualMethodNamesForType.Any())
7779
{
7880
nonTestableTypes.Add(type, nonVirtualMethodNamesForType);
7981
continue;
8082
}
8183

82-
if (!HasEmptyProtectedConstructor(type))
84+
if (!HasEmptyPublicOrProtectedConstructor(type))
8385
{
8486
nonTestableTypes.Add(type, new List<string>());
8587
}
@@ -131,11 +133,11 @@ private static IEnumerable<string> GetNonVirtualPublicMethodsNames(Type type)
131133
return from mi in publicMethods where !mi.IsVirtual && !mi.IsStatic select mi.ToString();
132134
}
133135

134-
private static bool HasEmptyProtectedConstructor(Type type)
136+
private static bool HasEmptyPublicOrProtectedConstructor(Type type)
135137
{
136-
ConstructorInfo[] nonPublicConstructors = type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance);
137-
138-
return nonPublicConstructors.Any(ci => !ci.IsPrivate && !ci.IsAssembly && !ci.IsFinal && !ci.GetParameters().Any());
138+
ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
139+
140+
return constructors.Any(ci => ci.GetParameters().Length == 0 && (ci.IsPublic || ci.IsFamily || ci.IsFamilyOrAssembly));
139141
}
140142

141143
private static bool IsStatic(Type type)

LibGit2Sharp/IndexEntry.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,30 @@ public class IndexEntry : IEquatable<IndexEntry>
2222
/// compared against the <see cref = "Blob" /> known from the <see cref = "Repository.Head" /> and the file in the working directory.
2323
/// </summary>
2424
[Obsolete("This method will be removed in the next release. Please use Repository.Index.RetrieveStatus(filePath) overload instead.")]
25-
public FileStatus State
25+
public virtual FileStatus State
2626
{
2727
get { return state(); }
2828
}
2929

3030
/// <summary>
3131
/// Gets the relative path to the file within the working directory.
3232
/// </summary>
33-
public string Path { get; private set; }
33+
public virtual string Path { get; private set; }
3434

3535
/// <summary>
3636
/// Gets the file mode.
3737
/// </summary>
38-
public Mode Mode { get; private set; }
38+
public virtual Mode Mode { get; private set; }
3939

4040
/// <summary>
4141
/// Gets the stage number.
4242
/// </summary>
43-
public StageLevel StageLevel { get; private set; }
43+
public virtual StageLevel StageLevel { get; private set; }
4444

4545
/// <summary>
4646
/// Gets the id of the <see cref = "Blob" /> pointed at by this index entry.
4747
/// </summary>
48-
public ObjectId Id { get; private set; }
48+
public virtual ObjectId Id { get; private set; }
4949

5050
internal static IndexEntry BuildFromPtr(Repository repo, IndexEntrySafeHandle handle)
5151
{

LibGit2Sharp/TreeDefinition.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private void AddEntry(string targetTreeEntryName, TreeEntryDefinition treeEntryD
5151
/// </summary>
5252
/// <param name="treeEntryPath">The path within this <see cref="TreeDefinition"/>.</param>
5353
/// <returns>The current <see cref="TreeDefinition"/>.</returns>
54-
public TreeDefinition Remove(string treeEntryPath)
54+
public virtual TreeDefinition Remove(string treeEntryPath)
5555
{
5656
Ensure.ArgumentNotNullOrEmptyString(treeEntryPath, "treeEntryPath");
5757

@@ -92,7 +92,7 @@ public TreeDefinition Remove(string treeEntryPath)
9292
/// <param name="targetTreeEntryPath">The path within this <see cref="TreeDefinition"/>.</param>
9393
/// <param name="treeEntryDefinition">The <see cref="TreeEntryDefinition"/> to be stored at the described location.</param>
9494
/// <returns>The current <see cref="TreeDefinition"/>.</returns>
95-
public TreeDefinition Add(string targetTreeEntryPath, TreeEntryDefinition treeEntryDefinition)
95+
public virtual TreeDefinition Add(string targetTreeEntryPath, TreeEntryDefinition treeEntryDefinition)
9696
{
9797
Ensure.ArgumentNotNullOrEmptyString(targetTreeEntryPath, "targetTreeEntryPath");
9898
Ensure.ArgumentNotNull(treeEntryDefinition, "treeEntryDefinition");
@@ -132,7 +132,7 @@ public TreeDefinition Add(string targetTreeEntryPath, TreeEntryDefinition treeEn
132132
/// <param name="blob">The <see cref="Blob"/> to be stored at the described location.</param>
133133
/// <param name="mode">The file related <see cref="Mode"/> attributes.</param>
134134
/// <returns>The current <see cref="TreeDefinition"/>.</returns>
135-
public TreeDefinition Add(string targetTreeEntryPath, Blob blob, Mode mode)
135+
public virtual TreeDefinition Add(string targetTreeEntryPath, Blob blob, Mode mode)
136136
{
137137
Ensure.ArgumentNotNull(blob, "blob");
138138
Ensure.ArgumentConformsTo(mode,
@@ -151,7 +151,7 @@ public TreeDefinition Add(string targetTreeEntryPath, Blob blob, Mode mode)
151151
/// <see cref="Repository" /> is a standard, non-bare, repository. The path will then be considered as a path relative to the root of the working directory.</param>
152152
/// <param name="mode">The file related <see cref="Mode"/> attributes.</param>
153153
/// <returns>The current <see cref="TreeDefinition"/>.</returns>
154-
public TreeDefinition Add(string targetTreeEntryPath, string filePath, Mode mode)
154+
public virtual TreeDefinition Add(string targetTreeEntryPath, string filePath, Mode mode)
155155
{
156156
Ensure.ArgumentNotNullOrEmptyString(filePath, "filePath");
157157

@@ -166,7 +166,7 @@ public TreeDefinition Add(string targetTreeEntryPath, string filePath, Mode mode
166166
/// <param name="targetTreeEntryPath">The path within this <see cref="TreeDefinition"/>.</param>
167167
/// <param name="tree">The <see cref="Tree"/> to be stored at the described location.</param>
168168
/// <returns>The current <see cref="TreeDefinition"/>.</returns>
169-
public TreeDefinition Add(string targetTreeEntryPath, Tree tree)
169+
public virtual TreeDefinition Add(string targetTreeEntryPath, Tree tree)
170170
{
171171
Ensure.ArgumentNotNull(tree, "tree");
172172

@@ -281,7 +281,7 @@ private void WrapTree(string entryName, TreeEntryDefinition treeEntryDefinition)
281281
/// </summary>
282282
/// <param name="treeEntryPath">The path within this <see cref="TreeDefinition"/>.</param>
283283
/// <returns>The found <see cref="TreeEntryDefinition"/> if any; null otherwise.</returns>
284-
public TreeEntryDefinition this[string treeEntryPath]
284+
public virtual TreeEntryDefinition this[string treeEntryPath]
285285
{
286286
get
287287
{

0 commit comments

Comments
 (0)