Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 610d5b0

Browse files
committed
Add unit tests for GitHubContainerProvider
1 parent 7a69a15 commit 610d5b0

File tree

5 files changed

+112
-5
lines changed

5 files changed

+112
-5
lines changed

GitHubVS.sln

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Octokit.GraphQL.Core", "sub
141141
EndProject
142142
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Octokit.GraphQL", "submodules\octokit.graphql.net\Octokit.GraphQL\Octokit.GraphQL.csproj", "{791B408C-0ABC-465B-9EB1-A2422D67F418}"
143143
EndProject
144+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitHub.StartPage.UnitTests", "test\GitHub.StartPage.UnitTests\GitHub.StartPage.UnitTests.csproj", "{B467682B-9F0E-42D8-8A20-1DE78F798793}"
145+
EndProject
144146
Global
145147
GlobalSection(SolutionConfigurationPlatforms) = preSolution
146148
Debug|Any CPU = Debug|Any CPU
@@ -493,6 +495,14 @@ Global
493495
{791B408C-0ABC-465B-9EB1-A2422D67F418}.Release|Any CPU.Build.0 = Release|Any CPU
494496
{791B408C-0ABC-465B-9EB1-A2422D67F418}.ReleaseWithoutVsix|Any CPU.ActiveCfg = Release|Any CPU
495497
{791B408C-0ABC-465B-9EB1-A2422D67F418}.ReleaseWithoutVsix|Any CPU.Build.0 = Release|Any CPU
498+
{B467682B-9F0E-42D8-8A20-1DE78F798793}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
499+
{B467682B-9F0E-42D8-8A20-1DE78F798793}.Debug|Any CPU.Build.0 = Debug|Any CPU
500+
{B467682B-9F0E-42D8-8A20-1DE78F798793}.DebugWithoutVsix|Any CPU.ActiveCfg = Debug|Any CPU
501+
{B467682B-9F0E-42D8-8A20-1DE78F798793}.DebugWithoutVsix|Any CPU.Build.0 = Debug|Any CPU
502+
{B467682B-9F0E-42D8-8A20-1DE78F798793}.Release|Any CPU.ActiveCfg = Release|Any CPU
503+
{B467682B-9F0E-42D8-8A20-1DE78F798793}.Release|Any CPU.Build.0 = Release|Any CPU
504+
{B467682B-9F0E-42D8-8A20-1DE78F798793}.ReleaseWithoutVsix|Any CPU.ActiveCfg = Release|Any CPU
505+
{B467682B-9F0E-42D8-8A20-1DE78F798793}.ReleaseWithoutVsix|Any CPU.Build.0 = Release|Any CPU
496506
EndGlobalSection
497507
GlobalSection(SolutionProperties) = preSolution
498508
HideSolutionNode = FALSE
@@ -527,6 +537,7 @@ Global
527537
{65542DEE-D3BE-4810-B85A-08E970413A21} = {8A7DA2E7-262B-4581-807A-1C45CE79CDFD}
528538
{3321CE72-26ED-4D1E-A8F5-6901FB783007} = {1E7F7253-A6AF-43C4-A955-37BEDDA01AC0}
529539
{791B408C-0ABC-465B-9EB1-A2422D67F418} = {1E7F7253-A6AF-43C4-A955-37BEDDA01AC0}
540+
{B467682B-9F0E-42D8-8A20-1DE78F798793} = {8A7DA2E7-262B-4581-807A-1C45CE79CDFD}
530541
EndGlobalSection
531542
GlobalSection(ExtensibilityGlobals) = postSolution
532543
SolutionGuid = {556014CF-5B35-4CE5-B3EF-6AB0007001AC}

src/GitHub.StartPage/GitHub.StartPage.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@
9999
<PackageReference Include="Microsoft.VisualStudio.SDK.Analyzers">
100100
<Version>15.8.33</Version>
101101
</PackageReference>
102-
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers">
103-
<Version>15.8.122</Version>
104-
</PackageReference>
105102
<PackageReference Include="Microsoft.VSSDK.BuildTools">
106103
<Version>15.8.3252</Version>
107104
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>

