diff --git a/index.js b/index.js index 2052913..02c1e0b 100644 --- a/index.js +++ b/index.js @@ -111,10 +111,6 @@ File.prototype.clone = function(opt) { // Clone our file contents var contents; if (this.isStream()) { - if (typeof this.contents.clone !== 'function') { - this.contents = cloneable(this.contents); - } - contents = this.contents.clone(); } else if (this.isBuffer()) { contents = opt.contents ? cloneBuffer(this.contents) : this.contents; @@ -177,7 +173,10 @@ Object.defineProperty(File.prototype, 'contents', { throw new Error('File.contents can only be a Buffer, a Stream, or null.'); } - if (isStream(val)) { + // Ask cloneable if the stream is a already a cloneable + // this avoid piping into many streams + // reducing the overhead of cloning + if (isStream(val) && !cloneable.isCloneable(val)) { val = cloneable(val); } diff --git a/package.json b/package.json index a35838f..1112f8c 100644 --- a/package.json +++ b/package.json @@ -13,30 +13,28 @@ "dependencies": { "clone": "^1.0.0", "clone-stats": "^1.0.0", - "cloneable-readable": "^0.4.0", + "cloneable-readable": "^0.5.0", "readable-stream": "^2.1.0", "replace-ext": "^1.0.0" }, "devDependencies": { - "buffer-equal": "0.0.1", "eslint": "^1.7.3", "eslint-config-gulp": "^2.0.0", - "event-stream": "^3.1.0", + "expect": "^1.20.2", "github-changes": "^1.0.1", - "istanbul": "^0.3.0", - "istanbul-coveralls": "^1.0.1", + "istanbul": "^0.4.3", + "istanbul-coveralls": "^1.0.3", "jscs": "^2.3.5", "jscs-preset-gulp": "^1.0.0", - "lodash.templatesettings": "^3.1.0", - "mocha": "^2.0.0", - "rimraf": "^2.2.5", - "should": "^7.0.0" + "mississippi": "^1.2.0", + "mocha": "^2.4.5" }, "scripts": { "lint": "eslint . && jscs index.js lib/ test/", "pretest": "npm run lint", - "test": "mocha", - "coveralls": "istanbul cover _mocha && istanbul-coveralls", + "test": "mocha --async-only", + "cover": "istanbul cover _mocha --report lcovonly", + "coveralls": "npm run cover && istanbul-coveralls", "changelog": "github-changes -o gulpjs -r vinyl -b master -f ./CHANGELOG.md --order-semver --use-commit-body" }, "engines": { diff --git a/test/File.js b/test/File.js deleted file mode 100644 index 5e4271d..0000000 --- a/test/File.js +++ /dev/null @@ -1,1333 +0,0 @@ -var Stream = require('readable-stream'); -var fs = require('fs'); -var path = require('path'); -var es = require('event-stream'); -var File = require('../'); - -var should = require('should'); -require('mocha'); - -describe('File', function() { - describe('isVinyl()', function() { - it('should return true on a vinyl object', function(done) { - var file = new File(); - File.isVinyl(file).should.equal(true); - done(); - }); - it('should return false on a normal object', function(done) { - File.isVinyl({}).should.equal(false); - done(); - }); - it('should return false on a null object', function(done) { - File.isVinyl(null).should.equal(false); - done(); - }); - }); - describe('constructor()', function() { - it('should default cwd to process.cwd', function(done) { - var file = new File(); - file.cwd.should.equal(process.cwd()); - done(); - }); - - it('should default base to cwd', function(done) { - var cwd = path.normalize('/'); - var file = new File({ cwd: cwd }); - file.base.should.equal(cwd); - done(); - }); - - it('should default base to cwd even when none is given', function(done) { - var file = new File(); - file.base.should.equal(process.cwd()); - done(); - }); - - it('should default path to null', function(done) { - var file = new File(); - should.not.exist(file.path); - done(); - }); - - it('should default history to []', function(done) { - var file = new File(); - file.history.should.eql([]); - done(); - }); - - it('should default stat to null', function(done) { - var file = new File(); - should.not.exist(file.stat); - done(); - }); - - it('should default contents to null', function(done) { - var file = new File(); - should.not.exist(file.contents); - done(); - }); - - it('should set base to given value', function(done) { - var val = path.normalize('/'); - var file = new File({ base: val }); - file.base.should.equal(val); - done(); - }); - - it('should set cwd to given value', function(done) { - var val = path.normalize('/'); - var file = new File({ cwd: val }); - file.cwd.should.equal(val); - done(); - }); - - it('should set path to given value', function(done) { - var val = path.normalize('/test.coffee'); - var file = new File({ path: val }); - file.path.should.equal(val); - file.history.should.eql([val]); - done(); - }); - - it('should set history to given value', function(done) { - var val = path.normalize('/test.coffee'); - var file = new File({ history: [val] }); - file.path.should.equal(val); - file.history.should.eql([val]); - done(); - }); - - it('should set stat to given value', function(done) { - var val = {}; - var file = new File({ stat: val }); - file.stat.should.equal(val); - done(); - }); - - it('should set contents to given value', function(done) { - var val = new Buffer('test'); - var file = new File({ contents: val }); - file.contents.should.equal(val); - done(); - }); - - it('should set custom properties', function(done) { - var sourceMap = {}; - var file = new File({ sourceMap: sourceMap }); - file.sourceMap.should.equal(sourceMap); - done(); - }); - - it('should normalize path', function() { - var file = new File({ path: '/test/foo/../test.coffee' }); - - if (process.platform === 'win32') { - file.path.should.equal('\\test\\test.coffee'); - file.history.should.eql(['\\test\\test.coffee']); - } else { - file.path.should.equal('/test/test.coffee'); - file.history.should.eql(['/test/test.coffee']); - } - }); - - it('should correctly normalize and strip trailing sep from path', function() { - var file = new File({ path: '/test/foo/../foo/' }); - - if (process.platform === 'win32') { - file.path.should.equal('\\test\\foo'); - } else { - file.path.should.equal('/test/foo'); - } - }); - - it('should correctly normalize and strip trailing sep from history', function() { - var file = new File({ - history: [ - '/test/foo/../foo/', - '/test/bar/../bar/', - ], - }); - - if (process.platform === 'win32') { - file.history.should.eql([ - '\\test\\foo', - '\\test\\bar', - ]); - } else { - file.history.should.eql([ - '/test/foo', - '/test/bar', - ]); - } - }); - - it('should normalize history', function() { - var history = [ - '/test/bar/../bar/test.coffee', - '/test/foo/../test.coffee', - ]; - var file = new File({ history: history }); - - if (process.platform === 'win32') { - file.path.should.equal('\\test\\test.coffee'); - file.history.should.eql([ - '\\test\\bar\\test.coffee', - '\\test\\test.coffee', - ]); - } else { - file.path.should.equal('/test/test.coffee'); - file.history.should.eql([ - '/test/bar/test.coffee', - '/test/test.coffee', - ]); - } - }); - - it('appends path to history if both exist and different from last', function(done) { - var p = path.normalize('/test/baz/test.coffee'); - var history = [ - path.normalize('/test/bar/test.coffee'), - path.normalize('/test/foo/test.coffee'), - ]; - var file = new File({ path: p, history: history }); - - var expectedHistory = history.concat(p); - - file.path.should.equal(path.normalize('/test/baz/test.coffee')); - file.history.should.eql(expectedHistory); - done(); - }); - - it('does not append path to history if both exist and same as last', function(done) { - var history = [ - path.normalize('/test/bar/test.coffee'), - path.normalize('/test/foo/test.coffee'), - path.normalize('/test/baz/test.coffee'), - ]; - var file = new File({ path: history[history.length - 1], history: history }); - - file.path.should.equal(path.normalize('/test/baz/test.coffee')); - file.history.should.eql(history); - done(); - }); - - it('does not mutate history array passed in', function(done) { - var p = path.normalize('/test/baz/test.coffee'); - var history = [ - path.normalize('/test/bar/test.coffee'), - path.normalize('/test/foo/test.coffee'), - ]; - var historyCopy = Array.prototype.slice.call(history); - var file = new File({ path: p, history: history }); - - var expectedHistory = history.concat(p); - - file.path.should.equal(path.normalize('/test/baz/test.coffee')); - file.history.should.eql(expectedHistory); - history.should.eql(historyCopy); - done(); - }); - - }); - - describe('isBuffer()', function() { - it('should return true when the contents are a Buffer', function(done) { - var val = new Buffer('test'); - var file = new File({ contents: val }); - file.isBuffer().should.equal(true); - done(); - }); - - it('should return false when the contents are a Stream', function(done) { - var val = new Stream(); - var file = new File({ contents: val }); - file.isBuffer().should.equal(false); - done(); - }); - - it('should return false when the contents are a null', function(done) { - var file = new File({ contents: null }); - file.isBuffer().should.equal(false); - done(); - }); - }); - - describe('isStream()', function() { - it('should return false when the contents are a Buffer', function(done) { - var val = new Buffer('test'); - var file = new File({ contents: val }); - file.isStream().should.equal(false); - done(); - }); - - it('should return true when the contents are a Stream', function(done) { - var val = new Stream(); - var file = new File({ contents: val }); - file.isStream().should.equal(true); - done(); - }); - - it('should return false when the contents are a null', function(done) { - var file = new File({ contents: null }); - file.isStream().should.equal(false); - done(); - }); - }); - - describe('isNull()', function() { - it('should return false when the contents are a Buffer', function(done) { - var val = new Buffer('test'); - var file = new File({ contents: val }); - file.isNull().should.equal(false); - done(); - }); - - it('should return false when the contents are a Stream', function(done) { - var val = new Stream(); - var file = new File({ contents: val }); - file.isNull().should.equal(false); - done(); - }); - - it('should return true when the contents are a null', function(done) { - var file = new File({ contents: null }); - file.isNull().should.equal(true); - done(); - }); - }); - - describe('isDirectory()', function() { - var fakeStat = { - isDirectory: function() { - return true; - }, - }; - - it('should return false when the contents are a Buffer', function(done) { - var val = new Buffer('test'); - var file = new File({ contents: val, stat: fakeStat }); - file.isDirectory().should.equal(false); - done(); - }); - - it('should return false when the contents are a Stream', function(done) { - var val = new Stream(); - var file = new File({ contents: val, stat: fakeStat }); - file.isDirectory().should.equal(false); - done(); - }); - - it('should return true when the contents are a null', function(done) { - var file = new File({ contents: null, stat: fakeStat }); - file.isDirectory().should.equal(true); - done(); - }); - - it('returns false when the stats exist but do not contain isDirectory method', function(done) { - var file = new File({ contents: null, stat: {} }); - file.isDirectory().should.equal(false); - done(); - }); - }); - - describe('isSymbolic()', function() { - var fakeStat = { - isSymbolicLink: function() { - return true; - }, - }; - - it('should return false when the contents are a Buffer', function(done) { - var val = new Buffer('test'); - var file = new File({ contents: val, stat: fakeStat }); - file.isSymbolic().should.equal(false); - done(); - }); - - it('should return false when the contents are a Stream', function(done) { - var val = new Stream(); - var file = new File({ contents: val, stat: fakeStat }); - file.isSymbolic().should.equal(false); - done(); - }); - - it('should return true when the contents are a null', function(done) { - var file = new File({ contents: null, stat: fakeStat }); - file.isSymbolic().should.equal(true); - done(); - }); - - it('returns false when the stats exist but do not contain isSymbolicLink method', function(done) { - var file = new File({ contents: null, stat: {} }); - file.isSymbolic().should.equal(false); - done(); - }); - }); - - describe('clone()', function() { - it('should copy all attributes over with Buffer', function(done) { - var options = { - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - contents: new Buffer('test'), - }; - var file = new File(options); - var file2 = file.clone(); - - file2.should.not.equal(file, 'refs should be different'); - file2.cwd.should.equal(file.cwd); - file2.base.should.equal(file.base); - file2.path.should.equal(file.path); - file2.contents.should.not.equal(file.contents, 'buffer ref should be different'); - file2.contents.toString('utf8').should.equal(file.contents.toString('utf8')); - done(); - }); - - it('should copy buffer\'s reference with option contents: false', function(done) { - var options = { - cwd: '/', - base: '/test/', - path: '/test/test.js', - contents: new Buffer('test'), - }; - - var file = new File(options); - - var copy1 = file.clone({ contents: false }); - copy1.contents.should.equal(file.contents); - - var copy2 = file.clone({}); - copy2.contents.should.not.equal(file.contents); - - var copy3 = file.clone({ contents: 'any string' }); - copy3.contents.should.not.equal(file.contents); - - done(); - }); - - it('should copy all attributes over with Stream', function(done) { - var contents = new Stream.PassThrough(); - var options = { - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - contents: contents, - }; - var file = new File(options); - var file2 = file.clone(); - - contents.write(new Buffer('wa')); - - process.nextTick(function() { - contents.write(new Buffer('dup')); - contents.end(); - }); - - file2.should.not.equal(file, 'refs should be different'); - file2.cwd.should.equal(file.cwd); - file2.base.should.equal(file.base); - file2.path.should.equal(file.path); - file2.contents.should.not.equal(file.contents, 'stream ref should not be the same'); - file.contents.pipe(es.wait(function(err, data) { - file2.contents.pipe(es.wait(function(err, data2) { - data2.should.not.equal(data, 'stream contents ref should not be the same'); - data2.should.eql(data, 'stream contents should be the same'); - })); - })); - done(); - }); - - it('should not start flowing until all clones flows', function(done) { - var contents = new Stream.PassThrough(); - var options = { - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - contents: contents, - }; - var file = new File(options); - var file2 = file.clone(); - var ends = 2; - - function latch() { - if (--ends === 0) { - done(); - } - } - - contents.write(new Buffer('wa')); - - process.nextTick(function() { - contents.write(new Buffer('dup')); - contents.end(); - }); - - // Start flowing file2 - file2.contents.on('data', function() {}); - - file2.contents.once('data', function() { - process.nextTick(function() { - // Starts flowing file - file.contents.on('data', function() { - ends.should.equal(2); - }); - }); - }); - - file2.contents.on('end', latch); - file.contents.on('end', latch); - }); - - it('should not start flowing until all clones flows', function(done) { - var contents = new Stream.PassThrough(); - var options = { - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - contents: contents, - }; - var file = new File(options); - var file2 = file.clone(); - var ends = 2; - - function latch() { - if (--ends === 0) { - done(); - } - } - - contents.write(new Buffer('wa')); - - process.nextTick(function() { - contents.write(new Buffer('dup')); - contents.end(); - }); - - // Start flowing file2 - file2.contents.on('readable', function() { - this.read(); - }); - - file2.contents.once('readable', function() { - process.nextTick(function() { - // Starts flowing file - file.contents.on('readable', function() { - ends.should.equal(2); - }); - }); - }); - - file2.contents.on('end', latch); - file.contents.on('end', latch); - }); - - it('should copy all attributes over with null', function(done) { - var options = { - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - contents: null, - }; - var file = new File(options); - var file2 = file.clone(); - - file2.should.not.equal(file, 'refs should be different'); - file2.cwd.should.equal(file.cwd); - file2.base.should.equal(file.base); - file2.path.should.equal(file.path); - should.not.exist(file2.contents); - done(); - }); - - it('should properly clone the `stat` property', function(done) { - var options = { - cwd: '/', - base: '/test/', - path: '/test/test.js', - contents: new Buffer('test'), - stat: fs.statSync(__filename), - }; - - var file = new File(options); - var copy = file.clone(); - - copy.stat.isFile().should.equal(true); - copy.stat.isDirectory().should.equal(false); - should(file.stat instanceof fs.Stats).equal(true); - should(copy.stat instanceof fs.Stats).equal(true); - - done(); - }); - - it('should properly clone the `history` property', function(done) { - var options = { - cwd: path.normalize('/'), - base: path.normalize('/test/'), - path: path.normalize('/test/test.js'), - contents: new Buffer('test'), - stat: fs.statSync(__filename), - }; - - var file = new File(options); - var copy = file.clone(); - - copy.history[0].should.equal(options.path); - copy.path = 'lol'; - file.path.should.not.equal(copy.path); - done(); - }); - - it('should copy custom properties', function(done) { - var options = { - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - contents: null, - }; - - var file = new File(options); - file.custom = { a: 'custom property' }; - - var file2 = file.clone(); - - file2.should.not.equal(file, 'refs should be different'); - file2.cwd.should.equal(file.cwd); - file2.base.should.equal(file.base); - file2.path.should.equal(file.path); - file2.custom.should.not.equal(file.custom); - file2.custom.a.should.equal(file.custom.a); - - done(); - }); - - it('should copy history', function(done) { - var options = { - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - contents: null, - }; - var history = [ - path.normalize('/test/test.coffee'), - path.normalize('/test/test.js'), - path.normalize('/test/test-938di2s.js'), - ]; - - var file = new File(options); - file.path = history[1]; - file.path = history[2]; - var file2 = file.clone(); - - file2.history.should.eql(history); - file2.history.should.not.equal(history); - file2.path.should.eql(history[2]); - - done(); - }); - - it('should copy all attributes deeply', function(done) { - var options = { - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - contents: null, - }; - - var file = new File(options); - file.custom = { a: 'custom property' }; - - var file2 = file.clone(true); - file2.custom.should.eql(file.custom); - file2.custom.should.not.equal(file.custom); - file2.custom.a.should.equal(file.custom.a); - - var file3 = file.clone({ deep: true }); - file3.custom.should.eql(file.custom); - file3.custom.should.not.equal(file.custom); - file3.custom.a.should.equal(file.custom.a); - - var file4 = file.clone(false); - file4.custom.should.eql(file.custom); - file4.custom.should.equal(file.custom); - file4.custom.a.should.equal(file.custom.a); - - var file5 = file.clone({ deep: false }); - file5.custom.should.eql(file.custom); - file5.custom.should.equal(file.custom); - file5.custom.a.should.equal(file.custom.a); - - done(); - }); - - it('should work with extended files', function(done) { - function ExtendedFile() { - File.apply(this, arguments); - } - ExtendedFile.prototype = Object.create(File.prototype); - ExtendedFile.prototype.constructor = ExtendedFile; - // Object.setPrototypeOf(ExtendedFile, File); - // Just copy static stuff since Object.setPrototypeOf is node >=0.12 - Object.keys(File).forEach(function(key) { - ExtendedFile[key] = File[key]; - }); - - var file = new ExtendedFile(); - var file2 = file.clone(); - - file2.should.not.equal(file, 'refs should be different'); - file2.constructor.should.equal(ExtendedFile); - (file2 instanceof ExtendedFile).should.equal(true); - (file2 instanceof File).should.equal(true); - ExtendedFile.prototype.isPrototypeOf(file2).should.equal(true); - File.prototype.isPrototypeOf(file2).should.equal(true); - done(); - }); - }); - - describe('inspect()', function() { - it('should return correct format when no contents and no path', function(done) { - var file = new File(); - file.inspect().should.equal(''); - done(); - }); - - it('should return correct format when Buffer and no path', function(done) { - var val = new Buffer('test'); - var file = new File({ - contents: val, - }); - file.inspect().should.equal('>'); - done(); - }); - - it('should return correct format when Buffer and relative path', function(done) { - var val = new Buffer('test'); - var file = new File({ - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - contents: val, - }); - file.inspect().should.equal('>'); - done(); - }); - - it('should return correct format when Stream and relative path', function(done) { - var file = new File({ - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - contents: new Stream.PassThrough(), - }); - file.inspect().should.equal('>'); - done(); - }); - - it('should return correct format when null and relative path', function(done) { - var file = new File({ - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - contents: null, - }); - file.inspect().should.equal(''); - done(); - }); - }); - - describe('contents get/set', function() { - it('should work with Buffer', function(done) { - var val = new Buffer('test'); - var file = new File(); - file.contents = val; - file.contents.should.equal(val); - done(); - }); - - it('should wrap Stream in Cloneable', function(done) { - var val = new Stream(); - var file = new File(); - file.contents = val; - (typeof file.contents.clone).should.equal('function'); - done(); - }); - - it('should work with null', function(done) { - var val = null; - var file = new File(); - file.contents = val; - (file.contents === null).should.equal(true); - done(); - }); - - it('should not work with string', function(done) { - var val = 'test'; - var file = new File(); - try { - file.contents = val; - } catch (err) { - should.exist(err); - done(); - } - }); - }); - - describe('cwd get/set', function() { - it('should return _cwd', function() { - var file = new File(); - file.cwd = '/test'; - file.cwd.should.equal(file._cwd); - }); - - it('should set cwd', function() { - var file = new File(); - file.cwd = '/test'; - file._cwd.should.equal(path.normalize('/test')); - }); - - it('should normalize and strip trailing sep on set', function() { - var file = new File(); - - file.cwd = '/test/foo/../foo/'; - - if (process.platform === 'win32') { - file.cwd.should.equal('\\test\\foo'); - } else { - file.cwd.should.equal('/test/foo'); - } - - file.cwd = '\\test\\foo\\..\\foo\\'; - - if (process.platform === 'win32') { - file.cwd.should.equal('\\test\\foo'); - } else { - file.cwd.should.equal('\\test\\foo\\..\\foo'); - } - }); - - it('should throw on set when value is empty or not a string', function() { - var notAllowed = [ - '', null, undefined, true, false, 0, Infinity, NaN, {}, [], - ]; - notAllowed.forEach(function(val) { - (function() { - new File().cwd = val; - }).should.throw('cwd must be a non-empty string.'); - }); - }); - }); - - describe('base get/set', function() { - it('should proxy to cwd when omitted', function() { - var file = new File({ - cwd: '/test', - }); - file.base.should.equal(path.normalize('/test')); - }); - - it('should proxy to cwd when same', function() { - var file = new File({ - cwd: '/test', - base: '/test', - }); - file.cwd = '/foo/'; - file.base.should.equal(path.normalize('/foo')); - - var file2 = new File({ - cwd: '/test', - }); - file2.base = '/test/'; - file2.cwd = '/foo/'; - file2.base.should.equal(path.normalize('/foo')); - }); - - it('should proxy to cwd when null or undefined', function() { - var file = new File({ - cwd: '/foo', - base: '/bar', - }); - file.base.should.equal(path.normalize('/bar')); - file.base = null; - file.base.should.equal(path.normalize('/foo')); - file.base = '/bar/'; - file.base.should.equal(path.normalize('/bar')); - file.base = undefined; - file.base.should.equal(path.normalize('/foo')); - }); - - it('should return _base', function() { - var file = new File(); - file._base = '/test/'; - file.base.should.equal('/test/'); - }); - - it('should set base', function() { - var file = new File(); - file.base = '/test/foo'; - file.base.should.equal(path.normalize('/test/foo')); - }); - - it('should normalize and strip trailing sep on set', function() { - var file = new File(); - - file.base = '/test/foo/../foo/'; - - if (process.platform === 'win32') { - file.base.should.equal('\\test\\foo'); - } else { - file.base.should.equal('/test/foo'); - } - - file.base = '\\test\\foo\\..\\foo\\'; - - if (process.platform === 'win32') { - file.base.should.equal('\\test\\foo'); - } else { - file.base.should.equal('\\test\\foo\\..\\foo'); - } - }); - - it('should throw on set when not null/undefined or a non-empty string', function() { - var notStrings = [true, false, 1, 0, Infinity, NaN, '', {}, []]; - notStrings.forEach(function(val) { - (function() { - new File().base = val; - }).should.throw('base must be a non-empty string, or null/undefined.'); - }); - }); - }); - - describe('relative get/set', function() { - it('should error on set', function(done) { - var file = new File(); - try { - file.relative = 'test'; - } catch (err) { - should.exist(err); - done(); - } - }); - - it('should error on get when no path', function(done) { - var a; - var file = new File(); - try { - a = file.relative; - } catch (err) { - should.exist(err); - done(); - } - }); - - it('should return a relative path from base', function(done) { - var file = new File({ - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - }); - file.relative.should.equal('test.coffee'); - done(); - }); - - it('should return a relative path from cwd', function(done) { - var file = new File({ - cwd: '/', - path: '/test/test.coffee', - }); - file.relative.should.equal(path.join('test','test.coffee')); - done(); - }); - - it('should not append sep when directory', function() { - var file = new File({ - base: '/test', - path: '/test/foo/bar', - stat: { - isDirectory: function() { - return true; - }, - }, - }); - file.relative.should.equal(path.normalize('foo/bar')); - }); - - it('should not append sep when directory & simlink', function() { - var file = new File({ - base: '/test', - path: '/test/foo/bar', - stat: { - isDirectory: function() { - return true; - }, - isSymbolicLink: function() { - return true; - }, - }, - }); - file.relative.should.equal(path.normalize('foo/bar')); - }); - }); - - describe('dirname get/set', function() { - it('should error on get when no path', function(done) { - var a; - var file = new File(); - try { - a = file.dirname; - } catch (err) { - should.exist(err); - done(); - } - }); - - it('should return the path without trailing sep', function(done) { - var file = new File({ - cwd: '/', - base: '/test', - path: '/test/test.coffee', - }); - file.dirname.should.equal(path.normalize('/test')); - done(); - }); - - it('should error on set when no path', function(done) { - var file = new File(); - try { - file.dirname = '/test'; - } catch (err) { - should.exist(err); - done(); - } - }); - - it('should set the dirname of the path', function(done) { - var file = new File({ - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - }); - file.dirname = '/test/foo'; - file.path.should.equal(path.normalize('/test/foo/test.coffee')); - done(); - }); - }); - - describe('basename get/set', function() { - it('should error on get when no path', function(done) { - var a; - var file = new File(); - try { - a = file.basename; - } catch (err) { - should.exist(err); - done(); - } - }); - - it('should return the basename of the path', function(done) { - var file = new File({ - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - }); - file.basename.should.equal('test.coffee'); - done(); - }); - - it('should not append trailing sep', function() { - var file = new File({ - path: '/test/foo', - stat: { - isDirectory: function() { - return true; - }, - }, - }); - file.basename.should.equal('foo'); - - var file2 = new File({ - path: '/test/foo', - stat: { - isSymbolicLink: function() { - return true; - }, - }, - }); - file2.basename.should.equal('foo'); - - var file3 = new File({ - path: '/test/foo', - stat: { - isDirectory: function() { - return true; - }, - isSymbolicLink: function() { - return true; - }, - }, - }); - file3.basename.should.equal('foo'); - }); - - it('should error on set when no path', function(done) { - var file = new File(); - try { - file.basename = 'test.coffee'; - } catch (err) { - should.exist(err); - done(); - } - }); - - it('should set the basename of the path', function(done) { - var file = new File({ - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - }); - file.basename = 'foo.png'; - file.path.should.equal(path.normalize('/test/foo.png')); - done(); - }); - }); - - describe('stem get/set', function() { - it('should error on get when no path', function(done) { - var a; - var file = new File(); - try { - a = file.stem; - } catch (err) { - should.exist(err); - done(); - } - }); - - it('should return the stem of the path', function(done) { - var file = new File({ - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - }); - file.stem.should.equal('test'); - done(); - }); - - it('should error on set when no path', function(done) { - var file = new File(); - try { - file.stem = 'test.coffee'; - } catch (err) { - should.exist(err); - done(); - } - }); - - it('should set the stem of the path', function(done) { - var file = new File({ - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - }); - file.stem = 'foo'; - file.path.should.equal(path.normalize('/test/foo.coffee')); - done(); - }); - }); - - describe('extname get/set', function() { - it('should error on get when no path', function(done) { - var a; - var file = new File(); - try { - a = file.extname; - } catch (err) { - should.exist(err); - done(); - } - }); - - it('should return the extname of the path', function(done) { - var file = new File({ - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - }); - file.extname.should.equal('.coffee'); - done(); - }); - - it('should error on set when no path', function(done) { - var file = new File(); - try { - file.extname = '.coffee'; - } catch (err) { - should.exist(err); - done(); - } - }); - - it('should set the extname of the path', function(done) { - var file = new File({ - cwd: '/', - base: '/test/', - path: '/test/test.coffee', - }); - file.extname = '.png'; - file.path.should.equal(path.normalize('/test/test.png')); - done(); - }); - }); - - describe('path get/set', function() { - it('should record history when instantiation', function() { - var file = new File({ - cwd: '/', - path: '/test/test.coffee', - }); - var history = [path.normalize('/test/test.coffee')]; - - file.path.should.eql(history[0]); - file.history.should.eql(history); - }); - - it('should record history when path change', function() { - var file = new File({ - cwd: '/', - path: '/test/test.coffee', - }); - var history = [ - path.normalize('/test/test.coffee'), - path.normalize('/test/test.js'), - ]; - - file.path = history[history.length - 1]; - file.path.should.eql(history[history.length - 1]); - file.history.should.eql(history); - - history.push(path.normalize('/test/test.es6')); - - file.path = history[history.length - 1]; - file.path.should.eql(history[history.length - 1]); - file.history.should.eql(history); - }); - - it('should not record history when set the same path', function() { - var val = path.normalize('/test/test.coffee'); - var file = new File({ - cwd: '/', - path: val, - }); - - file.path = val; - file.path = val; - file.path.should.eql(val); - file.history.should.eql([val]); - - // Ignore when set empty string - file.path = ''; - file.path.should.eql(val); - file.history.should.eql([val]); - }); - - it('should throw when set path null', function() { - var file = new File({ - cwd: '/', - path: null, - }); - - should.not.exist(file.path); - file.history.should.eql([]); - - (function() { - file.path = null; - }).should.throw('path should be a string.'); - }); - - it('should normalize the path on set', function() { - var file = new File(); - - file.path = '/test/foo/../test.coffee'; - - if (process.platform === 'win32') { - file.path.should.equal('\\test\\test.coffee'); - file.history.should.eql(['\\test\\test.coffee']); - } else { - file.path.should.equal('/test/test.coffee'); - file.history.should.eql(['/test/test.coffee']); - } - }); - - it('should strip trailing sep', function() { - var file = new File(); - file.path = '/test/'; - file.path.should.eql(path.normalize('/test')); - file.history.should.eql([path.normalize('/test')]); - - var file2 = new File({ - stat: { - isDirectory: function() { - return true; - }, - }, - }); - file2.path = '/test/'; - file2.path.should.eql(path.normalize('/test')); - file2.history.should.eql([path.normalize('/test')]); - }); - }); - - describe('symlink get/set', function() { - it('should return null on get when no symlink', function(done) { - var file = new File(); - var a = file.symlink; - should.not.exist(a); - done(); - }); - - it('should return the symlink if set', function(done) { - var file = new File({ - symlink: '/test/test.coffee', - }); - file.symlink.should.equal(path.normalize('/test/test.coffee')); - done(); - }); - - it('should error on set with non-string symlink', function(done) { - var file = new File(); - try { - file.symlink = null; - } catch (err) { - should.exist(err); - done(); - } - }); - - it('should set the symlink', function(done) { - var file = new File(); - file.symlink = '/test/test.coffee'; - file.symlink.should.equal(path.normalize('/test/test.coffee')); - done(); - }); - - it('should set the relative symlink', function(done) { - var file = new File(); - file.symlink = 'test.coffee'; - file.symlink.should.equal('test.coffee'); - done(); - }); - - it('should be normalized and stripped off a trailing sep on set', function() { - var file = new File(); - - file.symlink = '/test/foo/../bar/'; - - if (process.platform === 'win32') { - file.symlink.should.equal('\\test\\bar'); - } else { - file.symlink.should.equal('/test/bar'); - } - }); - }); -}); diff --git a/test/clone-buffer.js b/test/clone-buffer.js new file mode 100644 index 0000000..95ecdf6 --- /dev/null +++ b/test/clone-buffer.js @@ -0,0 +1,30 @@ +'use strict'; + +var expect = require('expect'); + +var cloneBuffer = require('../lib/cloneBuffer'); + +describe('cloneBuffer()', function() { + + it('returns a new Buffer reference', function(done) { + var testBuffer = new Buffer('test'); + var testBuffer2 = cloneBuffer(testBuffer); + + expect(testBuffer2).toExist(); + expect(testBuffer2).toBeA(Buffer); + expect(testBuffer2).toNotBe(testBuffer); + done(); + }); + + it('does not replicate modifications to the original Buffer', function(done) { + var testBuffer = new Buffer('test'); + var testBuffer2 = cloneBuffer(testBuffer); + + // Test that changes dont modify both pointers + testBuffer2.write('w'); + + expect(testBuffer.toString('utf8')).toEqual('test'); + expect(testBuffer2.toString('utf8')).toEqual('west'); + done(); + }); +}); diff --git a/test/cloneBuffer.js b/test/cloneBuffer.js deleted file mode 100644 index 20ea8b5..0000000 --- a/test/cloneBuffer.js +++ /dev/null @@ -1,27 +0,0 @@ -var cloneBuffer = require('../lib/cloneBuffer'); -var should = require('should'); -require('mocha'); - -describe('cloneBuffer()', function() { - it('should return a new Buffer reference', function(done) { - var testBuffer = new Buffer('test'); - var testBuffer2 = cloneBuffer(testBuffer); - - should.exist(testBuffer2, 'should return something'); - (testBuffer2 instanceof Buffer).should.equal(true, 'should return a Buffer'); - testBuffer2.should.not.equal(testBuffer, 'pointer should change'); - done(); - }); - - it('should not replicate modifications to the original buffer', function(done) { - var testBuffer = new Buffer('test'); - var testBuffer2 = cloneBuffer(testBuffer); - - // Test that changes dont modify both pointers - testBuffer2.write('w'); - - testBuffer.toString('utf8').should.equal('test', 'original should stay the same'); - testBuffer2.toString('utf8').should.equal('west', 'new buffer should be modified'); - done(); - }); -}); diff --git a/test/file.js b/test/file.js new file mode 100644 index 0000000..66827b1 --- /dev/null +++ b/test/file.js @@ -0,0 +1,1615 @@ +'use strict'; + +var fs = require('fs'); +var path = require('path'); +var expect = require('expect'); +var miss = require('mississippi'); +var cloneable = require('cloneable-readable'); + +var File = require('../'); + +var pipe = miss.pipe; +var from = miss.from; +var concat = miss.concat; +var isCloneable = cloneable.isCloneable; + +describe('File', function() { + + describe('isVinyl()', function() { + + it('returns true for a Vinyl object', function(done) { + var file = new File(); + var result = File.isVinyl(file); + expect(result).toEqual(true); + done(); + }); + + it('returns false for a normal object', function(done) { + var result = File.isVinyl({}); + expect(result).toEqual(false); + done(); + }); + + it('returns false for null', function(done) { + var result = File.isVinyl(null); + expect(result).toEqual(false); + done(); + }); + + it('returns false for a string', function(done) { + var result = File.isVinyl('foobar'); + expect(result).toEqual(false); + done(); + }); + + it('returns false for a String object', function(done) { + var result = File.isVinyl(new String('foobar')); + expect(result).toEqual(false); + done(); + }); + + it('returns false for a number', function(done) { + var result = File.isVinyl(1); + expect(result).toEqual(false); + done(); + }); + + it('returns false for a Number object', function(done) { + var result = File.isVinyl(new Number(1)); + expect(result).toEqual(false); + done(); + }); + + // This is based on current implementation + // A test was added to document and make aware during internal changes + // TODO: decide if this should be leak-able + it('returns true for a mocked object', function(done) { + var result = File.isVinyl({ _isVinyl: true }); + expect(result).toEqual(true); + done(); + }); + }); + + describe('defaults', function() { + + it('defaults cwd to process.cwd', function(done) { + var file = new File(); + expect(file.cwd).toEqual(process.cwd()); + done(); + }); + + it('defaults base to process.cwd', function(done) { + var file = new File(); + expect(file.base).toEqual(process.cwd()); + done(); + }); + + it('defaults base to cwd property', function(done) { + var cwd = path.normalize('/'); + var file = new File({ cwd: cwd }); + expect(file.base).toEqual(cwd); + done(); + }); + + it('defaults path to null', function(done) { + var file = new File(); + expect(file.path).toNotExist(); + expect(file.path).toEqual(null); + done(); + }); + + it('defaults history to an empty array', function(done) { + var file = new File(); + expect(file.history).toEqual([]); + done(); + }); + + it('defaults stat to null', function(done) { + var file = new File(); + expect(file.stat).toNotExist(); + expect(file.stat).toEqual(null); + done(); + }); + + it('defaults contents to null', function(done) { + var file = new File(); + expect(file.contents).toNotExist(); + expect(file.contents).toEqual(null); + done(); + }); + }); + + describe('constructor()', function() { + + it('sets base', function(done) { + var val = path.normalize('/'); + var file = new File({ base: val }); + expect(file.base).toEqual(val); + done(); + }); + + it('sets cwd', function(done) { + var val = path.normalize('/'); + var file = new File({ cwd: val }); + expect(file.cwd).toEqual(val); + done(); + }); + + it('sets path (and history)', function(done) { + var val = path.normalize('/test.coffee'); + var file = new File({ path: val }); + expect(file.path).toEqual(val); + expect(file.history).toEqual([val]); + done(); + }); + + it('sets history (and path)', function(done) { + var val = path.normalize('/test.coffee'); + var file = new File({ history: [val] }); + expect(file.path).toEqual(val); + expect(file.history).toEqual([val]); + done(); + }); + + it('sets stat', function(done) { + var val = {}; + var file = new File({ stat: val }); + expect(file.stat).toEqual(val); + done(); + }); + + it('sets contents', function(done) { + var val = new Buffer('test'); + var file = new File({ contents: val }); + expect(file.contents).toEqual(val); + done(); + }); + + it('sets custom properties', function(done) { + var sourceMap = {}; + var file = new File({ sourceMap: sourceMap }); + expect(file.sourceMap).toEqual(sourceMap); + done(); + }); + + it('normalizes path', function(done) { + var val = '/test/foo/../test.coffee'; + var expected = path.normalize(val); + var file = new File({ path: val }); + expect(file.path).toEqual(expected); + expect(file.history).toEqual([expected]); + done(); + }); + + it('normalizes and removes trailing separator from path', function(done) { + var val = '/test/foo/../foo/'; + var expected = path.normalize(val.slice(0, -1)); + var file = new File({ path: val }); + expect(file.path).toEqual(expected); + done(); + }); + + it('normalizes history', function(done) { + var val = [ + '/test/bar/../bar/test.coffee', + '/test/foo/../test.coffee', + ]; + var expected = val.map(function(p) { + return path.normalize(p); + }); + var file = new File({ history: val }); + expect(file.path).toEqual(expected[1]); + expect(file.history).toEqual(expected); + done(); + }); + + it('normalizes and removes trailing separator from history', function(done) { + var val = [ + '/test/foo/../foo/', + '/test/bar/../bar/', + ]; + var expected = val.map(function(p) { + return path.normalize(p.slice(0, -1)); + }); + var file = new File({ history: val }); + expect(file.history).toEqual(expected); + done(); + }); + + it('appends path to history if both exist and different from last', function(done) { + var val = path.normalize('/test/baz/test.coffee'); + var history = [ + path.normalize('/test/bar/test.coffee'), + path.normalize('/test/foo/test.coffee'), + ]; + var file = new File({ path: val, history: history }); + + var expectedHistory = history.concat(val); + + expect(file.path).toEqual(val); + expect(file.history).toEqual(expectedHistory); + done(); + }); + + it('does not append path to history if both exist and same as last', function(done) { + var val = path.normalize('/test/baz/test.coffee'); + var history = [ + path.normalize('/test/bar/test.coffee'), + path.normalize('/test/foo/test.coffee'), + val, + ]; + var file = new File({ path: val, history: history }); + + expect(file.path).toEqual(val); + expect(file.history).toEqual(history); + done(); + }); + + it('does not mutate history array passed in', function(done) { + var val = path.normalize('/test/baz/test.coffee'); + var history = [ + path.normalize('/test/bar/test.coffee'), + path.normalize('/test/foo/test.coffee'), + ]; + var historyCopy = Array.prototype.slice.call(history); + var file = new File({ path: val, history: history }); + + var expectedHistory = history.concat(val); + + expect(file.path).toEqual(val); + expect(file.history).toEqual(expectedHistory); + expect(history).toEqual(historyCopy); + done(); + }); + }); + + describe('isBuffer()', function() { + + it('returns true when the contents are a Buffer', function(done) { + var val = new Buffer('test'); + var file = new File({ contents: val }); + expect(file.isBuffer()).toEqual(true); + done(); + }); + + it('returns false when the contents are a Stream', function(done) { + var val = from([]); + var file = new File({ contents: val }); + expect(file.isBuffer()).toEqual(false); + done(); + }); + + it('returns false when the contents are null', function(done) { + var file = new File({ contents: null }); + expect(file.isBuffer()).toEqual(false); + done(); + }); + }); + + describe('isStream()', function() { + + it('returns false when the contents are a Buffer', function(done) { + var val = new Buffer('test'); + var file = new File({ contents: val }); + expect(file.isStream()).toEqual(false); + done(); + }); + + it('returns true when the contents are a Stream', function(done) { + var val = from([]); + var file = new File({ contents: val }); + expect(file.isStream()).toEqual(true); + done(); + }); + + it('returns false when the contents are null', function(done) { + var file = new File({ contents: null }); + expect(file.isStream()).toEqual(false); + done(); + }); + }); + + describe('isNull()', function() { + + it('returns false when the contents are a Buffer', function(done) { + var val = new Buffer('test'); + var file = new File({ contents: val }); + expect(file.isNull()).toEqual(false); + done(); + }); + + it('returns false when the contents are a Stream', function(done) { + var val = from([]); + var file = new File({ contents: val }); + expect(file.isNull()).toEqual(false); + done(); + }); + + it('returns true when the contents are null', function(done) { + var file = new File({ contents: null }); + expect(file.isNull()).toEqual(true); + done(); + }); + }); + + describe('isDirectory()', function() { + var fakeStat = { + isDirectory: function() { + return true; + }, + }; + + it('returns false when the contents are a Buffer', function(done) { + var val = new Buffer('test'); + var file = new File({ contents: val, stat: fakeStat }); + expect(file.isDirectory()).toEqual(false); + done(); + }); + + it('returns false when the contents are a Stream', function(done) { + var val = from([]); + var file = new File({ contents: val, stat: fakeStat }); + expect(file.isDirectory()).toEqual(false); + done(); + }); + + it('returns true when the contents are null & stat.isDirectory is true', function(done) { + var file = new File({ contents: null, stat: fakeStat }); + expect(file.isDirectory()).toEqual(true); + done(); + }); + + it('returns false when stat exists but does not contain an isDirectory method', function(done) { + var file = new File({ contents: null, stat: {} }); + expect(file.isDirectory()).toEqual(false); + done(); + }); + + it('returns false when stat does not exist', function(done) { + var file = new File({ contents: null }); + expect(file.isDirectory()).toEqual(false); + done(); + }); + }); + + describe('isSymbolic()', function() { + var fakeStat = { + isSymbolicLink: function() { + return true; + }, + }; + + it('returns false when the contents are a Buffer', function(done) { + var val = new Buffer('test'); + var file = new File({ contents: val, stat: fakeStat }); + expect(file.isSymbolic()).toEqual(false); + done(); + }); + + it('returns false when the contents are a Stream', function(done) { + var val = from([]); + var file = new File({ contents: val, stat: fakeStat }); + expect(file.isSymbolic()).toEqual(false); + done(); + }); + + it('returns true when the contents are null & stat.isSymbolicLink is true', function(done) { + var file = new File({ contents: null, stat: fakeStat }); + expect(file.isSymbolic()).toEqual(true); + done(); + }); + + it('returns false when stat exists but does not contain an isSymbolicLink method', function(done) { + var file = new File({ contents: null, stat: {} }); + expect(file.isSymbolic()).toEqual(false); + done(); + }); + + it('returns false when stat does not exist', function(done) { + var file = new File({ contents: null }); + expect(file.isSymbolic()).toEqual(false); + done(); + }); + }); + + describe('clone()', function() { + + it('copies all attributes over with Buffer contents', function(done) { + var options = { + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + contents: new Buffer('test'), + }; + var file = new File(options); + var file2 = file.clone(); + + expect(file2).toNotBe(file); + expect(file2.cwd).toEqual(file.cwd); + expect(file2.base).toEqual(file.base); + expect(file2.path).toEqual(file.path); + expect(file2.contents).toNotBe(file.contents); + expect(file2.contents.toString('utf8')).toEqual(file.contents.toString('utf8')); + done(); + }); + + it('assigns Buffer content reference when contents option is false', function(done) { + var options = { + cwd: '/', + base: '/test/', + path: '/test/test.js', + contents: new Buffer('test'), + }; + var file = new File(options); + + var copy1 = file.clone({ contents: false }); + expect(copy1.contents).toBe(file.contents); + + var copy2 = file.clone(); + expect(copy2.contents).toNotBe(file.contents); + + var copy3 = file.clone({ contents: 'invalid' }); + expect(copy3.contents).toNotBe(file.contents); + done(); + }); + + it('copies all attributes over with Stream contents', function(done) { + var options = { + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + contents: from(['wa', 'dup']), + }; + var file = new File(options); + var file2 = file.clone(); + + expect(file2).toNotBe(file); + expect(file2.cwd).toEqual(file.cwd); + expect(file2.base).toEqual(file.base); + expect(file2.path).toEqual(file.path); + expect(file2.contents).toNotBe(file.contents); + + var ends = 2; + var data = null; + var data2 = null; + + function assert(err) { + if (err) { + done(err); + return; + } + + if (--ends === 0) { + expect(data).toNotBe(data2); + expect(data.toString('utf8')).toEqual(data2.toString('utf8')); + done(); + } + } + + pipe([ + file.contents, + concat(function(d) { + data = d; + }), + ], assert); + + pipe([ + file2.contents, + concat(function(d) { + data2 = d; + }), + ], assert); + }); + + it('does not start flowing until all clones flows (data)', function(done) { + var options = { + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + contents: from(['wa', 'dup']), + }; + var file = new File(options); + var file2 = file.clone(); + var ends = 2; + + var data = ''; + var data2 = ''; + + function assert() { + if (--ends === 0) { + expect(data).toEqual(data2); + done(); + } + } + + // Start flowing file2 + file2.contents.on('data', function(chunk) { + data2 += chunk.toString('utf8'); + }); + + process.nextTick(function() { + // Nothing was written yet + expect(data).toEqual(''); + expect(data2).toEqual(''); + + // Starts flowing file + file.contents.on('data', function(chunk) { + data += chunk.toString('utf8'); + }); + }); + + file2.contents.on('end', assert); + file.contents.on('end', assert); + }); + + it('does not start flowing until all clones flows (readable)', function(done) { + var options = { + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + contents: from(['wa', 'dup']), + }; + var file = new File(options); + var file2 = file.clone(); + + var data2 = ''; + + function assert(data) { + expect(data.toString('utf8')).toEqual(data2); + } + + // Start flowing file2 + file2.contents.on('readable', function() { + var chunk; + while ((chunk = this.read()) !== null) { + data2 += chunk.toString(); + } + }); + + pipe([ + file.contents, + concat(assert), + ], done); + }); + + it('copies all attributes over with null contents', function(done) { + var options = { + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + contents: null, + }; + var file = new File(options); + var file2 = file.clone(); + + expect(file2).toNotBe(file); + expect(file2.cwd).toEqual(file.cwd); + expect(file2.base).toEqual(file.base); + expect(file2.path).toEqual(file.path); + expect(file2.contents).toNotExist(); + done(); + }); + + it('properly clones the `stat` property', function(done) { + var options = { + cwd: '/', + base: '/test/', + path: '/test/test.js', + contents: new Buffer('test'), + stat: fs.statSync(__filename), + }; + + var file = new File(options); + var copy = file.clone(); + + expect(copy.stat.isFile()).toEqual(true); + expect(copy.stat.isDirectory()).toEqual(false); + expect(file.stat).toBeAn(fs.Stats); + expect(copy.stat).toBeAn(fs.Stats); + done(); + }); + + it('properly clones the `history` property', function(done) { + var options = { + cwd: path.normalize('/'), + base: path.normalize('/test/'), + path: path.normalize('/test/test.js'), + contents: new Buffer('test'), + }; + + var file = new File(options); + var copy = file.clone(); + + expect(copy.history[0]).toEqual(options.path); + copy.path = 'lol'; + expect(file.path).toNotEqual(copy.path); + done(); + }); + + it('copies custom properties', function(done) { + var options = { + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + contents: null, + custom: { meta: {} }, + }; + + var file = new File(options); + var file2 = file.clone(); + + expect(file2).toNotBe(file); + expect(file2.cwd).toEqual(file.cwd); + expect(file2.base).toEqual(file.base); + expect(file2.path).toEqual(file.path); + expect(file2.custom).toNotBe(file.custom); + expect(file2.custom.meta).toNotBe(file.custom.meta); + expect(file2.custom).toEqual(file.custom); + done(); + }); + + it('copies history', function(done) { + var options = { + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + contents: null, + }; + var history = [ + path.normalize('/test/test.coffee'), + path.normalize('/test/test.js'), + path.normalize('/test/test-938di2s.js'), + ]; + + var file = new File(options); + file.path = history[1]; + file.path = history[2]; + var file2 = file.clone(); + + expect(file2.history).toEqual(history); + expect(file2.history).toNotBe(file.history); + expect(file2.path).toEqual(history[2]); + done(); + }); + + it('supports deep & shallow copy of all attributes', function(done) { + var options = { + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + contents: null, + custom: { meta: {} }, + }; + + var file = new File(options); + + var file2 = file.clone(); + expect(file2.custom).toEqual(file.custom); + expect(file2.custom).toNotBe(file.custom); + expect(file2.custom.meta).toEqual(file.custom.meta); + expect(file2.custom.meta).toNotBe(file.custom.meta); + + var file3 = file.clone(true); + expect(file3.custom).toEqual(file.custom); + expect(file3.custom).toNotBe(file.custom); + expect(file3.custom.meta).toEqual(file.custom.meta); + expect(file3.custom.meta).toNotBe(file.custom.meta); + + var file4 = file.clone({ deep: true }); + expect(file4.custom).toEqual(file.custom); + expect(file4.custom).toNotBe(file.custom); + expect(file4.custom.meta).toEqual(file.custom.meta); + expect(file4.custom.meta).toNotBe(file.custom.meta); + + var file5 = file.clone(false); + expect(file5.custom).toEqual(file.custom); + expect(file5.custom).toBe(file.custom); + expect(file5.custom.meta).toEqual(file.custom.meta); + expect(file5.custom.meta).toBe(file.custom.meta); + + var file6 = file.clone({ deep: false }); + expect(file6.custom).toEqual(file.custom); + expect(file6.custom).toBe(file.custom); + expect(file6.custom.meta).toEqual(file.custom.meta); + expect(file6.custom.meta).toBe(file.custom.meta); + + done(); + }); + + it('supports inheritance', function(done) { + function ExtendedFile() { + File.apply(this, arguments); + } + ExtendedFile.prototype = Object.create(File.prototype); + ExtendedFile.prototype.constructor = ExtendedFile; + // Just copy static stuff since Object.setPrototypeOf is node >=0.12 + Object.keys(File).forEach(function(key) { + ExtendedFile[key] = File[key]; + }); + + var file = new ExtendedFile(); + var file2 = file.clone(); + + expect(file2).toNotBe(file); + expect(file2.constructor).toBe(ExtendedFile); + expect(file2).toBeAn(ExtendedFile); + expect(file2).toBeA(File); + expect(ExtendedFile.prototype.isPrototypeOf(file2)).toEqual(true); + expect(File.prototype.isPrototypeOf(file2)).toEqual(true); + done(); + }); + }); + + describe('inspect()', function() { + + it('returns correct format when no contents and no path', function(done) { + var file = new File(); + expect(file.inspect()).toEqual(''); + done(); + }); + + it('returns correct format when Buffer contents and no path', function(done) { + var val = new Buffer('test'); + var file = new File({ contents: val }); + expect(file.inspect()).toEqual('>'); + done(); + }); + + it('returns correct format when Buffer contents and relative path', function(done) { + var val = new Buffer('test'); + var file = new File({ + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + contents: val, + }); + expect(file.inspect()).toEqual('>'); + done(); + }); + + it('returns correct format when Stream contents and relative path', function(done) { + var file = new File({ + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + contents: from([]), + }); + expect(file.inspect()).toEqual('>'); + done(); + }); + + it('returns correct format when null contents and relative path', function(done) { + var file = new File({ + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + contents: null, + }); + expect(file.inspect()).toEqual(''); + done(); + }); + }); + + describe('contents get/set', function() { + + it('returns _contents', function(done) { + var val = new Buffer('test'); + var file = new File(); + file._contents = val; + expect(file.contents).toEqual(val); + done(); + }); + + it('sets _contents', function(done) { + var val = new Buffer('test'); + var file = new File(); + file.contents = val; + expect(file._contents).toEqual(val); + done(); + }); + + it('sets a Buffer', function(done) { + var val = new Buffer('test'); + var file = new File(); + file.contents = val; + expect(file.contents).toEqual(val); + done(); + }); + + it('wraps Stream in Cloneable', function(done) { + var val = from([]); + var file = new File(); + file.contents = val; + expect(isCloneable(file.contents)).toEqual(true); + done(); + }); + + it('does not double wrap a Cloneable', function(done) { + var val = from([]); + var clone = cloneable(val); + var file = new File(); + file.contents = clone; + expect(file.contents._original).toBe(val); + done(); + }); + + it('sets null', function(done) { + var val = null; + var file = new File(); + file.contents = val; + expect(file.contents).toEqual(null); + done(); + }); + + it('does not set a string', function(done) { + var val = 'test'; + var file = new File(); + function invalid() { + file.contents = val; + } + expect(invalid).toThrow(); + done(); + }); + }); + + describe('cwd get/set', function() { + + it('returns _cwd', function(done) { + var val = '/test'; + var file = new File(); + file._cwd = val; + expect(file.cwd).toEqual(val); + done(); + }); + + it('sets _cwd', function(done) { + var val = '/test'; + var file = new File(); + file.cwd = val; + expect(file._cwd).toEqual(path.normalize(val)); + done(); + }); + + it('normalizes and removes trailing separator on set', function(done) { + var val = '/test/foo/../foo/'; + var expected = path.normalize(val.slice(0, -1)); + var file = new File(); + + file.cwd = val; + + expect(file.cwd).toEqual(expected); + + var val2 = '\\test\\foo\\..\\foo\\'; + var expected2 = path.normalize(val2.slice(0, -1)); + + file.cwd = val2; + + expect(file.cwd).toEqual(expected2); + done(); + }); + + it('throws on set with invalid values', function(done) { + var invalidValues = [ + '', + null, + undefined, + true, + false, + 0, + Infinity, + NaN, + {}, + [], + ]; + var file = new File(); + + invalidValues.forEach(function(val) { + function invalid() { + file.cwd = val; + } + expect(invalid).toThrow('cwd must be a non-empty string.'); + }); + + done(); + }); + }); + + describe('base get/set', function() { + + it('proxies cwd when omitted', function(done) { + var file = new File({ cwd: '/test' }); + expect(file.base).toEqual(file.cwd); + done(); + }); + + it('proxies cwd when same', function(done) { + var file = new File({ + cwd: '/test', + base: '/test', + }); + file.cwd = '/foo/'; + expect(file.base).toEqual(file.cwd); + + var file2 = new File({ + cwd: '/test', + }); + file2.base = '/test/'; + file2.cwd = '/foo/'; + expect(file2.base).toEqual(file.cwd); + done(); + }); + + it('proxies to cwd when null or undefined', function(done) { + var file = new File({ + cwd: '/foo', + base: '/bar', + }); + expect(file.base).toNotEqual(file.cwd); + file.base = null; + expect(file.base).toEqual(file.cwd); + file.base = '/bar/'; + expect(file.base).toNotEqual(file.cwd); + file.base = undefined; + expect(file.base).toEqual(file.cwd); + done(); + }); + + it('returns _base', function(done) { + var val = '/test/'; + var file = new File(); + file._base = val; + expect(file.base).toEqual(val); + done(); + }); + + it('sets _base', function(done) { + var val = '/test/foo'; + var file = new File(); + file.base = val; + expect(file._base).toEqual(path.normalize(val)); + done(); + }); + + it('normalizes and removes trailing separator on set', function(done) { + var val = '/test/foo/../foo/'; + var expected = path.normalize(val.slice(0, -1)); + var file = new File(); + + file.base = val; + + expect(file.base).toEqual(expected); + + var val2 = '\\test\\foo\\..\\foo\\'; + var expected2 = path.normalize(val2.slice(0, -1)); + + file.base = val2; + + expect(file.base).toEqual(expected2); + done(); + }); + + it('throws on set with invalid values', function(done) { + var invalidValues = [ + true, + false, + 1, + 0, + Infinity, + NaN, + '', + {}, + [], + ]; + var file = new File(); + + invalidValues.forEach(function(val) { + function invalid() { + file.base = val; + } + expect(invalid).toThrow('base must be a non-empty string, or null/undefined.'); + }); + + done(); + }); + }); + + describe('relative get/set', function() { + + it('throws on set', function(done) { + var file = new File(); + + function invalid() { + file.relative = 'test'; + } + + expect(invalid).toThrow('File.relative is generated from the base and path attributes. Do not modify it.'); + done(); + }); + + it('throws on get with no path', function(done) { + var file = new File(); + + function invalid() { + file.relative; + } + + expect(invalid).toThrow('No path specified! Can not get relative.'); + done(); + }); + + it('returns a relative path from base', function(done) { + var file = new File({ + base: '/test/', + path: '/test/test.coffee', + }); + + expect(file.relative).toEqual('test.coffee'); + done(); + }); + + it('returns a relative path from cwd', function(done) { + var file = new File({ + cwd: '/', + path: '/test/test.coffee', + }); + + expect(file.relative).toEqual(path.normalize('test/test.coffee')); + done(); + }); + + it('does not append separator when directory', function(done) { + var file = new File({ + base: '/test', + path: '/test/foo/bar', + stat: { + isDirectory: function() { + return true; + }, + }, + }); + + expect(file.relative).toEqual(path.normalize('foo/bar')); + done(); + }); + + it('does not append separator when symlink', function(done) { + var file = new File({ + base: '/test', + path: '/test/foo/bar', + stat: { + isSymbolicLink: function() { + return true; + }, + }, + }); + + expect(file.relative).toEqual(path.normalize('foo/bar')); + done(); + }); + + it('does not append separator when directory & symlink', function(done) { + var file = new File({ + base: '/test', + path: '/test/foo/bar', + stat: { + isDirectory: function() { + return true; + }, + isSymbolicLink: function() { + return true; + }, + }, + }); + + expect(file.relative).toEqual(path.normalize('foo/bar')); + done(); + }); + }); + + describe('dirname get/set', function() { + + it('throws on get with no path', function(done) { + var file = new File(); + + function invalid() { + file.dirname; + } + + expect(invalid).toThrow('No path specified! Can not get dirname.'); + done(); + }); + + it('returns the dirname without trailing separator', function(done) { + var file = new File({ + cwd: '/', + base: '/test', + path: '/test/test.coffee', + }); + + expect(file.dirname).toEqual(path.normalize('/test')); + done(); + }); + + it('throws on set with no path', function(done) { + var file = new File(); + + function invalid() { + file.dirname = '/test'; + } + + expect(invalid).toThrow('No path specified! Can not set dirname.'); + done(); + }); + + it('replaces the dirname of the path', function(done) { + var file = new File({ + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + }); + + file.dirname = '/test/foo'; + expect(file.path).toEqual(path.normalize('/test/foo/test.coffee')); + done(); + }); + }); + + describe('basename get/set', function() { + + it('throws on get with no path', function(done) { + var file = new File(); + + function invalid() { + a = file.basename; + } + + expect(invalid).toThrow('No path specified! Can not get basename.'); + done(); + }); + + it('returns the basename of the path', function(done) { + var file = new File({ + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + }); + + expect(file.basename).toEqual('test.coffee'); + done(); + }); + + it('does not append trailing separator when directory', function(done) { + var file = new File({ + path: '/test/foo', + stat: { + isDirectory: function() { + return true; + }, + }, + }); + + expect(file.basename).toEqual('foo'); + done(); + }); + + it('does not append trailing separator when symlink', function(done) { + var file = new File({ + path: '/test/foo', + stat: { + isSymbolicLink: function() { + return true; + }, + }, + }); + + expect(file.basename).toEqual('foo'); + done(); + }); + + it('does not append trailing separator when directory & symlink', function(done) { + var file = new File({ + path: '/test/foo', + stat: { + isDirectory: function() { + return true; + }, + isSymbolicLink: function() { + return true; + }, + }, + }); + + expect(file.basename).toEqual('foo'); + done(); + }); + + it('removes trailing separator', function(done) { + var file = new File({ + path: '/test/foo/', + }); + + expect(file.basename).toEqual('foo'); + done(); + }); + + it('removes trailing separator when directory', function(done) { + var file = new File({ + path: '/test/foo/', + stat: { + isDirectory: function() { + return true; + }, + }, + }); + + expect(file.basename).toEqual('foo'); + done(); + }); + + it('removes trailing separator when symlink', function(done) { + var file = new File({ + path: '/test/foo/', + stat: { + isSymbolicLink: function() { + return true; + }, + }, + }); + + expect(file.basename).toEqual('foo'); + done(); + }); + + it('removes trailing separator when directory & symlink', function(done) { + var file = new File({ + path: '/test/foo/', + stat: { + isDirectory: function() { + return true; + }, + isSymbolicLink: function() { + return true; + }, + }, + }); + + expect(file.basename).toEqual('foo'); + done(); + }); + + it('throws on set with no path', function(done) { + var file = new File(); + + function invalid() { + file.basename = 'test.coffee'; + } + + expect(invalid).toThrow('No path specified! Can not set basename.'); + done(); + }); + + it('replaces the basename of the path', function(done) { + var file = new File({ + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + }); + + file.basename = 'foo.png'; + expect(file.path).toEqual(path.normalize('/test/foo.png')); + done(); + }); + }); + + describe('stem get/set', function() { + + it('throws on get with no path', function(done) { + var file = new File(); + + function invalid() { + file.stem; + } + + expect(invalid).toThrow('No path specified! Can not get stem.'); + done(); + }); + + it('returns the stem of the path', function(done) { + var file = new File({ + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + }); + + expect(file.stem).toEqual('test'); + done(); + }); + + it('throws on set with no path', function(done) { + var file = new File(); + + function invalid() { + file.stem = 'test.coffee'; + } + + expect(invalid).toThrow('No path specified! Can not set stem.'); + done(); + }); + + it('replaces the stem of the path', function(done) { + var file = new File({ + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + }); + + file.stem = 'foo'; + expect(file.path).toEqual(path.normalize('/test/foo.coffee')); + done(); + }); + }); + + describe('extname get/set', function() { + + it('throws on get with no path', function(done) { + var file = new File(); + + function invalid() { + file.extname; + } + + expect(invalid).toThrow('No path specified! Can not get extname.'); + done(); + }); + + it('returns the extname of the path', function(done) { + var file = new File({ + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + }); + + expect(file.extname).toEqual('.coffee'); + done(); + }); + + it('throws on set with no path', function(done) { + var file = new File(); + + function invalid() { + file.extname = '.coffee'; + } + + expect(invalid).toThrow('No path specified! Can not set extname.'); + done(); + }); + + it('replaces the extname of the path', function(done) { + var file = new File({ + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + }); + + file.extname = '.png'; + expect(file.path).toEqual(path.normalize('/test/test.png')); + done(); + }); + }); + + describe('path get/set', function() { + + it('records path in history upon instantiation', function(done) { + var file = new File({ + cwd: '/', + path: '/test/test.coffee', + }); + var history = [ + path.normalize('/test/test.coffee'), + ]; + + expect(file.path).toEqual(history[0]); + expect(file.history).toEqual(history); + done(); + }); + + it('records path in history when set', function(done) { + var val = path.normalize('/test/test.js'); + var file = new File({ + cwd: '/', + path: '/test/test.coffee', + }); + var history = [ + path.normalize('/test/test.coffee'), + val, + ]; + + file.path = val; + expect(file.path).toEqual(val); + expect(file.history).toEqual(history); + + var val2 = path.normalize('/test/test.es6'); + history.push(val2); + + file.path = val2; + expect(file.path).toEqual(val2); + expect(file.history).toEqual(history); + done(); + }); + + it('does not record path in history when set to the current path', function(done) { + var val = path.normalize('/test/test.coffee'); + var file = new File({ + cwd: '/', + path: val, + }); + var history = [ + val, + ]; + + file.path = val; + file.path = val; + expect(file.path).toEqual(val); + expect(file.history).toEqual(history); + done(); + }); + + it('does not record path in history when set to empty string', function(done) { + var val = path.normalize('/test/test.coffee'); + var file = new File({ + cwd: '/', + path: val, + }); + var history = [ + val, + ]; + + file.path = ''; + expect(file.path).toEqual(val); + expect(file.history).toEqual(history); + done(); + }); + + it('throws on set with null path', function(done) { + var file = new File(); + + expect(file.path).toNotExist(); + expect(file.history).toEqual([]); + + function invalid() { + file.path = null; + } + + expect(invalid).toThrow('path should be a string.'); + done(); + }); + + it('normalizes the path upon set', function(done) { + var val = '/test/foo/../test.coffee'; + var expected = path.normalize(val); + var file = new File(); + + file.path = val; + + expect(file.path).toEqual(expected); + expect(file.history).toEqual([expected]); + done(); + }); + + it('removes the trailing separator upon set', function(done) { + var file = new File(); + file.path = '/test/'; + + expect(file.path).toEqual(path.normalize('/test')); + expect(file.history).toEqual([path.normalize('/test')]); + done(); + }); + + it('removes the trailing separator upon set when directory', function(done) { + var file = new File({ + stat: { + isDirectory: function() { + return true; + }, + }, + }); + file.path = '/test/'; + + expect(file.path).toEqual(path.normalize('/test')); + expect(file.history).toEqual([path.normalize('/test')]); + done(); + }); + + it('removes the trailing separator upon set when symlink', function(done) { + var file = new File({ + stat: { + isSymbolicLink: function() { + return true; + }, + }, + }); + file.path = '/test/'; + + expect(file.path).toEqual(path.normalize('/test')); + expect(file.history).toEqual([path.normalize('/test')]); + done(); + }); + + it('removes the trailing separator upon set when directory & symlink', function(done) { + var file = new File({ + stat: { + isDirectory: function() { + return true; + }, + isSymbolicLink: function() { + return true; + }, + }, + }); + file.path = '/test/'; + + expect(file.path).toEqual(path.normalize('/test')); + expect(file.history).toEqual([path.normalize('/test')]); + done(); + }); + }); + + describe('symlink get/set', function() { + + it('return null on get with no symlink', function(done) { + var file = new File(); + + expect(file.symlink).toEqual(null); + done(); + }); + + it('returns _symlink', function(done) { + var val = '/test/test.coffee'; + var file = new File(); + file._symlink = val; + + expect(file.symlink).toEqual(val); + done(); + }); + + it('throws on set with non-string', function(done) { + var file = new File(); + + function invalid() { + file.symlink = null; + } + + expect(invalid).toThrow('symlink should be a string'); + done(); + }); + + it('sets _symlink', function(done) { + var val = '/test/test.coffee'; + var expected = path.normalize(val); + var file = new File(); + file.symlink = val; + + expect(file._symlink).toEqual(expected); + done(); + }); + + it('allows relative symlink', function(done) { + var val = 'test.coffee'; + var file = new File(); + file.symlink = val; + + expect(file.symlink).toEqual(val); + done(); + }); + + it('normalizes and removes trailing separator upon set', function(done) { + var val = '/test/foo/../bar/'; + var expected = path.normalize(val.slice(0, -1)); + var file = new File(); + file.symlink = val; + + expect(file.symlink).toEqual(expected); + done(); + }); + }); +}); diff --git a/test/inspect-stream.js b/test/inspect-stream.js new file mode 100644 index 0000000..921e401 --- /dev/null +++ b/test/inspect-stream.js @@ -0,0 +1,78 @@ +'use strict'; + +var expect = require('expect'); +var Stream = require('readable-stream'); +var Cloneable = require('cloneable-readable'); + +var inspectStream = require('../lib/inspectStream'); + +describe('inspectStream()', function() { + + it('works on a Stream', function(done) { + var testStream = new Stream.Stream(); + var result = inspectStream(testStream); + expect(result).toEqual(''); + done(); + }); + + it('works on a Readable Stream', function(done) { + var testStream = new Stream.Readable(); + var result = inspectStream(testStream); + expect(result).toEqual(''); + done(); + }); + + it('works on a Writable Stream', function(done) { + var testStream = new Stream.Writable(); + var result = inspectStream(testStream); + expect(result).toEqual(''); + done(); + }); + + it('works on a Duplex Stream', function(done) { + var testStream = new Stream.Duplex(); + var result = inspectStream(testStream); + expect(result).toEqual(''); + done(); + }); + + it('works on a Transform Stream', function(done) { + var testStream = new Stream.Transform(); + var result = inspectStream(testStream); + expect(result).toEqual(''); + done(); + }); + + it('works on a PassThrough Stream', function(done) { + var testStream = new Stream.PassThrough(); + var result = inspectStream(testStream); + expect(result).toEqual(''); + done(); + }); + + it('works on a custom Stream', function(done) { + var testStream = new Cloneable(new Stream.Readable()); + var result = inspectStream(testStream); + expect(result).toEqual(''); + done(); + }); + + it('returns nothing for a Buffer', function(done) { + var testBuffer = new Buffer('test'); + var result = inspectStream(testBuffer); + expect(result).toNotExist(); + done(); + }); + + it('returns nothing for null', function(done) { + var result = inspectStream(null); + expect(result).toNotExist(); + done(); + }); + + it('returns nothing for a String', function(done) { + var result = inspectStream('foobar'); + expect(result).toNotExist(); + done(); + }); +}); diff --git a/test/inspectStream.js b/test/inspectStream.js deleted file mode 100644 index b981ec5..0000000 --- a/test/inspectStream.js +++ /dev/null @@ -1,53 +0,0 @@ -var inspectStream = require('../lib/inspectStream'); -var Stream = require('readable-stream'); -var should = require('should'); -require('mocha'); - -describe('inspectStream()', function() { - it('should work on a core Stream', function(done) { - var testStream = new Stream.Stream(); - inspectStream(testStream).should.equal(''); - done(); - }); - - it('should work on a core Readable Stream', function(done) { - var testStream = new Stream.Readable(); - inspectStream(testStream).should.equal(''); - done(); - }); - - it('should work on a core Writable Stream', function(done) { - var testStream = new Stream.Writable(); - inspectStream(testStream).should.equal(''); - done(); - }); - - it('should work on a core Duplex Stream', function(done) { - var testStream = new Stream.Duplex(); - inspectStream(testStream).should.equal(''); - done(); - }); - - it('should work on a core Transform Stream', function(done) { - var testStream = new Stream.Transform(); - inspectStream(testStream).should.equal(''); - done(); - }); - - it('should work on a core PassThrough Stream', function(done) { - var testStream = new Stream.PassThrough(); - inspectStream(testStream).should.equal(''); - done(); - }); - - it('should not work on a Buffer', function(done) { - var testBuffer = new Buffer('test'); - should.not.exist(inspectStream(testBuffer)); - done(); - }); - - it('should not work on a null', function(done) { - should.not.exist(inspectStream(null)); - done(); - }); -}); diff --git a/test/is-buffer.js b/test/is-buffer.js new file mode 100644 index 0000000..52beb31 --- /dev/null +++ b/test/is-buffer.js @@ -0,0 +1,36 @@ +'use strict'; + +var expect = require('expect'); +var Stream = require('readable-stream'); + +var isBuffer = require('../lib/isBuffer'); + +describe('isBuffer()', function() { + + it('returns true for a Buffer', function(done) { + var testBuffer = new Buffer('test'); + var result = isBuffer(testBuffer); + expect(result).toEqual(true); + done(); + }); + + it('returns false for a Stream', function(done) { + var testStream = new Stream(); + var result = isBuffer(testStream); + expect(result).toEqual(false); + done(); + }); + + it('returns false for a null', function(done) { + var result = isBuffer(null); + expect(result).toEqual(false); + done(); + }); + + it('returns false for a array of numbers', function(done) { + var testArray = [1, 2, 3]; + var result = isBuffer(testArray); + expect(result).toEqual(false); + done(); + }); +}); diff --git a/test/is-null.js b/test/is-null.js new file mode 100644 index 0000000..378ccc8 --- /dev/null +++ b/test/is-null.js @@ -0,0 +1,25 @@ +'use strict'; + +var expect = require('expect'); + +var isNull = require('../lib/isNull'); + +describe('isNull()', function() { + + it('returns true for null', function(done) { + expect(isNull(null)).toEqual(true); + done(); + }); + + it('returns false for undefined', function(done) { + expect(isNull()).toEqual(false); + expect(isNull(undefined)).toEqual(false); + done(); + }); + + it('returns false for defined values', function(done) { + expect(isNull(1)).toEqual(false); + expect(isNull('test')).toEqual(false); + done(); + }); +}); diff --git a/test/is-stream.js b/test/is-stream.js new file mode 100644 index 0000000..13bea85 --- /dev/null +++ b/test/is-stream.js @@ -0,0 +1,37 @@ +'use strict'; + +var expect = require('expect'); +// Use node stream to test readable-stream inherits from it +var Stream = require('stream'); + +var isStream = require('../lib/isStream'); + +describe('isStream()', function() { + + it('returns true for a Stream', function(done) { + var testStream = new Stream(); + var result = isStream(testStream); + expect(result).toEqual(true); + done(); + }); + + it('returns false for a Buffer', function(done) { + var testBuffer = new Buffer('test'); + var result = isStream(testBuffer); + expect(result).toEqual(false); + done(); + }); + + it('returns false for null', function(done) { + var result = isStream(null); + expect(result).toEqual(false); + done(); + }); + + it('returns false for an array of numbers', function(done) { + var testArray = [1, 2, 3]; + var result = isStream(testArray); + expect(result).toEqual(false); + done(); + }); +}); diff --git a/test/isBuffer.js b/test/isBuffer.js deleted file mode 100644 index 76cbeb3..0000000 --- a/test/isBuffer.js +++ /dev/null @@ -1,29 +0,0 @@ -var isBuffer = require('../lib/isBuffer'); -var Stream = require('readable-stream'); -require('should'); -require('mocha'); - -describe('isBuffer()', function() { - it('should return true on a Buffer', function(done) { - var testBuffer = new Buffer('test'); - isBuffer(testBuffer).should.equal(true); - done(); - }); - - it('should return false on a Stream', function(done) { - var testStream = new Stream(); - isBuffer(testStream).should.equal(false); - done(); - }); - - it('should return false on a null', function(done) { - isBuffer(null).should.equal(false); - done(); - }); - - it('should return false on a array of numbers', function(done) { - var testArray = [1, 2, 3]; - isBuffer(testArray).should.equal(false); - done(); - }); -}); diff --git a/test/isNull.js b/test/isNull.js deleted file mode 100644 index 356934a..0000000 --- a/test/isNull.js +++ /dev/null @@ -1,22 +0,0 @@ -var isNull = require('../lib/isNull'); -require('should'); -require('mocha'); - -describe('isNull()', function() { - it('should return true on null', function(done) { - isNull(null).should.equal(true); - done(); - }); - - it('should return false on undefined', function(done) { - isNull().should.equal(false); - isNull(undefined).should.equal(false); - done(); - }); - - it('should return false on defined values', function(done) { - isNull(1).should.equal(false); - isNull('test').should.equal(false); - done(); - }); -}); diff --git a/test/isStream.js b/test/isStream.js deleted file mode 100644 index 724cf01..0000000 --- a/test/isStream.js +++ /dev/null @@ -1,30 +0,0 @@ -var isStream = require('../lib/isStream'); -// Use node stream to test readable-stream inherits from it -var Stream = require('stream'); -require('should'); -require('mocha'); - -describe('isStream()', function() { - it('should return true on a Stream', function(done) { - var testStream = new Stream(); - isStream(testStream).should.equal(true); - done(); - }); - - it('should return false on a Buffer', function(done) { - var testBuffer = new Buffer('test'); - isStream(testBuffer).should.equal(false); - done(); - }); - - it('should return false on a null', function(done) { - isStream(null).should.equal(false); - done(); - }); - - it('should return false on a array of numbers', function(done) { - var testArray = [1, 2, 3]; - isStream(testArray).should.equal(false); - done(); - }); -}); diff --git a/test/normalize.js b/test/normalize.js index 2bfa9a2..c13ea12 100644 --- a/test/normalize.js +++ b/test/normalize.js @@ -1,15 +1,22 @@ -var normalize = require('../lib/normalize'); +'use strict'; + var path = require('path'); -require('should'); -require('mocha'); +var expect = require('expect'); + +var normalize = require('../lib/normalize'); describe('normalize()', function() { - it('should leave empty strings unmodified', function() { - normalize('').should.equal(''); + + it('leaves empty strings unmodified', function(done) { + var result = normalize(''); + expect(result).toEqual(''); + done(); }); - it('should apply path.normalize for everything else', function() { + it('applies path.normalize for everything else', function(done) { var str = '/foo//../bar/baz'; - normalize(str).should.equal(path.normalize(str)); + var result = normalize(str); + expect(result).toEqual(path.normalize(str)); + done(); }); }); diff --git a/test/strip-trailing-sep.js b/test/strip-trailing-sep.js new file mode 100644 index 0000000..003a07b --- /dev/null +++ b/test/strip-trailing-sep.js @@ -0,0 +1,45 @@ +'use strict'; + +var expect = require('expect'); + +var stripTrailingSep = require('../lib/stripTrailingSep'); + +describe('stripTrailingSep()', function() { + + it('removes trailing separator', function(done) { + expect(stripTrailingSep('foo/')).toEqual('foo'); + expect(stripTrailingSep('foo\\')).toEqual('foo'); + done(); + }); + + it('does not strip when separator is only char in the string', function(done) { + expect(stripTrailingSep('/')).toEqual('/'); + expect(stripTrailingSep('\\')).toEqual('\\'); + done(); + }); + + it('removes only the trailing separator', function(done) { + expect(stripTrailingSep('/test/foo/bar/')).toEqual('/test/foo/bar'); + expect(stripTrailingSep('\\test\\foo\\bar\\')).toEqual('\\test\\foo\\bar'); + done(); + }); + + it('removes multiple trailing separators', function(done) { + expect(stripTrailingSep('/test//')).toEqual('/test'); + expect(stripTrailingSep('\\test\\\\')).toEqual('\\test'); + done(); + }); + + it('leaves the 1st separator in a string of only separators', function(done) { + expect(stripTrailingSep('//')).toEqual('/'); + expect(stripTrailingSep('////')).toEqual('/'); + expect(stripTrailingSep('\\\\')).toEqual('\\'); + expect(stripTrailingSep('\\\\\\\\')).toEqual('\\'); + done(); + }); + + it('does not change an empty string', function(done) { + expect(stripTrailingSep('')).toEqual(''); + done(); + }); +}); diff --git a/test/stripTrailingSep.js b/test/stripTrailingSep.js deleted file mode 100644 index 2a432ce..0000000 --- a/test/stripTrailingSep.js +++ /dev/null @@ -1,36 +0,0 @@ -var stripTrailingSep = require('../lib/stripTrailingSep'); -require('should'); -require('mocha'); - -describe('stripTrailingSep()', function() { - it('should strip trailing separator', function() { - stripTrailingSep('foo/').should.equal('foo'); - stripTrailingSep('foo\\').should.equal('foo'); - }); - - it('should not strip when the only char in the string', function() { - stripTrailingSep('/').should.equal('/'); - stripTrailingSep('\\').should.equal('\\'); - }); - - it('should strip only the trailing separator', function() { - stripTrailingSep('/test/foo/bar/').should.equal('/test/foo/bar'); - stripTrailingSep('\\test\\foo\\bar\\').should.equal('\\test\\foo\\bar'); - }); - - it('should strip multiple trailing separators', function() { - stripTrailingSep('/test//').should.equal('/test'); - stripTrailingSep('\\test\\\\').should.equal('\\test'); - }); - - it('should leave 1st separator in a string of only separators', function() { - stripTrailingSep('//').should.equal('/'); - stripTrailingSep('////').should.equal('/'); - stripTrailingSep('\\\\').should.equal('\\'); - stripTrailingSep('\\\\\\\\').should.equal('\\'); - }); - - it('should return back empty string', function() { - stripTrailingSep('').should.equal(''); - }); -});