diff --git a/Code/CoreData/RKManagedObjectStore.m b/Code/CoreData/RKManagedObjectStore.m index 186a35b5..bfb196e2 100644 --- a/Code/CoreData/RKManagedObjectStore.m +++ b/Code/CoreData/RKManagedObjectStore.m @@ -113,8 +113,11 @@ static RKManagedObjectStore *defaultObjectStore = nil; _storeFilename = [storeFilename retain]; if (nilOrDirectoryPath == nil) { + // If initializing into Application Data directory, ensure the directory exists nilOrDirectoryPath = [RKDirectory applicationDataDirectory]; + [RKDirectory ensureDirectoryExistsAtPath:nilOrDirectoryPath error:nil]; } else { + // If path given, caller is responsible for directory's existence BOOL isDir; NSAssert1([[NSFileManager defaultManager] fileExistsAtPath:nilOrDirectoryPath isDirectory:&isDir] && isDir == YES, @"Specified storage directory exists", nilOrDirectoryPath); } diff --git a/Tests/Logic/CoreData/RKManagedObjectStoreTest.m b/Tests/Logic/CoreData/RKManagedObjectStoreTest.m index 28b369e9..6424ab5f 100644 --- a/Tests/Logic/CoreData/RKManagedObjectStoreTest.m +++ b/Tests/Logic/CoreData/RKManagedObjectStoreTest.m @@ -20,6 +20,7 @@ #import "RKTestEnvironment.h" #import "RKHuman.h" +#import "RKDirectory.h" @interface RKManagedObjectStoreTest : RKTestCase @@ -34,4 +35,24 @@ assertThat([context managedObjectStore], is(equalTo(store))); } +- (void)testCreationOfStoreInSpecificDirectoryRaisesIfDoesNotExist +{ + NSString *path = [[RKDirectory applicationDataDirectory] stringByAppendingPathComponent:@"/NonexistantSubdirectory"]; + BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path]; + assertThatBool(exists, is(equalToBool(NO))); + STAssertThrows([RKManagedObjectStore objectStoreWithStoreFilename:@"Whatever.sqlite" inDirectory:path usingSeedDatabaseName:nil managedObjectModel:nil delegate:nil], nil); +} + +- (void)testCreationOfStoryInApplicationDirectoryCreatesIfNonExistant +{ + // On OS X, the application directory is not created for you + NSString *path = [RKDirectory applicationDataDirectory]; + NSError *error = nil; + [[NSFileManager defaultManager] removeItemAtPath:path error:&error]; + assertThat(error, is(nilValue())); + STAssertNoThrow([RKManagedObjectStore objectStoreWithStoreFilename:@"Whatever.sqlite" inDirectory:nil usingSeedDatabaseName:nil managedObjectModel:nil delegate:nil], nil); + BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path]; + assertThatBool(exists, is(equalToBool(YES))); +} + @end