From 356a9edb757209227b3e818895c2fd490d33b15a Mon Sep 17 00:00:00 2001 From: utensil Date: Sat, 19 Apr 2014 21:30:52 +0800 Subject: [PATCH 1/9] add grep glob test, * for file name, passes --- test/grep.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/grep.js b/test/grep.js index c3b8642..1980cd0 100644 --- a/test/grep.js +++ b/test/grep.js @@ -48,4 +48,9 @@ var result = shell.grep(/test/, ['resources/file1.txt', 'resources/file2.txt']); assert.equal(shell.error(), null); assert.equal(result, 'test1\ntest2\n'); +// multiple files, glob syntax, * for file name +var result = shell.grep(/test/, 'resources/file*.txt'); +assert.equal(shell.error(), null); +assert.equal(result, 'test1\ntest2\n'); + shell.exit(123); From c8d63ef675e11279038cad9ea95b142321f83a7d Mon Sep 17 00:00:00 2001 From: utensil Date: Sat, 19 Apr 2014 21:31:46 +0800 Subject: [PATCH 2/9] add grep glob test, ** for dir name, fails it --- test/grep.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/grep.js b/test/grep.js index 1980cd0..8b94bc6 100644 --- a/test/grep.js +++ b/test/grep.js @@ -53,4 +53,14 @@ var result = shell.grep(/test/, 'resources/file*.txt'); assert.equal(shell.error(), null); assert.equal(result, 'test1\ntest2\n'); +// multiple files, glob syntax, * for directory name +var result = shell.grep(/test/, '*/file*.txt'); +assert.equal(shell.error(), null); +assert.equal(result, 'test1\ntest2\n'); + +// multiple files, glob syntax, ** for directory name +var result = shell.grep(/test/, '*/file*.txt'); +assert.equal(shell.error(), null); +assert.equal(result, 'test1\ntest2\n'); + shell.exit(123); From a2549f8589cb62e035a898413dea554d288c52f3 Mon Sep 17 00:00:00 2001 From: utensil Date: Sat, 19 Apr 2014 21:47:10 +0800 Subject: [PATCH 3/9] fix typo --- test/grep.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/grep.js b/test/grep.js index 8b94bc6..ce17023 100644 --- a/test/grep.js +++ b/test/grep.js @@ -59,7 +59,7 @@ assert.equal(shell.error(), null); assert.equal(result, 'test1\ntest2\n'); // multiple files, glob syntax, ** for directory name -var result = shell.grep(/test/, '*/file*.txt'); +var result = shell.grep(/test/, '**/file*.txt'); assert.equal(shell.error(), null); assert.equal(result, 'test1\ntest2\n'); From 668bd168c7f1b2eb91bead3ebda0888c390353e9 Mon Sep 17 00:00:00 2001 From: utensil Date: Sat, 19 Apr 2014 21:48:13 +0800 Subject: [PATCH 4/9] add test for common.expand() and pass it --- test/common.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test/common.js diff --git a/test/common.js b/test/common.js new file mode 100644 index 0000000..6939f1f --- /dev/null +++ b/test/common.js @@ -0,0 +1,41 @@ +var shell = require('..'); +var common = require('../src/common'); + +var assert = require('assert'); + +shell.config.silent = true; + +shell.rm('-rf', 'tmp'); +shell.mkdir('tmp'); + +// +// Invalids +// + +// too few args +assert.throws(function () { + common.expand(); +}, TypeError); + +// should be a list +assert.throws(function () { + common.expand("resources"); +}, TypeError); + +// +// Valids +// + +// single file, array syntax +var result = common.expand(['resources/file1.txt']); +assert.equal(shell.error(), null); +assert.deepEqual(result, ['resources/file1.txt']); + +// multiple file, glob syntax, * for file name +var result = common.expand(['resources/file*.txt']); +assert.equal(shell.error(), null); +assert.deepEqual(result, ['resources/file1.txt', 'resources/file2.txt']); + +shell.exit(123); + + From 98aad77ea450e55a1d843b11c4674fe367aee287 Mon Sep 17 00:00:00 2001 From: utensil Date: Sat, 19 Apr 2014 21:51:56 +0800 Subject: [PATCH 5/9] add ** glob tests for common.expand() and fail it --- test/common.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/common.js b/test/common.js index 6939f1f..b4517ef 100644 --- a/test/common.js +++ b/test/common.js @@ -36,6 +36,16 @@ var result = common.expand(['resources/file*.txt']); assert.equal(shell.error(), null); assert.deepEqual(result, ['resources/file1.txt', 'resources/file2.txt']); +// multiple file, glob syntax, * for directory name +var result = common.expand(['*/file*.txt']); +assert.equal(shell.error(), null); +assert.deepEqual(result, ['resources/file1.txt', 'resources/file2.txt']); + +// multiple file, glob syntax, ** for directory name +var result = common.expand(['**/file*.txt']); +assert.equal(shell.error(), null); +assert.deepEqual(result, ['resources/file1.txt', 'resources/file2.txt']); + shell.exit(123); From e88a8969a22757451555987f0b8cf9c55d344a82 Mon Sep 17 00:00:00 2001 From: utensil Date: Sat, 19 Apr 2014 22:22:06 +0800 Subject: [PATCH 6/9] implement globing and pass tests --- package.json | 4 +++- src/common.js | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 3d73cbe..07be200 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,9 @@ "bin": { "shjs": "./bin/shjs" }, - "dependencies": {}, + "dependencies": { + "minimatch": "0.2.14" + }, "devDependencies": { "jshint": "~2.1.11" }, diff --git a/src/common.js b/src/common.js index fe20871..6fc2443 100644 --- a/src/common.js +++ b/src/common.js @@ -1,6 +1,7 @@ var os = require('os'); var fs = require('fs'); var _ls = require('./ls'); +var minimatch = require('minimatch'); // Module globals var config = { @@ -92,8 +93,18 @@ exports.parseOptions = parseOptions; function expand(list) { var expanded = []; list.forEach(function(listEl) { - // Wildcard present? - if (listEl.search(/\*/) > -1) { + // Wildcard present on directory names ? + if(listEl.search(/\*[^\/]*\//) > -1 || listEl.search(/\*\*[^\/]*\//) > -1) { + var match = listEl.match(/^([^*]+\/|)(.*)/); + var root = match[1]; + var rest = match[2]; + + _ls('-R', root).filter(minimatch.filter(rest)).forEach(function(file) { + expanded.push(file); + }); + } + // Wildcard present on file names ? + else if (listEl.search(/\*/) > -1) { _ls('', listEl).forEach(function(file) { expanded.push(file); }); From 46231f432ed63f0b64e9cd720315008e1ae0c86f Mon Sep 17 00:00:00 2001 From: utensil Date: Sun, 20 Apr 2014 10:39:59 +0800 Subject: [PATCH 7/9] fix order issue under node 0.8 --- test/common.js | 6 +++--- test/grep.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/common.js b/test/common.js index b4517ef..da4ba25 100644 --- a/test/common.js +++ b/test/common.js @@ -34,17 +34,17 @@ assert.deepEqual(result, ['resources/file1.txt']); // multiple file, glob syntax, * for file name var result = common.expand(['resources/file*.txt']); assert.equal(shell.error(), null); -assert.deepEqual(result, ['resources/file1.txt', 'resources/file2.txt']); +assert.deepEqual(result.sort(), ['resources/file1.txt', 'resources/file2.txt'].sort()); // multiple file, glob syntax, * for directory name var result = common.expand(['*/file*.txt']); assert.equal(shell.error(), null); -assert.deepEqual(result, ['resources/file1.txt', 'resources/file2.txt']); +assert.deepEqual(result.sort(), ['resources/file1.txt', 'resources/file2.txt'].sort()); // multiple file, glob syntax, ** for directory name var result = common.expand(['**/file*.txt']); assert.equal(shell.error(), null); -assert.deepEqual(result, ['resources/file1.txt', 'resources/file2.txt']); +assert.deepEqual(result.sort(), ['resources/file1.txt', 'resources/file2.txt'].sort()); shell.exit(123); diff --git a/test/grep.js b/test/grep.js index ce17023..71d9493 100644 --- a/test/grep.js +++ b/test/grep.js @@ -51,16 +51,16 @@ assert.equal(result, 'test1\ntest2\n'); // multiple files, glob syntax, * for file name var result = shell.grep(/test/, 'resources/file*.txt'); assert.equal(shell.error(), null); -assert.equal(result, 'test1\ntest2\n'); +assert.ok(result == 'test1\ntest2\n' || result == 'test2\ntest1\n'); // multiple files, glob syntax, * for directory name var result = shell.grep(/test/, '*/file*.txt'); assert.equal(shell.error(), null); -assert.equal(result, 'test1\ntest2\n'); +assert.ok(result == 'test1\ntest2\n' || result == 'test2\ntest1\n'); // multiple files, glob syntax, ** for directory name var result = shell.grep(/test/, '**/file*.txt'); assert.equal(shell.error(), null); -assert.equal(result, 'test1\ntest2\n'); +assert.ok(result == 'test1\ntest2\n' || result == 'test2\ntest1\n'); shell.exit(123); From e5c26fdaa33ec5f87b55698d6bbb10bb0967eab5 Mon Sep 17 00:00:00 2001 From: utensil Date: Sun, 20 Apr 2014 10:40:27 +0800 Subject: [PATCH 8/9] remove dependcy on `minimatch` --- package.json | 4 +--- src/common.js | 7 +++++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 07be200..3d73cbe 100644 --- a/package.json +++ b/package.json @@ -24,9 +24,7 @@ "bin": { "shjs": "./bin/shjs" }, - "dependencies": { - "minimatch": "0.2.14" - }, + "dependencies": {}, "devDependencies": { "jshint": "~2.1.11" }, diff --git a/src/common.js b/src/common.js index 6fc2443..d8c2312 100644 --- a/src/common.js +++ b/src/common.js @@ -1,7 +1,6 @@ var os = require('os'); var fs = require('fs'); var _ls = require('./ls'); -var minimatch = require('minimatch'); // Module globals var config = { @@ -98,8 +97,12 @@ function expand(list) { var match = listEl.match(/^([^*]+\/|)(.*)/); var root = match[1]; var rest = match[2]; + var restRegex = rest.replace(/\*\*/g, ".*").replace(/\*/g, "[^\\/]*"); + restRegex = new RegExp(restRegex); - _ls('-R', root).filter(minimatch.filter(rest)).forEach(function(file) { + _ls('-R', root).filter(function (e) { + return restRegex.test(e); + }).forEach(function(file) { expanded.push(file); }); } From dd15cff4e66ecc30a0e1570167dcf2cb52f3ac99 Mon Sep 17 00:00:00 2001 From: utensil Date: Sun, 20 Apr 2014 15:13:31 +0800 Subject: [PATCH 9/9] improve tests on ** for directory name --- test/common.js | 4 ++-- test/grep.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/common.js b/test/common.js index da4ba25..74f824b 100644 --- a/test/common.js +++ b/test/common.js @@ -42,9 +42,9 @@ assert.equal(shell.error(), null); assert.deepEqual(result.sort(), ['resources/file1.txt', 'resources/file2.txt'].sort()); // multiple file, glob syntax, ** for directory name -var result = common.expand(['**/file*.txt']); +var result = common.expand(['**/file*.js']); assert.equal(shell.error(), null); -assert.deepEqual(result.sort(), ['resources/file1.txt', 'resources/file2.txt'].sort()); +assert.deepEqual(result.sort(), ["resources/file1.js","resources/file2.js","resources/ls/file1.js","resources/ls/file2.js"].sort()); shell.exit(123); diff --git a/test/grep.js b/test/grep.js index 71d9493..df57483 100644 --- a/test/grep.js +++ b/test/grep.js @@ -59,8 +59,8 @@ assert.equal(shell.error(), null); assert.ok(result == 'test1\ntest2\n' || result == 'test2\ntest1\n'); // multiple files, glob syntax, ** for directory name -var result = shell.grep(/test/, '**/file*.txt'); +var result = shell.grep(/test/, '**/file*.js'); assert.equal(shell.error(), null); -assert.ok(result == 'test1\ntest2\n' || result == 'test2\ntest1\n'); +assert.equal(result, 'test\ntest\ntest\ntest\n'); shell.exit(123);