[Pkg-javascript-commits] [node-klaw] 01/02: New upstream version 1.3.0
Julien Puydt
julien.puydt at laposte.net
Sun Oct 16 16:05:35 UTC 2016
This is an automated email from the git hooks/post-receive script.
jpuydt-guest pushed a commit to branch master
in repository node-klaw.
commit 7375abcc53c7079c82d6f21f93ba5cec6003bc3e
Author: Julien Puydt <julien.puydt at laposte.net>
Date: Sun Oct 16 17:31:26 2016 +0200
New upstream version 1.3.0
---
.gitignore | 1 +
.npmignore | 3 +
.travis.yml | 15 +++
CHANGELOG.md | 34 +++++
LICENSE | 15 +++
README.md | 270 ++++++++++++++++++++++++++++++++++++++
appveyor.yml | 27 ++++
package.json | 37 ++++++
src/assign.js | 16 +++
src/index.js | 52 ++++++++
tests/_test.js | 29 ++++
tests/fixtures.json | 7 +
tests/fixtures_path-sorter.json | 5 +
tests/walk.test.js | 65 +++++++++
tests/walk_filter.test.js | 36 +++++
tests/walk_mockfs.test.js | 41 ++++++
tests/walk_path-sorter.test.js | 60 +++++++++
tests/walk_read-dir-error.test.js | 29 ++++
18 files changed, 742 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c2658d7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules/
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..2db813b
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,3 @@
+tests/
+appveyor.yml
+.travis.yml
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..8662b6f
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,15 @@
+sudo: false
+language: node_js
+node_js:
+ - "0.10"
+ - "0.12"
+ - "io.js"
+ - "4"
+ - "5"
+matrix:
+ include:
+ - node_js: "4"
+ env: TEST_SUITE=lint
+env:
+ - TEST_SUITE=unit
+script: npm run-script $TEST_SUITE
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..35f0d72
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,34 @@
+1.3.0 / 2016-06-09
+------------------
+### Added
+- `filter` option to pre-filter and not walk directories.
+
+1.2.0 / 2016-04-16
+------------------
+- added support for custom `fs` implementation. Useful for https://github.com/tschaub/mock-fs
+
+1.1.3 / 2015-12-23
+------------------
+- bugfix: if `readdir` error, got hung up. See: https://github.com/jprichardson/node-klaw/issues/1
+
+1.1.2 / 2015-11-12
+------------------
+- assert that param `dir` is a `string`
+
+1.1.1 / 2015-10-25
+------------------
+- bug fix, options not being passed
+
+1.1.0 / 2015-10-25
+------------------
+- added `queueMethod` and `pathSorter` to `options` to affect searching strategy.
+
+1.0.0 / 2015-10-25
+------------------
+- removed unused `filter` param
+- bugfix: always set `streamOptions` to `objectMode`
+- simplified, converted from push mode (streams 1) to proper pull mode (streams 3)
+
+0.1.0 / 2015-10-25
+------------------
+- initial release
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..ddb217c
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,15 @@
+(The MIT License)
+
+Copyright (c) 2015-2016 JP Richardson
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
+(the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
+ merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
+OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..78386e4
--- /dev/null
+++ b/README.md
@@ -0,0 +1,270 @@
+Node.js - klaw
+==============
+
+A Node.js file system walker extracted from [fs-extra](https://github.com/jprichardson/node-fs-extra).
+
+[![npm Package](https://img.shields.io/npm/v/klaw.svg?style=flat-square)](https://www.npmjs.org/package/klaw)
+[![build status](https://api.travis-ci.org/jprichardson/node-klaw.svg)](http://travis-ci.org/jprichardson/node-klaw)
+[![windows build status](https://ci.appveyor.com/api/projects/status/github/jprichardson/node-klaw?branch=master&svg=true)](https://ci.appveyor.com/project/jprichardson/node-klaw/branch/master)
+
+<!-- [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) -->
+<a href="http://standardjs.com"><img src="https://cdn.rawgit.com/feross/standard/master/sticker.svg" alt="Standard" width="100"></a>
+
+Install
+-------
+
+ npm i --save klaw
+
+
+Name
+----
+
+`klaw` is `walk` backwards :p
+
+
+Usage
+-----
+
+### klaw(directory, [options])
+
+Returns a [Readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) that iterates
+through every file and directory starting with `dir` as the root. Every `read()` or `data` event
+returns an object with two properties: `path` and `stats`. `path` is the full path of the file and
+`stats` is an instance of [fs.Stats](https://nodejs.org/api/fs.html#fs_class_fs_stats).
+
+- `directory`: The directory to recursively walk. Type `string`.
+- `options`: [Readable stream options](https://nodejs.org/api/stream.html#stream_new_stream_readable_options) and
+the following:
+ - `queueMethod` (`string`, default: `'shift'`): Either `'shift'` or `'pop'`. On `readdir()` array, call either `shift()` or `pop()`.
+ - `pathSorter` (`function`, default: `undefined`): Sorting [function for Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort).
+ - `fs` (`object`, default: `require('fs')`): Use this to hook into the `fs` methods or to use [`mock-fs`](https://github.com/tschaub/mock-fs)
+ - `filter` (`function`, default: `undefined`): Filtering [function for Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
+
+**Streams 1 (push) example:**
+
+```js
+var klaw = require('klaw')
+
+var items = [] // files, directories, symlinks, etc
+klaw('/some/dir')
+ .on('data', function (item) {
+ items.push(item.path)
+ })
+ .on('end', function () {
+ console.dir(items) // => [ ... array of files]
+ })
+```
+
+**Streams 2 & 3 (pull) example:**
+
+```js
+var klaw = require('klaw')
+
+var items = [] // files, directories, symlinks, etc
+klaw('/some/dir')
+ .on('readable', function () {
+ var item
+ while ((item = this.read())) {
+ items.push(item.path)
+ }
+ })
+ .on('end', function () {
+ console.dir(items) // => [ ... array of files]
+ })
+```
+
+If you're not sure of the differences on Node.js streams 1, 2, 3 then I'd
+recommend this resource as a good starting point: https://strongloop.com/strongblog/whats-new-io-js-beta-streams3/.
+
+
+### Error Handling
+
+Listen for the `error` event.
+
+Example:
+
+```js
+var klaw = require('klaw')
+klaw('/some/dir')
+ .on('readable', function () {
+ var item
+ while ((item = this.read())) {
+ // do something with the file
+ }
+ })
+ .on('error', function (err, item) {
+ console.log(err.message)
+ console.log(item.path) // the file the error occurred on
+ })
+ .on('end', function () {
+ console.dir(items) // => [ ... array of files]
+ })
+
+```
+
+
+### Aggregation / Filtering / Executing Actions (Through Streams)
+
+On many occasions you may want to filter files based upon size, extension, etc.
+Or you may want to aggregate stats on certain file types. Or maybe you want to
+perform an action on certain file types.
+
+You should use the module [`through2`](https://www.npmjs.com/package/through2) to easily
+accomplish this.
+
+Install `through2`:
+
+ npm i --save through2
+
+
+**Example (skipping directories):**
+
+```js
+var klaw = require('klaw')
+var through2 = require('through2')
+
+var excludeDirFilter = through2.obj(function (item, enc, next) {
+ if (!item.stats.isDirectory()) this.push(item)
+ next()
+})
+
+var items = [] // files, directories, symlinks, etc
+klaw('/some/dir')
+ .pipe(excludeDirFilter)
+ .on('data', function (item) {
+ items.push(item.path)
+ })
+ .on('end', function () {
+ console.dir(items) // => [ ... array of files without directories]
+ })
+
+```
+**Example (ignore hidden directories):**
+```js
+var klaw = require('klaw')
+var path = require('path')
+
+var filterFunc = function(item){
+ var basename = path.basename(item)
+ return basename === '.' || basename[0] !== '.'
+}
+
+klaw('/some/dir', { filter : filterFunc })
+ .on('data', function(item){
+ // only items of none hidden folders will reach here
+ })
+
+```
+
+**Example (totaling size of PNG files):**
+
+```js
+var klaw = require('klaw')
+var path = require('path')
+var through2 = require('through2')
+
+var totalPngsInBytes = 0
+var aggregatePngSize = through2.obj(function (item, enc, next) {
+ if (path.extname(item.path) === '.png') {
+ totalPngsInBytes += item.stats.size
+ }
+ this.push(item)
+ next()
+})
+
+klaw('/some/dir')
+ .pipe(aggregatePngSize)
+ .on('data', function (item) {
+ items.push(item.path)
+ })
+ .on('end', function () {
+ console.dir(totalPngsInBytes) // => total of all pngs (bytes)
+ })
+```
+
+
+**Example (deleting all .tmp files):**
+
+```js
+var fs = require('fs')
+var klaw = require('klaw')
+var through2 = require('through2')
+
+var deleteAction = through2.obj(function (item, enc, next) {
+ this.push(item)
+
+ if (path.extname(item.path) === '.tmp') {
+ item.deleted = true
+ fs.unklink(item.path, next)
+ } else {
+ item.deleted = false
+ next()
+ }
+})
+
+var deletedFiles = []
+klaw('/some/dir')
+ .pipe(deleteAction)
+ .on('data', function (item) {
+ if (!item.deleted) return
+ deletedFiles.push(item.path)
+ })
+ .on('end', function () {
+ console.dir(deletedFiles) // => all deleted files
+ })
+```
+
+You can even chain a bunch of these filters and aggregators together. By using
+multiple pipes.
+
+**Example (using multiple filters / aggregators):**
+
+```js
+klaw('/some/dir')
+ .pipe(filterCertainFiles)
+ .pipe(deleteSomeOtherFiles)
+ .on('end', function () {
+ console.log('all done!')
+ })
+```
+
+**Example passing (piping) through errors:**
+
+Node.js does not `pipe()` errors. This means that the error on one stream, like
+`klaw` will not pipe through to the next. If you want to do this, do the following:
+
+```js
+var klaw = require('klaw')
+var through2 = require('through2')
+
+var excludeDirFilter = through2.obj(function (item, enc, next) {
+ if (!item.stats.isDirectory()) this.push(item)
+ next()
+})
+
+var items = [] // files, directories, symlinks, etc
+klaw('/some/dir')
+ .on('error', function (err) { excludeDirFilter.emit('error', err) }) // forward the error on
+ .pipe(excludeDirFilter)
+ .on('data', function (item) {
+ items.push(item.path)
+ })
+ .on('end', function () {
+ console.dir(items) // => [ ... array of files without directories]
+ })
+```
+
+
+### Searching Strategy
+
+Pass in options for `queueMethod` and `pathSorter` to affect how the file system
+is recursively iterated. See the code for more details, it's less than 50 lines :)
+
+
+
+License
+-------
+
+MIT
+
+Copyright (c) 2015 [JP Richardson](https://github.com/jprichardson)
diff --git a/appveyor.yml b/appveyor.yml
new file mode 100644
index 0000000..08a2803
--- /dev/null
+++ b/appveyor.yml
@@ -0,0 +1,27 @@
+# Test against this version of Node.js
+environment:
+ matrix:
+ # node.js
+ - nodejs_version: "0.10"
+ - nodejs_version: "0.12"
+ - nodejs_version: "4"
+ - nodejs_version: "5"
+
+# Install scripts. (runs after repo cloning)
+install:
+ # Get the latest stable version of Node.js or io.js
+ - ps: Install-Product node $env:nodejs_version
+ # install modules
+ - npm config set loglevel warn
+ - npm install --silent
+
+# Post-install test scripts.
+test_script:
+ # Output useful info for debugging.
+ - node --version
+ - npm --version
+ # run tests
+ - npm test
+
+# Don't actually build.
+build: off
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..6a6f5f3
--- /dev/null
+++ b/package.json
@@ -0,0 +1,37 @@
+{
+ "name": "klaw",
+ "version": "1.3.0",
+ "description": "File system walker with Readable stream interface.",
+ "main": "./src/index.js",
+ "scripts": {
+ "lint": "standard",
+ "test": "npm run lint && npm run unit",
+ "unit": "tape tests/**/*.js | tap-spec"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git+https://github.com/jprichardson/node-klaw.git"
+ },
+ "keywords": [
+ "walk",
+ "walker",
+ "fs",
+ "fs-extra",
+ "readable",
+ "streams"
+ ],
+ "author": "JP Richardson",
+ "license": "MIT",
+ "bugs": {
+ "url": "https://github.com/jprichardson/node-klaw/issues"
+ },
+ "homepage": "https://github.com/jprichardson/node-klaw#readme",
+ "devDependencies": {
+ "mkdirp": "^0.5.1",
+ "mock-fs": "^3.8.0",
+ "rimraf": "^2.4.3",
+ "standard": "^5.3.1",
+ "tap-spec": "^4.1.1",
+ "tape": "^4.2.2"
+ }
+}
diff --git a/src/assign.js b/src/assign.js
new file mode 100644
index 0000000..69740f6
--- /dev/null
+++ b/src/assign.js
@@ -0,0 +1,16 @@
+// simple mutable assign (extracted from fs-extra)
+// I really like object-assign package, but I wanted a lean package with zero deps
+function _assign () {
+ var args = [].slice.call(arguments).filter(function (i) { return i })
+ var dest = args.shift()
+ args.forEach(function (src) {
+ Object.keys(src).forEach(function (key) {
+ dest[key] = src[key]
+ })
+ })
+
+ return dest
+}
+
+// thank you baby Jesus for Node v4 and Object.assign
+module.exports = Object.assign || _assign
diff --git a/src/index.js b/src/index.js
new file mode 100644
index 0000000..3b33df6
--- /dev/null
+++ b/src/index.js
@@ -0,0 +1,52 @@
+var assert = require('assert')
+var fs = require('fs')
+var path = require('path')
+var Readable = require('stream').Readable
+var util = require('util')
+var assign = require('./assign')
+
+function Walker (dir, options) {
+ assert.strictEqual(typeof dir, 'string', '`dir` parameter should be of type string. Got type: ' + typeof dir)
+ var defaultStreamOptions = { objectMode: true }
+ var defaultOpts = { queueMethod: 'shift', pathSorter: undefined, filter: undefined }
+ options = assign(defaultOpts, options, defaultStreamOptions)
+
+ Readable.call(this, options)
+ this.root = path.resolve(dir)
+ this.paths = [this.root]
+ this.options = options
+ this.fs = options.fs || fs // mock-fs
+}
+util.inherits(Walker, Readable)
+
+Walker.prototype._read = function () {
+ if (this.paths.length === 0) return this.push(null)
+ var self = this
+ var pathItem = this.paths[this.options.queueMethod]()
+
+ self.fs.lstat(pathItem, function (err, stats) {
+ var item = { path: pathItem, stats: stats }
+ if (err) return self.emit('error', err, item)
+ if (!stats.isDirectory()) return self.push(item)
+
+ self.fs.readdir(pathItem, function (err, pathItems) {
+ if (err) {
+ self.push(item)
+ return self.emit('error', err, item)
+ }
+
+ pathItems = pathItems.map(function (part) { return path.join(pathItem, part) })
+ if (self.options.filter) pathItems = pathItems.filter(self.options.filter)
+ if (self.options.pathSorter) pathItems.sort(self.options.pathSorter)
+ pathItems.forEach(function (pi) { self.paths.push(pi) })
+
+ self.push(item)
+ })
+ })
+}
+
+function walk (root, options) {
+ return new Walker(root, options)
+}
+
+module.exports = walk
diff --git a/tests/_test.js b/tests/_test.js
new file mode 100644
index 0000000..84043d3
--- /dev/null
+++ b/tests/_test.js
@@ -0,0 +1,29 @@
+var mkdirp = require('mkdirp')
+var os = require('os')
+var path = require('path')
+var rimraf = require('rimraf')
+var tape = require('tape')
+
+// for all practical purposes, this is a beforeEach and afterEach
+function test (desc, testFn) {
+ tape(desc, function (t) {
+ var testDir = path.join(os.tmpdir(), 'klaw-tests')
+ rimraf(testDir, function (err) {
+ if (err) return t.end(err)
+ mkdirp(testDir, function (err) {
+ if (err) return t.end(err)
+
+ var oldEnd = t.end
+ t.end = function () {
+ rimraf(testDir, function (err) {
+ err ? oldEnd.apply(t, [err]) : oldEnd.apply(t, arguments)
+ })
+ }
+
+ testFn(t, testDir)
+ })
+ })
+ })
+}
+
+module.exports = test
diff --git a/tests/fixtures.json b/tests/fixtures.json
new file mode 100644
index 0000000..8f5df49
--- /dev/null
+++ b/tests/fixtures.json
@@ -0,0 +1,7 @@
+[
+ "a/b/c/d.txt",
+ "a/e.jpg",
+ "h/i/j/k.txt",
+ "h/i/l.txt",
+ "h/i/m.jpg"
+]
diff --git a/tests/fixtures_path-sorter.json b/tests/fixtures_path-sorter.json
new file mode 100644
index 0000000..d56a738
--- /dev/null
+++ b/tests/fixtures_path-sorter.json
@@ -0,0 +1,5 @@
+[
+ "a",
+ "b",
+ "c"
+]
diff --git a/tests/walk.test.js b/tests/walk.test.js
new file mode 100644
index 0000000..7da2dad
--- /dev/null
+++ b/tests/walk.test.js
@@ -0,0 +1,65 @@
+var fs = require('fs')
+var mkdirp = require('mkdirp')
+var path = require('path')
+var test = require('./_test')
+var klaw = require('../')
+var fixtures = require('./fixtures')
+
+test('should work w/ streams 1', function (t, testDir) {
+ fixtures.forEach(function (f) {
+ f = path.join(testDir, f)
+ var dir = path.dirname(f)
+ mkdirp.sync(dir)
+ fs.writeFileSync(f, path.basename(f, path.extname(f)))
+ })
+
+ var items = []
+ klaw(testDir)
+ .on('data', function (item) {
+ items.push(item.path)
+ })
+ .on('error', t.end)
+ .on('end', function () {
+ items.sort()
+ var expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 'h/i/j',
+ 'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg']
+ expected = expected.map(function (item) {
+ return path.join(path.join(testDir, item))
+ })
+ expected.unshift(testDir)
+
+ t.same(items, expected)
+ t.end()
+ })
+})
+
+test('should work w/ streams 2/3', function (t, testDir) {
+ fixtures.forEach(function (f) {
+ f = path.join(testDir, f)
+ var dir = path.dirname(f)
+ mkdirp.sync(dir)
+ fs.writeFileSync(f, path.basename(f, path.extname(f)))
+ })
+
+ var items = []
+ klaw(testDir)
+ .on('readable', function () {
+ var item
+ while ((item = this.read())) {
+ items.push(item.path)
+ }
+ })
+ .on('error', t.end)
+ .on('end', function () {
+ items.sort()
+ var expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 'h/i/j',
+ 'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg']
+ expected = expected.map(function (item) {
+ return path.join(path.join(testDir, item))
+ })
+ expected.unshift(testDir)
+
+ t.same(items, expected)
+ t.end()
+ })
+})
diff --git a/tests/walk_filter.test.js b/tests/walk_filter.test.js
new file mode 100644
index 0000000..9d2996b
--- /dev/null
+++ b/tests/walk_filter.test.js
@@ -0,0 +1,36 @@
+var fs = require('fs')
+var mkdirp = require('mkdirp')
+var path = require('path')
+var test = require('./_test')
+var klaw = require('../')
+var fixtures = require('./fixtures')
+
+test('should not fire event on filtered items', function (t, testDir) {
+ fixtures.forEach(function (f) {
+ f = path.join(testDir, f)
+ var dir = path.dirname(f)
+ mkdirp.sync(dir)
+ fs.writeFileSync(f, path.basename(f, path.extname(f)))
+ })
+
+ var items = []
+ var filter = function (filepath) {
+ return path.basename(filepath) !== 'a'
+ }
+
+ klaw(testDir, {filter: filter})
+ .on('data', function (item) {
+ if (fs.lstatSync(item.path).isFile()) items.push(item.path)
+ })
+ .on('error', t.end)
+ .on('end', function () {
+ var expected = ['c', 'b', 'a']
+ expected = expected.map(function (item) {
+ return path.join(testDir, item)
+ })
+ expected.unshift(testDir)
+
+ t.ok(items.length < fixtures.length, 'we should see less items due to filter')
+ t.end()
+ })
+})
diff --git a/tests/walk_mockfs.test.js b/tests/walk_mockfs.test.js
new file mode 100644
index 0000000..e3fc3cb
--- /dev/null
+++ b/tests/walk_mockfs.test.js
@@ -0,0 +1,41 @@
+var mkdirp = require('mkdirp')
+var mockfs = require('mock-fs')
+var os = require('os')
+var path = require('path')
+var test = require('tape')
+var klaw = require('../')
+var fixtures = require('./fixtures')
+var fs = mockfs.fs()
+
+test('walk directory on mockfs', function (t) {
+ var testDir = path.join(os.tmpdir(), '/does/not/matter/in/mockfs')
+ fixtures.forEach(function (f) {
+ f = path.join(testDir, f)
+ var dir = path.dirname(f)
+ mkdirp.sync(dir, { fs: fs })
+ fs.writeFileSync(f, path.basename(f, path.extname(f)))
+ })
+
+ t.plan(1)
+ var items = []
+ klaw(testDir, { fs: fs })
+ .on('readable', function () {
+ var item
+ while ((item = this.read())) {
+ items.push(item.path)
+ }
+ })
+ .on('error', t.end)
+ .on('end', function () {
+ items.sort()
+ var expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 'h/i/j',
+ 'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg']
+ expected = expected.map(function (item) {
+ return path.join(path.join(testDir, item))
+ })
+ expected.unshift(testDir)
+
+ t.same(items, expected)
+ t.end()
+ })
+})
diff --git a/tests/walk_path-sorter.test.js b/tests/walk_path-sorter.test.js
new file mode 100644
index 0000000..48c5795
--- /dev/null
+++ b/tests/walk_path-sorter.test.js
@@ -0,0 +1,60 @@
+var fs = require('fs')
+var mkdirp = require('mkdirp')
+var path = require('path')
+var test = require('./_test')
+var klaw = require('../')
+var fixtures = require('./fixtures_path-sorter')
+
+test('should sort in reverse order [z -> a]', function (t, testDir) {
+ fixtures.forEach(function (f) {
+ f = path.join(testDir, f)
+ var dir = path.dirname(f)
+ mkdirp.sync(dir)
+ fs.writeFileSync(f, path.basename(f, path.extname(f)))
+ })
+
+ var items = []
+ var pathSorter = function (a, b) { return b > a }
+ klaw(testDir, { pathSorter: pathSorter })
+ .on('data', function (item) {
+ items.push(item.path)
+ })
+ .on('error', t.end)
+ .on('end', function () {
+ var expected = ['c', 'b', 'a']
+ expected = expected.map(function (item) {
+ return path.join(testDir, item)
+ })
+ expected.unshift(testDir)
+
+ t.same(items, expected)
+ t.end()
+ })
+})
+
+test('should sort in order [a -> z]', function (t, testDir) {
+ fixtures.forEach(function (f) {
+ f = path.join(testDir, f)
+ var dir = path.dirname(f)
+ mkdirp.sync(dir)
+ fs.writeFileSync(f, path.basename(f, path.extname(f)))
+ })
+
+ var items = []
+ var pathSorter = function (a, b) { return a > b }
+ klaw(testDir, { pathSorter: pathSorter })
+ .on('data', function (item) {
+ items.push(item.path)
+ })
+ .on('error', t.end)
+ .on('end', function () {
+ var expected = ['a', 'b', 'c']
+ expected = expected.map(function (item) {
+ return path.join(testDir, item)
+ })
+ expected.unshift(testDir)
+
+ t.same(items, expected)
+ t.end()
+ })
+})
diff --git a/tests/walk_read-dir-error.test.js b/tests/walk_read-dir-error.test.js
new file mode 100644
index 0000000..8e50043
--- /dev/null
+++ b/tests/walk_read-dir-error.test.js
@@ -0,0 +1,29 @@
+var fs = require('fs')
+var mkdirp = require('mkdirp')
+var path = require('path')
+var test = require('./_test')
+var klaw = require('../')
+
+test('walk directory, if error on readdir, at least end', function (t, testDir) {
+ // simulate directory issue
+ var unreadableDir = path.join(testDir, 'unreadable-dir')
+ mkdirp.sync(unreadableDir)
+ fs.chmodSync(unreadableDir, '0222')
+
+ // not able to simulate on windows
+ if (process.platform === 'win32') return t.end()
+
+ t.plan(2)
+ var items = []
+ klaw(testDir)
+ .on('data', function (item) {
+ items.push(item.path)
+ })
+ .on('error', function (err) {
+ t.true(err, 'caught error')
+ })
+ .on('end', function () {
+ t.true(true, 'be sure we end')
+ t.end()
+ })
+})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-klaw.git
More information about the Pkg-javascript-commits
mailing list