Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically creating conflicting NSFileVersions

I've written a custom conflict handling routine that automatically kicks in to resolve conflicting versions of NSFileVersion. Now I want to write unit tests for in order to make sure it works. Here is the question:

How do I cause/create conflicting versions within a unit test?

This essentiall boils down to: how do I cause a conflict without manually doing it through iCloud? Since this is for testing purposes only, I don't care abput using private API or hacking directly into the system -- as long as the result is a conflict reported from NSFileVersion's +unresolvedConflictVersionsOfItemAtURL. Any advice would be highly appreciated!

Max

like image 667
Max Seelemann Avatar asked Aug 26 '12 19:08

Max Seelemann


1 Answers

You could patch +unresolvedConflictVersionsOfItemAtURL with your own version that returns an array of conflicting versions:

#import <objc/runtime.h>

static IMP __originalUnresolvedConflictVersionIMP = NULL ;
static NSArray * MyNSFileVersionUnresolvedConflictVersions( id self, SEL _cmd, NSURL * url )
{
    // this code just calls the original implementation... 
    // You can return an array of conflicting NSFileVersion objects instead...

    NSLog(@"%s called\n", __PRETTY_FUNCTION__ ) ;
    return (*__originalUnresolvedConflictVersionIMP)( self, _cmd, url ) ;
}


@implementation NSFileVersion (Test)

+(void)load
{
    __originalUnresolvedConflictVersionIMP = class_replaceMethod( objc_getMetaClass( "NSFileVersion") , @selector( unresolvedConflictVersionsOfItemAtURL: ), (IMP)MyNSFileVersionUnresolvedConflictVersions, "@@:@" ) ;
}

@end

Is that enough to go on? I might first try this in my replacement "method":

return [ [ self otherVersionsOfItemAtURL:url ] lastObject ] ;
like image 109
nielsbot Avatar answered Dec 10 '22 22:12

nielsbot