Skip to content

Commit

Permalink
Should solve Trott#13
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Dec 12, 2011
1 parent b1a91f4 commit c3dc966
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 140 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
/doc/
/node_modules/
/tmp/
*.swp
*.pdoc.yaml
*.log
5 changes: 5 additions & 0 deletions .travis.yml
@@ -0,0 +1,5 @@
language: node_js
node_js:
- 0.6
before_script: "npm install --dev"
script: "make test"
13 changes: 9 additions & 4 deletions Makefile
Expand Up @@ -10,12 +10,9 @@ CURR_HEAD := $(firstword $(shell git show-ref --hash HEAD | cut --bytes=-6) mas
GITHUB_NAME := nodeca/fs-tools
SRC_URL_FMT := https://github.com/${GITHUB_NAME}/blob/${CURR_HEAD}/{file}\#L{line}

test:
NODE_ENV=test node ./test/run.js

lint:
@if test ! `which jslint` ; then \
echo "You need 'jslint' installed in order to generate docs." >&2 ; \
echo "You need 'jslint' installed in order to run lint." >&2 ; \
echo " $ make dev-deps" >&2 ; \
exit 128 ; \
fi
Expand All @@ -24,6 +21,14 @@ lint:
# (nomen) -> tolerate underscores in identifiers (e.g. `var _val = 1`)
jslint --node --nomen --indent=2 ./lib/*.js

test: lint
@if test ! `which vows` ; then \
echo "You need 'vows' installed in order to run tests." >&2 ; \
echo " $ make dev-deps" >&2 ; \
exit 128 ; \
fi
NODE_ENV=test vows --spec

doc:
@if test ! `which ndoc` ; then \
echo "You need 'ndoc' installed in order to generate docs." >&2 ; \
Expand Down
37 changes: 20 additions & 17 deletions lib/fs-tools.js
Expand Up @@ -9,16 +9,16 @@


// stdlib
var fs = require('fs'),
path_join = require('path').join,
path_exists = require('path').exists,
path_normalize = require('path').normalize,
dirname = require('path').dirname;
var fs = require('fs');
var path_join = require('path').join;
var path_exists = require('path').exists;
var path_normalize = require('path').normalize;
var dirname = require('path').dirname;


// 3rd-party
var Promise = require('simple-promise'),
_ = require('underscore');
var Promise = require('simple-promise');
var _ = require('underscore');


// epxorts: walk, mkdir, copy, remove
Expand Down Expand Up @@ -71,7 +71,7 @@ function walk_flat(path, iterator, callback) {

all = new Promise.Joint(callback);

_(files).chain().map(function(file) {
_(files).chain().map(function (file) {
return path_join(path, file);
}).each(function (path) {
var promise = all.promise();
Expand All @@ -95,7 +95,7 @@ function walk_flat(path, iterator, callback) {

all.wait();
});
};
}


// walk_recursive(path, iterator, callback) -> void
Expand All @@ -115,7 +115,10 @@ function walk_recursive(path, iterator, callback) {
// call iterator if not directory
iterator(path, stats, callback);
}, callback);
};
}


var copy; // hack for lint :))


function copy_file(src, dst, callback) {
Expand All @@ -127,7 +130,7 @@ function copy_file(src, dst, callback) {

// pipe src to dst
ifd.pipe(ofd);
};
}


function copy_symlink(src, dst, callback) {
Expand All @@ -141,7 +144,7 @@ function copy_symlink(src, dst, callback) {
// create symlink
fs.symlink(linkpath, dst, callback);
});
};
}


function copy_directory(src, dst, callback) {
Expand All @@ -155,11 +158,11 @@ function copy_directory(src, dst, callback) {
copy(sub_src, dst + sub_src.replace(src, ''), sub_stats, next);
}, callback);
});
};
}


// copy src to dst recursively
function copy(src, dst, stats, callback) {
copy = function copy(src, dst, stats, callback) {
var _callback = function _callback(err) {
if (err) {
callback(err);
Expand All @@ -173,7 +176,7 @@ function copy(src, dst, stats, callback) {
// the only thing we really care about is permission mode
fs.chmod(dst, stats.mode.toString(8).slice(-4), callback);
});
}
};

// *** file
if (stats.isFile()) {
Expand All @@ -195,7 +198,7 @@ function copy(src, dst, stats, callback) {

// *** unsupported src
callback(new Error("Unsupported type of the source"));
}
};


function remove(path, stats, callback) {
Expand Down Expand Up @@ -324,7 +327,7 @@ fstools.walk = function (path, pattern, iterator, callback) {
}

// start walking
walk_recursive(path_normalize(path), function(path, stats, callback) {
walk_recursive(path_normalize(path), function (path, stats, callback) {
// call iterator on
if (match(path)) {
iterator(path, stats, callback);
Expand Down
6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -14,8 +14,12 @@

"main" : "./index.js",

"dependencies" : { "underscore": "1.1.7", "simple-promise": "0.1.0" },
"dependencies" : {
"underscore" : "1.1.7",
"simple-promise" : "0.1.0"
},
"devDependencies" : {
"vows" : "~ 0.6.0",
"jslint" : "https://github.com/reid/node-jslint/tarball/6131ebf5713274871b89735105e3286131804771"
},
"engines" : { "node": "> 0.4.11" }
Expand Down
2 changes: 0 additions & 2 deletions test/.gitignore

This file was deleted.

49 changes: 49 additions & 0 deletions test/copy-test.js
@@ -0,0 +1,49 @@
'use strict';


var Assert = require('assert');
var FsTools = require('../lib/fs-tools');
var Helper = require('./helper');
var exec = require('child_process').exec;


require('vows').describe('FsTools').addBatch({
'copy()': {
topic: function () {
var self = this;
Helper.createSandbox('copy', function (err, src) {
if (err) {
self.callback(err);
return;
}

FsTools.copy(src, src + '-dst', function (err) {
if (err) {
self.callback(err);
return;
}

exec('cd ' + src + ' && -R -p .', function (err, src_out) {
if (err) {
self.callback(err);
return;
}

exec('cd ' + src + '-dst && -R -p .', function (err, dst_out) {
if (err) {
self.callback(err);
return;
}

self.callback(null, src_out, dst_out);
});
});
});
});
},

'should make an exact copy': function (err, src, dst) {
Assert.equal(src, dst);
}
}
}).export(module);
32 changes: 32 additions & 0 deletions test/helper.js
@@ -0,0 +1,32 @@
'use strict';


var exec = require('child_process').exec;
var Helper = module.exports = {};
var TMP_DIR = require('fs').realpathSync(__dirname + '/..') + '/tmp';


function runExecs(err, funcs, callback) {
if (err || 0 === funcs.length) {
callback(err);
return;
}

exec(funcs.shift(), function (err) {
runExecs(err, funcs, callback);
});
}


Helper.createSandbox = function (name, cb) {
var base = TMP_DIR + '/' + name;

runExecs(null, [
'rm -rf ' + base,
'mkdir -p ' + base + '/foo/bar/baz',
'touch ' + base + '/foo/bar/abc',
'cd ' + base + '/foo && ln -s bar/baz baz'
], function (err) {
cb(err, base);
});
};
37 changes: 37 additions & 0 deletions test/remove-test.js
@@ -0,0 +1,37 @@
'use strict';


var Assert = require('assert');
var FsTools = require('../lib/fs-tools');
var Helper = require('./helper');
var Path = require('path');


require('vows').describe('FsTools').addBatch({
'remove()': {
topic: function () {
var self = this;
Helper.createSandbox('remove', function (err, src) {
if (err) {
self.callback(err);
return;
}

FsTools.remove(src, function (err) {
if (err) {
self.callback(err);
return;
}

process.nextTick(function () {
self.callback(null, Path.existsSync(src));
});
});
});
},

'should remove all': function (err, exists) {
Assert.isFalse(exists);
}
}
}).export(module);

0 comments on commit c3dc966

Please sign in to comment.