From 0777f44018859a8bc1d635018983bcebefaf8a76 Mon Sep 17 00:00:00 2001 From: Jamie Cansdale Date: Tue, 18 Sep 2018 17:11:14 +0100 Subject: [PATCH] Make VSGitExt use GitSccProvider UIContext It appears that using UICONTEXT.RepositoryOpen in VSGitExt doesn't work consistently. Revert back to using the GitSccProvider UIContext. --- src/GitHub.TeamFoundation.14/Services/VSGitExt.cs | 13 +++++++++---- .../VSGitExtTests.cs | 14 ++++++++------ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/GitHub.TeamFoundation.14/Services/VSGitExt.cs b/src/GitHub.TeamFoundation.14/Services/VSGitExt.cs index b2279c8b65..6bb382b4c7 100644 --- a/src/GitHub.TeamFoundation.14/Services/VSGitExt.cs +++ b/src/GitHub.TeamFoundation.14/Services/VSGitExt.cs @@ -12,7 +12,6 @@ using Microsoft.VisualStudio.Threading; using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility; using Task = System.Threading.Tasks.Task; -using static Microsoft.VisualStudio.VSConstants; namespace GitHub.VisualStudio.Base { @@ -52,9 +51,15 @@ public VSGitExt(IAsyncServiceProvider asyncServiceProvider, IVSUIContextFactory // Start with empty array until we have a chance to initialize. ActiveRepositories = Array.Empty(); - // Initialize when we enter the context of a Git repository - var context = factory.GetUIContext(UICONTEXT.RepositoryOpen_guid); - context.WhenActivated(() => JoinableTaskFactory.RunAsync(InitializeAsync).Task.Forget(log)); + // The IGitExt service isn't available when a TFS based solution is opened directly. + // It will become available when moving to a Git based solution (and cause a UIContext event to fire). + // NOTE: I tried using the RepositoryOpen context, but it didn't work consistently. + var context = factory.GetUIContext(new Guid(Guids.GitSccProviderId)); + context.WhenActivated(() => + { + log.Debug("WhenActivated"); + JoinableTaskFactory.RunAsync(InitializeAsync).Task.Forget(log); + }); } async Task InitializeAsync() diff --git a/test/GitHub.TeamFoundation.UnitTests/VSGitExtTests.cs b/test/GitHub.TeamFoundation.UnitTests/VSGitExtTests.cs index 8a2ebb4971..378956bb08 100644 --- a/test/GitHub.TeamFoundation.UnitTests/VSGitExtTests.cs +++ b/test/GitHub.TeamFoundation.UnitTests/VSGitExtTests.cs @@ -19,14 +19,15 @@ public class VSGitExtTests { public class TheConstructor : TestBaseClass { - [TestCase(true, 1)] - [TestCase(false, 0)] - public void GetServiceIGitExt_WhenRepositoryOpenIsActive(bool isActive, int expectCalls) + [TestCase(true, Guids.GitSccProviderId, 1)] + [TestCase(true, UICONTEXT.RepositoryOpen_string, 0, Description = "No longer using RepositoryOpen")] + [TestCase(false, Guids.GitSccProviderId, 0)] + public void GetServiceIGitExt_WhenGitSccProviderIsActive(bool isActive, string contextGuidString, int expectCalls) { var context = CreateVSUIContext(isActive); var sp = Substitute.For(); - var target = CreateVSGitExt(context, sp: sp); + var target = CreateVSGitExt(context, sp: sp, contextGuidString: contextGuidString); sp.Received(expectCalls).GetServiceAsync(typeof(IGitExt)); } @@ -210,15 +211,16 @@ static IReadOnlyList CreateActiveRepositories(params string[ } static VSGitExt CreateVSGitExt(IVSUIContext context = null, IGitExt gitExt = null, IAsyncServiceProvider sp = null, - ILocalRepositoryModelFactory repoFactory = null, JoinableTaskContext joinableTaskContext = null) + ILocalRepositoryModelFactory repoFactory = null, JoinableTaskContext joinableTaskContext = null, string contextGuidString = null) { context = context ?? CreateVSUIContext(true); gitExt = gitExt ?? CreateGitExt(); + var contextGuid = new Guid(contextGuidString ?? Guids.GitSccProviderId); sp = sp ?? Substitute.For(); repoFactory = repoFactory ?? Substitute.For(); joinableTaskContext = joinableTaskContext ?? new JoinableTaskContext(); var factory = Substitute.For(); - factory.GetUIContext(UICONTEXT.RepositoryOpen_guid).Returns(context); + factory.GetUIContext(contextGuid).Returns(context); sp.GetServiceAsync(typeof(IGitExt)).Returns(gitExt); var vsGitExt = new VSGitExt(sp, factory, repoFactory, joinableTaskContext); vsGitExt.JoinTillEmpty();