From bc02e97a0d416c255cc89eff693d50d9dea93015 Mon Sep 17 00:00:00 2001 From: Steve Lacy Date: Mon, 23 Feb 2015 12:15:41 -0800 Subject: [PATCH] breakout tests --- test/dotNotation.js | 51 +++++ test/errorCases.js | 82 ++++++++ test/fileFixtures.js | 111 +++++++++++ test/index.js | 111 +++++++++++ test/main.js | 377 ----------------------------------- test/whitespacePreserving.js | 86 ++++++++ 6 files changed, 441 insertions(+), 377 deletions(-) create mode 100644 test/dotNotation.js create mode 100644 test/errorCases.js create mode 100644 test/fileFixtures.js create mode 100644 test/index.js delete mode 100644 test/main.js create mode 100644 test/whitespacePreserving.js diff --git a/test/dotNotation.js b/test/dotNotation.js new file mode 100644 index 0000000..c3cd019 --- /dev/null +++ b/test/dotNotation.js @@ -0,0 +1,51 @@ +var gutil = require('gulp-util'); +var should = require('should'); +var bump = require('..'); + +require('mocha'); + + describe('gulp-bump: dot notation', function() { + var dotObject = { + subversion: { + version:'1.2.3' + } + }; + + it('should bump dot notation', function (done) { + var fakeFile = new gutil.File({ + base: 'test/', + cwd: 'test/', + path: 'test/fixtures/package.json', + contents: new Buffer(JSON.stringify(dotObject, null, 2)) + }); + var bumpS = bump({key: 'subversion.version'}); + + bumpS.once('data', function(newFile) { + var json = JSON.parse(String(newFile.contents)); + json.subversion.version.should.equal('1.2.4'); + done(); + }); + bumpS.write(fakeFile); + }); + + it('should bump dot notation with type', function (done) { + var fakeFile = new gutil.File({ + base: 'test/', + cwd: 'test/', + path: 'test/fixtures/package.json', + contents: new Buffer(JSON.stringify(dotObject, null, 2)) + }); + var bumpS = bump({ + key: 'subversion.version', + type: 'minor' + }); + + bumpS.once('data', function(newFile) { + var json = JSON.parse(String(newFile.contents)); + json.subversion.version.should.equal('1.3.0'); + done(); + }); + bumpS.write(fakeFile); + }); + + }); diff --git a/test/errorCases.js b/test/errorCases.js new file mode 100644 index 0000000..3e703e6 --- /dev/null +++ b/test/errorCases.js @@ -0,0 +1,82 @@ +var gutil = require('gulp-util'); +var should = require('should'); +var bump = require('..'); + +require('mocha'); + +describe('Test failure cases cases in gulp-bump', function() { + + it('should fail when not detect a valid semver version', function(done) { + var file = 'some-dir/dummyfile.js'; + var fakeFile = new gutil.File({ + path: file, + contents: new Buffer('{ "version": "0.A.1" }') + }); + + var bumpS = bump(); + + bumpS.on('error', function(e) { + should.exist(e); + e.message.should.equal('Detected invalid semver version'); + e.fileName.should.containEql(file); + return done(); + }); + bumpS.write(fakeFile); + bumpS.end(); + }); + + it('should fail when not detect a valid semver version and wrong key', function(done) { + var file = 'some-dir/dummyfile.js'; + var fakeFile = new gutil.File({ + path: file, + contents: new Buffer('{ "version": "0.0.1" }') + }); + + var bumpS = bump({key: 'appversion'}); + + bumpS.on('error', function(e) { + should.exist(e); + e.message.should.containEql('Detected invalid semver appversion'); + e.fileName.should.containEql(file); + return done(); + }); + bumpS.write(fakeFile); + bumpS.end(); + }); + + it('should fail when supplied with an invalid JSON', function(done) { + var file = 'some-dir/dummyfile.js'; + var fakeFile = new gutil.File({ + path: file, + contents: new Buffer('{ invalid json oh no!!!}') + }); + + var bumpS = bump(); + + bumpS.on('error', function(e) { + should.exist(e); + e.name.should.equal('Error'); + e.message.should.containEql('Problem parsing JSON file'); + e.fileName.should.containEql(file); + return done(); + }); + bumpS.write(fakeFile); + bumpS.end(); + }); + + it('should fallback to defaults when supplied with invalid semver version', function(done) { + var fakeFile = new gutil.File({ + contents: new Buffer('{ "version": "0.0.1" }') + }); + var bumpS = bump({version: '0.A.2'}); + + bumpS.once('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.contents); + JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2'); + return done(); + }); + bumpS.write(fakeFile); + bumpS.end(); + }); +}); diff --git a/test/fileFixtures.js b/test/fileFixtures.js new file mode 100644 index 0000000..5a2df69 --- /dev/null +++ b/test/fileFixtures.js @@ -0,0 +1,111 @@ +var fs = require('fs'); +var gutil = require('gulp-util'); +var should = require('should'); +var bump = require('..'); + +require('mocha'); + +var fixtureFile = fs.readFileSync('test/fixtures/package.json'); + +describe('gulp-bump: JSON File fixtures', function() { + + it('should bump minor by default', function(done) { + var fakeFile = new gutil.File({ + base: 'test/', + cwd: 'test/', + path: 'test/fixtures/package.json', + contents: fixtureFile + }); + + var bumpS = bump(); + + bumpS.once('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.path); + should.exist(newFile.contents); + String(newFile.contents).should.equal(fs.readFileSync('test/expected/default.json', 'utf8')); + done(); + }); + bumpS.write(fakeFile); + }); + + it('should bump major if options.bump = major', function(done) { + var fakeFile = new gutil.File({ + base: 'test/', + cwd: 'test/', + path: 'test/fixtures/package.json', + contents: fixtureFile + }); + + var bumpS = bump({type: 'major'}); + + bumpS.once('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.path); + should.exist(newFile.contents); + String(newFile.contents).should.equal(fs.readFileSync('test/expected/major.json', 'utf8')); + done(); + }); + bumpS.write(fakeFile); + }); + + it('should bump minor if options.bump = minor', function(done) { + var fakeFile = new gutil.File({ + base: 'test/', + cwd: 'test/', + path: 'test/fixtures/package.json', + contents: fixtureFile + }); + + var bumpS = bump({type: 'minor'}); + + bumpS.once('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.path); + should.exist(newFile.contents); + String(newFile.contents).should.equal(fs.readFileSync('test/expected/minor.json', 'utf8')); + done(); + }); + bumpS.write(fakeFile); + }); + + it('should set version to value specified by options.version', function(done) { + var fakeFile = new gutil.File({ + base: 'test/', + cwd: 'test/', + path: 'test/fixtures/package.json', + contents: fixtureFile + }); + + var bumpS = bump({version: '1.0.0'}); + + bumpS.once('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.path); + should.exist(newFile.contents); + String(newFile.contents).should.equal(fs.readFileSync('test/expected/version.json', 'utf8')); + done(); + }); + bumpS.write(fakeFile); + }); + + it('should set the key to a custom version', function(done) { + var fakeFile = new gutil.File({ + base: 'test/', + cwd: 'test/', + path: 'test/fixtures/key.json', + contents: fs.readFileSync('test/fixtures/key.json') + }); + + var bumpS = bump({key: 'appversion'}); + + bumpS.once('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.path); + should.exist(newFile.contents); + String(newFile.contents).should.equal(fs.readFileSync('test/expected/key.json', 'utf8')); + done(); + }); + bumpS.write(fakeFile); + }); +}); diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..3afb0dd --- /dev/null +++ b/test/index.js @@ -0,0 +1,111 @@ +var gutil = require('gulp-util'); +var should = require('should'); +var bump = require('..'); + +require('mocha'); + +describe('gulp-bump: JSON comparison fixtures', function() { + + it('should bump patch version by default', function(done) { + var fakeFile = new gutil.File({ + contents: new Buffer('{ "version": "0.0.9" }') + }); + + var bumpS = bump(); + + bumpS.once('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.contents); + JSON.parse(newFile.contents.toString()).version.should.equal('0.0.10'); + return done(); + }); + bumpS.write(fakeFile); + bumpS.end(); + }); + + it('should bump patch version as default and a key=appversion', function(done) { + var fakeFile = new gutil.File({ + contents: new Buffer('{ "appversion": "0.0.1" }') + }); + + var bumpS = bump({key: 'appversion'}); + + bumpS.once('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.contents); + JSON.parse(newFile.contents.toString()).appversion.should.equal('0.0.2'); + return done(); + }); + bumpS.write(fakeFile); + bumpS.end(); + }); + + it('should ignore invalid type and use type=patch', function(done) { + var fakeFile = new gutil.File({ + contents: new Buffer('{ "version": "0.0.1" }') + }); + + var bumpS = bump({type: 'invalidType'}); + + bumpS.once('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.contents); + JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2'); + return done(); + }); + + bumpS.write(fakeFile); + bumpS.end(); + }); + + it('should set the correct version when supplied', function(done) { + var fakeFile = new gutil.File({ + contents: new Buffer('{ "version": "0.0.1" }') + }); + + var bumpS = bump({version: '0.0.2'}); + + bumpS.once('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.contents); + JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2'); + return done(); + }); + bumpS.write(fakeFile); + bumpS.end(); + }); + + it('should set the correct version when supplied even if key did not exist', function(done) { + var fakeFile = new gutil.File({ + contents: new Buffer('{}') + }); + + var bumpS = bump({version: '0.0.2'}); + + bumpS.once('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.contents); + JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2'); + return done(); + }); + bumpS.write(fakeFile); + bumpS.end(); + }); + + it('should bump prerelease version', function(done) { + var fakeFile = new gutil.File({ + contents: new Buffer('{ "version": "0.0.1-0"}') + }); + + var bumpS = bump({type: 'prerelease'}); + + bumpS.once('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.contents); + JSON.parse(newFile.contents.toString()).version.should.equal('0.0.1-1'); + return done(); + }); + bumpS.write(fakeFile); + bumpS.end(); + }); +}); diff --git a/test/main.js b/test/main.js deleted file mode 100644 index 883a821..0000000 --- a/test/main.js +++ /dev/null @@ -1,377 +0,0 @@ -var fs = require('fs'); -var gutil = require('gulp-util'); -var should = require('should'); -var bump = require('../index'); - -require('mocha'); - -var fixtureFile = fs.readFileSync('test/fixtures/package.json'); - -describe('gulp-bump', function() { - - describe('gulp-bump - JSON comparison fixtures', function() { - - it('should bump patch version by default', function(done) { - var fakeFile = new gutil.File({ - contents: new Buffer('{ "version": "0.0.9" }') - }); - - var bumpS = bump(); - - bumpS.once('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.contents); - JSON.parse(newFile.contents.toString()).version.should.equal('0.0.10'); - return done(); - }); - bumpS.write(fakeFile); - bumpS.end(); - }); - - it('should bump patch version as default and a key=appversion', function(done) { - var fakeFile = new gutil.File({ - contents: new Buffer('{ "appversion": "0.0.1" }') - }); - - var bumpS = bump({key: 'appversion'}); - - bumpS.once('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.contents); - JSON.parse(newFile.contents.toString()).appversion.should.equal('0.0.2'); - return done(); - }); - bumpS.write(fakeFile); - bumpS.end(); - }); - - it('should ignore invalid type and use type=patch', function(done) { - var fakeFile = new gutil.File({ - contents: new Buffer('{ "version": "0.0.1" }') - }); - - var bumpS = bump({type: 'invalidType'}); - - bumpS.once('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.contents); - JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2'); - return done(); - }); - - bumpS.write(fakeFile); - bumpS.end(); - }); - - it('should set the correct version when supplied', function(done) { - var fakeFile = new gutil.File({ - contents: new Buffer('{ "version": "0.0.1" }') - }); - - var bumpS = bump({version: '0.0.2'}); - - bumpS.once('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.contents); - JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2'); - return done(); - }); - bumpS.write(fakeFile); - bumpS.end(); - }); - - it('should set the correct version when supplied even if key did not exist', function(done) { - var fakeFile = new gutil.File({ - contents: new Buffer('{}') - }); - - var bumpS = bump({version: '0.0.2'}); - - bumpS.once('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.contents); - JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2'); - return done(); - }); - bumpS.write(fakeFile); - bumpS.end(); - }); - - it('should bump prerelease version', function(done) { - var fakeFile = new gutil.File({ - contents: new Buffer('{ "version": "0.0.1-0"}') - }); - - var bumpS = bump({type: 'prerelease'}); - - bumpS.once('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.contents); - JSON.parse(newFile.contents.toString()).version.should.equal('0.0.1-1'); - return done(); - }); - bumpS.write(fakeFile); - bumpS.end(); - }); - }); - - describe('Test failure cases cases in gulp-bump', function() { - - it('should fail when not detect a valid semver version', function(done) { - var file = 'some-dir/dummyfile.js'; - var fakeFile = new gutil.File({ - path: file, - contents: new Buffer('{ "version": "0.A.1" }') - }); - - var bumpS = bump(); - - bumpS.on('error', function(e) { - should.exist(e); - e.message.should.equal('Detected invalid semver version'); - e.fileName.should.containEql(file); - return done(); - }); - bumpS.write(fakeFile); - bumpS.end(); - }); - - it('should fail when not detect a valid semver version and wrong key', function(done) { - var file = 'some-dir/dummyfile.js'; - var fakeFile = new gutil.File({ - path: file, - contents: new Buffer('{ "version": "0.0.1" }') - }); - - var bumpS = bump({key: 'appversion'}); - - bumpS.on('error', function(e) { - should.exist(e); - e.message.should.containEql('Detected invalid semver appversion'); - e.fileName.should.containEql(file); - return done(); - }); - bumpS.write(fakeFile); - bumpS.end(); - }); - - it('should fail when supplied with an invalid JSON', function(done) { - var file = 'some-dir/dummyfile.js'; - var fakeFile = new gutil.File({ - path: file, - contents: new Buffer('{ invalid json oh no!!!}') - }); - - var bumpS = bump(); - - bumpS.on('error', function(e) { - should.exist(e); - e.name.should.equal('Error'); - e.message.should.containEql('Problem parsing JSON file'); - e.fileName.should.containEql(file); - return done(); - }); - bumpS.write(fakeFile); - bumpS.end(); - }); - - it('should fallback to defaults when supplied with invalid semver version', function(done) { - var fakeFile = new gutil.File({ - contents: new Buffer('{ "version": "0.0.1" }') - }); - var bumpS = bump({version: '0.A.2'}); - - bumpS.once('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.contents); - JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2'); - return done(); - }); - bumpS.write(fakeFile); - bumpS.end(); - }); - }); - - describe('gulp-bump - JSON File fixtures', function() { - - it('should bump minor by default', function(done) { - var fakeFile = new gutil.File({ - base: 'test/', - cwd: 'test/', - path: 'test/fixtures/package.json', - contents: fixtureFile - }); - - var bumpS = bump(); - - bumpS.once('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.path); - should.exist(newFile.contents); - String(newFile.contents).should.equal(fs.readFileSync('test/expected/default.json', 'utf8')); - done(); - }); - bumpS.write(fakeFile); - }); - - it('should bump major if options.bump = major', function(done) { - var fakeFile = new gutil.File({ - base: 'test/', - cwd: 'test/', - path: 'test/fixtures/package.json', - contents: fixtureFile - }); - - var bumpS = bump({type: 'major'}); - - bumpS.once('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.path); - should.exist(newFile.contents); - String(newFile.contents).should.equal(fs.readFileSync('test/expected/major.json', 'utf8')); - done(); - }); - bumpS.write(fakeFile); - }); - - it('should bump minor if options.bump = minor', function(done) { - var fakeFile = new gutil.File({ - base: 'test/', - cwd: 'test/', - path: 'test/fixtures/package.json', - contents: fixtureFile - }); - - var bumpS = bump({type: 'minor'}); - - bumpS.once('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.path); - should.exist(newFile.contents); - String(newFile.contents).should.equal(fs.readFileSync('test/expected/minor.json', 'utf8')); - done(); - }); - bumpS.write(fakeFile); - }); - - it('should set version to value specified by options.version', function(done) { - var fakeFile = new gutil.File({ - base: 'test/', - cwd: 'test/', - path: 'test/fixtures/package.json', - contents: fixtureFile - }); - - var bumpS = bump({version: '1.0.0'}); - - bumpS.once('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.path); - should.exist(newFile.contents); - String(newFile.contents).should.equal(fs.readFileSync('test/expected/version.json', 'utf8')); - done(); - }); - bumpS.write(fakeFile); - }); - - it('should set the key to a custom version', function(done) { - var fakeFile = new gutil.File({ - base: 'test/', - cwd: 'test/', - path: 'test/fixtures/key.json', - contents: fs.readFileSync('test/fixtures/key.json') - }); - - var bumpS = bump({key: 'appversion'}); - - bumpS.once('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.path); - should.exist(newFile.contents); - String(newFile.contents).should.equal(fs.readFileSync('test/expected/key.json', 'utf8')); - done(); - }); - bumpS.write(fakeFile); - }); - }); - - describe('gulp-bump - Whitespace preserving', function() { - - var fixtureObj = { version: '1.0.0' }; - var expectedObj = { version: '1.0.1' }; - - var createFile = function (tabType) { - return new gutil.File({ - base: 'test/', - cwd: 'test/', - path: 'test/fixtures/package.json', - contents: new Buffer(JSON.stringify(fixtureObj, null, tabType)) - }); - }; - - it('should preserve tab whitespace settings', function (done) { - var fakeFile = createFile('\t'); - var bumpS = bump(); - - bumpS.once('data', function(newFile) { - String(newFile.contents).should.equal(JSON.stringify(expectedObj, null, '\t')); - done(); - }); - bumpS.write(fakeFile); - }); - - it('should preserve spaces whitespace settings', function (done) { - var fakeFile = createFile(3); - var bumpS = bump(); - - bumpS.once('data', function(newFile) { - String(newFile.contents).should.equal(JSON.stringify(expectedObj, null, 3)); - done(); - }); - bumpS.write(fakeFile); - }); - - it('should override whitespace if indent defined', function (done) { - var fakeFile = createFile(3); - var bumpS = bump({ indent: 2 }); - - bumpS.once('data', function(newFile) { - String(newFile.contents).should.equal(JSON.stringify(expectedObj, null, 2)); - done(); - }); - bumpS.write(fakeFile); - }); - - it('should preserve whitespace at end', function (done) { - var fakeFile = new gutil.File({ - base: 'test/', - cwd: 'test/', - path: 'test/fixtures/package.json', - contents: new Buffer(JSON.stringify(fixtureObj, null, 2) + '\n') - }); - var bumpS = bump(); - - bumpS.once('data', function(newFile) { - String(newFile.contents.slice(-1)).should.equal('\n'); - done(); - }); - bumpS.write(fakeFile); - }); - - it('should not add new line to file', function (done) { - var fakeFile = new gutil.File({ - base: 'test/', - cwd: 'test/', - path: 'test/fixtures/package.json', - contents: new Buffer(JSON.stringify(fixtureObj, null, 2)) - }); - var bumpS = bump(); - - bumpS.once('data', function(newFile) { - String(newFile.contents.slice(-1)).should.not.equal('\n'); - done(); - }); - bumpS.write(fakeFile); - }); - }); -}); diff --git a/test/whitespacePreserving.js b/test/whitespacePreserving.js new file mode 100644 index 0000000..9264a1b --- /dev/null +++ b/test/whitespacePreserving.js @@ -0,0 +1,86 @@ +var gutil = require('gulp-util'); +var should = require('should'); +var bump = require('..'); + +require('mocha'); + +describe('gulp-bump: Whitespace preserving', function() { + + var fixtureObj = { version: '1.0.0' }; + var expectedObj = { version: '1.0.1' }; + + var createFile = function (tabType) { + return new gutil.File({ + base: 'test/', + cwd: 'test/', + path: 'test/fixtures/package.json', + contents: new Buffer(JSON.stringify(fixtureObj, null, tabType)) + }); + }; + + it('should preserve tab whitespace settings', function (done) { + var fakeFile = createFile('\t'); + var bumpS = bump(); + + bumpS.once('data', function(newFile) { + String(newFile.contents).should.equal(JSON.stringify(expectedObj, null, '\t')); + done(); + }); + bumpS.write(fakeFile); + }); + + it('should preserve spaces whitespace settings', function (done) { + var fakeFile = createFile(3); + var bumpS = bump(); + + bumpS.once('data', function(newFile) { + String(newFile.contents).should.equal(JSON.stringify(expectedObj, null, 3)); + done(); + }); + bumpS.write(fakeFile); + }); + + it('should override whitespace if indent defined', function (done) { + var fakeFile = createFile(3); + var bumpS = bump({ indent: 2 }); + + bumpS.once('data', function(newFile) { + String(newFile.contents).should.equal(JSON.stringify(expectedObj, null, 2)); + done(); + }); + bumpS.write(fakeFile); + }); + + it('should preserve whitespace at end', function (done) { + var fakeFile = new gutil.File({ + base: 'test/', + cwd: 'test/', + path: 'test/fixtures/package.json', + contents: new Buffer(JSON.stringify(fixtureObj, null, 2) + '\n') + }); + var bumpS = bump(); + + bumpS.once('data', function(newFile) { + String(newFile.contents.slice(-1)).should.equal('\n'); + done(); + }); + bumpS.write(fakeFile); + }); + + it('should not add new line to file', function (done) { + var fakeFile = new gutil.File({ + base: 'test/', + cwd: 'test/', + path: 'test/fixtures/package.json', + contents: new Buffer(JSON.stringify(fixtureObj, null, 2)) + }); + var bumpS = bump(); + + bumpS.once('data', function(newFile) { + String(newFile.contents.slice(-1)).should.not.equal('\n'); + done(); + }); + bumpS.write(fakeFile); + }); +}); +