Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c78faec

Browse files
committed
Extract FetchOptionsBase and extend Clone tests
1 parent 1d7ed78 commit c78faec

File tree

9 files changed

+83
-52
lines changed

9 files changed

+83
-52
lines changed

LibGit2Sharp.Tests/CloneFixture.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,23 @@ public void WontCheckoutIfAskedNotTo(string url)
126126
public void CallsProgressCallbacks(string url)
127127
{
128128
bool transferWasCalled = false;
129+
bool progressWasCalled = false;
130+
bool updateTipsWasCalled = false;
129131
bool checkoutWasCalled = false;
130132

131133
var scd = BuildSelfCleaningDirectory();
132134

133135
Repository.Clone(url, scd.DirectoryPath, new CloneOptions()
134136
{
135137
OnTransferProgress = _ => { transferWasCalled = true; return true; },
138+
OnProgress = progress => { progressWasCalled = true; return true; },
139+
OnUpdateTips = (name, oldId, newId) => { updateTipsWasCalled = true; return true; },
136140
OnCheckoutProgress = (a, b, c) => checkoutWasCalled = true
137141
});
138142

139143
Assert.True(transferWasCalled);
144+
Assert.True(progressWasCalled);
145+
Assert.True(updateTipsWasCalled);
140146
Assert.True(checkoutWasCalled);
141147
}
142148

LibGit2Sharp.Tests/MetaFixture.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void TypesInLibGit2SharpMustBeExtensibleInATestingContext()
8787
var nonTestableTypes = new Dictionary<Type, IEnumerable<string>>();
8888

8989
IEnumerable<Type> libGit2SharpTypes = Assembly.GetAssembly(typeof(IRepository)).GetExportedTypes()
90-
.Where(t => !t.IsSealed && t.Namespace == typeof(IRepository).Namespace);
90+
.Where(t => MustBeMockable(t) && t.Namespace == typeof(IRepository).Namespace);
9191

9292
foreach (Type type in libGit2SharpTypes)
9393
{
@@ -113,6 +113,23 @@ public void TypesInLibGit2SharpMustBeExtensibleInATestingContext()
113113
}
114114
}
115115

