Skip to content

Commit

Permalink
Merge pull request #372 from mixonic/eager-ignore
Browse files Browse the repository at this point in the history
Eager ignore
  • Loading branch information
okuryu committed Jan 13, 2016
2 parents e8606b1 + 5c361c5 commit d68d714
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 36 deletions.
69 changes: 36 additions & 33 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,27 +336,45 @@ YUI.add('utils', function (Y) {

Y.getProjectData = getProjectData;


/**
* Walks the tree from this dir and returns all the subdirs
* @method getDirs
* @param {String} dir The dir to begin at
* @param {String} baseDir The dir to begin at
* @param {Array} ignore An array of paths to ignore
* @return {Array} The array of directories..
*/
var getDirs = function (dir) {
var dirs = fs.readdirSync(dir),
paths = [];
var getDirs = function (baseDir, ignore) {
var inputPaths = [],
paths = [],
i, d, isIgnored, subpath,
stat, possibleDirs, fullPath;

dirs.forEach(function (d) {
var _dir = path.join(dir, d),
stat = fs.lstatSync(_dir);
var inputPath = '';
while (inputPath !== undefined) {
fullPath = path.join(baseDir, inputPath);
stat = fs.lstatSync(fullPath);

if (stat.isDirectory()) {
if (_dir.indexOf('.') !== 0) {
paths = [].concat(paths, _dir, getDirs(_dir));
if (fullPath !== baseDir) {
paths.push(fullPath);
}
possibleDirs = fs.readdirSync(fullPath);
for (d=0;d<possibleDirs.length;d++) {
subpath = path.join(inputPath, possibleDirs[d]);
isIgnored = false;
for (i=0;i<ignore.length;i++) {
if (subpath.indexOf(ignore[i]) === 0) {
isIgnored = true;
break;
}
}
if (!isIgnored) {
inputPaths.push(subpath);
}
}
}
});
inputPath = inputPaths.pop();
}

return paths;
};
Expand All @@ -370,6 +388,12 @@ YUI.add('utils', function (Y) {
* @param {String} [ignore=false] A string to call `.indexOf` on a path to determine if it should be ignored
*/
var validatePaths = function (paths, ignore) {
if (!ignore) {
ignore = [];
} else if (!(ignore instanceof Array)) {
ignore = [ignore];
}

var newpaths = [];
//Shortcut the *, . & ./ shortcuts that shall globbing fixes for us
if (paths === '*' || paths === '.' || paths === './') {
Expand All @@ -388,7 +412,7 @@ YUI.add('utils', function (Y) {
glob = validatePath.replace(/\//g, '\\\\');
}

var glob_paths = getDirs('.'),
var glob_paths = getDirs('.', ignore),
is_globbed = false;

glob_paths.forEach(function (dir) {
Expand Down Expand Up @@ -426,27 +450,6 @@ YUI.add('utils', function (Y) {
throw ('Paths should be an array of paths');
}

if (ignore) {
if (!(ignore instanceof Array)) {
ignore = [ignore];
}
var p = [],
shouldIgnore = false;

paths.forEach(function (v) {
shouldIgnore = false;
ignore.forEach(function (i) {
if (!shouldIgnore && v.indexOf(i) !== -1) {
shouldIgnore = true;
}
});
if (!shouldIgnore) {
p.push(v);
}
});
paths = p;
}

paths = paths.sort();
return paths;
};
Expand Down
7 changes: 6 additions & 1 deletion tests/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ var suite = new YUITest.TestSuite({

options = {
quiet: true,
paths: ['input/'],
paths: [
'input/inherit',
'input/json',
'input/test'
],
outdir: './out',
helpers: [
path.join(__dirname, 'lib/davglass.js')
Expand All @@ -37,6 +41,7 @@ var suite = new YUITest.TestSuite({
langPrefix: 'language-'
}
};
process.chdir(__dirname);
json = (new Y.YUIDoc(options)).run();

this.project = json.project;
Expand Down
Empty file.
1 change: 1 addition & 0 deletions tests/input/with-symlink/a/b
Empty file.
Empty file.
Empty file.
Empty file.
9 changes: 8 additions & 1 deletion tests/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ var existsSync = fs.existsSync || path.existsSync;
var suite = new YUITest.TestSuite({
name: 'Parser Test Suite',
setUp: function () {
process.chdir(__dirname);
var json = (new Y.YUIDoc({
quiet: true,
paths: ['input/'],
paths: [
'input/charts',
'input/inherit',
'input/namespace',
'input/test',
'input/test2'
],
outdir: './out'
})).run();

Expand Down
64 changes: 63 additions & 1 deletion tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ var path = require('path');
var Y = require(path.join(__dirname, '../', 'lib', 'index'));

var suite = new YUITest.TestSuite({
name: 'Utils Test Suite'
name: 'Utils Test Suite',
setUp: function () {
process.chdir(__dirname);
},
tearDown: function () {
process.chdir(__dirname);
}
});

suite.add(new YUITest.TestCase({
Expand Down Expand Up @@ -74,6 +80,25 @@ suite.add(new YUITest.TestCase({

Assert.isArray(options.paths, 'Failed to set path');
Assert.areSame(3, options.paths.length, 'Failed to retrieve all path options');
},
'test: ignore paths': function () {
var options;

process.chdir(path.join(__dirname, 'input/with-symlink'));

// Simulate a path provided by a configuration
options = {
paths: 'a',
ignorePaths: [
'a/d',
'c'
]
};
options = Y.Project.init(options);

Assert.isArray(options.paths, 'paths are present');
Assert.areSame(1, options.paths.length, 'one path present');
Assert.areSame('a', options.paths[0], 'path a is in paths');
}
}));

Expand All @@ -93,4 +118,41 @@ suite.add(new YUITest.TestCase({
}
}));

suite.add(new YUITest.TestCase({
name: 'getDirs',
setUp: function () {
process.chdir(__dirname);
},
'test: gets paths as array': function() {
process.chdir(__dirname);
var pathPrefix = __dirname + '/input/folders1';
var dirs = Y.getDirs(pathPrefix, []);
Assert.isArray(dirs);
Assert.areSame(2, dirs.length);
Assert.areNotSame(-1, dirs.indexOf(pathPrefix + '/one'), 'contains path /one');
Assert.areNotSame(-1, dirs.indexOf(pathPrefix + '/one/two'), 'contains path /one/two');
},
'test: gets paths from . as array': function() {
var pathPrefix = __dirname + '/input/folders1';
process.chdir(pathPrefix);
var dirs = Y.getDirs('.', []);
Assert.isArray(dirs);
Assert.areSame(2, dirs.length);
Assert.areNotSame(-1, dirs.indexOf('one'), 'contains path /one');
Assert.areNotSame(-1, dirs.indexOf('one/two'), 'contains path /one/two');
},
'test: ignores paths': function() {
process.chdir(__dirname);
var pathPrefix = __dirname + '/input/with-symlink';
var dirs = Y.getDirs(pathPrefix, ['c']);
Assert.isArray(dirs);
Assert.areSame(2, dirs.length);
Assert.areSame(-1, dirs.indexOf(pathPrefix + '.gitignore'), 'does not contain file some-file');
Assert.areSame(-1, dirs.indexOf(pathPrefix + '/a/b'), 'does not contain symlink /a/b');
Assert.areSame(-1, dirs.indexOf(pathPrefix + '/c'), 'does not contain path /c');
Assert.areNotSame(-1, dirs.indexOf(pathPrefix + '/a'), 'contains path /a');
Assert.areNotSame(-1, dirs.indexOf(pathPrefix + '/a/d'), 'contains path /a/d');
}
}));

YUITest.TestRunner.add(suite);

0 comments on commit d68d714

Please sign in to comment.