support relative paths when opening realms

This commit is contained in:
Ari Lazier 2016-03-03 13:33:49 -08:00
parent c062d88cce
commit 5f61fd970c
3 changed files with 14 additions and 36 deletions

View File

@ -200,6 +200,11 @@ JSObjectRef RealmConstructor(JSContextRef ctx, JSObjectRef constructor, size_t a
*jsException = RJSMakeError(ctx, "Invalid arguments when constructing 'Realm'");
return NULL;
}
if (config.path.size() && config.path[0] != '/') {
config.path = default_realm_file_directory() + "/" + config.path;
}
ensure_directory_exists_for_file(config.path);
SharedRealm realm = Realm::get_shared_realm(config);
if (!realm->m_binding_context) {

View File

@ -22,7 +22,6 @@ var Realm = require('realm');
var BaseTest = require('./base-test');
var TestCase = require('./asserts');
var schemas = require('./schemas');
var util = require('./util');
module.exports = BaseTest.extend({
testRealmConstructor: function() {
@ -35,7 +34,7 @@ module.exports = BaseTest.extend({
new Realm('/invalidpath');
}, 'Realm cannot be created with an invalid path');
TestCase.assertThrows(function() {
new Realm(util.realmPathForFile('test1.realm'), 'invalidArgument');
new Realm('test1.realm', 'invalidArgument');
}, 'Realm constructor can only have 0 or 1 argument(s)');
var defaultRealm = new Realm({schema: []});
@ -44,15 +43,16 @@ module.exports = BaseTest.extend({
var defaultRealm2 = new Realm();
TestCase.assertEqual(defaultRealm2.path, Realm.defaultPath);
var testPath = util.realmPathForFile('test1.realm');
var defaultDir = Realm.defaultPath.substring(0, Realm.defaultPath.lastIndexOf("/") + 1)
var testPath = 'test1.realm';
var realm = new Realm({schema: [], path: testPath});
//TestCase.assertTrue(realm instanceof Realm);
TestCase.assertEqual(realm.path, testPath);
TestCase.assertEqual(realm.path, defaultDir + testPath);
var testPath2 = util.realmPathForFile('test2.realm');
var testPath2 = 'test2.realm';
var realm2 = new Realm({schema: [], path: testPath2});
//TestCase.assertTrue(realm2 instanceof Realm);
TestCase.assertEqual(realm2.path, testPath2);
TestCase.assertEqual(realm2.path, defaultDir + testPath2);
},
testRealmConstructorSchemaVersion: function() {
@ -66,8 +66,7 @@ module.exports = BaseTest.extend({
TestCase.assertEqual(new Realm().schemaVersion, 0);
TestCase.assertEqual(new Realm({schemaVersion: 0}).schemaVersion, 0);
var testPath = util.realmPathForFile('test1.realm');
var realm = new Realm({path: testPath, schema: [], schemaVersion: 1});
var realm = new Realm({path: 'test1.realm', schema: [], schemaVersion: 1});
TestCase.assertEqual(realm.schemaVersion, 1);
// FIXME - enable once Realm exposes a schema object
//TestCase.assertEqual(realm.schema.length, 0);
@ -78,7 +77,7 @@ module.exports = BaseTest.extend({
// realm = new Realm({path: testPath, schema: [schemas.TestObject], schemaVersion: 1});
// }, "schema changes require updating the schema version");
realm = new Realm({path: testPath, schema: [schemas.TestObject], schemaVersion: 2});
realm = new Realm({path: 'test1.realm', schema: [schemas.TestObject], schemaVersion: 2});
realm.write(function() {
realm.create('TestObject', {doubleCol: 1});
});
@ -111,7 +110,7 @@ module.exports = BaseTest.extend({
var defaultRealm = new Realm({schema: []});
TestCase.assertEqual(defaultRealm.path, Realm.defaultPath);
var newPath = util.realmPathForFile('default2.realm');
var newPath = Realm.defaultPath.substring(0, Realm.defaultPath.lastIndexOf("/") + 1) + 'default2.realm';
Realm.defaultPath = newPath;
defaultRealm = new Realm({schema: []});
TestCase.assertEqual(defaultRealm.path, newPath, "should use updated default realm path");

View File

@ -1,26 +0,0 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
'use strict';
var Realm = require('realm');
exports.realmPathForFile = function(str) {
var path = Realm.defaultPath;
return path.substring(0, path.lastIndexOf("/") + 1) + str;
};