From c78faec23f8ec7243b92f83af79f4bfc6f59e178 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Tue, 4 Nov 2014 20:45:44 -0600 Subject: [PATCH 1/3] Extract FetchOptionsBase and extend Clone tests --- LibGit2Sharp.Tests/CloneFixture.cs | 6 +++++ LibGit2Sharp.Tests/MetaFixture.cs | 19 ++++++++++++++- LibGit2Sharp/CloneOptions.cs | 14 ++--------- LibGit2Sharp/FetchOptions.cs | 26 +-------------------- LibGit2Sharp/FetchOptionsBase.cs | 37 ++++++++++++++++++++++++++++++ LibGit2Sharp/LibGit2Sharp.csproj | 1 + LibGit2Sharp/Network.cs | 8 +++---- LibGit2Sharp/RemoteCallbacks.cs | 22 +++++++++++------- LibGit2Sharp/Repository.cs | 2 +- 9 files changed, 83 insertions(+), 52 deletions(-) create mode 100644 LibGit2Sharp/FetchOptionsBase.cs diff --git a/LibGit2Sharp.Tests/CloneFixture.cs b/LibGit2Sharp.Tests/CloneFixture.cs index a3b35d8b3..c9f7a8e4a 100644 --- a/LibGit2Sharp.Tests/CloneFixture.cs +++ b/LibGit2Sharp.Tests/CloneFixture.cs @@ -126,6 +126,8 @@ public void WontCheckoutIfAskedNotTo(string url) public void CallsProgressCallbacks(string url) { bool transferWasCalled = false; + bool progressWasCalled = false; + bool updateTipsWasCalled = false; bool checkoutWasCalled = false; var scd = BuildSelfCleaningDirectory(); @@ -133,10 +135,14 @@ public void CallsProgressCallbacks(string url) Repository.Clone(url, scd.DirectoryPath, new CloneOptions() { OnTransferProgress = _ => { transferWasCalled = true; return true; }, + OnProgress = progress => { progressWasCalled = true; return true; }, + OnUpdateTips = (name, oldId, newId) => { updateTipsWasCalled = true; return true; }, OnCheckoutProgress = (a, b, c) => checkoutWasCalled = true }); Assert.True(transferWasCalled); + Assert.True(progressWasCalled); + Assert.True(updateTipsWasCalled); Assert.True(checkoutWasCalled); } diff --git a/LibGit2Sharp.Tests/MetaFixture.cs b/LibGit2Sharp.Tests/MetaFixture.cs index fb1b567cb..9a53cdd14 100644 --- a/LibGit2Sharp.Tests/MetaFixture.cs +++ b/LibGit2Sharp.Tests/MetaFixture.cs @@ -87,7 +87,7 @@ public void TypesInLibGit2SharpMustBeExtensibleInATestingContext() var nonTestableTypes = new Dictionary>(); IEnumerable libGit2SharpTypes = Assembly.GetAssembly(typeof(IRepository)).GetExportedTypes() - .Where(t => !t.IsSealed && t.Namespace == typeof(IRepository).Namespace); + .Where(t => MustBeMockable(t) && t.Namespace == typeof(IRepository).Namespace); foreach (Type type in libGit2SharpTypes) { @@ -113,6 +113,23 @@ public void TypesInLibGit2SharpMustBeExtensibleInATestingContext() } } + private static bool MustBeMockable(Type type) + { + if (type.IsSealed) + { + return false; + } + + if (type.IsAbstract) + { + return !type.Assembly.GetExportedTypes() + .Where(t => t.IsSubclassOf(type)) + .All(t => t.IsAbstract || t.IsSealed); + } + + return true; + } + [Fact] public void LibGit2SharpPublicInterfacesCoverAllPublicMembers() { diff --git a/LibGit2Sharp/CloneOptions.cs b/LibGit2Sharp/CloneOptions.cs index 068391ade..ce176eaaa 100644 --- a/LibGit2Sharp/CloneOptions.cs +++ b/LibGit2Sharp/CloneOptions.cs @@ -7,7 +7,7 @@ namespace LibGit2Sharp /// /// Options to define clone behaviour /// - public sealed class CloneOptions : IConvertableToGitCheckoutOpts + public sealed class CloneOptions : FetchOptionsBase, IConvertableToGitCheckoutOpts { /// /// Creates default for a non-bare clone @@ -29,20 +29,10 @@ public CloneOptions() public bool Checkout { get; set; } /// - /// Handler for network transfer and indexing progress information - /// - public TransferProgressHandler OnTransferProgress { get; set; } - - /// - /// Handler for checkout progress information + /// Handler for checkout progress information. /// public CheckoutProgressHandler OnCheckoutProgress { get; set; } - /// - /// Handler to generate for authentication. - /// - public CredentialsHandler CredentialsProvider { get; set; } - #region IConvertableToGitCheckoutOpts CheckoutCallbacks IConvertableToGitCheckoutOpts.GenerateCallbacks() diff --git a/LibGit2Sharp/FetchOptions.cs b/LibGit2Sharp/FetchOptions.cs index 020f47f45..ec42a2d6e 100644 --- a/LibGit2Sharp/FetchOptions.cs +++ b/LibGit2Sharp/FetchOptions.cs @@ -6,7 +6,7 @@ namespace LibGit2Sharp /// /// Collection of parameters controlling Fetch behavior. /// - public sealed class FetchOptions + public sealed class FetchOptions : FetchOptionsBase { /// /// Specifies the tag-following behavior of the fetch operation. @@ -19,29 +19,5 @@ public sealed class FetchOptions /// retrieved during this fetch will be retrieved as well). /// public TagFetchMode? TagFetchMode { get; set; } - - /// - /// Delegate that progress updates of the network transfer portion of fetch - /// will be reported through. - /// - public ProgressHandler OnProgress { get; set; } - - /// - /// Delegate that updates of remote tracking branches will be reported through. - /// - public UpdateTipsHandler OnUpdateTips { get; set; } - - /// - /// Callback method that transfer progress will be reported through. - /// - /// Reports the client's state regarding the received and processed (bytes, objects) from the server. - /// - /// - public TransferProgressHandler OnTransferProgress { get; set; } - - /// - /// Handler to generate for authentication. - /// - public CredentialsHandler CredentialsProvider { get; set; } } } diff --git a/LibGit2Sharp/FetchOptionsBase.cs b/LibGit2Sharp/FetchOptionsBase.cs new file mode 100644 index 000000000..c2f2aeb44 --- /dev/null +++ b/LibGit2Sharp/FetchOptionsBase.cs @@ -0,0 +1,37 @@ +using LibGit2Sharp.Handlers; + +namespace LibGit2Sharp +{ + /// + /// Base collection of parameters controlling Fetch behavior. + /// + public abstract class FetchOptionsBase + { + internal FetchOptionsBase() + { + } + + /// + /// Handler for network transfer and indexing progress information. + /// + public ProgressHandler OnProgress { get; set; } + + /// + /// Handler for updates to remote tracking branches. + /// + public UpdateTipsHandler OnUpdateTips { get; set; } + + /// + /// Handler for data transfer progress. + /// + /// Reports the client's state regarding the received and processed (bytes, objects) from the server. + /// + /// + public TransferProgressHandler OnTransferProgress { get; set; } + + /// + /// Handler to generate for authentication. + /// + public CredentialsHandler CredentialsProvider { get; set; } + } +} diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index 5d302db71..e6537ed69 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -84,6 +84,7 @@ + diff --git a/LibGit2Sharp/Network.cs b/LibGit2Sharp/Network.cs index e473f0d74..a4cc8bac2 100644 --- a/LibGit2Sharp/Network.cs +++ b/LibGit2Sharp/Network.cs @@ -56,7 +56,7 @@ public virtual IEnumerable ListReferences(Remote remote, Creden { if (credentialsProvider != null) { - var callbacks = new RemoteCallbacks(null, null, null, credentialsProvider); + var callbacks = new RemoteCallbacks(credentialsProvider); GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks(); Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks); } @@ -100,8 +100,7 @@ static void DoFetch(RemoteSafeHandle remoteHandle, FetchOptions options, Signatu Proxy.git_remote_set_autotag(remoteHandle, options.TagFetchMode.Value); } - var callbacks = new RemoteCallbacks( - options.OnProgress, options.OnTransferProgress, options.OnUpdateTips, options.CredentialsProvider); + var callbacks = new RemoteCallbacks(options); GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks(); // 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( // Load the remote. using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, remote.Name, true)) { - var callbacks = new RemoteCallbacks( - null, null, null, pushOptions.CredentialsProvider); + var callbacks = new RemoteCallbacks(pushOptions.CredentialsProvider); GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks(); Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks); diff --git a/LibGit2Sharp/RemoteCallbacks.cs b/LibGit2Sharp/RemoteCallbacks.cs index b2540dfbd..2a65ca3f2 100644 --- a/LibGit2Sharp/RemoteCallbacks.cs +++ b/LibGit2Sharp/RemoteCallbacks.cs @@ -12,18 +12,24 @@ namespace LibGit2Sharp /// internal class RemoteCallbacks { - internal RemoteCallbacks( - ProgressHandler onProgress = null, - TransferProgressHandler onDownloadProgress = null, - UpdateTipsHandler onUpdateTips = null, - CredentialsHandler credentialsProvider = null) + internal RemoteCallbacks(CredentialsHandler credentialsProvider) { - Progress = onProgress; - DownloadTransferProgress = onDownloadProgress; - UpdateTips = onUpdateTips; CredentialsProvider = credentialsProvider; } + internal RemoteCallbacks(FetchOptionsBase fetchOptions) + { + if (fetchOptions == null) + { + return; + } + + Progress = fetchOptions.OnProgress; + DownloadTransferProgress = fetchOptions.OnTransferProgress; + UpdateTips = fetchOptions.OnUpdateTips; + CredentialsProvider = fetchOptions.CredentialsProvider; + } + #region Delegates /// diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index 69e0e195d..d92aed911 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -544,7 +544,7 @@ public static string Clone(string sourceUrl, string workdirPath, { var gitCheckoutOptions = checkoutOptionsWrapper.Options; - var remoteCallbacks = new RemoteCallbacks(null, options.OnTransferProgress, null, options.CredentialsProvider); + var remoteCallbacks = new RemoteCallbacks(options); var gitRemoteCallbacks = remoteCallbacks.GenerateCallbacks(); var cloneOpts = new GitCloneOptions From c07261bee1ca326d078f4bafdb1c1d5e4f43a4b2 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Tue, 4 Nov 2014 20:45:59 -0600 Subject: [PATCH 2/3] Allman all the way --- LibGit2Sharp.Tests/CloneFixture.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/LibGit2Sharp.Tests/CloneFixture.cs b/LibGit2Sharp.Tests/CloneFixture.cs index c9f7a8e4a..a78a238cd 100644 --- a/LibGit2Sharp.Tests/CloneFixture.cs +++ b/LibGit2Sharp.Tests/CloneFixture.cs @@ -179,8 +179,10 @@ public void CanCloneFromBBWithCredentials(string url, string user, string pass) { var scd = BuildSelfCleaningDirectory(); - string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath, new CloneOptions() { - CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials { + string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath, new CloneOptions() + { + CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials + { Username = user, Password = pass, } From fe932f35cc29125102c8467b554d9dd0a102fed9 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Tue, 4 Nov 2014 20:46:26 -0600 Subject: [PATCH 3/3] Add TransferProgress.DebuggerDisplay --- LibGit2Sharp/TransferProgress.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/LibGit2Sharp/TransferProgress.cs b/LibGit2Sharp/TransferProgress.cs index 4ef072a8a..29638103a 100644 --- a/LibGit2Sharp/TransferProgress.cs +++ b/LibGit2Sharp/TransferProgress.cs @@ -1,10 +1,13 @@ -using LibGit2Sharp.Core; +using System.Diagnostics; +using System.Globalization; +using LibGit2Sharp.Core; namespace LibGit2Sharp { /// /// Expose progress values from a fetch operation. /// + [DebuggerDisplay("{DebuggerDisplay,nq}")] public class TransferProgress { private GitTransferProgress gitTransferProgress; @@ -66,5 +69,14 @@ public virtual long ReceivedBytes return (long) gitTransferProgress.received_bytes; } } + + private string DebuggerDisplay + { + get + { + return string.Format(CultureInfo.InvariantCulture, + "{0}/{1}, {2} bytes", ReceivedObjects, TotalObjects, ReceivedBytes); + } + } } }