|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using GitHub.Models; |
| 4 | +using GitHub.Services; |
| 5 | +using GitHub.VisualStudio.Commands; |
| 6 | +using NSubstitute; |
| 7 | +using NUnit.Framework; |
| 8 | + |
| 9 | +public static class OpenFromUrlCommandTests |
| 10 | +{ |
| 11 | + public class TheExecuteMethod |
| 12 | + { |
| 13 | + [Test] |
| 14 | + public async Task Executed_From_Menu() |
| 15 | + { |
| 16 | + var target = CreateOpenFromUrlCommand(); |
| 17 | + |
| 18 | + await target.Execute(null); |
| 19 | + } |
| 20 | + |
| 21 | + [Test] |
| 22 | + public async Task Executed_From_Command_Window() |
| 23 | + { |
| 24 | + var target = CreateOpenFromUrlCommand(); |
| 25 | + |
| 26 | + await target.Execute(""); |
| 27 | + } |
| 28 | + |
| 29 | + [TestCase("https://github.com/github/visualstudio", null, null, 0, 1, Description = "No active repository")] |
| 30 | + [TestCase("https://github.com/github/visualstudio", null, @"c:\source\visualstudio", 0, 1, Description = "Active repository with no remote")] |
| 31 | + [TestCase("https://github.com/github/visualstudio", "https://github.com/github/visualstudio", @"c:\source\visualstudio", 1, 0, Description = "Matching active repository")] |
| 32 | + [TestCase("HTTPS://GITHUB.COM/GITHUB/VISUALSTUDIO", "https://github.com/github/visualstudio", @"c:\source\visualstudio", 1, 0, Description = "Matching active repository with different case")] |
| 33 | + [TestCase("https://github.com/jcansdale/visualstudio", "https://github.com/github/visualstudio", @"c:\source\visualstudio", 0, 1, Description = "Fork of target repository")] |
| 34 | + [TestCase("https://github.com/owner1/repo1", "https://github.com/owner2/repo2", @"c:\source", 0, 1, Description = "Different repository")] |
| 35 | + public async Task Execute(string url, string activeUrl, string activePath, int tryNavigateToContextCalls, int showCloneDialogCalls) |
| 36 | + { |
| 37 | + var dialogService = Substitute.For<IDialogService>(); |
| 38 | + var teamExplorerContext = Substitute.For<ITeamExplorerContext>(); |
| 39 | + var activeRepository = new LocalRepositoryModel { CloneUrl = activeUrl, LocalPath = activePath }; |
| 40 | + teamExplorerContext.ActiveRepository.Returns(activeRepository); |
| 41 | + var gitHubContextService = Substitute.For<IGitHubContextService>(); |
| 42 | + gitHubContextService.FindContextFromUrl(url).Returns(new GitHubContext()); |
| 43 | + dialogService.ShowCloneDialog(null, url).Returns(new CloneDialogResult(@"c:\source", url)); |
| 44 | + var target = CreateOpenFromUrlCommand(dialogService: dialogService, |
| 45 | + teamExplorerContext: teamExplorerContext, gitHubContextService: gitHubContextService); |
| 46 | + |
| 47 | + await target.Execute(url); |
| 48 | + |
| 49 | + gitHubContextService.ReceivedWithAnyArgs(tryNavigateToContextCalls).TryNavigateToContext(null, null); |
| 50 | + await dialogService.ReceivedWithAnyArgs(showCloneDialogCalls).ShowCloneDialog(null, null); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + static OpenFromUrlCommand CreateOpenFromUrlCommand( |
| 55 | + IDialogService dialogService = null, |
| 56 | + IRepositoryCloneService repositoryCloneService = null, |
| 57 | + ITeamExplorerContext teamExplorerContext = null, |
| 58 | + IGitHubContextService gitHubContextService = null) |
| 59 | + { |
| 60 | + dialogService = dialogService ?? Substitute.For<IDialogService>(); |
| 61 | + repositoryCloneService = repositoryCloneService ?? Substitute.For<IRepositoryCloneService>(); |
| 62 | + teamExplorerContext = teamExplorerContext ?? Substitute.For<ITeamExplorerContext>(); |
| 63 | + gitHubContextService = gitHubContextService ?? Substitute.For<IGitHubContextService>(); |
| 64 | + |
| 65 | + return new OpenFromUrlCommand( |
| 66 | + new Lazy<IDialogService>(() => dialogService), |
| 67 | + new Lazy<IRepositoryCloneService>(() => repositoryCloneService), |
| 68 | + new Lazy<ITeamExplorerContext>(() => teamExplorerContext), |
| 69 | + new Lazy<IGitHubContextService>(() => gitHubContextService)); |
| 70 | + } |
| 71 | +} |
0 commit comments