Skip to content

Commit 0e2ba10

Browse files
committed
Add tests for checkout+notify
1 parent 0566639 commit 0e2ba10

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

ObjectiveGitTests/GTRepositorySpec.m

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
QuickSpecBegin(GTRepositorySpec)
1616

17+
static NSString * const mainFile = @"main.m";
18+
static NSString * const readmeFile = @"README1.txt";
19+
20+
1721
__block GTRepository *repository;
1822

1923
beforeEach(^{
@@ -376,6 +380,65 @@
376380
});
377381
});
378382

383+
describe(@"-checkout:strategy:notifyFlags:error:notifyBlock:progressBlock:", ^{
384+
it(@"should notify dirty file and stop ref checkout", ^{
385+
NSError *error = nil;
386+
GTReference *ref = [repository lookUpReferenceWithName:@"refs/heads/other-branch" error:&error];
387+
expect(ref).notTo(beNil());
388+
expect(error.localizedDescription).to(beNil());
389+
// Write something to a file in the repo
390+
BOOL writeResult = [@"Replacement data in main.m\n" writeToURL:[repository.fileURL URLByAppendingPathComponent:mainFile] atomically:YES encoding:NSUTF8StringEncoding error:&error];
391+
expect(@(writeResult)).to(beTruthy());
392+
// Now attempt to checkout the other-branch
393+
__block NSUInteger notifyCount = 0;
394+
__block BOOL mainFileDirty = NO;
395+
int (^notifyBlock)(GTCheckoutNotifyFlags, NSString *, GTDiffFile *, GTDiffFile *, GTDiffFile *);
396+
notifyBlock = ^(GTCheckoutNotifyFlags why, NSString *path, GTDiffFile *baseline, GTDiffFile *target, GTDiffFile *workdir) {
397+
notifyCount++;
398+
if([path isEqualToString:mainFile] && (why & GTCheckoutNotifyDirty)) {
399+
mainFileDirty = YES;
400+
return GIT_EUSER;
401+
} else {
402+
return 0;
403+
}
404+
};
405+
406+
BOOL result = [repository checkoutReference:ref strategy:GTCheckoutStrategySafe notifyFlags:GTCheckoutNotifyDirty error:&error progressBlock:nil notifyBlock:notifyBlock];
407+
expect(@(notifyCount)).to(equal(@(1)));
408+
expect(@(mainFileDirty)).to(beTruthy());
409+
expect(@(result)).to(beFalsy());
410+
expect(@(error.code)).to(equal(@(GIT_EUSER)));
411+
});
412+
413+
it(@"should notify dirty file and stop commit checkout", ^{
414+
NSError *error = nil;
415+
GTCommit *commit = [repository lookUpObjectBySHA:@"1d69f3c0aeaf0d62e25591987b93b8ffc53abd77" objectType:GTObjectTypeCommit error:&error];
416+
expect(commit).notTo(beNil());
417+
expect(error.localizedDescription).to(beNil());
418+
BOOL writeResult = [@"Replacement data in README\n" writeToURL:[repository.fileURL URLByAppendingPathComponent:readmeFile] atomically:YES encoding:NSUTF8StringEncoding error:&error];
419+
expect(@(writeResult)).to(beTruthy());
420+
// Now attempt to checkout the other-branch
421+
__block NSUInteger notifyCount = 0;
422+
__block BOOL readmeFileDirty = NO;
423+
int (^notifyBlock)(GTCheckoutNotifyFlags, NSString *, GTDiffFile *, GTDiffFile *, GTDiffFile *);
424+
notifyBlock = ^(GTCheckoutNotifyFlags why, NSString *path, GTDiffFile *baseline, GTDiffFile *target, GTDiffFile *workdir) {
425+
notifyCount++;
426+
if([path isEqualToString:readmeFile] && (why & GTCheckoutNotifyDirty)) {
427+
readmeFileDirty = YES;
428+
return GIT_EUSER;
429+
} else {
430+
return 0;
431+
}
432+
};
433+
434+
BOOL result = [repository checkoutCommit:commit strategy:GTCheckoutStrategySafe notifyFlags:GTCheckoutNotifyDirty error:&error progressBlock:nil notifyBlock:notifyBlock];
435+
expect(@(notifyCount)).to(equal(@(1)));
436+
expect(@(readmeFileDirty)).to(beTruthy());
437+
expect(@(result)).to(beFalsy());
438+
expect(@(error.code)).to(equal(@(GIT_EUSER)));
439+
});
440+
});
441+
379442
describe(@"-remoteNamesWithError:", ^{
380443
it(@"allows access to remote names", ^{
381444
NSError *error = nil;

0 commit comments

Comments
 (0)