remove hardcoded test paths

This commit is contained in:
Ari Lazier 2015-10-15 17:28:51 -07:00
parent eb598a2408
commit 45a3c89605
1 changed files with 10 additions and 27 deletions

View File

@ -46,31 +46,13 @@ JSClassRef RJSRealmTypeClass() {
return JSClassCreate(&realmTypesDefinition); return JSClassCreate(&realmTypesDefinition);
} }
NSString *RealmPathForFile(NSString *fileName) { NSString *RealmFileDirectory() {
#if TARGET_OS_IPHONE #if TARGET_OS_IPHONE
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
#else #else
NSString *path = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)[0]; NSString *path = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)[0];
path = [path stringByAppendingPathComponent:[[[NSBundle mainBundle] executablePath] lastPathComponent]]; return [path stringByAppendingPathComponent:[[[NSBundle mainBundle] executablePath] lastPathComponent]];
#endif #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) { 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.invalidate_all();
realm::Realm::s_global_cache.clear(); realm::Realm::s_global_cache.clear();
// FIXME - find all realm files in the docs dir and delete them rather than hardcoding these NSFileManager *manager = [NSFileManager defaultManager];
NSString *fileDir = RealmFileDirectory();
DeleteRealmFilesAtPath(RealmPathForFile(@"test.realm")); for (NSString *path in [manager enumeratorAtPath:fileDir]) {
DeleteRealmFilesAtPath(RealmPathForFile(@"test1.realm")); if (![manager removeItemAtPath:[fileDir stringByAppendingPathComponent:path] error:nil]) {
DeleteRealmFilesAtPath(RealmPathForFile(@"test2.realm")); @throw [NSException exceptionWithName:@"removeItemAtPath error" reason:@"Failed to delete file" userInfo:nil];
DeleteRealmFilesAtPath(@(RJSDefaultPath().c_str())); }
}
} }
@end @end