Skip to content

Commit 307936c

Browse files
author
Ben Chatelain
committed
Further test revisions
Created convenience localBranchWithName block. Make branch references local to examples. Also fixed case where afterEach wasn't deleting localRepo, which allowed removal of redundant removeItemAtURL call. Added pending dual branch example.
1 parent c947b80 commit 307936c

File tree

1 file changed

+74
-63
lines changed

1 file changed

+74
-63
lines changed

ObjectiveGitTests/GTRemotePushSpec.m

Lines changed: 74 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#import "QuickSpec+GTFixtures.h"
1414

1515
// Helper to quickly create commits
16-
GTCommit *(^createCommitInRepository)(NSString *, NSData *, NSString *, GTRepository *) = ^(NSString *message, NSData *fileData, NSString *fileName, GTRepository *repo) {
16+
GTCommit *(^createCommitInRepository)(NSString *, NSData *, NSString *, GTRepository *) = ^ GTCommit * (NSString *message, NSData *fileData, NSString *fileName, GTRepository *repo) {
1717
GTTreeBuilder *treeBuilder = [[GTTreeBuilder alloc] initWithTree:nil error:nil];
1818
[treeBuilder addEntryWithData:fileData fileName:fileName fileMode:GTFileModeBlob error:nil];
1919

@@ -26,12 +26,22 @@
2626
[commitEnum pushSHA:[headReference targetSHA] error:nil];
2727
GTCommit *parent = [commitEnum nextObject];
2828

29-
GTCommit *testCommit = [repo createCommitWithTree:testTree message:message parents:@[parent] updatingReferenceNamed:headReference.name error:nil];
29+
GTCommit *testCommit = [repo createCommitWithTree:testTree message:message parents:@[ parent ] updatingReferenceNamed:headReference.name error:nil];
3030
expect(testCommit).notTo(beNil());
3131

3232
return testCommit;
3333
};
3434

35+
GTBranch *(^localBranchWithName)(NSString *, GTRepository *) = ^ GTBranch * (NSString *branchName, GTRepository *repo) {
36+
NSString *reference = [GTBranch.localNamePrefix stringByAppendingString:branchName];
37+
NSArray *branches = [repo branchesWithPrefix:reference error:NULL];
38+
expect(branches).notTo(beNil());
39+
expect(@(branches.count)).to(equal(@1));
40+
expect(((GTBranch *)branches[0]).shortName).to(equal(branchName));
41+
42+
return branches[0];
43+
};
44+
3545
#pragma mark - GTRemotePushSpec
3646

3747
QuickSpecBegin(GTRemotePushSpec)
@@ -43,9 +53,7 @@
4353
__block GTRemote *remote;
4454
__block NSURL *remoteRepoURL;
4555
__block NSURL *localRepoURL;
46-
__block GTBranch *masterBranch;
47-
__block GTBranch *remoteMasterBranch;
48-
__block NSError *error = nil;
56+
__block NSError *error;
4957

5058
beforeEach(^{
5159
// This repo is not really "bare"
@@ -64,20 +72,9 @@
6472
expect(remoteRepo).notTo(beNil());
6573
expect(@(remoteRepo.isBare)).to(beTruthy()); // that's better
6674

67-
// Get the remote master branch
68-
NSArray *remoteBranches = [remoteRepo localBranchesWithError:&error];
69-
expect(error).to(beNil());
70-
expect(remoteBranches).notTo(beNil());
71-
expect(@(remoteBranches.count)).to(beGreaterThanOrEqualTo(@1));
72-
remoteMasterBranch = remoteBranches[0];
73-
expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3));
74-
75-
NSURL *localRepoURL = [remoteRepoURL.URLByDeletingLastPathComponent URLByAppendingPathComponent:@"local_push_repo"];
75+
localRepoURL = [remoteRepoURL.URLByDeletingLastPathComponent URLByAppendingPathComponent:@"local_push_repo"];
7676
expect(localRepoURL).notTo(beNil());
7777

78-
// Ensure repo destination is clear before clone
79-
[NSFileManager.defaultManager removeItemAtURL:localRepoURL error:NULL];
80-
8178
// Local clone for testing pushes
8279
localRepo = [GTRepository cloneFromURL:remoteRepoURL toWorkingDirectory:localRepoURL options:nil error:&error transferProgressBlock:NULL checkoutProgressBlock:NULL];
8380

@@ -92,24 +89,20 @@
9289

9390
remote = configuration.remotes[0];
9491
expect(remote.name).to(equal(@"origin"));
95-
96-
NSArray *branches = [localRepo localBranchesWithError:&error];
97-
expect(error).to(beNil());
98-
expect(branches).notTo(beNil());
99-
expect(@(branches.count)).to(beGreaterThanOrEqualTo(@1));
100-
101-
masterBranch = branches[0];
102-
expect(masterBranch.shortName).to(equal(@"master"));
103-
expect(@([masterBranch numberOfCommitsWithError:NULL])).to(equal(@3));
10492
});
10593

10694
afterEach(^{
10795
[NSFileManager.defaultManager removeItemAtURL:remoteRepoURL error:NULL];
10896
[NSFileManager.defaultManager removeItemAtURL:localRepoURL error:NULL];
97+
error = NULL;
10998
});
11099

111100
context(@"when the local and remote branches are in sync", ^{
112101
it(@"should push no commits", ^{
102+
GTBranch *masterBranch = localBranchWithName(@"master", localRepo);
103+
expect(@([masterBranch numberOfCommitsWithError:NULL])).to(equal(@3));
104+
105+
GTBranch *remoteMasterBranch = localBranchWithName(@"master", remoteRepo);
113106
expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3));
114107

115108
// Push
@@ -121,55 +114,73 @@
121114
expect(@(result)).to(beTruthy());
122115
expect(@(transferProgressed)).to(beFalse()); // Local transport doesn't currently call progress callbacks
123116

124-
// Same number of commits after push
117+
// Same number of commits after push, refresh branch first
118+
remoteMasterBranch = localBranchWithName(@"master", remoteRepo);
125119
expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3));
126120
});
127121
});
128122

