diff --git a/src/GitHub.Exports/Services/VSServices.cs b/src/GitHub.Exports/Services/VSServices.cs index ae65dd9c1..18d600a7f 100644 --- a/src/GitHub.Exports/Services/VSServices.cs +++ b/src/GitHub.Exports/Services/VSServices.cs @@ -83,6 +83,13 @@ namespace GitHub.Services /// True if a transient solution was successfully created in target directory (which should trigger opening of repository). public bool TryOpenRepository(string repoPath) { + var os = serviceProvider.TryGetService(); + if (os == null) + { + VsOutputLogger.WriteLine("TryOpenRepository couldn't find IOperatingSystem service."); + return false; + } + var dte = serviceProvider.TryGetService(); if (dte == null) { @@ -90,12 +97,16 @@ namespace GitHub.Services return false; } - bool solutionCreated = false; - const string slnName = TempSolutionName; + var repoDir = os.Directory.GetDirectory(repoPath); + if(!repoDir.Exists) + { + return false; + } + bool solutionCreated = false; try { - dte.Solution.Create(repoPath, slnName); + dte.Solution.Create(repoPath, TempSolutionName); solutionCreated = true; dte.Solution.Close(false); // Don't create a .sln file when we close. @@ -106,21 +117,13 @@ namespace GitHub.Services } finally { - TryCleanupSolutionUserFiles(repoPath, slnName); + TryCleanupSolutionUserFiles(os, repoPath, TempSolutionName); } - return solutionCreated; } - void TryCleanupSolutionUserFiles(string repoPath, string slnName) + void TryCleanupSolutionUserFiles(IOperatingSystem os, string repoPath, string slnName) { - var os = serviceProvider.TryGetService(); - if (os == null) - { - VsOutputLogger.WriteLine("TryOpenRepository couldn't find IOperatingSystem service."); - return; - } - var vsTempPath = Path.Combine(repoPath, ".vs", slnName); try { diff --git a/src/GitHub.TeamFoundation.14/Connect/GitHubConnectSection.cs b/src/GitHub.TeamFoundation.14/Connect/GitHubConnectSection.cs index dc9a90906..fae132e38 100644 --- a/src/GitHub.TeamFoundation.14/Connect/GitHubConnectSection.cs +++ b/src/GitHub.TeamFoundation.14/Connect/GitHubConnectSection.cs @@ -353,11 +353,10 @@ namespace GitHub.VisualStudio.TeamExplorer.Connect var opened = vsServices.TryOpenRepository(SelectedRepository.LocalPath); if (!opened) { - // Fall back to making the user select a solution to open. + // TryOpenRepository might fail because dir no longer exists. Let user find solution themselves. opened = ErrorHandler.Succeeded(ServiceProvider.GetSolution().OpenSolutionViaDlg(SelectedRepository.LocalPath, 1)); if (!opened) { - SelectedRepository = old; return false; } } diff --git a/src/UnitTests/GitHub.Exports/VSServicesTests.cs b/src/UnitTests/GitHub.Exports/VSServicesTests.cs index 7075bbcdc..bf23f4959 100644 --- a/src/UnitTests/GitHub.Exports/VSServicesTests.cs +++ b/src/UnitTests/GitHub.Exports/VSServicesTests.cs @@ -14,9 +14,10 @@ public class VSServicesTests [Fact] public void NoExceptions_ReturnsTrue() { - var target = CreateVSServices(); + var repoDir = @"x:\repo"; + var target = CreateVSServices(repoDir); - var success = target.TryOpenRepository(""); + var success = target.TryOpenRepository(repoDir); Assert.True(success); } @@ -24,16 +25,32 @@ public class VSServicesTests [Fact] public void SolutionCreateThrows_ReturnsFalse() { + var repoDir = @"x:\repo"; var dte = Substitute.For(); dte.Solution.When(s => s.Create(Arg.Any(), Arg.Any())).Do( ci => { throw new COMException(); }); - var target = CreateVSServices(dte: dte); + var target = CreateVSServices(repoDir, dte: dte); var success = target.TryOpenRepository(""); Assert.False(success); } + [Fact] + public void RepoDirExistsFalse_ReturnFalse() + { + var repoDir = @"x:\repo"; + var os = Substitute.For(); + //var directoryInfo = Substitute.For(); + //directoryInfo.Exists.Returns(false); + //os.Directory.GetDirectory(repoDir).Returns(directoryInfo); + var target = CreateVSServices(null, os: os); + + var success = target.TryOpenRepository(repoDir); + + Assert.False(success); + } + [Fact] public void DeleteThrowsIOException_ReturnTrue() { @@ -43,9 +60,9 @@ public class VSServicesTests var directoryInfo = Substitute.For(); directoryInfo.Exists.Returns(true); os.Directory.GetDirectory(tempDir).Returns(directoryInfo); - directoryInfo.When(di => di.Delete(true)).Do( + directoryInfo.When(di => di.Delete(true)).Do( ci => { throw new IOException(); }); - var target = CreateVSServices(os: os); + var target = CreateVSServices(repoDir, os: os); var success = target.TryOpenRepository(repoDir); @@ -61,17 +78,25 @@ public class VSServicesTests var directoryInfo = Substitute.For(); directoryInfo.Exists.Returns(true); os.Directory.GetDirectory(tempDir).Returns(directoryInfo); - var target = CreateVSServices(os: os); + var target = CreateVSServices(repoDir, os: os); var success = target.TryOpenRepository(repoDir); directoryInfo.Received().Delete(true); } - VSServices CreateVSServices(IOperatingSystem os = null, DTE dte = null) + VSServices CreateVSServices(string repoDir, IOperatingSystem os = null, DTE dte = null) { os = os ?? Substitute.For(); dte = dte ?? Substitute.For(); + + if (repoDir != null) + { + var directoryInfo = Substitute.For(); + directoryInfo.Exists.Returns(true); + os.Directory.GetDirectory(repoDir).Returns(directoryInfo); + } + var provider = Substitute.For(); provider.TryGetService().Returns(dte); provider.TryGetService().Returns(os);