diff --git a/src/RealmJS.mm b/src/RealmJS.mm index 246d11d6..5fb5a7d1 100644 --- a/src/RealmJS.mm +++ b/src/RealmJS.mm @@ -46,31 +46,13 @@ JSClassRef RJSRealmTypeClass() { return JSClassCreate(&realmTypesDefinition); } -NSString *RealmPathForFile(NSString *fileName) { +NSString *RealmFileDirectory() { #if TARGET_OS_IPHONE - NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; + return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; #else NSString *path = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)[0]; - path = [path stringByAppendingPathComponent:[[[NSBundle mainBundle] executablePath] lastPathComponent]]; + return [path stringByAppendingPathComponent:[[[NSBundle mainBundle] executablePath] lastPathComponent]]; #endif - return [path stringByAppendingPathComponent:fileName]; -} - -static void DeleteOrThrow(NSString *path) { - NSError *error; - if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) { - if (error.code != NSFileNoSuchFileError) { - @throw [NSException exceptionWithName:@"RLMTestException" - reason:[@"Unable to delete realm: " stringByAppendingString:error.description] - userInfo:nil]; - } - } -} - -static void DeleteRealmFilesAtPath(NSString *path) { - DeleteOrThrow(path); - DeleteOrThrow([path stringByAppendingString:@".lock"]); - DeleteOrThrow([path stringByAppendingString:@".note"]); } static JSValueRef DeleteTestFiles(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { @@ -103,12 +85,13 @@ static JSValueRef DeleteTestFiles(JSContextRef ctx, JSObjectRef function, JSObje realm::Realm::s_global_cache.invalidate_all(); realm::Realm::s_global_cache.clear(); - // FIXME - find all realm files in the docs dir and delete them rather than hardcoding these - - DeleteRealmFilesAtPath(RealmPathForFile(@"test.realm")); - DeleteRealmFilesAtPath(RealmPathForFile(@"test1.realm")); - DeleteRealmFilesAtPath(RealmPathForFile(@"test2.realm")); - DeleteRealmFilesAtPath(@(RJSDefaultPath().c_str())); + NSFileManager *manager = [NSFileManager defaultManager]; + NSString *fileDir = RealmFileDirectory(); + for (NSString *path in [manager enumeratorAtPath:fileDir]) { + if (![manager removeItemAtPath:[fileDir stringByAppendingPathComponent:path] error:nil]) { + @throw [NSException exceptionWithName:@"removeItemAtPath error" reason:@"Failed to delete file" userInfo:nil]; + } + } } @end