Skip to content

Commit ae37962

Browse files
committed
Fix the recents list opening nothing and saying nothing (issue #603)
Picking a repository in the Open Recent window handed the document controller an empty completion handler, so a repository that could not be opened took the window down and put nothing in its place, while the same repository opened from the File menu says what went wrong. * Report the error the document controller hands back, the way the application delegate already does for files opened from the Finder * Open the selection in one place rather than once per key and once per double click
1 parent b79eaef commit ae37962

4 files changed

Lines changed: 114 additions & 14 deletions

File tree

‎Classes/Controllers/OpenRecentController.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
- (void)hide;
2222
- (void)show;
23+
- (void)openSelectedResult;
24+
- (void)reportFailureToOpen:(NSError *)error;
2325

2426
- (IBAction)doSearch:(id)sender;
2527
- (IBAction)changeSelection:(id)sender;

‎Classes/Controllers/OpenRecentController.m‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,36 @@ - (void)awakeFromNib
7777
- (IBAction)tableDoubleClick:(id)sender
7878
{
7979
[self changeSelection:self];
80-
if (selectedResult != nil) {
81-
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:selectedResult
82-
display:YES
83-
completionHandler:^(NSDocument *_Nullable document, BOOL documentWasAlreadyOpen, NSError *_Nullable error){
84-
85-
}];
86-
}
80+
[self openSelectedResult];
8781
[self hide];
8882
}
8983

