From 5b2351b10b83b5e588f8f9959e23224640475664 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 19 Sep 2016 16:40:45 -0700 Subject: [PATCH 1/8] Update: Kebab-Case test filenames --- test/{cloneBuffer.js => clone-buffer.js} | 0 test/{File.js => file.js} | 0 test/{inspectStream.js => inspect-stream.js} | 0 test/{isBuffer.js => is-buffer.js} | 0 test/{isNull.js => is-null.js} | 0 test/{isStream.js => is-stream.js} | 0 test/{stripTrailingSep.js => strip-trailing-sep.js} | 0 7 files changed, 0 insertions(+), 0 deletions(-) rename test/{cloneBuffer.js => clone-buffer.js} (100%) rename test/{File.js => file.js} (100%) rename test/{inspectStream.js => inspect-stream.js} (100%) rename test/{isBuffer.js => is-buffer.js} (100%) rename test/{isNull.js => is-null.js} (100%) rename test/{isStream.js => is-stream.js} (100%) rename test/{stripTrailingSep.js => strip-trailing-sep.js} (100%) diff --git a/test/cloneBuffer.js b/test/clone-buffer.js similarity index 100% rename from test/cloneBuffer.js rename to test/clone-buffer.js diff --git a/test/File.js b/test/file.js similarity index 100% rename from test/File.js rename to test/file.js diff --git a/test/inspectStream.js b/test/inspect-stream.js similarity index 100% rename from test/inspectStream.js rename to test/inspect-stream.js diff --git a/test/isBuffer.js b/test/is-buffer.js similarity index 100% rename from test/isBuffer.js rename to test/is-buffer.js diff --git a/test/isNull.js b/test/is-null.js similarity index 100% rename from test/isNull.js rename to test/is-null.js diff --git a/test/isStream.js b/test/is-stream.js similarity index 100% rename from test/isStream.js rename to test/is-stream.js diff --git a/test/stripTrailingSep.js b/test/strip-trailing-sep.js similarity index 100% rename from test/stripTrailingSep.js rename to test/strip-trailing-sep.js From 5ab056548ebc3d9066ee4ef6c637f1579dc8f4c8 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 19 Sep 2016 18:10:45 -0700 Subject: [PATCH 2/8] Update: Refactor tests (closes #103) --- package.json | 3 +- test/clone-buffer.js | 21 +- test/file.js | 414 ++++++++++++++++++++----------------- test/inspect-stream.js | 63 ++++-- test/is-buffer.js | 29 ++- test/is-null.js | 23 ++- test/is-stream.js | 29 ++- test/normalize.js | 21 +- test/strip-trailing-sep.js | 51 +++-- 9 files changed, 381 insertions(+), 273 deletions(-) diff --git a/package.json b/package.json index a35838f..fe8ec49 100644 --- a/package.json +++ b/package.json @@ -21,13 +21,14 @@ "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", "jscs": "^2.3.5", "jscs-preset-gulp": "^1.0.0", "lodash.templatesettings": "^3.1.0", + "mississippi": "^1.2.0", "mocha": "^2.0.0", "rimraf": "^2.2.5", "should": "^7.0.0" diff --git a/test/clone-buffer.js b/test/clone-buffer.js index 20ea8b5..95ecdf6 100644 --- a/test/clone-buffer.js +++ b/test/clone-buffer.js @@ -1,27 +1,30 @@ +'use strict'; + +var expect = require('expect'); + var cloneBuffer = require('../lib/cloneBuffer'); -var should = require('should'); -require('mocha'); describe('cloneBuffer()', function() { - it('should return a new Buffer reference', function(done) { + + it('returns 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'); + expect(testBuffer2).toExist(); + expect(testBuffer2).toBeA(Buffer); + expect(testBuffer2).toNotBe(testBuffer); done(); }); - it('should not replicate modifications to the original buffer', function(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'); - testBuffer.toString('utf8').should.equal('test', 'original should stay the same'); - testBuffer2.toString('utf8').should.equal('west', 'new buffer should be modified'); + expect(testBuffer.toString('utf8')).toEqual('test'); + expect(testBuffer2.toString('utf8')).toEqual('west'); done(); }); }); diff --git a/test/file.js b/test/file.js index 5e4271d..05b2c02 100644 --- a/test/file.js +++ b/test/file.js @@ -1,297 +1,330 @@ -var Stream = require('readable-stream'); +'use strict'; + var fs = require('fs'); var path = require('path'); -var es = require('event-stream'); +var expect = require('expect'); +var Stream = require('readable-stream'); +var miss = require('mississippi'); + var File = require('../'); var should = require('should'); -require('mocha'); + +var pipe = miss.pipe; +var from = miss.from; +var concat = miss.concat; describe('File', function() { + describe('isVinyl()', function() { - it('should return true on a vinyl object', function(done) { + + it('returns true for a Vinyl object', function(done) { var file = new File(); - File.isVinyl(file).should.equal(true); + var result = File.isVinyl(file); + expect(result).toEqual(true); done(); }); - it('should return false on a normal object', function(done) { - File.isVinyl({}).should.equal(false); + + it('returns false for a normal object', function(done) { + var result = File.isVinyl({}); + expect(result).toEqual(false); done(); }); - it('should return false on a null object', function(done) { - File.isVinyl(null).should.equal(false); + + 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('constructor()', function() { - it('should default cwd to process.cwd', function(done) { + + describe('defaults', function() { + + it('defaults cwd to process.cwd', function(done) { var file = new File(); - file.cwd.should.equal(process.cwd()); + expect(file.cwd).toEqual(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); + it('defaults base to process.cwd', function(done) { + var file = new File(); + expect(file.base).toEqual(process.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()); + 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('should default path to null', function(done) { + it('defaults path to null', function(done) { var file = new File(); - should.not.exist(file.path); + expect(file.path).toNotExist(); + expect(file.path).toEqual(null); done(); }); - it('should default history to []', function(done) { + it('defaults history to an empty array', function(done) { var file = new File(); - file.history.should.eql([]); + expect(file.history).toEqual([]); done(); }); - it('should default stat to null', function(done) { + it('defaults stat to null', function(done) { var file = new File(); - should.not.exist(file.stat); + expect(file.stat).toNotExist(); + expect(file.stat).toEqual(null); done(); }); - it('should default contents to null', function(done) { + it('defaults contents to null', function(done) { var file = new File(); - should.not.exist(file.contents); + expect(file.contents).toNotExist(); + expect(file.contents).toEqual(null); done(); }); + }); + + describe('constructor()', function() { - it('should set base to given value', function(done) { + it('sets base', function(done) { var val = path.normalize('/'); var file = new File({ base: val }); - file.base.should.equal(val); + expect(file.base).toEqual(val); done(); }); - it('should set cwd to given value', function(done) { + it('sets cwd', function(done) { var val = path.normalize('/'); var file = new File({ cwd: val }); - file.cwd.should.equal(val); + expect(file.cwd).toEqual(val); done(); }); - it('should set path to given value', function(done) { + it('sets path (and history)', function(done) { var val = path.normalize('/test.coffee'); var file = new File({ path: val }); - file.path.should.equal(val); - file.history.should.eql([val]); + expect(file.path).toEqual(val); + expect(file.history).toEqual([val]); done(); }); - it('should set history to given value', function(done) { + it('sets history (and path)', function(done) { var val = path.normalize('/test.coffee'); var file = new File({ history: [val] }); - file.path.should.equal(val); - file.history.should.eql([val]); + expect(file.path).toEqual(val); + expect(file.history).toEqual([val]); done(); }); - it('should set stat to given value', function(done) { + it('sets stat', function(done) { var val = {}; var file = new File({ stat: val }); - file.stat.should.equal(val); + expect(file.stat).toEqual(val); done(); }); - it('should set contents to given value', function(done) { + it('sets contents', function(done) { var val = new Buffer('test'); var file = new File({ contents: val }); - file.contents.should.equal(val); + expect(file.contents).toEqual(val); done(); }); - it('should set custom properties', function(done) { + it('sets custom properties', function(done) { var sourceMap = {}; var file = new File({ sourceMap: sourceMap }); - file.sourceMap.should.equal(sourceMap); + expect(file.sourceMap).toEqual(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('normalizes path', function() { + 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]); }); - 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('normalizes and strips trailing separator from path', function() { + var val = '/test/foo/../foo/'; + var expected = path.normalize(val.slice(0, -1)); + var file = new File({ path: val }); + expect(file.path).toEqual(expected); }); - it('should normalize history', function() { - var history = [ + it('normalizes history', function() { + var val = [ '/test/bar/../bar/test.coffee', '/test/foo/../test.coffee', ]; - var file = new File({ history: history }); + 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); + }); - 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('normalizes and strips trailing separator from history', function() { + 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); }); it('appends path to history if both exist and different from last', function(done) { - var p = path.normalize('/test/baz/test.coffee'); + 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: p, history: history }); + var file = new File({ path: val, history: history }); - var expectedHistory = history.concat(p); + var expectedHistory = history.concat(val); - file.path.should.equal(path.normalize('/test/baz/test.coffee')); - file.history.should.eql(expectedHistory); + 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'), - path.normalize('/test/baz/test.coffee'), + val, ]; - var file = new File({ path: history[history.length - 1], history: history }); + var file = new File({ path: val, history: history }); - file.path.should.equal(path.normalize('/test/baz/test.coffee')); - file.history.should.eql(history); + expect(file.path).toEqual(val); + expect(file.history).toEqual(history); done(); }); it('does not mutate history array passed in', function(done) { - var p = path.normalize('/test/baz/test.coffee'); + 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: p, history: history }); + var file = new File({ path: val, history: history }); - var expectedHistory = history.concat(p); + var expectedHistory = history.concat(val); - file.path.should.equal(path.normalize('/test/baz/test.coffee')); - file.history.should.eql(expectedHistory); - history.should.eql(historyCopy); + expect(file.path).toEqual(val); + expect(file.history).toEqual(expectedHistory); + expect(history).toEqual(historyCopy); done(); }); - }); describe('isBuffer()', function() { - it('should return true when the contents are a Buffer', function(done) { + + it('returns 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); + expect(file.isBuffer()).toEqual(true); done(); }); - it('should return false when the contents are a Stream', function(done) { + it('returns false when the contents are a Stream', function(done) { var val = new Stream(); var file = new File({ contents: val }); - file.isBuffer().should.equal(false); + expect(file.isBuffer()).toEqual(false); done(); }); - it('should return false when the contents are a null', function(done) { + it('returns false when the contents are null', function(done) { var file = new File({ contents: null }); - file.isBuffer().should.equal(false); + expect(file.isBuffer()).toEqual(false); done(); }); }); describe('isStream()', function() { - it('should return false when the contents are a Buffer', function(done) { + + it('returns 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); + expect(file.isStream()).toEqual(false); done(); }); - it('should return true when the contents are a Stream', function(done) { + it('returns true when the contents are a Stream', function(done) { var val = new Stream(); var file = new File({ contents: val }); - file.isStream().should.equal(true); + expect(file.isStream()).toEqual(true); done(); }); - it('should return false when the contents are a null', function(done) { + it('returns false when the contents are null', function(done) { var file = new File({ contents: null }); - file.isStream().should.equal(false); + expect(file.isStream()).toEqual(false); done(); }); }); describe('isNull()', function() { - it('should return false when the contents are a Buffer', function(done) { + + it('returns 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); + expect(file.isNull()).toEqual(false); done(); }); - it('should return false when the contents are a Stream', function(done) { + it('returns false when the contents are a Stream', function(done) { var val = new Stream(); var file = new File({ contents: val }); - file.isNull().should.equal(false); + expect(file.isNull()).toEqual(false); done(); }); - it('should return true when the contents are a null', function(done) { + it('returns true when the contents are null', function(done) { var file = new File({ contents: null }); - file.isNull().should.equal(true); + expect(file.isNull()).toEqual(true); done(); }); }); @@ -303,29 +336,35 @@ describe('File', function() { }, }; - it('should return false when the contents are a Buffer', function(done) { + it('returns 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); + expect(file.isDirectory()).toEqual(false); done(); }); - it('should return false when the contents are a Stream', function(done) { + it('returns 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); + expect(file.isDirectory()).toEqual(false); done(); }); - it('should return true when the contents are a null', function(done) { + it('returns true when the contents are null & stat.isDirectory is true', function(done) { var file = new File({ contents: null, stat: fakeStat }); - file.isDirectory().should.equal(true); + expect(file.isDirectory()).toEqual(true); done(); }); - it('returns false when the stats exist but do not contain isDirectory method', function(done) { + it('returns false when stat exists but does not contain an isDirectory method', function(done) { var file = new File({ contents: null, stat: {} }); - file.isDirectory().should.equal(false); + 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(); }); }); @@ -337,35 +376,42 @@ describe('File', function() { }, }; - it('should return false when the contents are a Buffer', function(done) { + it('returns 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); + expect(file.isSymbolic()).toEqual(false); done(); }); - it('should return false when the contents are a Stream', function(done) { + it('returns 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); + expect(file.isSymbolic()).toEqual(false); done(); }); - it('should return true when the contents are a null', function(done) { + it('returns true when the contents are null & stat.isSymbolicLink is true', function(done) { var file = new File({ contents: null, stat: fakeStat }); - file.isSymbolic().should.equal(true); + expect(file.isSymbolic()).toEqual(true); done(); }); - it('returns false when the stats exist but do not contain isSymbolicLink method', function(done) { + it('returns false when stat exists but does not contain an isSymbolicLink method', function(done) { var file = new File({ contents: null, stat: {} }); - file.isSymbolic().should.equal(false); + 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('should copy all attributes over with Buffer', function(done) { + + it('copies all attributes over with Buffer contents', function(done) { var options = { cwd: '/', base: '/test/', @@ -375,67 +421,67 @@ describe('File', function() { 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')); + 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('should copy buffer\'s reference with option contents: false', function(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 }); - copy1.contents.should.equal(file.contents); + expect(copy1.contents).toBe(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); + var copy2 = file.clone(); + expect(copy2.contents).toNotBe(file.contents); + var copy3 = file.clone({ contents: 'invalid' }); + expect(copy3.contents).toNotBe(file.contents); done(); }); - it('should copy all attributes over with Stream', function(done) { - var contents = new Stream.PassThrough(); + it('copies all attributes over with Stream contents', function(done) { var options = { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: contents, + contents: from(['wa', 'dup']), }; var file = new File(options); var file2 = file.clone(); - contents.write(new Buffer('wa')); - - process.nextTick(function() { - contents.write(new Buffer('dup')); - contents.end(); - }); + 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); + + function combine(data) { + pipe([ + file2.contents, + concat(assert), + ], done); + + function assert(data2) { + expect(data).toNotBe(data2); + expect(data.toString('utf8')).toEqual(data2.toString('utf8')); + } + } - 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(); + pipe([ + file.contents, + concat(combine), + ]); }); it('should not start flowing until all clones flows', function(done) { diff --git a/test/inspect-stream.js b/test/inspect-stream.js index b981ec5..921e401 100644 --- a/test/inspect-stream.js +++ b/test/inspect-stream.js @@ -1,53 +1,78 @@ -var inspectStream = require('../lib/inspectStream'); +'use strict'; + +var expect = require('expect'); var Stream = require('readable-stream'); -var should = require('should'); -require('mocha'); +var Cloneable = require('cloneable-readable'); + +var inspectStream = require('../lib/inspectStream'); describe('inspectStream()', function() { - it('should work on a core Stream', function(done) { + + it('works on a Stream', function(done) { var testStream = new Stream.Stream(); - inspectStream(testStream).should.equal(''); + var result = inspectStream(testStream); + expect(result).toEqual(''); done(); }); - it('should work on a core Readable Stream', function(done) { + it('works on a Readable Stream', function(done) { var testStream = new Stream.Readable(); - inspectStream(testStream).should.equal(''); + var result = inspectStream(testStream); + expect(result).toEqual(''); done(); }); - it('should work on a core Writable Stream', function(done) { + it('works on a Writable Stream', function(done) { var testStream = new Stream.Writable(); - inspectStream(testStream).should.equal(''); + var result = inspectStream(testStream); + expect(result).toEqual(''); done(); }); - it('should work on a core Duplex Stream', function(done) { + it('works on a Duplex Stream', function(done) { var testStream = new Stream.Duplex(); - inspectStream(testStream).should.equal(''); + var result = inspectStream(testStream); + expect(result).toEqual(''); done(); }); - it('should work on a core Transform Stream', function(done) { + it('works on a Transform Stream', function(done) { var testStream = new Stream.Transform(); - inspectStream(testStream).should.equal(''); + var result = inspectStream(testStream); + expect(result).toEqual(''); done(); }); - it('should work on a core PassThrough Stream', function(done) { + it('works on a PassThrough Stream', function(done) { var testStream = new Stream.PassThrough(); - inspectStream(testStream).should.equal(''); + var result = inspectStream(testStream); + expect(result).toEqual(''); done(); }); - it('should not work on a Buffer', function(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'); - should.not.exist(inspectStream(testBuffer)); + var result = inspectStream(testBuffer); + expect(result).toNotExist(); + done(); + }); + + it('returns nothing for null', function(done) { + var result = inspectStream(null); + expect(result).toNotExist(); done(); }); - it('should not work on a null', function(done) { - should.not.exist(inspectStream(null)); + it('returns nothing for a String', function(done) { + var result = inspectStream('foobar'); + expect(result).toNotExist(); done(); }); }); diff --git a/test/is-buffer.js b/test/is-buffer.js index 76cbeb3..52beb31 100644 --- a/test/is-buffer.js +++ b/test/is-buffer.js @@ -1,29 +1,36 @@ -var isBuffer = require('../lib/isBuffer'); +'use strict'; + +var expect = require('expect'); var Stream = require('readable-stream'); -require('should'); -require('mocha'); + +var isBuffer = require('../lib/isBuffer'); describe('isBuffer()', function() { - it('should return true on a Buffer', function(done) { + + it('returns true for a Buffer', function(done) { var testBuffer = new Buffer('test'); - isBuffer(testBuffer).should.equal(true); + var result = isBuffer(testBuffer); + expect(result).toEqual(true); done(); }); - it('should return false on a Stream', function(done) { + it('returns false for a Stream', function(done) { var testStream = new Stream(); - isBuffer(testStream).should.equal(false); + var result = isBuffer(testStream); + expect(result).toEqual(false); done(); }); - it('should return false on a null', function(done) { - isBuffer(null).should.equal(false); + it('returns false for a null', function(done) { + var result = isBuffer(null); + expect(result).toEqual(false); done(); }); - it('should return false on a array of numbers', function(done) { + it('returns false for a array of numbers', function(done) { var testArray = [1, 2, 3]; - isBuffer(testArray).should.equal(false); + var result = isBuffer(testArray); + expect(result).toEqual(false); done(); }); }); diff --git a/test/is-null.js b/test/is-null.js index 356934a..378ccc8 100644 --- a/test/is-null.js +++ b/test/is-null.js @@ -1,22 +1,25 @@ +'use strict'; + +var expect = require('expect'); + 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); + + it('returns true for null', function(done) { + expect(isNull(null)).toEqual(true); done(); }); - it('should return false on undefined', function(done) { - isNull().should.equal(false); - isNull(undefined).should.equal(false); + it('returns false for undefined', function(done) { + expect(isNull()).toEqual(false); + expect(isNull(undefined)).toEqual(false); done(); }); - it('should return false on defined values', function(done) { - isNull(1).should.equal(false); - isNull('test').should.equal(false); + 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 index 724cf01..13bea85 100644 --- a/test/is-stream.js +++ b/test/is-stream.js @@ -1,30 +1,37 @@ -var isStream = require('../lib/isStream'); +'use strict'; + +var expect = require('expect'); // Use node stream to test readable-stream inherits from it var Stream = require('stream'); -require('should'); -require('mocha'); + +var isStream = require('../lib/isStream'); describe('isStream()', function() { - it('should return true on a Stream', function(done) { + + it('returns true for a Stream', function(done) { var testStream = new Stream(); - isStream(testStream).should.equal(true); + var result = isStream(testStream); + expect(result).toEqual(true); done(); }); - it('should return false on a Buffer', function(done) { + it('returns false for a Buffer', function(done) { var testBuffer = new Buffer('test'); - isStream(testBuffer).should.equal(false); + var result = isStream(testBuffer); + expect(result).toEqual(false); done(); }); - it('should return false on a null', function(done) { - isStream(null).should.equal(false); + it('returns false for null', function(done) { + var result = isStream(null); + expect(result).toEqual(false); done(); }); - it('should return false on a array of numbers', function(done) { + it('returns false for an array of numbers', function(done) { var testArray = [1, 2, 3]; - isStream(testArray).should.equal(false); + var result = isStream(testArray); + expect(result).toEqual(false); done(); }); }); diff --git a/test/normalize.js b/test/normalize.js index 2bfa9a2..a25cf79 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('should leave empty strings unmodified', function(done) { + var result = normalize(''); + expect(result).toEqual(''); + done(); }); - it('should apply path.normalize for everything else', function() { + it('should apply 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 index 2a432ce..c8227e6 100644 --- a/test/strip-trailing-sep.js +++ b/test/strip-trailing-sep.js @@ -1,36 +1,45 @@ +'use strict'; + +var expect = require('expect'); + 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('strips trailing separator', function(done) { + expect(stripTrailingSep('foo/')).toEqual('foo'); + expect(stripTrailingSep('foo\\')).toEqual('foo'); + done(); }); - it('should not strip when the only char in the string', function() { - stripTrailingSep('/').should.equal('/'); - stripTrailingSep('\\').should.equal('\\'); + it('does not strip when separator is only char in the string', function(done) { + expect(stripTrailingSep('/')).toEqual('/'); + expect(stripTrailingSep('\\')).toEqual('\\'); + done(); }); - 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('strips 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('should strip multiple trailing separators', function() { - stripTrailingSep('/test//').should.equal('/test'); - stripTrailingSep('\\test\\\\').should.equal('\\test'); + it('strips multiple trailing separators', function(done) { + expect(stripTrailingSep('/test//')).toEqual('/test'); + expect(stripTrailingSep('\\test\\\\')).toEqual('\\test'); + done(); }); - 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('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('should return back empty string', function() { - stripTrailingSep('').should.equal(''); + it('does not change an empty string', function(done) { + expect(stripTrailingSep('')).toEqual(''); + done(); }); }); From 38096364be6d82323f310a3c44d90899f552e45d Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 19 Sep 2016 19:31:03 -0700 Subject: [PATCH 3/8] Failing cloneable-stream test --- test/file.js | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/test/file.js b/test/file.js index 05b2c02..1600e32 100644 --- a/test/file.js +++ b/test/file.js @@ -484,40 +484,36 @@ describe('File', function() { ]); }); - it('should not start flowing until all clones flows', function(done) { - var contents = new Stream.PassThrough(); + it('does not start flowing until all clones flows', function(done) { var options = { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: contents, + contents: from(['wa', 'dup']), }; var file = new File(options); var file2 = file.clone(); var ends = 2; + var data = ''; + var data2 = ''; + function latch() { if (--ends === 0) { + expect(data).toEqual(data2); 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.on('data', function(chunk) { + data2 += chunk.toString('utf8'); + }); file2.contents.once('data', function() { - process.nextTick(function() { - // Starts flowing file - file.contents.on('data', function() { - ends.should.equal(2); - }); + // Starts flowing file + file.contents.on('data', function(chunk) { + data += chunk.toString('utf8'); }); }); From 1a1e0de230c2a1e8487ff506e14bfee67e63afc4 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 21 Sep 2016 22:48:19 +0200 Subject: [PATCH 4/8] Update: Leverage the latest cloneable-readable (#109) --- index.js | 9 ++++----- package.json | 2 +- test/file.js | 46 +++++++++++++++++++++++++++++++--------------- 3 files changed, 36 insertions(+), 21 deletions(-) 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 fe8ec49..850d9c6 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "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" }, diff --git a/test/file.js b/test/file.js index 1600e32..42758af 100644 --- a/test/file.js +++ b/test/file.js @@ -466,22 +466,36 @@ describe('File', function() { expect(file2.path).toEqual(file.path); expect(file2.contents).toNotBe(file.contents); - function combine(data) { - pipe([ - file2.contents, - concat(assert), - ], done); + var ends = 2; + var data = null; + var data2 = null; + + function latch(err) { + if (err) { + done(err); + return; + } - function assert(data2) { + if (--ends === 0) { expect(data).toNotBe(data2); expect(data.toString('utf8')).toEqual(data2.toString('utf8')); + done(); } } pipe([ file.contents, - concat(combine), - ]); + concat(function(d) { + data = d; + }), + ], latch); + + pipe([ + file2.contents, + concat(function(d) { + data2 = d; + }), + ], latch); }); it('does not start flowing until all clones flows', function(done) { @@ -510,7 +524,11 @@ describe('File', function() { data2 += chunk.toString('utf8'); }); - file2.contents.once('data', function() { + 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'); @@ -551,12 +569,10 @@ describe('File', function() { this.read(); }); - file2.contents.once('readable', function() { - process.nextTick(function() { - // Starts flowing file - file.contents.on('readable', function() { - ends.should.equal(2); - }); + process.nextTick(function() { + // Starts flowing file + file.contents.on('readable', function() { + ends.should.equal(2); }); }); From bc1f9907a1641eba02c61c0e30701dba6b94d705 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 21 Sep 2016 16:28:38 -0700 Subject: [PATCH 5/8] Update: More refactor --- test/file.js | 172 +++++++++++++++++++++++++-------------------------- 1 file changed, 83 insertions(+), 89 deletions(-) diff --git a/test/file.js b/test/file.js index 42758af..fbbc9f2 100644 --- a/test/file.js +++ b/test/file.js @@ -470,7 +470,7 @@ describe('File', function() { var data = null; var data2 = null; - function latch(err) { + function assert(err) { if (err) { done(err); return; @@ -488,17 +488,17 @@ describe('File', function() { concat(function(d) { data = d; }), - ], latch); + ], assert); pipe([ file2.contents, concat(function(d) { data2 = d; }), - ], latch); + ], assert); }); - it('does not start flowing until all clones flows', function(done) { + it('does not start flowing until all clones flows (data)', function(done) { var options = { cwd: '/', base: '/test/', @@ -512,7 +512,7 @@ describe('File', function() { var data = ''; var data2 = ''; - function latch() { + function assert() { if (--ends === 0) { expect(data).toEqual(data2); done(); @@ -535,52 +535,41 @@ describe('File', function() { }); }); - file2.contents.on('end', latch); - file.contents.on('end', latch); + file2.contents.on('end', assert); + file.contents.on('end', assert); }); - it('should not start flowing until all clones flows', function(done) { - var contents = new Stream.PassThrough(); + it('does not start flowing until all clones flows (readable)', function(done) { var options = { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: contents, + contents: from(['wa', 'dup']), }; var file = new File(options); var file2 = file.clone(); - var ends = 2; - function latch() { - if (--ends === 0) { - done(); - } - } - - contents.write(new Buffer('wa')); + var data2 = ''; - process.nextTick(function() { - contents.write(new Buffer('dup')); - contents.end(); - }); + function assert(data) { + expect(data.toString('utf8')).toEqual(data2); + } // Start flowing file2 file2.contents.on('readable', function() { - this.read(); - }); - - process.nextTick(function() { - // Starts flowing file - file.contents.on('readable', function() { - ends.should.equal(2); - }); + var chunk; + while ((chunk = this.read()) !== null) { + data2 += chunk.toString(); + } }); - file2.contents.on('end', latch); - file.contents.on('end', latch); + pipe([ + file.contents, + concat(assert), + ], done); }); - it('should copy all attributes over with null', function(done) { + it('copies all attributes over with null contents', function(done) { var options = { cwd: '/', base: '/test/', @@ -590,15 +579,15 @@ describe('File', function() { 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); + 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('should properly clone the `stat` property', function(done) { + it('properly clones the `stat` property', function(done) { var options = { cwd: '/', base: '/test/', @@ -610,56 +599,53 @@ describe('File', function() { 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); - + 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('should properly clone the `history` property', function(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'), - stat: fs.statSync(__filename), }; var file = new File(options); var copy = file.clone(); - copy.history[0].should.equal(options.path); + expect(copy.history[0]).toEqual(options.path); copy.path = 'lol'; - file.path.should.not.equal(copy.path); + expect(file.path).toNotEqual(copy.path); done(); }); - it('should copy custom properties', function(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); - 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); - + 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('should copy history', function(done) { + it('copies history', function(done) { var options = { cwd: '/', base: '/test/', @@ -677,54 +663,62 @@ describe('File', function() { 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]); - + expect(file2.history).toEqual(history); + expect(file2.history).toNotBe(file.history); + expect(file2.path).toEqual(history[2]); done(); }); - it('should copy all attributes deeply', function(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); - 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 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 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({ 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 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(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 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); + 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('should work with extended files', function(done) { + it('supports inheritance', 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]; @@ -733,12 +727,12 @@ describe('File', function() { 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); + 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(); }); }); From ea2ff0bd356ed9f387d9907fbb7fc1c7a3baf5b7 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 21 Sep 2016 17:04:54 -0700 Subject: [PATCH 6/8] Update: Even more test refactor --- test/file.js | 242 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 152 insertions(+), 90 deletions(-) diff --git a/test/file.js b/test/file.js index fbbc9f2..407abf1 100644 --- a/test/file.js +++ b/test/file.js @@ -5,6 +5,7 @@ var path = require('path'); var expect = require('expect'); var Stream = require('readable-stream'); var miss = require('mississippi'); +var cloneable = require('cloneable-readable'); var File = require('../'); @@ -13,6 +14,7 @@ var should = require('should'); var pipe = miss.pipe; var from = miss.from; var concat = miss.concat; +var isCloneable = cloneable.isCloneable; describe('File', function() { @@ -738,22 +740,21 @@ describe('File', function() { }); describe('inspect()', function() { - it('should return correct format when no contents and no path', function(done) { + + it('returns correct format when no contents and no path', function(done) { var file = new File(); - file.inspect().should.equal(''); + expect(file.inspect()).toEqual(''); done(); }); - it('should return correct format when Buffer and no path', function(done) { + it('returns correct format when Buffer contents and no path', function(done) { var val = new Buffer('test'); - var file = new File({ - contents: val, - }); - file.inspect().should.equal('>'); + var file = new File({ contents: val }); + expect(file.inspect()).toEqual('>'); done(); }); - it('should return correct format when Buffer and relative path', function(done) { + it('returns correct format when Buffer contents and relative path', function(done) { var val = new Buffer('test'); var file = new File({ cwd: '/', @@ -761,192 +762,253 @@ describe('File', function() { path: '/test/test.coffee', contents: val, }); - file.inspect().should.equal('>'); + expect(file.inspect()).toEqual('>'); done(); }); - it('should return correct format when Stream and relative path', function(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: new Stream.PassThrough(), + contents: from([]), }); - file.inspect().should.equal('>'); + expect(file.inspect()).toEqual('>'); done(); }); - it('should return correct format when null and relative path', function(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, }); - file.inspect().should.equal(''); + expect(file.inspect()).toEqual(''); done(); }); }); describe('contents get/set', function() { - it('should work with Buffer', function(done) { + + 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; - file.contents.should.equal(val); + expect(file._contents).toEqual(val); done(); }); - it('should wrap Stream in Cloneable', function(done) { - var val = new Stream(); + 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; - (typeof file.contents.clone).should.equal('function'); + 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('should work with null', function(done) { + it('sets null', function(done) { var val = null; var file = new File(); file.contents = val; - (file.contents === null).should.equal(true); + expect(file.contents).toEqual(null); done(); }); - it('should not work with string', function(done) { + it('does not set a string', function(done) { var val = 'test'; var file = new File(); - try { + function invalid() { file.contents = val; - } catch (err) { - should.exist(err); - done(); } + expect(invalid).toThrow(); + done(); }); }); describe('cwd get/set', function() { - it('should return _cwd', function() { + + it('returns _cwd', function(done) { + var val = '/test'; var file = new File(); - file.cwd = '/test'; - file.cwd.should.equal(file._cwd); + file._cwd = val; + expect(file.cwd).toEqual(val); + done(); }); - it('should set cwd', function() { + it('sets _cwd', function(done) { + var val = '/test'; var file = new File(); - file.cwd = '/test'; - file._cwd.should.equal(path.normalize('/test')); + file.cwd = val; + expect(file._cwd).toEqual(path.normalize(val)); + done(); }); - it('should normalize and strip trailing sep on set', function() { + 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 = '/test/foo/../foo/'; + file.cwd = val; - if (process.platform === 'win32') { - file.cwd.should.equal('\\test\\foo'); - } else { - file.cwd.should.equal('/test/foo'); - } + expect(file.cwd).toEqual(expected); - file.cwd = '\\test\\foo\\..\\foo\\'; + var val2 = '\\test\\foo\\..\\foo\\'; + var expected2 = path.normalize(val2.slice(0, -1)); - if (process.platform === 'win32') { - file.cwd.should.equal('\\test\\foo'); - } else { - file.cwd.should.equal('\\test\\foo\\..\\foo'); - } + file.cwd = val2; + + expect(file.cwd).toEqual(expected2); + done(); }); - it('should throw on set when value is empty or not a string', function() { - var notAllowed = [ - '', null, undefined, true, false, 0, Infinity, NaN, {}, [], + it('throws on set with invalid values', function(done) { + var invalidValues = [ + '', + 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.'); + 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('should proxy to cwd when omitted', function() { - var file = new File({ - cwd: '/test', - }); - file.base.should.equal(path.normalize('/test')); + + it('proxies cwd when omitted', function(done) { + var file = new File({ cwd: '/test' }); + expect(file.base).toEqual(file.cwd); + done(); }); - it('should proxy to cwd when same', function() { + it('proxies cwd when same', function(done) { var file = new File({ cwd: '/test', base: '/test', }); file.cwd = '/foo/'; - file.base.should.equal(path.normalize('/foo')); + expect(file.base).toEqual(file.cwd); var file2 = new File({ cwd: '/test', }); file2.base = '/test/'; file2.cwd = '/foo/'; - file2.base.should.equal(path.normalize('/foo')); + expect(file2.base).toEqual(file.cwd); + done(); }); - it('should proxy to cwd when null or undefined', function() { + it('proxies to cwd when null or undefined', function(done) { var file = new File({ cwd: '/foo', base: '/bar', }); - file.base.should.equal(path.normalize('/bar')); + expect(file.base).toNotEqual(file.cwd); file.base = null; - file.base.should.equal(path.normalize('/foo')); + expect(file.base).toEqual(file.cwd); file.base = '/bar/'; - file.base.should.equal(path.normalize('/bar')); + expect(file.base).toNotEqual(file.cwd); file.base = undefined; - file.base.should.equal(path.normalize('/foo')); + expect(file.base).toEqual(file.cwd); + done(); }); - it('should return _base', function() { + it('returns _base', function(done) { + var val = '/test/'; var file = new File(); - file._base = '/test/'; - file.base.should.equal('/test/'); + file._base = val; + expect(file.base).toEqual(val); + done(); }); - it('should set base', function() { + it('sets _base', function(done) { + var val = '/test/foo'; var file = new File(); - file.base = '/test/foo'; - file.base.should.equal(path.normalize('/test/foo')); + file.base = val; + expect(file._base).toEqual(path.normalize(val)); + done(); }); - it('should normalize and strip trailing sep on set', function() { + 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 = '/test/foo/../foo/'; + file.base = val; - if (process.platform === 'win32') { - file.base.should.equal('\\test\\foo'); - } else { - file.base.should.equal('/test/foo'); - } + expect(file.base).toEqual(expected); - file.base = '\\test\\foo\\..\\foo\\'; + var val2 = '\\test\\foo\\..\\foo\\'; + var expected2 = path.normalize(val2.slice(0, -1)); - if (process.platform === 'win32') { - file.base.should.equal('\\test\\foo'); - } else { - file.base.should.equal('\\test\\foo\\..\\foo'); - } + file.base = val2; + + expect(file.base).toEqual(expected2); + done(); }); - 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.'); + 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(); }); }); From 095fc408c2cfcc3116c6af5ea356670f29ce73ee Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Thu, 22 Sep 2016 16:03:09 -0700 Subject: [PATCH 7/8] Update: Even more refactor --- test/file.js | 246 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 172 insertions(+), 74 deletions(-) diff --git a/test/file.js b/test/file.js index 407abf1..2a6d8ef 100644 --- a/test/file.js +++ b/test/file.js @@ -1013,47 +1013,50 @@ describe('File', function() { }); describe('relative get/set', function() { - it('should error on set', function(done) { + + it('throws on set', function(done) { var file = new File(); - try { + + function invalid() { file.relative = 'test'; - } catch (err) { - should.exist(err); - done(); } + + expect(invalid).toThrow('File.relative is generated from the base and path attributes. Do not modify it.'); + done(); }); - it('should error on get when no path', function(done) { - var a; + it('throws on get with no path', function(done) { var file = new File(); - try { - a = file.relative; - } catch (err) { - should.exist(err); - done(); + + function invalid() { + file.relative; } + + expect(invalid).toThrow('No path specified! Can not get relative.'); + done(); }); - it('should return a relative path from base', function(done) { + it('returns a relative path from base', function(done) { var file = new File({ - cwd: '/', base: '/test/', path: '/test/test.coffee', }); - file.relative.should.equal('test.coffee'); + + expect(file.relative).toEqual('test.coffee'); done(); }); - it('should return a relative path from cwd', function(done) { + it('returns 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')); + + expect(file.relative).toEqual(path.normalize('test/test.coffee')); done(); }); - it('should not append sep when directory', function() { + it('does not append separator when directory', function(done) { var file = new File({ base: '/test', path: '/test/foo/bar', @@ -1063,10 +1066,27 @@ describe('File', function() { }, }, }); - file.relative.should.equal(path.normalize('foo/bar')); + + 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('should not append sep when directory & simlink', function() { + it('does not append separator when directory & symlink', function(done) { var file = new File({ base: '/test', path: '/test/foo/bar', @@ -1079,77 +1099,85 @@ describe('File', function() { }, }, }); - file.relative.should.equal(path.normalize('foo/bar')); + + expect(file.relative).toEqual(path.normalize('foo/bar')); + done(); }); }); describe('dirname get/set', function() { - it('should error on get when no path', function(done) { - var a; + + it('throws on get with no path', function(done) { var file = new File(); - try { - a = file.dirname; - } catch (err) { - should.exist(err); - done(); + + function invalid() { + file.dirname; } + + expect(invalid).toThrow('No path specified! Can not get dirname.'); + done(); }); - it('should return the path without trailing sep', function(done) { + it('returns the dirname without trailing separator', function(done) { var file = new File({ cwd: '/', base: '/test', path: '/test/test.coffee', }); - file.dirname.should.equal(path.normalize('/test')); + + expect(file.dirname).toEqual(path.normalize('/test')); done(); }); - it('should error on set when no path', function(done) { + it('throws on set with no path', function(done) { var file = new File(); - try { + + function invalid() { file.dirname = '/test'; - } catch (err) { - should.exist(err); - done(); } + + expect(invalid).toThrow('No path specified! Can not set dirname.'); + done(); }); - it('should set the dirname of the path', function(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'; - file.path.should.equal(path.normalize('/test/foo/test.coffee')); + expect(file.path).toEqual(path.normalize('/test/foo/test.coffee')); done(); }); }); describe('basename get/set', function() { - it('should error on get when no path', function(done) { - var a; + + it('throws on get with no path', function(done) { var file = new File(); - try { + + function invalid() { a = file.basename; - } catch (err) { - should.exist(err); - done(); } + + expect(invalid).toThrow('No path specified! Can not get basename.'); + done(); }); - it('should return the basename of the path', function(done) { + it('returns the basename of the path', function(done) { var file = new File({ cwd: '/', base: '/test/', path: '/test/test.coffee', }); - file.basename.should.equal('test.coffee'); + + expect(file.basename).toEqual('test.coffee'); done(); }); - it('should not append trailing sep', function() { + it('does not append trailing separator when directory', function(done) { var file = new File({ path: '/test/foo', stat: { @@ -1158,9 +1186,13 @@ describe('File', function() { }, }, }); - file.basename.should.equal('foo'); - var file2 = new File({ + 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() { @@ -1168,9 +1200,13 @@ describe('File', function() { }, }, }); - file2.basename.should.equal('foo'); - var file3 = new File({ + 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() { @@ -1181,71 +1217,133 @@ describe('File', function() { }, }, }); - file3.basename.should.equal('foo'); + + expect(file.basename).toEqual('foo'); + done(); }); - it('should error on set when no path', function(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(); - try { + + function invalid() { file.basename = 'test.coffee'; - } catch (err) { - should.exist(err); - done(); } + + expect(invalid).toThrow('No path specified! Can not set basename.'); + done(); }); - it('should set the basename of the path', function(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'; - file.path.should.equal(path.normalize('/test/foo.png')); + expect(file.path).toEqual(path.normalize('/test/foo.png')); done(); }); }); describe('stem get/set', function() { - it('should error on get when no path', function(done) { - var a; + + it('throws on get with no path', function(done) { var file = new File(); - try { - a = file.stem; - } catch (err) { - should.exist(err); - done(); + + function invalid() { + file.stem; } + + expect(invalid).toThrow('No path specified! Can not get stem.'); + done(); }); - it('should return the stem of the path', function(done) { + it('returns the stem of the path', function(done) { var file = new File({ cwd: '/', base: '/test/', path: '/test/test.coffee', }); - file.stem.should.equal('test'); + + expect(file.stem).toEqual('test'); done(); }); - it('should error on set when no path', function(done) { + it('throws on set with no path', function(done) { var file = new File(); - try { + + function invalid() { file.stem = 'test.coffee'; - } catch (err) { - should.exist(err); - done(); } + + expect(invalid).toThrow('No path specified! Can not set stem.'); + done(); }); - it('should set the stem of the path', function(done) { + it('replaces 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')); + expect(file.path).toEqual(path.normalize('/test/foo.coffee')); done(); }); }); From 37888253c3f5cd60c16385ff9af80a762c109ef8 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Thu, 22 Sep 2016 16:54:15 -0700 Subject: [PATCH 8/8] Update: Finish test refactor --- package.json | 15 +-- test/file.js | 266 +++++++++++++++++++++++-------------- test/normalize.js | 4 +- test/strip-trailing-sep.js | 6 +- 4 files changed, 179 insertions(+), 112 deletions(-) diff --git a/package.json b/package.json index 850d9c6..1112f8c 100644 --- a/package.json +++ b/package.json @@ -18,26 +18,23 @@ "replace-ext": "^1.0.0" }, "devDependencies": { - "buffer-equal": "0.0.1", "eslint": "^1.7.3", "eslint-config-gulp": "^2.0.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", "mississippi": "^1.2.0", - "mocha": "^2.0.0", - "rimraf": "^2.2.5", - "should": "^7.0.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 index 2a6d8ef..66827b1 100644 --- a/test/file.js +++ b/test/file.js @@ -3,14 +3,11 @@ var fs = require('fs'); var path = require('path'); var expect = require('expect'); -var Stream = require('readable-stream'); var miss = require('mississippi'); var cloneable = require('cloneable-readable'); var File = require('../'); -var should = require('should'); - var pipe = miss.pipe; var from = miss.from; var concat = miss.concat; @@ -175,22 +172,24 @@ describe('File', function() { done(); }); - it('normalizes path', function() { + 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 strips trailing separator from path', function() { + 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() { + it('normalizes history', function(done) { var val = [ '/test/bar/../bar/test.coffee', '/test/foo/../test.coffee', @@ -201,9 +200,10 @@ describe('File', function() { var file = new File({ history: val }); expect(file.path).toEqual(expected[1]); expect(file.history).toEqual(expected); + done(); }); - it('normalizes and strips trailing separator from history', function() { + it('normalizes and removes trailing separator from history', function(done) { var val = [ '/test/foo/../foo/', '/test/bar/../bar/', @@ -213,6 +213,7 @@ describe('File', function() { }); 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) { @@ -272,7 +273,7 @@ describe('File', function() { }); it('returns false when the contents are a Stream', function(done) { - var val = new Stream(); + var val = from([]); var file = new File({ contents: val }); expect(file.isBuffer()).toEqual(false); done(); @@ -295,7 +296,7 @@ describe('File', function() { }); it('returns true when the contents are a Stream', function(done) { - var val = new Stream(); + var val = from([]); var file = new File({ contents: val }); expect(file.isStream()).toEqual(true); done(); @@ -318,7 +319,7 @@ describe('File', function() { }); it('returns false when the contents are a Stream', function(done) { - var val = new Stream(); + var val = from([]); var file = new File({ contents: val }); expect(file.isNull()).toEqual(false); done(); @@ -346,7 +347,7 @@ describe('File', function() { }); it('returns false when the contents are a Stream', function(done) { - var val = new Stream(); + var val = from([]); var file = new File({ contents: val, stat: fakeStat }); expect(file.isDirectory()).toEqual(false); done(); @@ -386,7 +387,7 @@ describe('File', function() { }); it('returns false when the contents are a Stream', function(done) { - var val = new Stream(); + var val = from([]); var file = new File({ contents: val, stat: fakeStat }); expect(file.isSymbolic()).toEqual(false); done(); @@ -1349,197 +1350,266 @@ describe('File', function() { }); describe('extname get/set', function() { - it('should error on get when no path', function(done) { - var a; + + it('throws on get with no path', function(done) { var file = new File(); - try { - a = file.extname; - } catch (err) { - should.exist(err); - done(); + + function invalid() { + file.extname; } + + expect(invalid).toThrow('No path specified! Can not get extname.'); + done(); }); - it('should return the extname of the path', function(done) { + it('returns the extname of the path', function(done) { var file = new File({ cwd: '/', base: '/test/', path: '/test/test.coffee', }); - file.extname.should.equal('.coffee'); + + expect(file.extname).toEqual('.coffee'); done(); }); - it('should error on set when no path', function(done) { + it('throws on set with no path', function(done) { var file = new File(); - try { + + function invalid() { file.extname = '.coffee'; - } catch (err) { - should.exist(err); - done(); } + + expect(invalid).toThrow('No path specified! Can not set extname.'); + done(); }); - it('should set the extname of the path', function(done) { + it('replaces 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')); + expect(file.path).toEqual(path.normalize('/test/test.png')); done(); }); }); describe('path get/set', function() { - it('should record history when instantiation', 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')]; + var history = [ + path.normalize('/test/test.coffee'), + ]; - file.path.should.eql(history[0]); - file.history.should.eql(history); + expect(file.path).toEqual(history[0]); + expect(file.history).toEqual(history); + done(); }); - it('should record history when path change', function() { + 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'), - path.normalize('/test/test.js'), + val, ]; - file.path = history[history.length - 1]; - file.path.should.eql(history[history.length - 1]); - file.history.should.eql(history); + file.path = val; + expect(file.path).toEqual(val); + expect(file.history).toEqual(history); - history.push(path.normalize('/test/test.es6')); + var val2 = path.normalize('/test/test.es6'); + history.push(val2); - file.path = history[history.length - 1]; - file.path.should.eql(history[history.length - 1]); - file.history.should.eql(history); + file.path = val2; + expect(file.path).toEqual(val2); + expect(file.history).toEqual(history); + done(); }); - it('should not record history when set the same path', function() { + 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; - 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]); + expect(file.path).toEqual(val); + expect(file.history).toEqual(history); + done(); }); - it('should throw when set path null', function() { + 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: null, + path: val, }); + var history = [ + val, + ]; - should.not.exist(file.path); - file.history.should.eql([]); + file.path = ''; + expect(file.path).toEqual(val); + expect(file.history).toEqual(history); + done(); + }); - (function() { + 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; - }).should.throw('path should be a string.'); + } + + expect(invalid).toThrow('path should be a string.'); + done(); }); - it('should normalize the path on set', function() { + 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 = '/test/foo/../test.coffee'; + file.path = val; - 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']); - } + expect(file.path).toEqual(expected); + expect(file.history).toEqual([expected]); + done(); }); - it('should strip trailing sep', function() { + it('removes the trailing separator upon set', function(done) { 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({ + 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; }, }, }); - file2.path = '/test/'; - file2.path.should.eql(path.normalize('/test')); - file2.history.should.eql([path.normalize('/test')]); + 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('should return null on get when no symlink', function(done) { + + it('return null on get with no symlink', function(done) { var file = new File(); - var a = file.symlink; - should.not.exist(a); + + expect(file.symlink).toEqual(null); 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')); + it('returns _symlink', function(done) { + var val = '/test/test.coffee'; + var file = new File(); + file._symlink = val; + + expect(file.symlink).toEqual(val); done(); }); - it('should error on set with non-string symlink', function(done) { + it('throws on set with non-string', function(done) { var file = new File(); - try { + + function invalid() { file.symlink = null; - } catch (err) { - should.exist(err); - done(); } + + expect(invalid).toThrow('symlink should be a string'); + done(); }); - it('should set the symlink', function(done) { + it('sets _symlink', function(done) { + var val = '/test/test.coffee'; + var expected = path.normalize(val); var file = new File(); - file.symlink = '/test/test.coffee'; - file.symlink.should.equal(path.normalize('/test/test.coffee')); + file.symlink = val; + + expect(file._symlink).toEqual(expected); done(); }); - it('should set the relative symlink', function(done) { + it('allows relative symlink', function(done) { + var val = 'test.coffee'; var file = new File(); - file.symlink = 'test.coffee'; - file.symlink.should.equal('test.coffee'); + file.symlink = val; + + expect(file.symlink).toEqual(val); done(); }); - it('should be normalized and stripped off a trailing sep on set', function() { + 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; - file.symlink = '/test/foo/../bar/'; - - if (process.platform === 'win32') { - file.symlink.should.equal('\\test\\bar'); - } else { - file.symlink.should.equal('/test/bar'); - } + expect(file.symlink).toEqual(expected); + done(); }); }); }); diff --git a/test/normalize.js b/test/normalize.js index a25cf79..c13ea12 100644 --- a/test/normalize.js +++ b/test/normalize.js @@ -7,13 +7,13 @@ var normalize = require('../lib/normalize'); describe('normalize()', function() { - it('should leave empty strings unmodified', function(done) { + it('leaves empty strings unmodified', function(done) { var result = normalize(''); expect(result).toEqual(''); done(); }); - it('should apply path.normalize for everything else', function(done) { + it('applies path.normalize for everything else', function(done) { var str = '/foo//../bar/baz'; var result = normalize(str); expect(result).toEqual(path.normalize(str)); diff --git a/test/strip-trailing-sep.js b/test/strip-trailing-sep.js index c8227e6..003a07b 100644 --- a/test/strip-trailing-sep.js +++ b/test/strip-trailing-sep.js @@ -6,7 +6,7 @@ var stripTrailingSep = require('../lib/stripTrailingSep'); describe('stripTrailingSep()', function() { - it('strips trailing separator', function(done) { + it('removes trailing separator', function(done) { expect(stripTrailingSep('foo/')).toEqual('foo'); expect(stripTrailingSep('foo\\')).toEqual('foo'); done(); @@ -18,13 +18,13 @@ describe('stripTrailingSep()', function() { done(); }); - it('strips only the trailing separator', function(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('strips multiple trailing separators', function(done) { + it('removes multiple trailing separators', function(done) { expect(stripTrailingSep('/test//')).toEqual('/test'); expect(stripTrailingSep('\\test\\\\')).toEqual('\\test'); done();