6

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

Max Seelemann
  • 9,344
  • 4
  • 34
  • 40
  • There seems to be no public way. I've filed a radar for now: [rdar://12196293](http://openradar.appspot.com/12196293). Any *hack suggestions* would still be appreciated. – Max Seelemann Aug 29 '12 at 08:53

1 Answers1

3

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 ] ;
nielsbot
  • 15,922
  • 4
  • 48
  • 73
  • Thanks! This is an approach I have thought about already. This would, however, still not cause the right methods (i.e. `didGainVersion`) to be called, and there would not really be a conflict, just simulated through one API. But in the end it could be work (i.e. by adding faked Version objects via OCMock). – Max Seelemann Sep 04 '12 at 06:39
  • You said hacks were ok :) not sure this is strong enough for your testing purposes. Seems if you wrote a bit more code and/or used a separate pted machine you could automatically create actually conflicting versions? – nielsbot Sep 04 '12 at 16:49
  • A DTS engineer replied to one of my emails saying that currently the only public way involves more than one machine. I could use a VM, or just use this hack. Thanks! – Max Seelemann Sep 06 '12 at 18:58