Skip to content

Commit

Permalink
return absolute paths only if project root is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
rostik404 committed Dec 19, 2016
1 parent fd160fc commit 5b6b117
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 105 deletions.
4 changes: 2 additions & 2 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const _ = require('lodash');

module.exports = (options) => {
return _.defaults(options || {}, {
formats: [],
root: process.cwd()
root: null,
formats: []
});
};
21 changes: 4 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,23 @@ const _ = require('lodash');
const glob = require('glob');
const defaults = require('./defaults');
const utils = require('./utils');
const resolvePath = require('path').resolve;

const isMask = (pattern) => glob.hasMagic(pattern);

const getFilesByMask = (pattern, options) => {
return q.nfcall(glob, pattern, options)
.then((paths) => {
return _.isEmpty(paths) && !isMask(pattern)
? q.reject(new Error(`Cannot find files using path '${pattern}'`))
: paths;
});
};
const getFilesByMask = (pattern, options) => q.nfcall(glob, pattern, options);

const listFiles = (path) => {
return qfs.listTree(path)
.then((paths) => utils.asyncFilter(paths, utils.isFile));
};

const expandPath = (path, options) => {
if (!qfs.isAbsolute(path)) {
path = qfs.join(options.root, path);
}
path = options.root ? resolvePath(options.root, path) : path;

return utils.isFile(path)
.then((isFile) => isFile ? [path] : listFiles(path))
.then((paths) => paths.filter((path) => utils.matchesFormats(path, options.formats)))
.then((paths) => paths.map((path) => qfs.absolute(path)));
.then((paths) => paths.filter((path) => utils.matchesFormats(path, options.formats)));
};

