From 06d277aacdd04f10940d61bfce2456bca1ae9235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Vind?= Date: Mon, 14 Nov 2016 10:48:50 -0800 Subject: [PATCH] Bumped version of ESLint, added eslinting of specs, make all linting pass --- .eslintignore | 3 +- .eslintrc.json | 11 ++- docs/sync.js | 47 ++++++------ lib/.eslintrc.json | 3 - lib/browser/{.eslintrc => .eslintrc.json} | 4 +- lib/browser/collections.js | 2 +- lib/browser/index.js | 2 +- lib/browser/rpc.js | 2 +- lib/extensions.js | 3 +- lib/index.js | 1 + lib/user-methods.js | 3 +- package.json | 8 +- tests/{js => }/.eslintrc.json | 11 ++- tests/js/async-tests.js | 4 +- tests/js/schemas.js | 2 - tests/js/user-tests.js | 7 +- tests/react-test-app/index.ios.js | 2 - tests/spec/helpers/reporters.js | 2 + tests/spec/sync_integration_tests.js | 93 ++++++++++++----------- 19 files changed, 115 insertions(+), 95 deletions(-) rename lib/browser/{.eslintrc => .eslintrc.json} (85%) rename tests/{js => }/.eslintrc.json (52%) diff --git a/.eslintignore b/.eslintignore index 43adc689..988d8689 100644 --- a/.eslintignore +++ b/.eslintignore @@ -6,4 +6,5 @@ build/ node_modules/ vendor/ -/tests/test-runners/ \ No newline at end of file +/tests/test-runners/ +/object-server-for-testing/ \ No newline at end of file diff --git a/.eslintrc.json b/.eslintrc.json index d257f28b..49e3f61b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,10 +1,19 @@ { "root": true, + "env": { + "es6": true, + "node": true + }, "extends": "eslint:recommended", "rules": { "comma-dangle": 0, "no-empty": 0, - "no-unused-vars": 1, + "no-unused-vars": [ + "warn", { + "varsIgnorePattern": "^_", + "argsIgnorePattern": "^_" + } + ], "strict": [2, "global"] } } diff --git a/docs/sync.js b/docs/sync.js index 4bdbe4fa..5ba015d2 100644 --- a/docs/sync.js +++ b/docs/sync.js @@ -20,29 +20,30 @@ * @memberof Realm */ class Sync { + + /** + * Set a global listener function. + * @param {string} local_path - The path to the directory where realm files are stored [deprecated] + * @param {string} server_url - The sync server to listen to + * @param {SyncUser} admin_user - An admin user obtained by calling `new Realm.Sync.User.Admin` + * @param {function(realm_name)} filter_callback - Return true to recieve changes for the given realm + * @param {function(realm_name, realm, change_set)} change_callback - called on any realm changes with + * the following arguments: + * - `realm_name` - path of the Realm on which changes occurred + * - `realm` - a `Realm` object for the changed Realm + * - `change_set` - a dictionary of object names to arays of indexes indicating the indexes of objects of each type + * which have been added, removed, or modified + */ + static setGlobalListener(local_path, server_url, admin_user, filter_callback, change_callback) {} + + /** + * Set the sync log level. + * @param {string} log_level + */ + static setLogLevel(log_level) {} + } -/** - * Set a global listener function. - * @param {string} local_path - The path to the directory where realm files are stored [deprecated] - * @param {string} server_url - The sync server to listen to - * @param {SyncUser} admin_user - An admin user obtained by calling `new Realm.Sync.User.Admin` - * @param {function(realm_name)} filter_callback - Return true to recieve changes for the given realm - * @param {function(realm_name, realm, change_set)} change_callback - called on any realm changes with - * the following arguments: - * - `realm_name` - path of the Realm on which changes occurred - * - `realm` - a `Realm` object for the changed Realm - * - `change_set` - a dictionary of object names to arays of indexes indicating the indexes of objects of each type - * which have been added, removed, or modified - */ -Sync.setGlobalListener = function(local_path, server_url, admin_user, filter_callback, change_callback) {}; - -/** - * Set the sync log level. - * @param {string} log_level - */ -Sync.setLogLevel = function(log_level) {}; - /** * @typedef Realm.Sync~LogLevel * @type {("error"|"info"|"debug")} @@ -117,12 +118,12 @@ class User { * The keys in the dictionary are user identities, values are corresponding User objects. * @type {object} */ - get all() {}; + get all() {} /** * Get the currently logged in user. * Throws error if > 1 user logged in, returns undefined if no users logged in. * @type {User} */ - get current() {}; + get current() {} } diff --git a/lib/.eslintrc.json b/lib/.eslintrc.json index e84df8c3..2c63c085 100644 --- a/lib/.eslintrc.json +++ b/lib/.eslintrc.json @@ -1,5 +1,2 @@ { - "env": { - "commonjs": true - } } diff --git a/lib/browser/.eslintrc b/lib/browser/.eslintrc.json similarity index 85% rename from lib/browser/.eslintrc rename to lib/browser/.eslintrc.json index db3133ab..c27c7a11 100644 --- a/lib/browser/.eslintrc +++ b/lib/browser/.eslintrc.json @@ -1,7 +1,7 @@ { "env": { - "es6": true, - "worker": true + "worker": true, + "node": false }, "globals": { "global": true diff --git a/lib/browser/collections.js b/lib/browser/collections.js index e50c4cbe..fcc9b8f5 100644 --- a/lib/browser/collections.js +++ b/lib/browser/collections.js @@ -52,7 +52,7 @@ export function fireMutationListeners(realmId) { } } -export function createCollection(prototype, realmId, info, mutable) { +export function createCollection(prototype, realmId, info, _mutable) { let collection = Object.create(prototype); let size; diff --git a/lib/browser/index.js b/lib/browser/index.js index add249a2..6668f389 100644 --- a/lib/browser/index.js +++ b/lib/browser/index.js @@ -145,7 +145,7 @@ Object.defineProperties(Realm, { set: util.setterForProperty('defaultPath'), }, schemaVersion: { - value: function(path, encryptionKey) { + value: function(_path, _encryptionKey) { return rpc.callMethod(undefined, Realm[keys.id], 'schemaVersion', Array.from(arguments)); } }, diff --git a/lib/browser/rpc.js b/lib/browser/rpc.js index 209eaf3a..28d531d6 100644 --- a/lib/browser/rpc.js +++ b/lib/browser/rpc.js @@ -21,7 +21,7 @@ import * as base64 from './base64'; import { keys, objectTypes } from './constants'; -const {id: idKey, realm: realmKey} = keys; +const {id: idKey, realm: _realmKey} = keys; const registeredCallbacks = []; const typeConverters = {}; diff --git a/lib/extensions.js b/lib/extensions.js index f9ceb48b..83cb05bd 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -28,7 +28,8 @@ module.exports = function(realmConstructor) { realmConstructor.Sync.AuthError = require('./errors').AuthError; if (realmConstructor.Sync.cleanup) { - process.on('exit', () => realmConstructor.Sync.cleanup()); + // FIXME: DOES THIS WORK ON BOTH NODE AND REACT NATIVE? + process.on('exit', realmConstructor.Sync.cleanup); } } diff --git a/lib/index.js b/lib/index.js index 0e5fe471..cef11a10 100644 --- a/lib/index.js +++ b/lib/index.js @@ -33,6 +33,7 @@ if (typeof Realm != 'undefined') { realmConstructor = require('./browser').default; // (exported as ES6 module) // eslint-disable-next-line } else if (typeof process == 'object' && (('' + process) == '[object process]' || typeof jest == 'object')) { + // If process is defined, we're running in node. // Prevent React Native packager from seeing this module. var binary = node_require('node-pre-gyp'); var path = node_require('path'); diff --git a/lib/user-methods.js b/lib/user-methods.js index e4cdc409..204c71f9 100644 --- a/lib/user-methods.js +++ b/lib/user-methods.js @@ -7,9 +7,10 @@ function node_require(module) { } var post; -if (typeof fetch != 'undefined') { +if (typeof fetch !== 'undefined') { post = function(options, callback) { options.method = 'POST'; + // eslint-disable-next-line no-undef fetch(options.url, options) .then((response) => { if (response.status != 200) { diff --git a/package.json b/package.json index 3d0362e3..29ab50d4 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "sqlite", "asyncstorage", "rocksdb", - "leveldb" + "leveldb", + "realm" ], "author": { "name": "Realm", @@ -57,8 +58,9 @@ }, "devDependencies": { "babel-eslint": "^6.0.4", - "eslint": "^2.10.2", - "eslint-plugin-react": "^5.1.1", + "eslint": "^3.2.2", + "eslint-plugin-jasmine": "^2.1.0", + "eslint-plugin-react": "^6.7.0", "jsdoc": "^3.4.0", "semver": "^5.1.0" }, diff --git a/tests/js/.eslintrc.json b/tests/.eslintrc.json similarity index 52% rename from tests/js/.eslintrc.json rename to tests/.eslintrc.json index acc84647..051d0da1 100644 --- a/tests/js/.eslintrc.json +++ b/tests/.eslintrc.json @@ -1,7 +1,9 @@ { "env": { - "commonjs": true + "jasmine": true }, + "plugins": ["jasmine"], + "extends": "plugin:jasmine/recommended", "globals": { "ArrayBuffer": false, "DataView": false, @@ -14,5 +16,12 @@ "Uint32Array": false, "Uint8Array": false, "Uint8ClampedArray": false + }, + "rules": { + "no-invalid-this": "off", + "no-magic-numbers": "off", + "jasmine/no-spec-dupes": ["error", "branch"], + "jasmine/no-suite-dupes": ["error", "branch"], + "no-console": "off" } } diff --git a/tests/js/async-tests.js b/tests/js/async-tests.js index a9db9c61..5a4ccdc5 100644 --- a/tests/js/async-tests.js +++ b/tests/js/async-tests.js @@ -71,7 +71,7 @@ function createNotificationTest(config, getObservable, addListener, removeListen worker.postMessage(messages[messageIndex++]); }); -}; +} function createCollectionChangeTest(config, createCollection, messages, expected, removeAll) { return createNotificationTest( @@ -97,7 +97,7 @@ function createCollectionChangeTest(config, createCollection, messages, expected messages, expected.length ); -}; +} const ListObject = { name: 'ListObject', diff --git a/tests/js/schemas.js b/tests/js/schemas.js index 04f28ee1..beb452f9 100644 --- a/tests/js/schemas.js +++ b/tests/js/schemas.js @@ -18,8 +18,6 @@ 'use strict'; -var Realm = require('realm'); - exports.TestObject = { name: 'TestObject', properties: { diff --git a/tests/js/user-tests.js b/tests/js/user-tests.js index f8f60cbe..11cd515a 100644 --- a/tests/js/user-tests.js +++ b/tests/js/user-tests.js @@ -22,7 +22,6 @@ const Realm = require('realm'); const TestCase = require('./asserts'); -const schemas = require('./schemas'); function uuid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { @@ -104,7 +103,7 @@ module.exports = { // Can we open a realm with the registered user? TestCase.assertThrows(function() { - var realm = new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}}); + var _realm = new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}}); }); }) }, @@ -209,7 +208,7 @@ module.exports = { }, testAll() { - return new Promise((resolve, reject) => { + return new Promise((resolve, _reject) => { let all; all = Realm.Sync.User.all; TestCase.assertArrayLength(Object.keys(all), 0); @@ -246,7 +245,7 @@ module.exports = { }, testCurrent() { - return new Promise((resolve, reject) => { + return new Promise((resolve, _reject) => { TestCase.assertUndefined(Realm.Sync.User.current); callbackTest((callback) => Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', callback), (error, user1) => { diff --git a/tests/react-test-app/index.ios.js b/tests/react-test-app/index.ios.js index b9a3dc40..d1aaaa7b 100644 --- a/tests/react-test-app/index.ios.js +++ b/tests/react-test-app/index.ios.js @@ -28,8 +28,6 @@ import { import React from 'react'; import { runTests } from './tests'; -const Realm = require('realm'); - class ReactTests extends React.Component { render() { return ( diff --git a/tests/spec/helpers/reporters.js b/tests/spec/helpers/reporters.js index 5e56821a..3920e991 100644 --- a/tests/spec/helpers/reporters.js +++ b/tests/spec/helpers/reporters.js @@ -1,3 +1,5 @@ +'use strict'; + var jasmineReporters = require('jasmine-reporters'); var junitReporter = new jasmineReporters.JUnitXmlReporter({ savePath: '.', diff --git a/tests/spec/sync_integration_tests.js b/tests/spec/sync_integration_tests.js index c4ac38fd..925ec1aa 100644 --- a/tests/spec/sync_integration_tests.js +++ b/tests/spec/sync_integration_tests.js @@ -7,57 +7,57 @@ const Realm = require("realm"); jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000; -beforeEach(function(done) { - this.objectServer = spawn("sync-bundle/start-object-server.command"); - this.objectServer.once("close", (code) => { - if (typeof code === "number" && code != 0) { - console.error(`Object Server exited with code ${code}`); - process.exit(-1); - } - }); +describe('Sync Integration', () => { + beforeEach(function(done) { + this.objectServer = spawn("sync-bundle/start-object-server.command"); + this.objectServer.once("close", (code) => { + if (typeof code === "number" && code != 0) { + console.error(`Object Server exited with code ${code}`); + process.exit(-1); + } + }); - this.rl = readline.createInterface({ input: this.objectServer.stdout }); - this.rl.on("line", (line) => { - var match; - if ((match = line.match(/Connection\[1\]: Session\[1\]: Received: BIND\(server_path='\/(.+)',/))) { - var adminUser = Realm.Sync.User.adminUser('http://127.0.0.1:9080/', - fs.readFileSync("sync-bundle/admin_token.base64", "utf-8")); - this.adminRealmPath = match[1]; - this.adminRealm = new Realm({ - path: "__admin.realm", - sync: { - user: adminUser, - url: `realm://127.0.0.1:9080/${this.adminRealmPath}` - }, - schema: [ - { - name: "RealmFile", - properties: { - id: 'string', - path: 'string' + this.rl = readline.createInterface({ input: this.objectServer.stdout }); + this.rl.on("line", (line) => { + var match; + if ((match = line.match(/Connection\[1\]: Session\[1\]: Received: BIND\(server_path='\/(.+)',/))) { + var adminUser = Realm.Sync.User.adminUser('http://127.0.0.1:9080/', + fs.readFileSync("sync-bundle/admin_token.base64", "utf-8")); + this.adminRealmPath = match[1]; + this.adminRealm = new Realm({ + path: "__admin.realm", + sync: { + user: adminUser, + url: `realm://127.0.0.1:9080/${this.adminRealmPath}` + }, + schema: [ + { + name: "RealmFile", + properties: { + id: 'string', + path: 'string' + } } - } - ] - }); + ] + }); - done(); - } + done(); + } + }); }); -}); -afterEach(function(done) { - this.rl.close(); - this.objectServer.kill('SIGKILL'); - this.adminRealm.close(); + afterEach(function(done) { + this.rl.close(); + this.objectServer.kill('SIGKILL'); + this.adminRealm.close(); - let reset = spawn("sync-bundle/reset-server-realms.command"); - reset.once("close", done); - reset.stdin.write("yes\n"); + let reset = spawn("sync-bundle/reset-server-realms.command"); + reset.once("close", done); + reset.stdin.write("yes\n"); - Realm.clearTestState(); -}); + Realm.clearTestState(); + }); -describe("Sync", function() { it("should work", function(done) { Realm.Sync.User.create('http://127.0.0.1:9080/', 'foo', 'bar', function(error) { if (error) { @@ -71,11 +71,11 @@ describe("Sync", function() { return; } - var realm = new Realm({ + var _realm = new Realm({ syncConfig: { - identity: user.identity, + identity: user.identity, url: 'realm://127.0.0.1:9080/~/demo/realm1' - }, + }, schema: [ { name: 'IntObject', @@ -96,4 +96,5 @@ describe("Sync", function() { } }); }); + }); \ No newline at end of file