116+
private static bool MustBeMockable(Type type)
117+
{
118+
if (type.IsSealed)
119+
{
120+
return false;
121+
}
122+
123+
if (type.IsAbstract)
124+
{
125+
return !type.Assembly.GetExportedTypes()
126+
.Where(t => t.IsSubclassOf(type))
127+
.All(t => t.IsAbstract || t.IsSealed);
128+
}
129+
130+
return true;
131+
}
132+
116133
[Fact]
117134
public void LibGit2SharpPublicInterfacesCoverAllPublicMembers()
118135
{

LibGit2Sharp/CloneOptions.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace LibGit2Sharp
77
/// <summary>
88
/// Options to define clone behaviour
99
/// </summary>
10-
public sealed class CloneOptions : IConvertableToGitCheckoutOpts
10+
public sealed class CloneOptions : FetchOptionsBase, IConvertableToGitCheckoutOpts
1111
{
1212
/// <summary>
1313
/// Creates default <see cref="CloneOptions"/> for a non-bare clone
@@ -29,20 +29,10 @@ public CloneOptions()
2929
public bool Checkout { get; set; }
3030

3131
/// <summary>
32-
/// Handler for network transfer and indexing progress information
33-
/// </summary>
34-
public TransferProgressHandler OnTransferProgress { get; set; }
35-
36-
/// <summary>
37-
/// Handler for checkout progress information
32+
/// Handler for checkout progress information.
3833
/// </summary>
3934
public CheckoutProgressHandler OnCheckoutProgress { get; set; }
4035

41-
/// <summary>
42-
/// Handler to generate <see cref="LibGit2Sharp.Credentials"/> for authentication.
43-
/// </summary>
44-
public CredentialsHandler CredentialsProvider { get; set; }
45-
4636
#region IConvertableToGitCheckoutOpts
4737

4838
CheckoutCallbacks IConvertableToGitCheckoutOpts.GenerateCallbacks()

LibGit2Sharp/FetchOptions.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace LibGit2Sharp
66
/// <summary>
77
/// Collection of parameters controlling Fetch behavior.
88
/// </summary>
9-
public sealed class FetchOptions
9+
public sealed class FetchOptions : FetchOptionsBase
1010
{
1111
/// <summary>
1212
/// Specifies the tag-following behavior of the fetch operation.
@@ -19,29 +19,5 @@ public sealed class FetchOptions
1919
/// retrieved during this fetch will be retrieved as well).</para>
2020
/// </summary>
2121
public TagFetchMode? TagFetchMode { get; set; }
22-
23-
/// <summary>
24-
/// Delegate that progress updates of the network transfer portion of fetch
25-
/// will be reported through.
26-
/// </summary>
27-
public ProgressHandler OnProgress { get; set; }
28-
29-
/// <summary>
30-
/// Delegate that updates of remote tracking branches will be reported through.
31-
/// </summary>
32-
public UpdateTipsHandler OnUpdateTips { get; set; }
33-
34-
/// <summary>
35-
/// Callback method that transfer progress will be reported through.
36-
/// <para>
37-
/// Reports the client's state regarding the received and processed (bytes, objects) from the server.
38-
/// </para>
39-
/// </summary>
40-
public TransferProgressHandler OnTransferProgress { get; set; }
41-
42-
/// <summary>
43-
/// Handler to generate <see cref="LibGit2Sharp.Credentials"/> for authentication.
44-
/// </summary>
45-
public CredentialsHandler CredentialsProvider { get; set; }
4622
}
4723
}

LibGit2Sharp/FetchOptionsBase.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using LibGit2Sharp.Handlers;
2+
3+
namespace LibGit2Sharp
4+
{
5+
/// <summary>
6+
/// Base collection of parameters controlling Fetch behavior.
7+
/// </summary>
8+
public abstract class FetchOptionsBase
9+
{
10+
internal FetchOptionsBase()
11+
{
12+
}
13+
14+
/// <summary>
15+
/// Handler for network transfer and indexing progress information.
16+
/// </summary>
17+
public ProgressHandler OnProgress { get; set; }
18+
19+
/// <summary>
20+
/// Handler for updates to remote tracking branches.
21+
/// </summary>
22+
public UpdateTipsHandler OnUpdateTips { get; set; }
23+
24+
/// <summary>
25+
/// Handler for data transfer progress.
26+
/// <para>
27+
/// Reports the client's state regarding the received and processed (bytes, objects) from the server.
28+
/// </para>
29+
/// </summary>
30+
public TransferProgressHandler OnTransferProgress { get; set; }
31+
32+
/// <summary>
33+
/// Handler to generate <see cref="LibGit2Sharp.Credentials"/> for authentication.
34+
/// </summary>
35+
public CredentialsHandler CredentialsProvider { get; set; }
36+
}
37+
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="Core\Handles\IndexNameEntrySafeHandle.cs" />
8585
<Compile Include="Core\Handles\IndexReucEntrySafeHandle.cs" />
8686
<Compile Include="EntryExistsException.cs" />
87+
<Compile Include="FetchOptionsBase.cs" />
8788
<Compile Include="IBelongToARepository.cs" />
8889
<Compile Include="IndexNameEntryCollection.cs" />
8990
<Compile Include="ContentChangeStats.cs" />

LibGit2Sharp/Network.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public virtual IEnumerable<DirectReference> ListReferences(Remote remote, Creden
5656
{
5757
if (credentialsProvider != null)
5858
{
59-
var callbacks = new RemoteCallbacks(null, null, null, credentialsProvider);
59+
var callbacks = new RemoteCallbacks(credentialsProvider);
6060
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
6161
Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks);
6262
}
@@ -100,8 +100,7 @@ static void DoFetch(RemoteSafeHandle remoteHandle, FetchOptions options, Signatu
100100
Proxy.git_remote_set_autotag(remoteHandle, options.TagFetchMode.Value);
101101
}
102102

103-
var callbacks = new RemoteCallbacks(
104-
options.OnProgress, options.OnTransferProgress, options.OnUpdateTips, options.CredentialsProvider);
103+
var callbacks = new RemoteCallbacks(options);
105104
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
106105

107106
// It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
@@ -273,8 +272,7 @@ public virtual void Push(
273272
// Load the remote.
274273
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, remote.Name, true))
275274
{
276-
var callbacks = new RemoteCallbacks(
277-
null, null, null, pushOptions.CredentialsProvider);
275+
var callbacks = new RemoteCallbacks(pushOptions.CredentialsProvider);
278276
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
279277
Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks);
280278

LibGit2Sharp/RemoteCallbacks.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,24 @@ namespace LibGit2Sharp
1212
/// </summary>
1313
internal class RemoteCallbacks
1414
{
15-
internal RemoteCallbacks(
16-
ProgressHandler onProgress = null,
17-
TransferProgressHandler onDownloadProgress = null,
18-
UpdateTipsHandler onUpdateTips = null,
19-
CredentialsHandler credentialsProvider = null)
15+
internal RemoteCallbacks(CredentialsHandler credentialsProvider)
2016
{
21-
Progress = onProgress;
22-
DownloadTransferProgress = onDownloadProgress;
23-
UpdateTips = onUpdateTips;
2417
CredentialsProvider = credentialsProvider;
2518
}
2619

20+
internal RemoteCallbacks(FetchOptionsBase fetchOptions)
21+
{
22+
if (fetchOptions == null)
23+
{
24+
return;
25+
}
26+
27+
Progress = fetchOptions.OnProgress;
28+
DownloadTransferProgress = fetchOptions.OnTransferProgress;
29+
UpdateTips = fetchOptions.OnUpdateTips;
30+
CredentialsProvider = fetchOptions.CredentialsProvider;
31+
}
32+
2733
#region Delegates
2834

2935
/// <summary>

LibGit2Sharp/Repository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ public static string Clone(string sourceUrl, string workdirPath,
544544
{
545545
var gitCheckoutOptions = checkoutOptionsWrapper.Options;
546546

547-
var remoteCallbacks = new RemoteCallbacks(null, options.OnTransferProgress, null, options.CredentialsProvider);
547+
var remoteCallbacks = new RemoteCallbacks(options);
548548
var gitRemoteCallbacks = remoteCallbacks.GenerateCallbacks();
549549

550550
var cloneOpts = new GitCloneOptions

0 commit comments

Comments
 (0)