129-
context(@"when there is a new local commit", ^{
130-
it(@"should push one commit", ^{
131-
// Create a new commit in the master repo
132-
NSString *testData = @"Test";
133-
NSString *fileName = @"test.txt";
134-
GTCommit *testCommit = createCommitInRepository(@"Test commit", [testData dataUsingEncoding:NSUTF8StringEncoding], fileName, localRepo);
135-
expect(testCommit).notTo(beNil());
123+
it(@"can push one commit", ^{
124+
// Create a new commit in the master repo
125+
NSString *testData = @"Test";
126+
NSString *fileName = @"test.txt";
127+
GTCommit *testCommit = createCommitInRepository(@"Test commit", [testData dataUsingEncoding:NSUTF8StringEncoding], fileName, localRepo);
128+
expect(testCommit).notTo(beNil());
136129

137-
// Refetch master branch to ensure the commit count is accurate
138-
masterBranch = [localRepo localBranchesWithError:NULL][0];
139-
expect(@([masterBranch numberOfCommitsWithError:NULL])).to(equal(@4));
130+
// Refetch master branch to ensure the commit count is accurate
131+
GTBranch *masterBranch = localBranchWithName(@"master", localRepo);
132+
expect(@([masterBranch numberOfCommitsWithError:NULL])).to(equal(@4));
140133

141-
// Number of commits on remote before push
142-
expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3));
134+
// Number of commits on remote before push
135+
GTBranch *remoteMasterBranch = localBranchWithName(@"master", remoteRepo);
136+
expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3));
143137

144-
// Push
145-
__block BOOL transferProgressed = NO;
146-
BOOL result = [localRepo pushBranch:masterBranch toRemote:remote withOptions:nil error:&error progress:^(unsigned int current, unsigned int total, size_t bytes, BOOL *stop) {
147-
transferProgressed = YES;
148-
}];
149-
expect(error).to(beNil());
150-
expect(@(result)).to(beTruthy());
151-
expect(@(transferProgressed)).to(beFalse()); // Local transport doesn't currently call progress callbacks
138+
// Push
139+
__block BOOL transferProgressed = NO;
140+
BOOL result = [localRepo pushBranch:masterBranch toRemote:remote withOptions:nil error:&error progress:^(unsigned int current, unsigned int total, size_t bytes, BOOL *stop) {
141+
transferProgressed = YES;
142+
}];
143+
expect(error).to(beNil());
144+
expect(@(result)).to(beTruthy());
145+
expect(@(transferProgressed)).to(beFalse()); // Local transport doesn't currently call progress callbacks
152146

153-
// Refetch master branch to ensure the commit count is accurate
154-
remoteMasterBranch = [remoteRepo localBranchesWithError:NULL][0];
147+
// Refetch master branch to ensure the commit count is accurate
148+
remoteMasterBranch = localBranchWithName(@"master", remoteRepo);
155149

156-
// Number of commits on remote after push
157-
expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@4));
150+
// Number of commits on remote after push
151+
expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@4));
158152

159-
// Verify commit is in remote
160-
GTCommit *pushedCommit = [remoteRepo lookUpObjectByOID:testCommit.OID objectType:GTObjectTypeCommit error:&error];
161-
expect(error).to(beNil());
162-
expect(pushedCommit).notTo(beNil());
163-
expect(pushedCommit.OID).to(equal(testCommit.OID));
153+
// Verify commit is in remote
154+
GTCommit *pushedCommit = [remoteRepo lookUpObjectByOID:testCommit.OID objectType:GTObjectTypeCommit error:&error];
155+
expect(error).to(beNil());
156+
expect(pushedCommit).notTo(beNil());
157+
expect(pushedCommit.OID).to(equal(testCommit.OID));
164158

165-
GTTreeEntry *entry = [[pushedCommit tree] entryWithName:fileName];
166-
expect(entry).notTo(beNil());
159+
GTTreeEntry *entry = [[pushedCommit tree] entryWithName:fileName];
160+
expect(entry).notTo(beNil());
167161

168-
GTBlob *fileData = (GTBlob *)[entry GTObject:&error];
169-
expect(error).to(beNil());
170-
expect(fileData).notTo(beNil());
171-
expect(fileData.content).to(equal(testData));
172-
});
162+
GTBlob *fileData = (GTBlob *)[entry GTObject:&error];
163+
expect(error).to(beNil());
164+
expect(fileData).notTo(beNil());
165+
expect(fileData.content).to(equal(testData));
166+
});
167+
168+
pending(@"can push two branches", ^{
169+
GTBranch *masterBranch = localBranchWithName(@"master", localRepo);
170+
GTBranch *remoteMasterBranch = localBranchWithName(@"master", remoteRepo);
171+
expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3));
172+
173+
// Push
174+
__block BOOL transferProgressed = NO;
175+
BOOL result = [localRepo pushBranch:masterBranch toRemote:remote withOptions:nil error:&error progress:^(unsigned int current, unsigned int total, size_t bytes, BOOL *stop) {
176+
transferProgressed = YES;
177+
}];
178+
expect(error).to(beNil());
179+
expect(@(result)).to(beTruthy());
180+
expect(@(transferProgressed)).to(beFalse()); // Local transport doesn't currently call progress callbacks
181+
182+
// Same number of commits after push
183+
expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3));
173184
});
174185
});
175186

0 commit comments

Comments
 (0)