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

Commit dd0cc9e

Browse files
authored
Merge pull request #688 from github/fixes/fix-build-on-ci
Fix unit tests on the 2.1 tree
2 parents c94818a + bd8a9dc commit dd0cc9e

File tree

13 files changed

+124
-13
lines changed

13 files changed

+124
-13
lines changed

appveyor.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ install:
1212
Set-Content c:\users\appveyor\.ssh\id_rsa $fileContent
1313
} else {
1414
git submodule deinit script
15-
git submodule deinit submodules/externalpackages/StartPage
1615
}
1716
1817
git submodule update

src/GitHub.App/Services/RepositoryCloneService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.VisualStudio.Shell;
88
using NLog;
99
using Rothko;
10+
using GitHub.Helpers;
1011

1112
namespace GitHub.Services
1213
{
@@ -48,7 +49,7 @@ public IObservable<Unit> CloneRepository(string cloneUrl, string repositoryName,
4849

4950
// Once we've done IO switch to the main thread to call vsGitServices.Clone() as this must be
5051
// called on the main thread.
51-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
52+
await ThreadingHelper.SwitchToMainThreadAsync();
5253

5354
try
5455
{

src/GitHub.Exports/GitHub.Exports.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@
9696
<HintPath>..\..\packages\Microsoft.VisualStudio.TextManager.Interop.8.0.8.0.50727\lib\Microsoft.VisualStudio.TextManager.Interop.8.0.dll</HintPath>
9797
<Private>True</Private>
9898
</Reference>
99+
<Reference Include="Microsoft.VisualStudio.Threading, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
100+
<HintPath>..\..\packages\Microsoft.VisualStudio.Threading.14.1.131\lib\net45\Microsoft.VisualStudio.Threading.dll</HintPath>
101+
<Private>True</Private>
102+
</Reference>
99103
<Reference Include="PresentationCore" />
100104
<Reference Include="PresentationFramework" />
101105
<Reference Include="System" />
@@ -133,6 +137,7 @@
133137
<Compile Include="Extensions\PropertyNotifierExtensions.cs" />
134138
<Compile Include="Extensions\SimpleRepositoryModelExtensions.cs" />
135139
<Compile Include="Helpers\SettingsStore.cs" />
140+
<Compile Include="Helpers\ThreadingHelper.cs" />
136141
<Compile Include="Info\ApplicationInfo.cs" />
137142
<Compile Include="Models\BranchModel.cs" />
138143
<Compile Include="Models\IAccount.cs" />
446 Bytes
Binary file not shown.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System.Threading.Tasks;
2+
using ThreadHelper = Microsoft.VisualStudio.Shell.ThreadHelper;
3+
using GitHub.Extensions;
4+
using System.Runtime.CompilerServices;
5+
using System;
6+
using static Microsoft.VisualStudio.Threading.JoinableTaskFactory;
7+
using static Microsoft.VisualStudio.Threading.AwaitExtensions;
8+
9+
namespace GitHub.Helpers
10+
{
11+
public interface IAwaitable
12+
{
13+
IAwaiter GetAwaiter();
14+
}
15+
16+
public interface IAwaiter : INotifyCompletion
17+
{
18+
bool IsCompleted { get; }
19+
void GetResult();
20+
}
21+
22+
public static class ThreadingHelper
23+
{
24+
/// <summary>
25+
/// Switch to the UI thread using ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync
26+
/// Auto-disables switching when running in unit test mode
27+
/// </summary>
28+
/// <returns></returns>
29+
public static IAwaitable SwitchToMainThreadAsync()
30+
{
31+
return Guard.InUnitTestRunner() ?
32+
new AwaitableWrapper() :
33+
new AwaitableWrapper(ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync());
34+
}
35+
36+
/// <summary>
37+
/// Switch to a thread pool background thread if the current thread isn't one, otherwise does nothing
38+
/// Auto-disables switching when running in unit test mode
39+
/// </summary>
40+
/// <param name="scheduler"></param>
41+
/// <returns></returns>
42+
public static IAwaitable SwitchToPoolThreadAsync(TaskScheduler scheduler)
43+
{
44+
return Guard.InUnitTestRunner() ?
45+
new AwaitableWrapper() :
46+
new AwaitableWrapper(scheduler);
47+
}
48+
49+
class AwaitableWrapper : IAwaitable
50+
{
51+
Func<IAwaiter> getAwaiter;
52+
53+
public AwaitableWrapper()
54+
{
55+
getAwaiter = () => new AwaiterWrapper();
56+
}
57+
58+
public AwaitableWrapper(MainThreadAwaitable awaitable)
59+
{
60+
getAwaiter = () => new AwaiterWrapper(awaitable.GetAwaiter());
61+
}
62+
63+
public AwaitableWrapper(TaskScheduler scheduler)
64+
{
65+
getAwaiter = () => new AwaiterWrapper(new TaskSchedulerAwaiter(scheduler));
66+
}
67+
68+
public IAwaiter GetAwaiter() => getAwaiter();
69+
}
70+
71+
class AwaiterWrapper : IAwaiter
72+
{
73+
Func<bool> isCompleted;
74+
Action<Action> onCompleted;
75+
Action getResult;
76+
77+
public AwaiterWrapper()
78+
{
79+
isCompleted = () => true;
80+
onCompleted = c => c();
81+
getResult = () => {};
82+
}
83+
84+
public AwaiterWrapper(MainThreadAwaiter awaiter)
85+
{
86+
isCompleted = () => awaiter.IsCompleted;
87+
onCompleted = c => awaiter.OnCompleted(c);
88+
getResult = () => awaiter.GetResult();
89+
}
90+
91+
public AwaiterWrapper(TaskSchedulerAwaiter awaiter)
92+
{
93+
isCompleted = () => awaiter.IsCompleted;
94+
onCompleted = c => awaiter.OnCompleted(c);
95+
getResult = () => awaiter.GetResult();
96+
}
97+
98+
public bool IsCompleted => isCompleted();
99+
100+
public void OnCompleted(Action continuation) => onCompleted(continuation);
101+
102+
public void GetResult() => getResult();
103+
}
104+
}
105+
}

src/GitHub.Exports/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<package id="Microsoft.VisualStudio.Shell.Interop.9.0" version="9.0.30729" targetFramework="net461" />
1111
<package id="Microsoft.VisualStudio.TextManager.Interop" version="7.10.6070" targetFramework="net461" />
1212
<package id="Microsoft.VisualStudio.TextManager.Interop.8.0" version="8.0.50727" targetFramework="net461" />
13+
<package id="Microsoft.VisualStudio.Threading" version="14.1.131" targetFramework="net461" />
1314
<package id="SimpleJson" version="0.38.0" targetFramework="net461" />
1415
<package id="VSSDK.ComponentModelHost" version="12.0.4" targetFramework="net461" />
1516
<package id="VSSDK.IDE.12" version="12.0.4" targetFramework="net461" />

src/GitHub.Extensions/Guard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static void ArgumentInRange(int value, int minValue, int maxValue, string
9090
}
9191

9292
// Borrowed from Splat.
93-
static bool InUnitTestRunner()
93+
public static bool InUnitTestRunner()
9494
{
9595
return Splat.ModeDetector.InUnitTestRunner();
9696
}

src/GitHub.Extensions/TaskExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public static async Task Catch(this Task source, Action<Exception> handler = nul
3535
}
3636
}
3737

38-
public static void Forget(this Task task)
38+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "task")]
39+
public static void Forget([AllowNull] this Task task)
3940
{
4041
}
4142
}

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async Task InitializeMenus()
6262
{
6363
var menus = await GetServiceAsync(typeof(IMenuProvider)) as IMenuProvider;
6464

65-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
65+
await ThreadingHelper.SwitchToMainThreadAsync();
6666

6767
foreach (var menu in menus.Menus)
6868
serviceProvider.AddCommandHandler(menu.Guid, menu.CmdId, (s, e) => menu.Activate());

src/GitHub.VisualStudio/Services/UsageTracker.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
using System.Windows.Threading;
88
using GitHub.Models;
99
using GitHub.Settings;
10-
using Microsoft.VisualStudio.ComponentModelHost;
11-
using Microsoft.VisualStudio.Shell;
1210
using Task = System.Threading.Tasks.Task;
1311
using GitHub.Extensions;
1412
using System.Threading.Tasks;
13+
using GitHub.Helpers;
1514

1615
namespace GitHub.Services
1716
{
@@ -142,7 +141,7 @@ async Task Initialize()
142141
// improve the startup time of the extension.
143142
if (userSettings == null)
144143
{
145-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
144+
await ThreadingHelper.SwitchToMainThreadAsync();
146145

147146
client = uiProvider.GetService<IMetricsService>();
148147
connectionManager = uiProvider.GetService<IConnectionManager>();

src/UnitTests/GitHub.App/Services/RepositoryCreationServiceTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public class TheCreateRepositoryMethod : TestBaseClass
1616
[Fact]
1717
public void CreatesRepositoryOnlineViaApiAndThenClonesIt()
1818
{
19-
var provider = Substitutes.ServiceProvider;
19+
var cloneService = Substitutes.RepositoryCloneService;
20+
var provider = Substitutes.GetServiceProvider(cloneService);
21+
2022
var newRepository = new NewRepository("octokit.net");
2123
var repository = new TestRepository("octokit.net", "https://github.com/octokit/octokit.net");
2224
var account = Substitute.For<IAccount>();
@@ -25,7 +27,6 @@ public void CreatesRepositoryOnlineViaApiAndThenClonesIt()
2527
var apiClient = Substitute.For<IApiClient>();
2628
apiClient.CreateRepository(newRepository, "octokit", false)
2729
.Returns(Observable.Return(repository));
28-
var cloneService = provider.GetRepositoryCloneService();
2930
cloneService.CloneRepository("https://github.com/octokit/octokit.net", "octokit.net", @"c:\dev")
3031
.Returns(Observable.Return(Unit.Default));
3132
var creator = provider.GetRepositoryCreationService();

src/UnitTests/GitHub.App/ViewModels/RepositoryCloneViewModelTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,7 @@ public async Task UpdatesMetricsWhenRepositoryCloned()
358358

359359
vm.SelectedRepository = Substitute.For<IRepositoryModel>();
360360
await vm.CloneCommand.ExecuteAsync();
361-
362-
usageTracker.Received().IncrementCloneCount().Forget();
361+
Received.InOrder(async () => await usageTracker.IncrementCloneCount());
363362
}
364363
}
365364
}

src/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
</Reference>
7474
<Reference Include="Microsoft.VisualStudio.ComponentModelHost, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
7575
<HintPath>..\..\packages\VSSDK.ComponentModelHost.12.0.4\lib\net45\Microsoft.VisualStudio.ComponentModelHost.dll</HintPath>
76-
<Private>False</Private>
76+
<Private>True</Private>
7777
</Reference>
7878
<Reference Include="Microsoft.VisualStudio.CoreUtility, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
7979
<HintPath>..\..\packages\Microsoft.VisualStudio.CoreUtility.14.3.25407\lib\net45\Microsoft.VisualStudio.CoreUtility.dll</HintPath>

0 commit comments

Comments
 (0)