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

Commit 95ef4f2

Browse files
committed
Make sure we never blow up in release mode
Tests in debug mode will signal invalid method calls. We might need to run tests in debug and release mode in the buildbot so we're sure we catch all the problems.
1 parent 471b6cb commit 95ef4f2

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

src/GitHub.App/Controllers/UIController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ void End(bool success)
156156
completion?.OnNext(success);
157157
completion?.OnCompleted();
158158
transition.OnCompleted();
159-
completion = null;
160159
}
161160

162161
void RunView(UIViewType viewType)

src/GitHub.VisualStudio/Services/UIProvider.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public class UIProvider : IUIProvider, IDisposable
3131
CompositionContainer tempContainer;
3232
readonly Dictionary<string, ComposablePart> tempParts;
3333
ExportLifetimeContext<IUIController> currentUIFlow;
34-
IUIController currentUI;
3534
readonly Version currentVersion;
3635
bool initializingLogging = false;
3736

@@ -202,9 +201,9 @@ public IObservable<UserControl> SetupUI(UIControllerFlow controllerFlow, [AllowN
202201

203202
var factory = GetService<IExportFactoryProvider>();
204203
currentUIFlow = factory.UIControllerFactory.CreateExport();
205-
currentUI = currentUIFlow.Value;
206204
var disposable = currentUIFlow;
207-
var creation = currentUI.SelectFlow(controllerFlow);
205+
var ui = currentUIFlow.Value;
206+
var creation = ui.SelectFlow(controllerFlow);
208207
windowController = new UI.WindowController(creation);
209208
windowController.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
210209
windowController.Closed += StopUIFlowWhenWindowIsClosedByUser;
@@ -217,14 +216,21 @@ public IObservable<UserControl> SetupUI(UIControllerFlow controllerFlow, [AllowN
217216
else
218217
StopUI();
219218
});
220-
currentUI.Start(connection);
219+
ui.Start(connection);
221220
return creation;
222221
}
223222

224223
public IObservable<bool> ListenToCompletionState()
225224
{
226-
Debug.Assert(currentUI != null, "Cannot call ListenToCompletionState without calling SetupUI first");
227-
return currentUI?.ListenToCompletionState();
225+
var ui = currentUIFlow?.Value;
226+
if (ui == null)
227+
{
228+
log.Error("UIProvider:ListenToCompletionState:Cannot call ListenToCompletionState without calling SetupUI first");
229+
#if DEBUG
230+
throw new InvalidOperationException("Cannot call ListenToCompletionState without calling SetupUI first");
231+
#endif
232+
}
233+
return ui?.ListenToCompletionState() ?? Observable.Return(false);
228234
}
229235

230236
public void RunUI()
@@ -279,7 +285,6 @@ public void StopUI()
279285
}
280286

281287
StopUI(currentUIFlow);
282-
currentUI = null;
283288
currentUIFlow = null;
284289
}
285290

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.ComponentModel.Composition;
3+
using System.Reactive.Linq;
4+
using System.Windows.Controls;
5+
using GitHub.Controllers;
6+
using GitHub.Models;
7+
using GitHub.Services;
8+
using GitHub.UI;
9+
using NSubstitute;
10+
using Xunit;
11+
using UnitTests;
12+
using GitHub.ViewModels;
13+
using ReactiveUI;
14+
using System.Collections.Generic;
15+
using GitHub.Authentication;
16+
using System.Collections.ObjectModel;
17+
using GitHub.VisualStudio;
18+
19+
public class UIProviderTests : TestBaseClass
20+
{
21+
[Fact]
22+
public void ListenToCompletionDoesNotThrowInRelease()
23+
{
24+
var provider = Substitutes.GetFullyMockedServiceProvider();
25+
26+
using (var p = new UIProvider(provider))
27+
{
28+
#if DEBUG
29+
Assert.ThrowsAny<InvalidOperationException>(() =>
30+
{
31+
#endif
32+
p.ListenToCompletionState();
33+
#if DEBUG
34+
});
35+
#endif
36+
}
37+
}
38+
}

src/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
<Compile Include="GitHub.Api\SimpleApiClientTests.cs" />
140140
<Compile Include="GitHub.App\Caches\CredentialCacheTests.cs" />
141141
<Compile Include="GitHub.App\Caches\ImageCacheTests.cs" />
142+
<Compile Include="GitHub.App\Controllers\UIProviderTests.cs" />
142143
<Compile Include="GitHub.App\Models\ModelServiceTests.cs" />
143144
<Compile Include="GitHub.App\Controllers\UIControllerTests.cs" />
144145
<Compile Include="GitHub.App\Models\PullRequestModelTests.cs" />

0 commit comments

Comments
 (0)