src/GitHub.StartPage/StartPagePackage.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,20 @@ public class GitHubContainerProvider : ICodeContainerProvider
3636
{
3737
static readonly ILogger log = LogManager.ForContext<GitHubContainerProvider>();
3838

39-
public async Task<CodeContainer> AcquireCodeContainerAsync(IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken)
39+
readonly Lazy<IGitHubServiceProvider> gitHubServiceProvider;
40+
41+
public GitHubContainerProvider() : this(
42+
new Lazy<IGitHubServiceProvider>(() => Package.GetGlobalService(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider))
43+
{
44+
}
45+
46+
public GitHubContainerProvider(Lazy<IGitHubServiceProvider> gitHubServiceProvider)
4047
{
48+
this.gitHubServiceProvider = gitHubServiceProvider;
49+
}
4150

51+
public async Task<CodeContainer> AcquireCodeContainerAsync(IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken)
52+
{
4253
return await RunAcquisition(downloadProgress, null, cancellationToken);
4354
}
4455

@@ -55,7 +66,7 @@ async Task<CodeContainer> RunAcquisition(IProgress<ServiceProgressData> download
5566

5667
try
5768
{
58-
var uiProvider = await Task.Run(() => Package.GetGlobalService(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider);
69+
var uiProvider = await Task.Run(() => gitHubServiceProvider.Value);
5970
request = await ShowCloneDialog(uiProvider, downloadProgress, cancellationToken, repository);
6071
}
6172
catch (Exception e)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net46</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="..\Helpers\GlobalSuppressions.cs" Link="GlobalSuppressions.cs" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\src\GitHub.StartPage\GitHub.StartPage.csproj" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="NSubstitute" Version="2.0.3" />
17+
<PackageReference Include="NUnit" version="3.9.0" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using GitHub.Models;
5+
using GitHub.Services;
6+
using GitHub.StartPage;
7+
using Microsoft.VisualStudio.Shell.CodeContainerManagement;
8+
using NSubstitute;
9+
using NUnit.Framework;
10+
using Task = System.Threading.Tasks.Task;
11+
12+
public class GitHubContainerProviderTests
13+
{
14+
public class TheAcquireCodeContainerAsyncMethod
15+
{
16+
[Test]
17+
public async Task CloneOrOpenRepository_CloneDialogResult_Returned_By_ShowCloneDialog()
18+
{
19+
var downloadProgress = Substitute.For<IProgress<Microsoft.VisualStudio.Shell.ServiceProgressData>>();
20+
var cancellationToken = CancellationToken.None;
21+
var dialogService = Substitute.For<IDialogService>();
22+
var result = new CloneDialogResult(@"x:\repo", "https://github.com/owner/repo");
23+
dialogService.ShowCloneDialog(null).ReturnsForAnyArgs(result);
24+
var cloneService = Substitute.For<IRepositoryCloneService>();
25+
var target = CreateGitHubContainerProvider(dialogService: dialogService, cloneService: cloneService);
26+
27+
await target.AcquireCodeContainerAsync(downloadProgress, cancellationToken);
28+
29+
await cloneService.Received(1).CloneOrOpenRepository(result, downloadProgress, cancellationToken);
30+
}
31+
32+
[Test]
33+
public async Task Pass_DisplayUrl_To_ShowCloneDialog()
34+
{
35+
var displayUrl = "https://github.com/owner/displayUrl";
36+
var browseOnlineUrl = "https://github.com/owner/browseOnlineUrl";
37+
var remoteCodeContainer = new RemoteCodeContainer("Name", Guid.NewGuid(), new Uri(displayUrl), new Uri(browseOnlineUrl),
38+
DateTimeOffset.Now, new Dictionary<string, string>());
39+
var downloadProgress = Substitute.For<IProgress<Microsoft.VisualStudio.Shell.ServiceProgressData>>();
40+
var cancellationToken = CancellationToken.None;
41+
var dialogService = Substitute.For<IDialogService>();
42+
var result = new CloneDialogResult(@"x:\repo", "https://github.com/owner/repo");
43+
dialogService.ShowCloneDialog(null).ReturnsForAnyArgs(result);
44+
var cloneService = Substitute.For<IRepositoryCloneService>();
45+
var target = CreateGitHubContainerProvider(dialogService: dialogService, cloneService: cloneService);
46+
47+
await target.AcquireCodeContainerAsync(remoteCodeContainer, downloadProgress, cancellationToken);
48+
49+
await dialogService.Received(1).ShowCloneDialog(Arg.Any<IConnection>(), displayUrl);
50+
}
51+
52+
static GitHubContainerProvider CreateGitHubContainerProvider(IDialogService dialogService = null,
53+
IRepositoryCloneService cloneService = null, IUsageTracker usageTracker = null)
54+
{
55+
dialogService = dialogService ?? Substitute.For<IDialogService>();
56+
cloneService = cloneService ?? Substitute.For<IRepositoryCloneService>();
57+
usageTracker = usageTracker ?? Substitute.For<IUsageTracker>();
58+
59+
var sp = Substitute.For<IGitHubServiceProvider>();
60+
sp.GetService<IDialogService>().Returns(dialogService);
61+
sp.GetService<IRepositoryCloneService>().Returns(cloneService);
62+
sp.GetService<IUsageTracker>().Returns(usageTracker);
63+
64+
var gitHubServiceProvider = new Lazy<IGitHubServiceProvider>(() => sp);
65+
return new GitHubContainerProvider(gitHubServiceProvider);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)