84+
- (void)openSelectedResult
85+
{
86+
if (selectedResult == nil)
87+
return;
88+
89+
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:selectedResult
90+
display:YES
91+
completionHandler:^(NSDocument *_Nullable document, BOOL documentWasAlreadyOpen, NSError *_Nullable error) {
92+
if (document == nil)
93+
[self reportFailureToOpen:error];
94+
}];
95+
}
96+
97+
- (void)reportFailureToOpen:(NSError *)error
98+
{
99+
if (error == nil)
100+
return;
101+
102+
[[NSDocumentController sharedDocumentController] presentError:error];
103+
}
104+
90105
- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
91106
{
92107
BOOL result = NO;
93108
if (commandSelector == @selector(insertNewline:)) {
94-
if (selectedResult != nil) {
95-
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:selectedResult
96-
display:YES
97-
completionHandler:^(NSDocument *_Nullable document, BOOL documentWasAlreadyOpen, NSError *_Nullable error){
98-
99-
}];
100-
}
109+
[self openSelectedResult];
101110
[self hide];
102111
// [searchWindow makeKeyAndOrderFront: nil];
103112
result = YES;

‎GitX.xcodeproj/project.pbxproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
551BF176112F3F4B00265053 /* gitx_askpasswd in Resources */ = {isa = PBXBuildFile; fileRef = 551BF111112F371800265053 /* gitx_askpasswd */; };
155155
63E155858B2CC783EE10F1D7 /* PBGitRevListRaceConditionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 689D57B4A78CEF3B32EB0044 /* PBGitRevListRaceConditionTests.m */; };
156156
7A1C4E2B90D3F5A6C1B80011 /* PBGitRepositoryFetchArgumentsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */; };
157+
8A1C64D2B3E7492FA0561D40 /* OpenRecentControllerErrorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8A1C64D2B3E7492FA0561D41 /* OpenRecentControllerErrorTests.m */; };
157158
3D8A5F14C7E9420B96741C80 /* PBGitRepositoryIgnoreTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3D8A5F14C7E9420B96741C81 /* PBGitRepositoryIgnoreTests.m */; };
158159
6B2E70F3A19C4D5E8A730C90 /* GLFileViewLayoutTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B2E70F3A19C4D5E8A730C91 /* GLFileViewLayoutTests.m */; };
159160
63E155858B2CC783EE10F1D8 /* PBGitIndexReconcileTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 689D57B4A78CEF3B32EB0045 /* PBGitIndexReconcileTests.m */; };
@@ -700,6 +701,7 @@
700701
643952761603EF9B00BB7AFF /* PBSourceViewGitSubmoduleItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBSourceViewGitSubmoduleItem.m; sourceTree = "<group>"; };
701702
689D57B4A78CEF3B32EB0044 /* PBGitRevListRaceConditionTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitRevListRaceConditionTests.m; path = GitXTests/PBGitRevListRaceConditionTests.m; sourceTree = "<group>"; };
702703
7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitRepositoryFetchArgumentsTests.m; path = GitXTests/PBGitRepositoryFetchArgumentsTests.m; sourceTree = "<group>"; };
704+
8A1C64D2B3E7492FA0561D41 /* OpenRecentControllerErrorTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = OpenRecentControllerErrorTests.m; path = GitXTests/OpenRecentControllerErrorTests.m; sourceTree = "<group>"; };
703705
3D8A5F14C7E9420B96741C81 /* PBGitRepositoryIgnoreTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitRepositoryIgnoreTests.m; path = GitXTests/PBGitRepositoryIgnoreTests.m; sourceTree = "<group>"; };
704706
6B2E70F3A19C4D5E8A730C91 /* GLFileViewLayoutTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = GLFileViewLayoutTests.m; path = GitXTests/GLFileViewLayoutTests.m; sourceTree = "<group>"; };
705707
689D57B4A78CEF3B32EB0045 /* PBGitIndexReconcileTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitIndexReconcileTests.m; path = GitXTests/PBGitIndexReconcileTests.m; sourceTree = "<group>"; };
@@ -832,6 +834,7 @@
832834
children = (
833835
689D57B4A78CEF3B32EB0044 /* PBGitRevListRaceConditionTests.m */,
834836
7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */,
837+
8A1C64D2B3E7492FA0561D41 /* OpenRecentControllerErrorTests.m */,
835838
3D8A5F14C7E9420B96741C81 /* PBGitRepositoryIgnoreTests.m */,
836839
6B2E70F3A19C4D5E8A730C91 /* GLFileViewLayoutTests.m */,
837840
689D57B4A78CEF3B32EB0045 /* PBGitIndexReconcileTests.m */,
@@ -1779,6 +1782,7 @@
17791782
files = (
17801783
63E155858B2CC783EE10F1D7 /* PBGitRevListRaceConditionTests.m in Sources */,
17811784
7A1C4E2B90D3F5A6C1B80011 /* PBGitRepositoryFetchArgumentsTests.m in Sources */,
1785+
8A1C64D2B3E7492FA0561D40 /* OpenRecentControllerErrorTests.m in Sources */,
17821786
3D8A5F14C7E9420B96741C80 /* PBGitRepositoryIgnoreTests.m in Sources */,
17831787
6B2E70F3A19C4D5E8A730C90 /* GLFileViewLayoutTests.m in Sources */,
17841788
63E155858B2CC783EE10F1D8 /* PBGitIndexReconcileTests.m in Sources */,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// OpenRecentControllerErrorTests.m
3+
// GitXTests
4+
//
5+
6+
#import <XCTest/XCTest.h>
7+
#import "OpenRecentController.h"
8+
9+
// Takes the selection a test hands it, and records the failure instead of
10+
// putting an alert on screen.
11+
@interface PBRecordingOpenRecentController : OpenRecentController
12+
@property (nonatomic, strong) NSError *reportedError;
13+
@property (nonatomic, assign) NSUInteger reportCount;
14+
- (void)useSelectedResult:(NSURL *)url;
15+
@end
16+
17+
@implementation PBRecordingOpenRecentController
18+
19+
- (void)useSelectedResult:(NSURL *)url
20+
{
21+
selectedResult = url;
22+
}
23+
24+
- (void)reportFailureToOpen:(NSError *)error
25+
{
26+
self.reportCount++;
27+
self.reportedError = error;
28+
}
29+
30+
@end
31+
32+
@interface OpenRecentControllerErrorTests : XCTestCase
33+
@property (nonatomic, strong) PBRecordingOpenRecentController *controller;
34+
@property (nonatomic, strong) NSURL *directory;
35+
@end
36+
37+
@implementation OpenRecentControllerErrorTests
38+
39+
- (void)setUp
40+
{
41+
[super setUp];
42+
43+
self.controller = [[PBRecordingOpenRecentController alloc] init];
44+
45+
self.directory = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSUUID UUID] UUIDString]]];
46+
[[NSFileManager defaultManager] createDirectoryAtURL:self.directory
47+
withIntermediateDirectories:YES
48+
attributes:nil
49+
error:NULL];
50+
}
51+
52+
- (void)tearDown
53+
{
54+
[[NSFileManager defaultManager] removeItemAtURL:self.directory error:NULL];
55+
56+
[super tearDown];
57+
}
58+
59+
- (void)waitForReportUpTo:(NSTimeInterval)seconds
60+
{
61+
NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:seconds];
62+
while (self.controller.reportCount == 0 && [deadline timeIntervalSinceNow] > 0)
63+
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
64+
}
65+
66+
- (void)testAnEntryThatCannotBeOpenedIsReported
67+
{
68+
[self.controller useSelectedResult:self.directory];
69+
70+
[self.controller openSelectedResult];
71+
[self waitForReportUpTo:10];
72+
73+
XCTAssertEqual(self.controller.reportCount, 1u, @"the recents list is the only thing the user can see this fail in");
74+
XCTAssertNotNil(self.controller.reportedError, @"there is nothing to put on screen without the error");
75+
}
76+
77+
- (void)testNothingIsReportedWithoutASelection
78+
{
79+
[self.controller openSelectedResult];
80+
[self waitForReportUpTo:0.5];
81+
82+
XCTAssertEqual(self.controller.reportCount, 0u);
83+
}
84+
85+
@end

0 commit comments

Comments
 (0)