Skip to content

Commit

Permalink
Update bower version tests, refs bower#2139.
Browse files Browse the repository at this point in the history
`bower version` currently takes the version from the "version" field of
bower.json, but the spec says that bower ignores it.

This commit updates the `bower version` tests so that they are
consistent with the spec. Now, the tests are based on the assumption
that `bower version` should only use git tags.
  • Loading branch information
hdgarrood committed Apr 2, 2016
1 parent f4cb047 commit c0c2858
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 41 deletions.
116 changes: 75 additions & 41 deletions test/commands/version.js
Expand Up @@ -3,87 +3,121 @@ var expect = require('expect.js');
var helpers = require('../helpers');
var version = helpers.require('lib/commands').version;

describe('bower list', function () {
describe('bower version', function () {

var mainPackage = new helpers.TempDir({
'bower.json': {
name: 'foobar',
version: '0.0.0'
}
});

var gitPackage = new helpers.TempDir({
'v0.0.0': {
'bower.json': {
name: 'foobar',
version: '0.0.0'
}
}
});

it('bumps patch version', function () {
mainPackage.prepare();
var packageWithoutTags = new helpers.TempDir({});


return helpers.run(version, ['patch', {}, { cwd: mainPackage.path }]).then(function () {
expect(mainPackage.readJson('bower.json').version).to.be('0.0.1');
it('bumps patch version', function() {
mainPackage.prepareGit();

return helpers.run(version, ['patch', {}, { cwd: mainPackage.path }]).then(function() {
expect(mainPackage.latestGitTag()).to.be('0.0.1');
});
});

it('bumps minor version', function () {
mainPackage.prepare();
it('bumps minor version', function() {
mainPackage.prepareGit();

return helpers.run(version, ['minor', {}, { cwd: mainPackage.path }]).then(function () {
expect(mainPackage.readJson('bower.json').version).to.be('0.1.0');
return helpers.run(version, ['minor', {}, { cwd: mainPackage.path }]).then(function() {
expect(mainPackage.latestGitTag()).to.be('0.1.0');
});
});

it('bumps major version', function () {
mainPackage.prepare();
it('bumps major version', function() {
mainPackage.prepareGit();

return helpers.run(version, ['major', {}, { cwd: mainPackage.path }]).then(function () {
expect(mainPackage.readJson('bower.json').version).to.be('1.0.0');
return helpers.run(version, ['major', {}, { cwd: mainPackage.path }]).then(function() {
expect(mainPackage.latestGitTag()).to.be('1.0.0');
});
});

it('changes version', function () {
mainPackage.prepare();
it('changes version', function() {
mainPackage.prepareGit();

return helpers.run(version, ['1.2.3', {}, { cwd: mainPackage.path }]).then(function () {
expect(mainPackage.readJson('bower.json').version).to.be('1.2.3');
return helpers.run(version, ['1.2.3', {}, { cwd: mainPackage.path }]).then(function() {
expect(mainPackage.latestGitTag()).to.be('1.2.3');
});
});

it('returns the new version', function () {
mainPackage.prepare();
it('returns the new version', function() {
mainPackage.prepareGit();

return helpers.run(version, ['major', {}, { cwd: mainPackage.path }]).then(function (results) {
expect(results[0]).to.be('1.0.0');
});
});

it('bumps patch version, create commit, and tag', function () {
gitPackage.prepareGit();
it('fails on a dirty git repository', function() {
mainPackage.prepareGit();
mainPackage.create({
'dirty.txt': 'This file has not been committed'
});

return helpers.run(version, ['patch', {}, { cwd: gitPackage.path }]).then(function () {
expect(gitPackage.readJson('bower.json').version).to.be('0.0.1');
return helpers.run(version, ['patch', {}, { cwd: mainPackage.path }]).then(null, function(err) {
expect(err).to.be.an(Error);
expect(err.code).to.be('ENOTGITREPOSITORY');
});
});

var tags = gitPackage.git('tag');
expect(tags).to.be('v0.0.0\nv0.0.1\n');
var message = gitPackage.git('log', '--pretty=format:%s', '-n1');
expect(message).to.be('v0.0.1');
it('fails when the version is not changed', function() {
mainPackage.prepareGit();

return helpers.run(version, ['0.0.0', {}, { cwd: mainPackage.path }]).then(null, function(err) {
expect(err).to.be.an(Error);
expect(err.code).to.be('EVERSIONNOTCHANGED');
});
});

it('bumps with custom commit message', function () {
gitPackage.prepareGit();
it('fails with an invalid argument', function() {
mainPackage.prepareGit();

return helpers.run(version, ['patch', { message: 'Bumping %s, because what'}, { cwd: gitPackage.path }]).then(function () {
expect(gitPackage.readJson('bower.json').version).to.be('0.0.1');
return helpers.run(version, ['lol', {}, { cwd: mainPackage.path }]).then(null, function(err) {
expect(err).to.be.an(Error);
expect(err.code).to.be('EINVALIDVERSION');
});
});

var tags = gitPackage.git('tag');
it('bumps with custom commit message', function() {
mainPackage.prepareGit();

return helpers.run(version, ['patch', { message: 'Bumping %s, because what'}, { cwd: mainPackage.path }]).then(function() {
var tags = mainPackage.git('tag');
expect(tags).to.be('v0.0.0\nv0.0.1\n');
var message = gitPackage.git('log', '--pretty=format:%s', '-n1');
var message = mainPackage.git('log', '--pretty=format:%s', '-n1');
expect(message).to.be('Bumping 0.0.1, because what');
});
});

it('creates commit and tags', function() {
mainPackage.prepareGit();

return helpers.run(version, ['patch', {}, { cwd: mainPackage.path }]).then(function() {
var tags = mainPackage.git('tag');
expect(tags).to.be('v0.0.0\nv0.0.1\n');
var message = mainPackage.git('log', '--pretty=format:%s', '-n1');
expect(message).to.be('v0.0.1');
});
});

it('assumes v0.0.0 when no tags exist', function() {
packageWithoutTags.prepareGit();
packageWithoutTags.create({
'index.js': 'console.log("hello, world");'
});
packageWithoutTags.git('add', '-A');
packageWithoutTags.git('commit', '-m"commit"');

return helpers.run(version, ['major', {}, { cwd: packageWithoutTags.path }]).then(function() {
expect(packageWithoutTags.latestGitTag()).to.be('1.0.0');
});
});
});
15 changes: 15 additions & 0 deletions test/helpers.js
Expand Up @@ -14,6 +14,7 @@ var proxyquire = require('proxyquire').noCallThru().noPreserveCache();
var spawnSync = require('spawn-sync');
var config = require('../lib/config');
var nock = require('nock');
var semver = require('semver');

// For better promise errors
Q.longStackSupport = true;
Expand Down Expand Up @@ -155,6 +156,20 @@ exports.TempDir = (function () {
}
};

TempDir.prototype.latestGitTag = function () {
var versions = this.git('tag')
.split(/\r?\n/)
.map(function(t) { return t.slice(1); })
.filter(semver.valid)
.sort(semver.compare);

if (versions.length >= 1) {
return versions[versions.length - 1];
} else {
throw new Error('No valid git version tags found.');
}
};

TempDir.prototype.exists = function (name) {
return fs.existsSync(path.join(this.path, name));
};
Expand Down

0 comments on commit c0c2858

Please sign in to comment.