const processPaths = (paths, cb) => {
Expand All @@ -53,7 +44,3 @@ exports.expandPaths = (paths, expandOpts, globOpts) => {
};

exports.isMask = isMask;

// :TODO: Backward compatibility
exports.isMasks = (patterns) => [].concat(patterns).every(isMask);

157 changes: 71 additions & 86 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const proxyquire = require('proxyquire');
const qfs = require('q-io/fs');
const q = require('q');
const utils = require('../lib/utils');

describe('path-utils', () => {
const sandbox = sinon.sandbox.create();
Expand All @@ -27,25 +28,11 @@ describe('path-utils', () => {
sandbox.stub(qfs, 'absolute');
});

it('should get absolute file path from passed mask', () => {
it('should get file path from passed mask', () => {
glob.withArgs('some/deep/**/*.js').yields(null, ['some/deep/path/file.js']);

qfs.absolute.withArgs('some/deep/path/file.js').returns('/absolute/some/deep/path/file.js');

return globExtra.expandPaths(['some/deep/**/*.js'])
.then((absolutePaths) => {
assert.deepEqual(absolutePaths, ['/absolute/some/deep/path/file.js']);
});
});

it('should throw error for unexistent file path', () => {
glob.withArgs('bad/mask/file.js').yields(null, []);
return assert.isRejected(globExtra.expandPaths(['bad/mask/file.js']), 'Cannot find files by mask bad/mask/file.js');
});

it('should throw error for unexistent directory path', () => {
glob.withArgs('bad/mask').yields(null, []);
return assert.isRejected(globExtra.expandPaths(['bad/mask']), 'Cannot find files by mask bad/mask');
.then((paths) => assert.deepEqual(paths, ['some/deep/path/file.js']));
});

it('should ignore masks which do not match to files', () => {
Expand All @@ -57,29 +44,24 @@ describe('path-utils', () => {
return globExtra.expandPaths([
'bad/mask/*.js',
'some/path/*.js'
]).then((absolutePaths) => assert.deepEqual(absolutePaths, ['some/path/file.js']));
]).then((paths) => assert.deepEqual(paths, ['some/path/file.js']));
});

it('should get absolute file path from passed mask according to formats option', () => {
it('should get file path from passed mask according to formats option', () => {
glob.withArgs('some/path/*.*').yields(null, ['some/path/file.js', 'some/path/file.txt']);

qfs.absolute
.withArgs('some/path/file.js').returns('/absolute/some/path/file.js');

return globExtra.expandPaths(['some/path/*.*'], {formats: ['.js']})
.then((absolutePaths) => {
assert.deepEqual(absolutePaths, ['/absolute/some/path/file.js']);
.then((paths) => {
assert.deepEqual(paths, ['some/path/file.js']);
});
});

it('should get uniq absolute file path from passed masks', () => {
it('should get uniq file path from passed masks', () => {
glob.withArgs('some/path/*.js').yields(null, ['some/path/file.js']);

qfs.absolute.withArgs('some/path/file.js').returns('/absolute/some/path/file.js');

return globExtra.expandPaths(['some/path/*.js', 'some/path/*.js'])
.then((absolutePaths) => {
assert.deepEqual(absolutePaths, ['/absolute/some/path/file.js']);
.then((paths) => {
assert.deepEqual(paths, ['some/path/file.js']);
});
});
});
Expand All @@ -90,52 +72,46 @@ describe('path-utils', () => {
sandbox.stub(qfs, 'absolute');
});

it('should get absolute paths for all files from passed dir', () => {
glob.withArgs('some/path/').yields(null, ['some/path/']);
it('should get paths for all files from passed dir', () => {
glob.withArgs('some/path').yields(null, ['some/path']);

qfs.listTree.withArgs('some/path').returns(q(['some/path/first.js', 'some/path/second.txt']));
qfs.absolute
.withArgs('some/path/first.js').returns('/absolute/some/path/first.js')
.withArgs('some/path/second.txt').returns('/absolute/some/path/second.txt');

return globExtra.expandPaths(['some/path/'])
.then((absolutePaths) => {
assert.deepEqual(absolutePaths, ['/absolute/some/path/first.js', '/absolute/some/path/second.txt']);
return globExtra.expandPaths(['some/path'])
.then((paths) => {
assert.deepEqual(paths, ['some/path/first.js', 'some/path/second.txt']);
});
});

it('should get absolute file paths according to formats option', () => {
glob.withArgs('some/path/').yields(null, ['some/path/']);
it('should get file paths according to formats option', () => {
glob.withArgs('some/path').yields(null, ['some/path']);

qfs.listTree.withArgs('some/path').returns(q(['some/path/first.js', 'some/path/second.txt']));
qfs.absolute.withArgs('some/path/first.js').returns('/absolute/some/path/first.js');

return globExtra.expandPaths(['some/path/'], {formats: ['.js']})
.then((absolutePaths) => assert.deepEqual(absolutePaths, ['/absolute/some/path/first.js']));
return globExtra.expandPaths(['some/path'], {formats: ['.js']})
.then((paths) => assert.deepEqual(paths, ['some/path/first.js']));
});

it('should get uniq absolute file path from passed dirs', () => {
glob.withArgs('some/path/').yields(null, ['some/path/']);
glob.withArgs('some/path').yields(null, ['some/path']);

qfs.listTree.withArgs('some/path').returns(q(['some/path/file.js']));
qfs.absolute.withArgs('some/path/file.js').returns('/absolute/some/path/file.js');

return globExtra.expandPaths(['some/path/', 'some/path/'])
.then((absolutePaths) => {
assert.deepEqual(absolutePaths, ['/absolute/some/path/file.js']);
return globExtra.expandPaths(['some/path', 'some/path'])
.then((paths) => {
assert.deepEqual(paths, ['some/path/file.js']);
});
});

it('should get only file paths from dir tree', () => {
glob.withArgs('some/path/').yields(null, ['some/path/']);
glob.withArgs('some/path').yields(null, ['some/path']);

qfs.stat.withArgs('some/path/dir').returns(q({isFile: () => false}));
qfs.listTree.withArgs('some/path').returns(q(['some/path/file.js', 'some/path/dir']));
qfs.absolute.withArgs('some/path/file.js').returns('/absolute/some/path/file.js');

return globExtra.expandPaths(['some/path/'])
.then((absolutePaths) => {
assert.deepEqual(absolutePaths, ['/absolute/some/path/file.js']);
return globExtra.expandPaths(['some/path'])
.then((paths) => {
assert.deepEqual(paths, ['some/path/file.js']);
});
});
});
Expand All @@ -145,25 +121,21 @@ describe('path-utils', () => {
sandbox.stub(qfs, 'absolute');
});

it('should get absolute file path from passed string file path', () => {
it('should get file path from passed string file path', () => {
glob.withArgs('some/path/file.js').yields(null, ['some/path/file.js']);

qfs.absolute.withArgs('some/path/file.js').returns('/absolute/some/path/file.js');

return globExtra.expandPaths('some/path/file.js')
.then((absolutePaths) => {
assert.deepEqual(absolutePaths, ['/absolute/some/path/file.js']);
.then((paths) => {
assert.deepEqual(paths, ['some/path/file.js']);
});
});

it('should get absolute file path from passed file path', () => {
it('should get file path from passed file path', () => {
glob.withArgs('some/path/file.js').yields(null, ['some/path/file.js']);

qfs.absolute.withArgs('some/path/file.js').returns('/absolute/some/path/file.js');

return globExtra.expandPaths(['some/path/file.js'])
.then((absolutePaths) => {
assert.deepEqual(absolutePaths, ['/absolute/some/path/file.js']);
.then((paths) => {
assert.deepEqual(paths, ['some/path/file.js']);
});
});

Expand All @@ -172,23 +144,18 @@ describe('path-utils', () => {
.withArgs('some/path/file.js').yields(null, ['some/path/file.js'])
.withArgs('some/path/file.txt').yields(null, ['some/path/file.txt']);

qfs.absolute
.withArgs('some/path/file.js').returns('/absolute/some/path/file.js');

return globExtra.expandPaths(['some/path/file.js', 'some/path/file.txt'], {formats: ['.js']})
.then((absolutePaths) => {
assert.deepEqual(absolutePaths, ['/absolute/some/path/file.js']);
.then((paths) => {
assert.deepEqual(paths, ['some/path/file.js']);
});
});

it('should get uniq absolute file path', () => {
glob.withArgs('some/path/file.js').yields(null, ['some/path/file.js']);

qfs.absolute.withArgs('some/path/file.js').returns('/absolute/some/path/file.js');

return globExtra.expandPaths(['some/path/file.js', 'some/path/file.js'])
.then((absolutePaths) => {
assert.deepEqual(absolutePaths, ['/absolute/some/path/file.js']);
.then((paths) => {
assert.deepEqual(paths, ['some/path/file.js']);
});
});
});
Expand All @@ -201,22 +168,8 @@ describe('path-utils', () => {
qfs.listTree.withArgs('/project/root/some/path').returns(q(['/project/root/some/path/file.js']));

return globExtra.expandPaths(['some/path/'], {root: '/project/root'})
.then((absolutePaths) => {
assert.deepEqual(absolutePaths, ['/project/root/some/path/file.js']);
});
});

it('should use current project directory if project root is not passed from root option', () => {
glob.withArgs('some/path/').yields(null, ['some/path/']);

process.cwd.returns('/root');

qfs.stat.withArgs('/root/some/path').returns(q({isFile: () => false}));
qfs.listTree.withArgs('/root/some/path').returns(q(['/root/some/path/file.js']));

return globExtra.expandPaths(['some/path/'])
.then((absolutePaths) => {
assert.deepEqual(absolutePaths, ['/root/some/path/file.js']);
.then((paths) => {
assert.deepEqual(paths, ['/project/root/some/path/file.js']);
});
});
});
Expand Down Expand Up @@ -244,4 +197,36 @@ describe('path-utils', () => {
assert.isNotOk(globExtra.isMask('some/path/file.js'));
});
});

describe('project root', () => {
beforeEach(() => sandbox.stub(utils, 'isFile').returns(q(true)));

it('shout resolve relative paths using project root', () => {
glob.withArgs('some/path').yields(null, ['some/path/file.js']);

return globExtra.expandPaths('some/path', {root: '/root'})
.then(() => assert.calledWith(utils.isFile, '/root/some/path/file.js'));
});

it('should not resolve absolute paths using project root', () => {
glob.withArgs('/absolute/some/path').yields(null, ['/absolute/some/path/file.js']);

return globExtra.expandPaths('/absolute/some/path', {root: '/root'})
.then(() => assert.calledWith(utils.isFile, '/absolute/some/path/file.js'));
});

it('should use relative paths if project root is not specified', () => {
glob.withArgs('some/path').yields(null, ['some/path/file.js']);

return globExtra.expandPaths('some/path')
.then(() => assert.calledWith(utils.isFile, 'some/path/file.js'));
});

it('should use absolute paths if project roout is not specified', () => {
glob.withArgs('/absolute/some/path').yields(null, ['/absolute/some/path/file.js']);

return globExtra.expandPaths('/absolute/some/path')
.then(() => assert.calledWith(utils.isFile, '/absolute/some/path/file.js'));
});
});
});

0 comments on commit 5b6b117

Please sign in to comment.