Add unit tests for GitHubContainerProvider

This commit is contained in:
Jamie Cansdale 2019-02-04 12:33:46 +00:00
Родитель 7a69a150fa
Коммит 610d5b0366
5 изменённых файлов: 112 добавлений и 5 удалений

Просмотреть файл

@ -141,6 +141,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Octokit.GraphQL.Core", "sub
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Octokit.GraphQL", "submodules\octokit.graphql.net\Octokit.GraphQL\Octokit.GraphQL.csproj", "{791B408C-0ABC-465B-9EB1-A2422D67F418}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitHub.StartPage.UnitTests", "test\GitHub.StartPage.UnitTests\GitHub.StartPage.UnitTests.csproj", "{B467682B-9F0E-42D8-8A20-1DE78F798793}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -493,6 +495,14 @@ Global
{791B408C-0ABC-465B-9EB1-A2422D67F418}.Release|Any CPU.Build.0 = Release|Any CPU
{791B408C-0ABC-465B-9EB1-A2422D67F418}.ReleaseWithoutVsix|Any CPU.ActiveCfg = Release|Any CPU
{791B408C-0ABC-465B-9EB1-A2422D67F418}.ReleaseWithoutVsix|Any CPU.Build.0 = Release|Any CPU
{B467682B-9F0E-42D8-8A20-1DE78F798793}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B467682B-9F0E-42D8-8A20-1DE78F798793}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B467682B-9F0E-42D8-8A20-1DE78F798793}.DebugWithoutVsix|Any CPU.ActiveCfg = Debug|Any CPU
{B467682B-9F0E-42D8-8A20-1DE78F798793}.DebugWithoutVsix|Any CPU.Build.0 = Debug|Any CPU
{B467682B-9F0E-42D8-8A20-1DE78F798793}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B467682B-9F0E-42D8-8A20-1DE78F798793}.Release|Any CPU.Build.0 = Release|Any CPU
{B467682B-9F0E-42D8-8A20-1DE78F798793}.ReleaseWithoutVsix|Any CPU.ActiveCfg = Release|Any CPU
{B467682B-9F0E-42D8-8A20-1DE78F798793}.ReleaseWithoutVsix|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -527,6 +537,7 @@ Global
{65542DEE-D3BE-4810-B85A-08E970413A21} = {8A7DA2E7-262B-4581-807A-1C45CE79CDFD}
{3321CE72-26ED-4D1E-A8F5-6901FB783007} = {1E7F7253-A6AF-43C4-A955-37BEDDA01AC0}
{791B408C-0ABC-465B-9EB1-A2422D67F418} = {1E7F7253-A6AF-43C4-A955-37BEDDA01AC0}
{B467682B-9F0E-42D8-8A20-1DE78F798793} = {8A7DA2E7-262B-4581-807A-1C45CE79CDFD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {556014CF-5B35-4CE5-B3EF-6AB0007001AC}

Просмотреть файл

@ -99,9 +99,6 @@
<PackageReference Include="Microsoft.VisualStudio.SDK.Analyzers">
<Version>15.8.33</Version>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers">
<Version>15.8.122</Version>
</PackageReference>
<PackageReference Include="Microsoft.VSSDK.BuildTools">
<Version>15.8.3252</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>

Просмотреть файл

@ -36,9 +36,20 @@ namespace GitHub.StartPage
{
static readonly ILogger log = LogManager.ForContext<GitHubContainerProvider>();
readonly Lazy<IGitHubServiceProvider> gitHubServiceProvider;
public GitHubContainerProvider() : this(
new Lazy<IGitHubServiceProvider>(() => Package.GetGlobalService(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider))
{
}
public GitHubContainerProvider(Lazy<IGitHubServiceProvider> gitHubServiceProvider)
{
this.gitHubServiceProvider = gitHubServiceProvider;
}
public async Task<CodeContainer> AcquireCodeContainerAsync(IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken)
{
return await RunAcquisition(downloadProgress, null, cancellationToken);
}
@ -55,7 +66,7 @@ namespace GitHub.StartPage
try
{
var uiProvider = await Task.Run(() => Package.GetGlobalService(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider);
var uiProvider = await Task.Run(() => gitHubServiceProvider.Value);
request = await ShowCloneDialog(uiProvider, downloadProgress, cancellationToken, repository);
}
catch (Exception e)

Просмотреть файл

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net46</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Helpers\GlobalSuppressions.cs" Link="GlobalSuppressions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\GitHub.StartPage\GitHub.StartPage.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NSubstitute" Version="2.0.3" />
<PackageReference Include="NUnit" version="3.9.0" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Threading;
using GitHub.Models;
using GitHub.Services;
using GitHub.StartPage;
using Microsoft.VisualStudio.Shell.CodeContainerManagement;
using NSubstitute;
using NUnit.Framework;
using Task = System.Threading.Tasks.Task;
public class GitHubContainerProviderTests
{
public class TheAcquireCodeContainerAsyncMethod
{
[Test]
public async Task CloneOrOpenRepository_CloneDialogResult_Returned_By_ShowCloneDialog()
{
var downloadProgress = Substitute.For<IProgress<Microsoft.VisualStudio.Shell.ServiceProgressData>>();
var cancellationToken = CancellationToken.None;
var dialogService = Substitute.For<IDialogService>();
var result = new CloneDialogResult(@"x:\repo", "https://github.com/owner/repo");
dialogService.ShowCloneDialog(null).ReturnsForAnyArgs(result);
var cloneService = Substitute.For<IRepositoryCloneService>();
var target = CreateGitHubContainerProvider(dialogService: dialogService, cloneService: cloneService);
await target.AcquireCodeContainerAsync(downloadProgress, cancellationToken);
await cloneService.Received(1).CloneOrOpenRepository(result, downloadProgress, cancellationToken);
}
[Test]
public async Task Pass_DisplayUrl_To_ShowCloneDialog()
{
var displayUrl = "https://github.com/owner/displayUrl";
var browseOnlineUrl = "https://github.com/owner/browseOnlineUrl";
var remoteCodeContainer = new RemoteCodeContainer("Name", Guid.NewGuid(), new Uri(displayUrl), new Uri(browseOnlineUrl),
DateTimeOffset.Now, new Dictionary<string, string>());
var downloadProgress = Substitute.For<IProgress<Microsoft.VisualStudio.Shell.ServiceProgressData>>();
var cancellationToken = CancellationToken.None;
var dialogService = Substitute.For<IDialogService>();
var result = new CloneDialogResult(@"x:\repo", "https://github.com/owner/repo");
dialogService.ShowCloneDialog(null).ReturnsForAnyArgs(result);
var cloneService = Substitute.For<IRepositoryCloneService>();
var target = CreateGitHubContainerProvider(dialogService: dialogService, cloneService: cloneService);
await target.AcquireCodeContainerAsync(remoteCodeContainer, downloadProgress, cancellationToken);
await dialogService.Received(1).ShowCloneDialog(Arg.Any<IConnection>(), displayUrl);
}
static GitHubContainerProvider CreateGitHubContainerProvider(IDialogService dialogService = null,
IRepositoryCloneService cloneService = null, IUsageTracker usageTracker = null)
{
dialogService = dialogService ?? Substitute.For<IDialogService>();
cloneService = cloneService ?? Substitute.For<IRepositoryCloneService>();
usageTracker = usageTracker ?? Substitute.For<IUsageTracker>();
var sp = Substitute.For<IGitHubServiceProvider>();
sp.GetService<IDialogService>().Returns(dialogService);
sp.GetService<IRepositoryCloneService>().Returns(cloneService);
sp.GetService<IUsageTracker>().Returns(usageTracker);
var gitHubServiceProvider = new Lazy<IGitHubServiceProvider>(() => sp);
return new GitHubContainerProvider(gitHubServiceProvider);
}
}
}