diff --git a/src/datalayout.js b/src/datalayout.js index 329dd0fd0..e32dd91e5 100644 --- a/src/datalayout.js +++ b/src/datalayout.js @@ -6,6 +6,7 @@ const assert = require('assert'), class DataLayout { constructor(localRoot, dirMap) { assert.strictEqual(typeof localRoot, 'string'); + assert(path.isAbsolute(localRoot)); assert(Array.isArray(dirMap), 'Expecting layout to be an array'); this._localRoot = localRoot; @@ -13,6 +14,7 @@ class DataLayout { this._remoteRegexps = dirMap.map((l) => new RegExp('^\\./' + l.remoteDir + '/?')); this._localRegexps = dirMap.map((l) => new RegExp('^' + l.localDir + '/?')); } + // returns absolute path toLocalPath(remoteName) { assert.strictEqual(typeof remoteName, 'string'); diff --git a/src/test/datalayout-test.js b/src/test/datalayout-test.js new file mode 100644 index 000000000..96f9f23da --- /dev/null +++ b/src/test/datalayout-test.js @@ -0,0 +1,87 @@ +'use strict'; + +/* global it:false */ +/* global describe:false */ +/* global before:false */ + +'use strict'; + +const DataLayout = require('../datalayout.js'), + expect = require('expect.js'); + +describe('DataLayout', function () { + describe('no dirMap', function () { + const BOX_DATA_DIR = '/home/yellowtent/boxdata/box'; + const dataLayout = new DataLayout(BOX_DATA_DIR, []); + + it('localRoot', function () { + expect(dataLayout.localRoot()).to.be(BOX_DATA_DIR); + }); + + it('getBasename', function () { + expect(dataLayout.getBasename()).to.be('box'); + }); + + it('localPaths', function () { + expect(dataLayout.localPaths()).to.eql([ BOX_DATA_DIR ]); + }); + + it('toLocalPath', function () { + expect(dataLayout.toLocalPath('./s1')).to.be(`${BOX_DATA_DIR}/s1`); + expect(dataLayout.toLocalPath('./s1/')).to.be(`${BOX_DATA_DIR}/s1/`); + expect(dataLayout.toLocalPath('./s1/s2')).to.be(`${BOX_DATA_DIR}/s1/s2`); + }); + + it('toRemotePath', function () { + expect(dataLayout.toRemotePath(`${BOX_DATA_DIR}`)).to.be('./'); + expect(dataLayout.toRemotePath(`${BOX_DATA_DIR}/s1`)).to.be('./s1'); + expect(dataLayout.toRemotePath(`${BOX_DATA_DIR}/s1/`)).to.be('./s1/'); + expect(dataLayout.toRemotePath(`${BOX_DATA_DIR}/s1/s2`)).to.be('./s1/s2'); + }); + }); + + describe('with dirMap', function () { + const APP_DATA_DIR = '/home/yellowtent/appsdata/appid'; + const EXT_DIR = '/srv/hsb'; + const dataLayout = new DataLayout(APP_DATA_DIR, [ { localDir: EXT_DIR, remoteDir: 'data' }]); + + it('localRoot', function () { + expect(dataLayout.localRoot()).to.be(APP_DATA_DIR); + }); + + it('getBasename', function () { + expect(dataLayout.getBasename()).to.be('appid'); + }); + + it('localPaths', function () { + expect(dataLayout.localPaths()).to.eql([ APP_DATA_DIR, '/srv/hsb' ]); + }); + + it('toLocalPath - root', function () { + expect(dataLayout.toLocalPath('./s1')).to.be(`${APP_DATA_DIR}/s1`); + expect(dataLayout.toLocalPath('./s1/')).to.be(`${APP_DATA_DIR}/s1/`); + expect(dataLayout.toLocalPath('./s1/s2')).to.be(`${APP_DATA_DIR}/s1/s2`); + }); + + it('toLocalPath - extdir', function () { + expect(dataLayout.toLocalPath('./data/s1')).to.be(`${EXT_DIR}/s1`); + expect(dataLayout.toLocalPath('./data/s1/')).to.be(`${EXT_DIR}/s1/`); + expect(dataLayout.toLocalPath('./data/s1/s2')).to.be(`${EXT_DIR}/s1/s2`); + }); + + it('toRemotePath - root', function () { + expect(dataLayout.toRemotePath(`${APP_DATA_DIR}`)).to.be('./'); + expect(dataLayout.toRemotePath(`${APP_DATA_DIR}/s1`)).to.be('./s1'); + expect(dataLayout.toRemotePath(`${APP_DATA_DIR}/s1/`)).to.be('./s1/'); + expect(dataLayout.toRemotePath(`${APP_DATA_DIR}/s1/s2`)).to.be('./s1/s2'); + }); + + it('toRemotePath - extdir', function () { + expect(dataLayout.toRemotePath(`${EXT_DIR}`)).to.be('./data/'); + expect(dataLayout.toRemotePath(`${EXT_DIR}/s1`)).to.be('./data/s1'); + expect(dataLayout.toRemotePath(`${EXT_DIR}/s1/`)).to.be('./data/s1/'); + expect(dataLayout.toRemotePath(`${EXT_DIR}/s1/s2`)).to.be('./data/s1/s2'); + }); + }); + +}); \ No newline at end of file