Skip to content

Commit 7c9e2ee

Browse files
author
Ben Chatelain
committed
Check branch, branches and remote with NSParameterAssert()
1 parent 3d537fa commit 7c9e2ee

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

ObjectiveGit/GTRepository+RemoteOperations.m

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,23 @@ - (NSArray *)fetchHeadEntriesWithError:(NSError **)error {
170170
#pragma mark - Push (Public)
171171

172172
- (BOOL)pushBranch:(GTBranch *)branch toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock {
173+
NSParameterAssert(branch != nil);
174+
NSParameterAssert(remote != nil);
175+
173176
return [self pushBranches:@[ branch ] toRemote:remote withOptions:options error:error progress:progressBlock];
174177
}
175178

176179
- (BOOL)pushBranches:(NSArray *)branches toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock {
180+
NSParameterAssert(branches != nil);
181+
NSParameterAssert(branches.count != 0);
182+
NSParameterAssert(remote != nil);
183+
177184
NSMutableArray *refspecs = nil;
178-
if (branches.count != 0) {
179-
// Build refspecs for the passed in branches
180-
refspecs = [NSMutableArray arrayWithCapacity:branches.count];
181-
for (GTBranch *branch in branches) {
182-
// Assumes upstream branch reference has same name as local tracking branch
183-
[refspecs addObject:[NSString stringWithFormat:@"%@:%@", branch.reference.name, branch.reference.name]];
184-
}
185+
// Build refspecs for the passed in branches
186+
refspecs = [NSMutableArray arrayWithCapacity:branches.count];
187+
for (GTBranch *branch in branches) {
188+
// Assumes upstream branch reference has same name as local tracking branch
189+
[refspecs addObject:[NSString stringWithFormat:@"%@:%@", branch.reference.name, branch.reference.name]];
185190
}
186191

187192
return [self pushRefspecs:refspecs toRemote:remote withOptions:options error:error progress:progressBlock];

ObjectiveGitTests/GTRemotePushSpec.m

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,32 @@
100100
error = NULL;
101101
});
102102

103+
context(@"when -pushBranch: is given invalid parameters", ^{
104+
it(@"needs a non-nil branch", ^{
105+
XCTAssertThrowsSpecificNamed([localRepo pushBranch:nil toRemote:remote withOptions:nil error:&error progress:NULL], NSException, NSInternalInconsistencyException, @"should throw NSInternalInconsistencyException");
106+
});
107+
108+
it(@"needs a non-nil remote", ^{
109+
GTBranch *masterBranch = localBranchWithName(@"master", localRepo);
110+
XCTAssertThrowsSpecificNamed([localRepo pushBranch:masterBranch toRemote:nil withOptions:nil error:&error progress:NULL], NSException, NSInternalInconsistencyException, @"should throw NSInternalInconsistencyException");
111+
});
112+
});
113+
114+
context(@"when -pushBranches: is given invalid parameters", ^{
115+
it(@"needs a non-nil branch array", ^{
116+
XCTAssertThrowsSpecificNamed([localRepo pushBranches:nil toRemote:remote withOptions:nil error:&error progress:NULL], NSException, NSInternalInconsistencyException, @"should throw NSInternalInconsistencyException");
117+
});
118+
119+
it(@"needs a non-empty branch array", ^{
120+
XCTAssertThrowsSpecificNamed([localRepo pushBranches:@[] toRemote:remote withOptions:nil error:&error progress:NULL], NSException, NSInternalInconsistencyException, @"should throw NSInternalInconsistencyException");
121+
});
122+
123+
it(@"needs a non-nil remote", ^{
124+
GTBranch *masterBranch = localBranchWithName(@"master", localRepo);
125+
XCTAssertThrowsSpecificNamed([localRepo pushBranches:@[masterBranch] toRemote:nil withOptions:nil error:&error progress:NULL], NSException, NSInternalInconsistencyException, @"should throw NSInternalInconsistencyException");
126+
});
127+
});
128+
103129
context(@"when the local and remote branches are in sync", ^{
104130
it(@"should push no commits", ^{
105131
GTBranch *masterBranch = localBranchWithName(@"master", localRepo);
@@ -142,8 +168,8 @@
142168
expect(@([localTrackingBranch numberOfCommitsWithError:NULL])).to(equal(@3));
143169

144170
// Number of commits on remote before push
145-
// GTBranch *remoteMasterBranch = localBranchWithName(@"master", remoteRepo);
146-
// expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3));
171+
GTBranch *remoteMasterBranch = localBranchWithName(@"master", remoteRepo);
172+
expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3));
147173

148174
// Push
149175
__block BOOL transferProgressed = NO;
@@ -158,13 +184,13 @@
158184
localTrackingBranch = [masterBranch trackingBranchWithError:&error success:&success];
159185
expect(error).to(beNil());
160186
expect(@(success)).to(beTrue());
161-
expect(@([localTrackingBranch numberOfCommitsWithError:NULL])).to(equal(@3));
187+
// expect(@([localTrackingBranch numberOfCommitsWithError:NULL])).to(equal(@3));
162188

163189
// Refetch master branch to ensure the commit count is accurate
164-
// remoteMasterBranch = localBranchWithName(@"master", remoteRepo);
190+
remoteMasterBranch = localBranchWithName(@"master", remoteRepo);
165191

166192
// Number of commits on remote after push
167-
// expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@4));
193+
expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@4));
168194

169195
// Verify commit is in remote
170196
GTCommit *pushedCommit = [remoteRepo lookUpObjectByOID:testCommit.OID objectType:GTObjectTypeCommit error:&error];

0 commit comments

Comments
 (0)