Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions GitHubVS.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
6 changes: 6 additions & 0 deletions scripts/test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ if (!$?) {
$exitcode = 11
}

Write-Output "Running GitHub.StartPage.UnitTests..."
Run-NUnit test GitHub.StartPage.UnitTests $TimeoutDuration $config
if (!$?) {
$exitcode = 12
}

if ($exitcode -ne 0) {
$host.SetShouldExit($exitcode)
}
Expand Down
3 changes: 0 additions & 3 deletions src/GitHub.StartPage/GitHub.StartPage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
19 changes: 17 additions & 2 deletions src/GitHub.StartPage/StartPagePackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,20 @@ public class GitHubContainerProvider : ICodeContainerProvider
{
static readonly ILogger log = LogManager.ForContext<GitHubContainerProvider>();

public async Task<CodeContainer> AcquireCodeContainerAsync(IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken)
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);
}

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

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)
Expand All @@ -68,6 +79,10 @@ async Task<CodeContainer> RunAcquisition(IProgress<ServiceProgressData> download

var uri = request.Url.ToRepositoryUrl();
var repositoryName = request.Url.RepositoryName;

// Report all steps complete before returning a CodeContainer
downloadProgress.Report(new ServiceProgressData(string.Empty, string.Empty, 1, 1));

return new CodeContainer(
localProperties: new CodeContainerLocalProperties(request.Path, CodeContainerType.Folder,
new CodeContainerSourceControlProperties(repositoryName, request.Path, new Guid(Guids.GitSccProviderId))),
Expand Down
20 changes: 20 additions & 0 deletions test/GitHub.StartPage.UnitTests/GitHub.StartPage.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -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>
105 changes: 105 additions & 0 deletions test/GitHub.StartPage.UnitTests/GitHubContainerProviderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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;
using ServiceProgressData = Microsoft.VisualStudio.Shell.ServiceProgressData;

public class GitHubContainerProviderTests
{
public class TheAcquireCodeContainerAsyncMethod
{
[Test]
public async Task CloneOrOpenRepository_CloneDialogResult_Returned_By_ShowCloneDialog()
{
var downloadProgress = Substitute.For<IProgress<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<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);
}

[Test]
public async Task Completes_When_Returning_CodeContainer()
{
var downloadProgress = Substitute.For<IProgress<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);

var codeContainer = await target.AcquireCodeContainerAsync(downloadProgress, cancellationToken);

Assert.That(codeContainer, Is.Not.Null);
downloadProgress.Received(1).Report(
Arg.Is<ServiceProgressData>(x => x.TotalSteps > 0 && x.CurrentStep == x.TotalSteps));
}

[Test]
public async Task Does_Not_Complete_When_CloneDialog_Canceled()
{
var downloadProgress = Substitute.For<IProgress<ServiceProgressData>>();
var cancellationToken = CancellationToken.None;
var dialogService = Substitute.For<IDialogService>();
var result = (CloneDialogResult)null;
dialogService.ShowCloneDialog(null).ReturnsForAnyArgs(result);
var cloneService = Substitute.For<IRepositoryCloneService>();
var target = CreateGitHubContainerProvider(dialogService: dialogService, cloneService: cloneService);

var codeContainer = await target.AcquireCodeContainerAsync(downloadProgress, cancellationToken);

await cloneService.ReceivedWithAnyArgs(0).CloneOrOpenRepository(null, null, null);
downloadProgress.ReceivedWithAnyArgs(0).Report(null);
Assert.That(codeContainer, Is.Null);
}

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);
}
}
}