|
| 1 | +using EnvDTE; |
| 2 | +using EnvDTE80; |
| 3 | +using GitHub.Services; |
| 4 | +using Microsoft.VisualStudio.ComponentModelHost; |
| 5 | +using Microsoft.VisualStudio.Shell; |
| 6 | +using System; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Xunit; |
| 11 | +using Xunit.Abstractions; |
| 12 | +using Task = System.Threading.Tasks.Task; |
| 13 | + |
| 14 | +namespace IntegrationTests |
| 15 | +{ |
| 16 | + public class VSGitExtIntegrationTests |
| 17 | + { |
| 18 | + public class TheActiveRepositoriesProperty |
| 19 | + { |
| 20 | + readonly ITestOutputHelper output; |
| 21 | + |
| 22 | + public TheActiveRepositoriesProperty(ITestOutputHelper output) |
| 23 | + { |
| 24 | + this.output = output; |
| 25 | + } |
| 26 | + |
| 27 | + [VsFact(UIThread = true, Version = "2015-")] |
| 28 | + public async Task SolutionNotOnGit_NoActiveRepositoriesAsync() |
| 29 | + { |
| 30 | + var dte = (DTE)ServiceProvider.GlobalProvider.GetService(typeof(DTE)); |
| 31 | + var componentModel = (IComponentModel)await AsyncServiceProvider.GlobalProvider.GetServiceAsync(typeof(SComponentModel)); |
| 32 | + var gitExt = componentModel.GetService<IVSGitExt>(); |
| 33 | + |
| 34 | + dte.Solution.Open(@"C:\test\ClassLibraryNotInGit\ClassLibraryNotInGit.sln"); |
| 35 | + Assert.True(await WaitForLocalPath(gitExt, null)); |
| 36 | + } |
| 37 | + |
| 38 | + [VsFact(UIThread = true, Version = "2015-")] |
| 39 | + public async Task ClassLibraryInGit_HasActiveRepositoriesAsync() |
| 40 | + { |
| 41 | + var dte = (DTE)ServiceProvider.GlobalProvider.GetService(typeof(DTE)); |
| 42 | + var componentModel = (IComponentModel)await AsyncServiceProvider.GlobalProvider.GetServiceAsync(typeof(SComponentModel)); |
| 43 | + var gitExt = componentModel.GetService<IVSGitExt>(); |
| 44 | + |
| 45 | + dte.Solution.Open(@"C:\test\ClassLibraryInGit\ClassLibraryInGit.sln"); |
| 46 | + Assert.True(await WaitForLocalPath(gitExt, @"C:\test\ClassLibraryInGit")); |
| 47 | + } |
| 48 | + |
| 49 | + [VsFact(UIThread = true, Version = "2015-")] |
| 50 | + public async Task OpenClassLibraryNotInGitThenClassLibraryInGit_ActiveRepositoriesChangesAsync() |
| 51 | + { |
| 52 | + var dte = (DTE)ServiceProvider.GlobalProvider.GetService(typeof(DTE)); |
| 53 | + var componentModel = (IComponentModel)await AsyncServiceProvider.GlobalProvider.GetServiceAsync(typeof(SComponentModel)); |
| 54 | + var gitExt = componentModel.GetService<IVSGitExt>(); |
| 55 | + |
| 56 | + dte.Solution.Open(@"C:\test\ClassLibraryNotInGit\ClassLibraryNotInGit.sln"); |
| 57 | + Assert.True(await WaitForLocalPath(gitExt, null)); |
| 58 | + |
| 59 | + dte.Solution.Open(@"C:\test\ClassLibraryInGit\ClassLibraryInGit.sln"); |
| 60 | + Assert.True(await WaitForLocalPath(gitExt, @"C:\test\ClassLibraryInGit")); |
| 61 | + |
| 62 | + dte.Solution.Open(@"C:\test\ClassLibraryNotInGit\ClassLibraryNotInGit.sln"); |
| 63 | + Assert.True(await WaitForLocalPath(gitExt, null)); |
| 64 | + |
| 65 | + dte.Solution.Open(@"C:\test\ClassLibraryInGit\ClassLibraryInGit.sln"); |
| 66 | + Assert.True(await WaitForLocalPath(gitExt, @"C:\test\ClassLibraryInGit")); |
| 67 | + } |
| 68 | + |
| 69 | + async Task AddSolutionToSourceControlAsync() |
| 70 | + { |
| 71 | + var dte = (DTE2)ServiceProvider.GlobalProvider.GetService(typeof(DTE)); |
| 72 | + dte.ExecuteCommand("Team.Git.AddSolutionToSourceControl"); |
| 73 | + await Task.Yield(); |
| 74 | + } |
| 75 | + |
| 76 | + async Task<Solution> CreateSolutionAsync() |
| 77 | + { |
| 78 | + var dte = (DTE2)ServiceProvider.GlobalProvider.GetService(typeof(DTE)); |
| 79 | + var solution = (Solution2)dte.Solution; |
| 80 | + var templatePath = solution.GetProjectTemplate("ClassLibrary.zip", "CSharp"); |
| 81 | + var tempDir = Path.Combine(@"c:\test", Guid.NewGuid().ToString()); |
| 82 | + var solutionPath = Path.Combine(tempDir, "test.sln"); |
| 83 | + solution.Create(tempDir, "test"); |
| 84 | + solution.AddFromTemplate(templatePath, tempDir, "MyClassLibrary", false); |
| 85 | + solution.SaveAs(solutionPath); |
| 86 | + output.WriteLine(solutionPath); |
| 87 | + await Task.Yield(); |
| 88 | + return (Solution)solution; |
| 89 | + } |
| 90 | + |
| 91 | + Task<bool> WaitForLocalPath(IVSGitExt gitExt, string expectLocalPath) |
| 92 | + { |
| 93 | + return WaitFor(() => |
| 94 | + { |
| 95 | + var localPath = gitExt.ActiveRepositories.FirstOrDefault()?.LocalPath; |
| 96 | + output.WriteLine($"found {localPath}, looking for {expectLocalPath}"); |
| 97 | + return localPath == expectLocalPath; |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + async Task<bool> WaitFor(Func<bool> condition, int timeout = 20000, int delay = 100) |
| 102 | + { |
| 103 | + for (int count = 0; count < timeout; count += delay) |
| 104 | + { |
| 105 | + if (condition()) |
| 106 | + { |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + await Task.Delay(delay); |
| 111 | + } |
| 112 | + |
| 113 | + return false; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments