Permalink
Browse files

Ensure creation of Application Data directory when initializing Manag…

…ed Object Store. fixes #657
  • Loading branch information...
blakewatters committed May 19, 2012
1 parent 30f1198 commit f585d34f9188725b812a4e0179e38e6e8bb4cbd2
Showing with 24 additions and 0 deletions.
  1. +3 −0 Code/CoreData/RKManagedObjectStore.m
  2. +21 −0 Tests/Logic/CoreData/RKManagedObjectStoreTest.m
@@ -113,8 +113,11 @@ - (id)initWithStoreFilename:(NSString *)storeFilename inDirectory:(NSString *)ni
_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);
}
@@ -20,6 +20,7 @@
#import "RKTestEnvironment.h"
#import "RKHuman.h"
+#import "RKDirectory.h"
@interface RKManagedObjectStoreTest : RKTestCase
@@ -34,4 +35,24 @@ - (void)testInstantiationOfNewManagedObjectContextAssociatesWithObjectStore
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

0 comments on commit f585d34

Please sign in to comment.