diff --git a/.editorconfig b/.editorconfig index 2f868c7..ec0fd49 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,7 +5,7 @@ charset = utf-8 end_of_line = lf insert_final_newline = true indent_style = space -indent_size = 4 +indent_size = 2 trim_trailing_whitespace = true # Do not trim in jade files diff --git a/.eslintrc b/.eslintrc index c6e7fd9..95a4da2 100644 --- a/.eslintrc +++ b/.eslintrc @@ -12,5 +12,8 @@ ], "plugins": [ "@coorpacademy/coorpacademy" - ] -} \ No newline at end of file + ], + "rules": { + "promise/no-native": 0 + } +} diff --git a/package.json b/package.json index b903476..055e485 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "rxjs": "^5.0.0-beta.7" }, "devDependencies": { - "@coorpacademy/eslint-plugin-coorpacademy": "^1.2.1", + "@coorpacademy/eslint-plugin-coorpacademy": "^2.0.0", "ava": "^0.16.0", "babel-cli": "^6.18.0", "babel-core": "^6.18.2", @@ -49,6 +49,8 @@ "coveralls": "^2.11.15", "eslint": "^3.10.2", "eslint-plugin-ava": "^4.0.0", + "eslint-plugin-fp": "^2.2.0", + "eslint-plugin-import": "^2.2.0", "eslint-plugin-lodash-fp": "^2.1.3", "nyc": "^9.0.1" }, diff --git a/src/api.js b/src/api.js index 88439fc..bb3e7fa 100644 --- a/src/api.js +++ b/src/api.js @@ -1,4 +1,7 @@ -import path from 'path'; +import { + join, + relative +} from 'path'; import { find, getOr, @@ -6,9 +9,9 @@ import { startsWith, curry } from 'lodash/fp'; -import {stringify, parseValue} from './parse'; -import {set$} from './util/etcd'; import createDebug from 'debug'; +import {set$} from './util/etcd'; +import {stringify, parseValue} from './parse'; const debug = createDebug('squirrel'); @@ -30,29 +33,29 @@ const createAPI = (store, client, options = {cwd: '/'}) => { return getByRaw(index, key).then(getOr(null, ['value'])); }; + const _get = curry((_path, node) => { + if (!node) return null; + if (relative(node.key, _path) === '') return node; + + return _get(_path, find(function(child) { + return startsWith(child.key, _path); + }, node.nodes)); + }); + const get = path => { debug(`get => ${path}`); return store('node').then(_get(path)); }; const set = (_path, value) => { - _path = path.join(options.cwd, _path); - debug(`set => ${_path}`); - return set$(client, _path, stringify(value)) + const fullPath = join(options.cwd, _path); + debug(`set => ${fullPath}`); + return set$(client, fullPath, stringify(value)) .pluck('node', 'value') .map(parseValue) .toPromise(); }; - const _get = curry((_path, node) => { - if (!node) return null; - if (path.relative(node.key, _path) === '') return node; - - return _get(_path, find(function(child) { - return startsWith(child.key, _path); - }, node.nodes)); - }); - return { getBy, getByRaw, diff --git a/src/bin/sync.js b/src/bin/sync.js index b2e5c0b..c7164c6 100644 --- a/src/bin/sync.js +++ b/src/bin/sync.js @@ -2,9 +2,8 @@ import {resolve} from 'path'; import minimist from 'minimist'; - -import makeEtcdClient from './etcd'; import {sync$} from '../util/cli'; +import makeEtcdClient from './etcd'; const argz = minimist(process.argv.slice(2)); @@ -13,6 +12,7 @@ const pathETCD = resolve('/', argz._[1]); const client = makeEtcdClient(argz); -sync$(client, pathFS, pathETCD).toPromise().then(() => - process.stdout.write(`The end.\n`) +sync$(client, pathFS, pathETCD).toPromise().then( // eslint-disable-line promise/catch-or-return + () => process.stdout.write(`The end.\n`), + err => process.stderr.write(`${err.stack}\n`) ); diff --git a/src/bin/watch.js b/src/bin/watch.js index 3931010..83b84ec 100644 --- a/src/bin/watch.js +++ b/src/bin/watch.js @@ -2,10 +2,9 @@ import {resolve} from 'path'; import minimist from 'minimist'; - -import makeEtcdClient from './etcd'; import createWatcher$ from '../watch'; import {stringify} from '../parse'; +import makeEtcdClient from './etcd'; const argz = minimist(process.argv.slice(2)); diff --git a/src/build-index.js b/src/build-index.js index 1340e25..4ffbef9 100644 --- a/src/build-index.js +++ b/src/build-index.js @@ -1,31 +1,39 @@ import { assign, + cond, + constant, flatMap, get, + getOr, map, pipe, reduce, zipObject } from 'lodash/fp'; import createDebug from 'debug'; -const debug = createDebug('squirrel:indexer'); -const updateIndexes = indexes => store => { - debug(`Update indexes ${indexes.join(',')}`); - return zipObject(indexes, map(function(index) { - return buildIndex(index, store); - }, indexes)); -}; +const debug = createDebug('squirrel:indexer'); const buildIndex = (index, node) => { const value = get(index, node.value); return pipe( + cond([ + [get('dir'), getOr([], 'nodes')], + [constant(true), constant([])] + ]), flatMap(function(child) { return buildIndex(index, child); }), reduce(assign, value ? zipObject([get(index, node.value)], [node]) : {}) - )(node.dir && node.nodes || []); + )(node); +}; + +const updateIndexes = indexes => store => { + debug(`Update indexes ${indexes.join(',')}`); + return zipObject(indexes, map(function(index) { + return buildIndex(index, store); + }, indexes)); }; export default updateIndexes; diff --git a/src/combine.js b/src/combine.js index d7cfed7..41cbe6e 100644 --- a/src/combine.js +++ b/src/combine.js @@ -1,5 +1,6 @@ -import {set, del} from './patch'; import createDebug from 'debug'; +import {set, del} from './patch'; + const debug = createDebug('squirrel:combine'); const createCombiner$ = event$ => { diff --git a/src/fallback.js b/src/fallback.js index c1af9ea..b62e3c8 100644 --- a/src/fallback.js +++ b/src/fallback.js @@ -1,8 +1,9 @@ import {readFile} from 'fs'; import {isString, identity} from 'lodash/fp'; import {Observable} from 'rxjs'; -import {parseAction} from './parse'; import createDebug from 'debug'; +import {parseAction} from './parse'; + const debug = createDebug('squirrel:fallback'); const readFile$ = Observable.bindNodeCallback(readFile); diff --git a/src/fetch.js b/src/fetch.js index c26a9d9..c90459c 100644 --- a/src/fetch.js +++ b/src/fetch.js @@ -1,6 +1,7 @@ import {Observable} from 'rxjs'; -import {parseAction} from './parse'; import createDebug from 'debug'; +import {parseAction} from './parse'; + const debug = createDebug('squirrel:etcd'); const createFetch$ = (client, cwd) => { diff --git a/src/index.js b/src/index.js index bff63e6..269e46e 100644 --- a/src/index.js +++ b/src/index.js @@ -6,7 +6,6 @@ import { import Etcd from 'node-etcd'; import {Observable} from 'rxjs'; import createDebug from 'debug'; - import createEtcd$ from './etcd'; import createFallback$ from './fallback'; import createCombiner$ from './combine'; @@ -17,9 +16,9 @@ import createIndexBuilder from './build-index'; const debug = createDebug('squirrel'); -const createSquirrel = options => { +const createSquirrel = _options => { debug('Init'); - options = assign({ + const options = assign({ hosts: 'http://localhost:2379', auth: null, ca: null, @@ -30,7 +29,7 @@ const createSquirrel = options => { cwd: '/', indexes: [] - }, options); + }, _options); const client = new Etcd(options.hosts, pick(['auth', 'ca', 'key', 'cert'], options)); const indexBuilder = createIndexBuilder(options.indexes); diff --git a/src/parse.js b/src/parse.js index f8e06b1..6bf10ae 100644 --- a/src/parse.js +++ b/src/parse.js @@ -26,10 +26,10 @@ export const parseNode = pipe( ); export const parseAction = action => { - return reduce((action, key) => { - if (has(key, action)) { - return set(key, parseNode(get(key, action)), action); + return reduce((_action, key) => { + if (has(key, _action)) { + return set(key, parseNode(get(key, _action)), _action); } - return action; + return _action; }, action, ['node', 'prevNode']); }; diff --git a/src/patch.js b/src/patch.js index 55409cb..c2e3aad 100644 --- a/src/patch.js +++ b/src/patch.js @@ -7,6 +7,7 @@ import { startsWith } from 'lodash/fp'; import createDebug from 'debug'; + const debug = createDebug('squirrel:patch'); const get = (store, {action, node}) => { diff --git a/src/resync.js b/src/resync.js index 598620f..b11f258 100644 --- a/src/resync.js +++ b/src/resync.js @@ -1,6 +1,7 @@ import {Observable} from 'rxjs'; -import createFetcher$ from './fetch'; import createDebug from 'debug'; +import createFetcher$ from './fetch'; + const debug = createDebug('squirrel:etcd'); const createResync$ = (client, cwd, events$) => diff --git a/src/save.js b/src/save.js index a74a288..55b628e 100644 --- a/src/save.js +++ b/src/save.js @@ -3,6 +3,7 @@ import {Observable} from 'rxjs'; import {isString} from 'lodash/fp'; import createDebug from 'debug'; import {stringify} from './parse'; + const debug = createDebug('squirrel:save'); const writeFile$ = Observable.bindNodeCallback(writeFile); diff --git a/src/test/api.js b/src/test/api.js index 8dc24d3..85c7771 100644 --- a/src/test/api.js +++ b/src/test/api.js @@ -1,8 +1,8 @@ import test from 'ava'; import createEtcdMock from '../util/test/helpers/etcd'; import {stringify} from '../parse'; - import createAPI from '../api'; + const node = { key: '/', dir: true, @@ -213,9 +213,9 @@ test('should get null if no node matches this path', async t => { ); }); -const createAction = (type, node) => ({ +const createAction = (type, _node) => ({ type, - node + node: _node }); const createNode = (key, value) => ({ key, @@ -233,9 +233,9 @@ test('should set value if value setted', async t => { values: [null, createAction('set', createNode('/foo', {foo: 'baz'})), null] }] }); - const api = createAPI(getStore, client); + const _api = createAPI(getStore, client); t.deepEqual( - await api.set('/foo', {foo: 'baz'}), + await _api.set('/foo', {foo: 'baz'}), {foo: 'baz'} ); }); @@ -244,9 +244,9 @@ test('should failed if error occured', async t => { const client = createEtcdMock({ set: [[new Error('boom'), null, null]] }); - const api = createAPI(getStore, client); + const _api = createAPI(getStore, client); t.throws( - api.set('/foo', {foo: 'baz'}), + _api.set('/foo', {foo: 'baz'}), /boom/ ); }); diff --git a/src/test/combine.js b/src/test/combine.js index a2b7397..d777041 100644 --- a/src/test/combine.js +++ b/src/test/combine.js @@ -1,8 +1,6 @@ import test from 'ava'; import {Observable} from 'rxjs'; - import createCombiner$ from '../combine'; - import emptyRoot from './fixtures/empty-root'; import setEvent from './fixtures/set-event'; diff --git a/src/test/etcd.js b/src/test/etcd.js index a06f656..a6cc712 100644 --- a/src/test/etcd.js +++ b/src/test/etcd.js @@ -1,12 +1,10 @@ -import {EventEmitter} from 'events'; +import {EventEmitter} from 'events'; // eslint-disable-line fp/no-events import test from 'ava'; - import createEtcd$ from '../etcd'; - +import createEtcdMock from '../util/test/helpers/etcd'; import setEvent from './fixtures/set-event'; import deleteEvent from './fixtures/delete-event'; import resyncEvent from './fixtures/resync-event'; -import createEtcdMock from '../util/test/helpers/etcd'; test('should composite events observable', async t => { const getMocks = [[null, { diff --git a/src/test/fallback.js b/src/test/fallback.js index 1bd2d2c..7a270f9 100644 --- a/src/test/fallback.js +++ b/src/test/fallback.js @@ -1,9 +1,7 @@ -import test from 'ava'; import {join} from 'path'; +import test from 'ava'; import {parseNode} from '../parse'; - import createFallback$ from '../fallback'; - import expected from './fixtures/fallback'; test('should load fallback', async t => { diff --git a/src/test/fetch.js b/src/test/fetch.js index 86aebdf..78bba95 100644 --- a/src/test/fetch.js +++ b/src/test/fetch.js @@ -1,10 +1,8 @@ import test from 'ava'; import {pipe, fill, map, concat} from 'lodash/fp'; - import createFetch$ from '../fetch'; - -import emptyRoot from './fixtures/empty-root'; import createEtcdMock from '../util/test/helpers/etcd'; +import emptyRoot from './fixtures/empty-root'; test('should fetch nodes', t => { const client = createEtcdMock({ diff --git a/src/test/index.js b/src/test/index.js index 203c1f4..014872f 100644 --- a/src/test/index.js +++ b/src/test/index.js @@ -1,5 +1,5 @@ import test from 'ava'; -import createClient from '../index'; +import createClient from '../index'; // eslint-disable-line import/default test('should createClient', t => { const client = createClient(); diff --git a/src/test/resync.js b/src/test/resync.js index d43c556..ed1c13b 100644 --- a/src/test/resync.js +++ b/src/test/resync.js @@ -1,11 +1,9 @@ import test from 'ava'; import {Observable} from 'rxjs'; - import createResyncer$ from '../resync'; - +import createEtcdMock from '../util/test/helpers/etcd'; import setEvent from './fixtures/set-event'; import resyncEvent from './fixtures/resync-event'; -import createEtcdMock from '../util/test/helpers/etcd'; test('should transform resync event', async t => { const client = createEtcdMock({ diff --git a/src/test/save.js b/src/test/save.js index 192857b..81b8d88 100644 --- a/src/test/save.js +++ b/src/test/save.js @@ -4,7 +4,6 @@ import {readFile} from 'fs'; import test from 'ava'; import {Observable} from 'rxjs'; import createSave from '../save'; - import fallback from './fixtures/fallback'; const readFile$ = Observable.bindNodeCallback(readFile); diff --git a/src/test/store.js b/src/test/store.js index 57c8f9e..7c2f864 100644 --- a/src/test/store.js +++ b/src/test/store.js @@ -1,7 +1,6 @@ import {Observable, Subject, Subscription} from 'rxjs'; import test from 'ava'; import {identity} from 'lodash/fp'; - import createStore from '../store'; test('should get node and indexes', t => { @@ -12,8 +11,8 @@ test('should get node and indexes', t => { } = createStore(node$, identity); const assert = Promise.all([ - store('node').then(store => - t.deepEqual(store, 'foo') + store('node').then(node => + t.deepEqual(node, 'foo') ), store('indexes').then(indexes => t.deepEqual(indexes, 'foo') @@ -48,7 +47,7 @@ test('should return subscription', t => { t.true(subscription instanceof Subscription); }); -test('should', t => { +test('should', async t => { const node$ = Observable.of('foo', 'bar'); const { @@ -56,13 +55,13 @@ test('should', t => { subscription } = createStore(node$, identity); - return Promise.all([ - store('node').then(store => - t.deepEqual(store, 'bar') - ), - store('indexes').then(indexes => - t.deepEqual(indexes, 'bar') - ) - ]).then(() => subscription.unsubscribe()); + const [node, indexes] = await Promise.all([ + store('node'), + store('indexes') + ]); + t.deepEqual(node, 'bar'); + t.deepEqual(indexes, 'bar'); + + subscription.unsubscribe(); }); diff --git a/src/test/watch.js b/src/test/watch.js index babf44a..ce70958 100644 --- a/src/test/watch.js +++ b/src/test/watch.js @@ -1,12 +1,10 @@ -import {EventEmitter} from 'events'; +import {EventEmitter} from 'events'; // eslint-disable-line fp/no-events import test from 'ava'; - import createWatcher$ from '../watch'; - +import createEtcdMock from '../util/test/helpers/etcd'; import setEvent from './fixtures/set-event'; import deleteEvent from './fixtures/delete-event'; import resyncEvent from './fixtures/resync-event'; -import createEtcdMock from '../util/test/helpers/etcd'; test('should create watcher observable', t => { t.plan(2); @@ -20,9 +18,9 @@ test('should create watcher observable', t => { const watcher$ = createWatcher$(client, '/'); - const assertion = watcher$.first().toPromise().then(event => { + const assertion = watcher$.first().do(event => { t.deepEqual(event, setEvent); - }); + }).toPromise(); watcher.emit(setEvent.action, setEvent); @@ -55,9 +53,9 @@ test('should, emit set/delete/resync events', t => { const expected = [setEvent, deleteEvent, resyncEvent]; - const assertion = watcher$.take(3).toArray().toPromise().then(events => { + const assertion = watcher$.take(3).toArray().do(events => { t.deepEqual(events, expected); - }); + }).toPromise(); expected.forEach(event => watcher.emit(event.action, event) diff --git a/src/util/cli.js b/src/util/cli.js index 0eb6c8d..0ed8d5b 100644 --- a/src/util/cli.js +++ b/src/util/cli.js @@ -1,21 +1,11 @@ import {join, relative} from 'path'; import {Observable} from 'rxjs'; import {includes, get, filter} from 'lodash/fp'; +import makeDebug from 'debug'; import {isDirectory$, readdir$, readFileUTF8$} from './fs'; import {get$, set$, delRecursive$, del$, mkdir$, rmdirRecursive$} from './etcd'; -import makeDebug from 'debug'; -const debug = makeDebug('squirrel:util:cli'); -export const sync$ = (client, pathFS, pathETCD) => { - debug('sync', pathFS, pathETCD); - return isDirectory$(pathFS).flatMap(isDirectory => { - if (isDirectory) - return syncDirectory$(client, pathFS, pathETCD); - return syncFile$(client, pathFS, pathETCD); - }).flatMap(entry => - sync$(client, join(pathFS, entry), join(pathETCD, entry)) - ); -}; +const debug = makeDebug('squirrel:util:cli'); export const syncDirectory$ = (client, pathFS, pathETCD) => { debug('syncDirectory', pathFS, pathETCD); @@ -33,12 +23,12 @@ export const syncDirectory$ = (client, pathFS, pathETCD) => { const entries = readdir$(pathFS); - return entries.toArray().flatMap(entries => { - const nodeToDelete = filter(node => { - return !includes(join(pathFS, relative(pathETCD, node.key)), entries); + return entries.toArray().flatMap(_entries => { + const nodeToDelete = filter(_node => { + return !includes(join(pathFS, relative(pathETCD, _node.key)), _entries); }, nodes); - const nodeToDelete$ = Observable.from(nodeToDelete).flatMap(node => - delRecursive$(client, node.key) + const nodeToDelete$ = Observable.from(nodeToDelete).flatMap(_node => + delRecursive$(client, _node.key) ); return nodeToDelete$; }).toArray().flatMap(() => entries); @@ -61,3 +51,14 @@ export const syncFile$ = (client, pathFS, pathETCD) => { return set$(client, pathETCD, file); }).combineAll().ignoreElements(); }; + +export const sync$ = (client, pathFS, pathETCD) => { + debug('sync', pathFS, pathETCD); + return isDirectory$(pathFS).flatMap(isDirectory => { + if (isDirectory) + return syncDirectory$(client, pathFS, pathETCD); + return syncFile$(client, pathFS, pathETCD); + }).flatMap(entry => + sync$(client, join(pathFS, entry), join(pathETCD, entry)) + ); +}; diff --git a/src/util/etcd.js b/src/util/etcd.js index aef5076..ce1cfeb 100644 --- a/src/util/etcd.js +++ b/src/util/etcd.js @@ -1,6 +1,7 @@ import {Observable} from 'rxjs'; import {invokeArgs} from 'lodash/fp'; import makeDebug from 'debug'; + const debug = makeDebug('squirrel:util:etcd'); const wrap = fnName => (client, ...argz) => diff --git a/src/util/fs.js b/src/util/fs.js index 06f0b82..20e0920 100644 --- a/src/util/fs.js +++ b/src/util/fs.js @@ -1,7 +1,7 @@ -import {curry} from 'lodash/fp'; -import {Observable} from 'rxjs'; import {stat, readdir, readFile} from 'fs'; import {join} from 'path'; +import {curry} from 'lodash/fp'; +import {Observable} from 'rxjs'; export const stat$ = Observable.bindNodeCallback(stat); @@ -13,10 +13,10 @@ export const readFile$ = Observable.bindNodeCallback(readFile); export const readFileUTF8$ = file => readFile$(file, {encoding: 'UTF8'}); export const isFile$ = pathFS => - stat$(pathFS).map(stat => stat.isFile()); + stat$(pathFS).map(_stat => _stat.isFile()); export const isDirectory$ = pathFS => - stat$(pathFS).map(stat => stat.isDirectory()); + stat$(pathFS).map(_stat => _stat.isDirectory()); export const filter$ = curry((predicate, value) => predicate(value) diff --git a/src/util/test/cli.js b/src/util/test/cli.js index 336a0fe..0ff98a8 100644 --- a/src/util/test/cli.js +++ b/src/util/test/cli.js @@ -34,13 +34,14 @@ test('should create etcd directory if not exists', t => { }] }); - return syncDirectory$(client, join(__dirname, 'fixtures/fs'), '/test').toArray().toPromise().then(events => { + return syncDirectory$(client, join(__dirname, 'fixtures/fs'), '/test').toArray().do(events => { t.deepEqual(xor(events, ['foo', 'bar']), []); t.plan(3); - }); + }).toPromise(); }); -test('should throw error on directory sync if etcd throw unknown error', t => { +test('should throw error on directory sync if etcd throw unknown error', async t => { + t.plan(2); const client = createEtcdMock({ get: [{ assert: key => @@ -54,14 +55,17 @@ test('should throw error on directory sync if etcd throw unknown error', t => { }] }); - return syncDirectory$(client, join(__dirname, 'fixtures/fs'), '/test').toArray().toPromise().then(() => - t.fail() - , () => - Promise.resolve() - ); + try { + await syncDirectory$(client, join(__dirname, 'fixtures/fs'), '/test').toArray().toPromise(); + t.fail(); + } + catch (err) { + t.pass(); + } }); test('should remove extra entry of directory', t => { + t.plan(3); const client = createEtcdMock({ get: [{ assert: key => @@ -85,13 +89,13 @@ test('should remove extra entry of directory', t => { }] }); - syncDirectory$(client, join(__dirname, 'fixtures/fs'), '/test').toArray().toPromise().then(events => { + return syncDirectory$(client, join(__dirname, 'fixtures/fs'), '/test').toArray().do(events => { t.deepEqual(xor(events, ['bar', 'foo']), []); - t.plan(3); - }); + }).toPromise(); }); -test('should create file if doesn\t exists', t => { +test('should create file if doesn\t exists', async t => { + t.plan(4); const client = createEtcdMock({ get: [{ assert: key => @@ -113,13 +117,12 @@ test('should create file if doesn\t exists', t => { }] }); - return syncFile$(client, join(__dirname, 'fixtures/fs/foo'), '/test/foo').toArray().toPromise().then(events => { - t.deepEqual(events, []); - t.plan(4); - }); + const events = await syncFile$(client, join(__dirname, 'fixtures/fs/foo'), '/test/foo').toArray().toPromise(); + t.deepEqual(events, []); }); -test('should throw error on file sync if etcd throw unknown error', t => { +test('should throw error on file sync if etcd throw unknown error', async t => { + t.plan(2); const client = createEtcdMock({ get: [{ assert: key => @@ -132,14 +135,17 @@ test('should throw error on file sync if etcd throw unknown error', t => { }] }); - return syncFile$(client, join(__dirname, 'fixtures/fs/foo'), '/test/foo').toArray().toPromise().then(() => - t.fail() - , () => - Promise.resolve() - ); + try { + await syncFile$(client, join(__dirname, 'fixtures/fs/foo'), '/test/foo').toArray().toPromise(); + t.fail(); + } + catch (err) { + t.pass(); + } }); -test('should remove directory if it should be a file', t => { +test('should remove directory if it should be a file', async t => { + t.plan(6); const client = createEtcdMock({ get: [{ assert: key => @@ -185,13 +191,12 @@ test('should remove directory if it should be a file', t => { }] }); - return syncFile$(client, join(__dirname, 'fixtures/fs/foo'), '/test/foo').toArray().toPromise().then(events => { - t.deepEqual(events, []); - t.plan(6); - }); + const events = await syncFile$(client, join(__dirname, 'fixtures/fs/foo'), '/test/foo').toArray().toPromise(); + t.deepEqual(events, []); }); -test('should remove file if it should be a directory', t => { +test('should remove file if it should be a directory', async t => { + t.plan(4); const client = createEtcdMock({ get: [{ assert: key => @@ -232,13 +237,12 @@ test('should remove file if it should be a directory', t => { }] }); - return syncDirectory$(client, join(__dirname, 'fixtures/fs/bar'), '/test/bar').toArray().toPromise().then(events => { - t.deepEqual(events, ['baz']); - t.plan(4); - }); + const events = await syncDirectory$(client, join(__dirname, 'fixtures/fs/bar'), '/test/bar').toArray().toPromise(); + t.deepEqual(events, ['baz']); }); -test('should update file if it\'s outdated', t => { +test('should update file if it\'s outdated', async t => { + t.plan(4); const client = createEtcdMock({ get: [{ assert: key => @@ -270,13 +274,12 @@ test('should update file if it\'s outdated', t => { }] }); - return syncFile$(client, join(__dirname, 'fixtures/fs/foo'), '/test/foo').toArray().toPromise().then(events => { - t.deepEqual(events, []); - t.plan(4); - }); + const events = await syncFile$(client, join(__dirname, 'fixtures/fs/foo'), '/test/foo').toArray().toPromise(); + t.deepEqual(events, []); }); -test('shouldn\'t update file if it\'s updated', t => { +test('shouldn\'t update file if it\'s updated', async t => { + t.plan(2); const client = createEtcdMock({ get: [{ assert: key => @@ -295,13 +298,12 @@ test('shouldn\'t update file if it\'s updated', t => { }] }); - return syncFile$(client, join(__dirname, 'fixtures/fs/foo'), '/test/foo').toArray().toPromise().then(events => { - t.deepEqual(events, []); - t.plan(2); - }); + const events = await syncFile$(client, join(__dirname, 'fixtures/fs/foo'), '/test/foo').toArray().toPromise(); + t.deepEqual(events, []); }); -test('should loop on sync', t => { +test('should loop on sync', async t => { + t.plan(7); const client = createEtcdMock({ get: [{ assert: key => @@ -361,7 +363,5 @@ test('should loop on sync', t => { }] }); - return sync$(client, join(__dirname, 'fixtures/fs'), '/test').toPromise().then(() => { - t.plan(7); - }); + await sync$(client, join(__dirname, 'fixtures/fs'), '/test').toPromise(); }); diff --git a/src/util/test/etcd.js b/src/util/test/etcd.js index 017b093..27e2fe9 100644 --- a/src/util/test/etcd.js +++ b/src/util/test/etcd.js @@ -14,7 +14,7 @@ import { isFile } from '../etcd'; -const action = (action, node, prevNode) => ({action, node, prevNode}); +const action = (_action, _node, _prevNode) => ({action: _action, node: _node, prevNode: _prevNode}); const node = (key, value, dir) => ({key, value, dir}); const headers = () => ({'content-type': 'application/json'}); @@ -75,9 +75,9 @@ const headers = () => ({'content-type': 'application/json'}); argz: ['foo', 'foo'], results: [action('set', node('/foo', 'foo'), node('/foo', 'bar')), headers] }].forEach(({title, fn$, fn, argz, expected, results}) => { - test(title, t => { + test(title, async t => { t.plan(3); - return fn$({ + const events = await fn$({ [fn]: (..._argz) => { const cb = _argz.pop(); t.deepEqual(expected || argz, _argz); @@ -88,9 +88,8 @@ const headers = () => ({'content-type': 'application/json'}); } }; } - }, ...argz).toArray().toPromise().then(events => - t.deepEqual(events, slice(0, results.length - 1, results)) - ); + }, ...argz).toArray().toPromise(); + t.deepEqual(events, slice(0, results.length - 1, results)); }); test(`${title} be cancellable`, t => { @@ -106,22 +105,25 @@ const headers = () => ({'content-type': 'application/json'}); }, ...argz).subscribe().unsubscribe(); }); - test(`${title} raise error`, t => { - return fn$({ - [fn]: (..._argz) => { - const cb = _argz.pop(); - cb(new Error('za')); - return { - abort: () => { - t.pass(); - } - }; - } - }, ...argz).toPromise().then(() => { - throw new Error(); - }, () => { - return Promise.resolve(); - }); + test(`${title} raise error`, async t => { + t.plan(2); + try { + await fn$({ + [fn]: (..._argz) => { + const cb = _argz.pop(); + cb(new Error('za')); + return { + abort: () => { + t.pass(); + } + }; + } + }, ...argz).toPromise(); + t.fail(); + } + catch (err) { + t.pass(); + } }); }); diff --git a/src/util/test/fs.js b/src/util/test/fs.js index d9f5973..fbab9fb 100644 --- a/src/util/test/fs.js +++ b/src/util/test/fs.js @@ -1,7 +1,6 @@ import {join} from 'path'; import {xor, map, isBuffer} from 'lodash/fp'; import test from 'ava'; - import {isFile$, readdir$, isDirectory$, filter$, readFile$, readFileUTF8$} from '../fs'; const joinTestFolder = (...argz) => join(__dirname, 'fixtures/fs', ...argz); @@ -61,16 +60,14 @@ test('should filterDirectory directory content', t => ) ); -test('should read file', t => - readFile$(joinTestFolder('foo')).toPromise().then(data => { - t.true(isBuffer(data)); - t.deepEqual(data.toString(), 'foo'); - }) -); +test('should read file', async t => { + const data = await readFile$(joinTestFolder('foo')).toPromise(); + t.true(isBuffer(data)); + t.deepEqual(data.toString(), 'foo'); +}); -test('should read UTF8 file', t => - readFileUTF8$(joinTestFolder('foo')).toPromise().then(data => { - t.falsy(isBuffer(data)); - t.deepEqual(data, 'foo'); - }) -); +test('should read UTF8 file', async t => { + const data = await readFileUTF8$(joinTestFolder('foo')).toPromise(); + t.falsy(isBuffer(data)); + t.deepEqual(data, 'foo'); +}); diff --git a/src/util/test/helpers/etcd.js b/src/util/test/helpers/etcd.js index b9d6c7c..e62e617 100644 --- a/src/util/test/helpers/etcd.js +++ b/src/util/test/helpers/etcd.js @@ -1,4 +1,4 @@ -import EventEmitter from 'events'; +import EventEmitter from 'events'; // eslint-disable-line fp/no-events import {assign, mapValues, noop, pipe} from 'lodash/fp'; const createWatcher = () => () => {