Skip to content

Commit

Permalink
Add utility methods for getting git info
Browse files Browse the repository at this point in the history
Closes #314
  • Loading branch information
SBoudrias authored and sindresorhus committed Jul 14, 2013
1 parent 720b23b commit 53402e9
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/actions/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var logger = process.logging || require('./utils/log');
var logger = process.logging || require('../util/log');
var log = logger('generators:action');

var fs = require('fs');
Expand Down
48 changes: 48 additions & 0 deletions lib/actions/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// User related helper methods

var shell = require('shelljs');
var _ = require('lodash');


// Exports module
var user = module.exports = {};


// Git related method
//
// The value will come from the global scope or the project scope (it'll take what git
// will use in the current context)

var usernameCache = {};
var emailCache = {};
user.git = Object.create(Object.prototype, {

// current git user.name
username: {
get: function () {
var username = usernameCache[process.cwd()];

if (username) { return username; }

username = shell.exec('git config --get user.name', { silent: true }).output.trim();
usernameCache[process.cwd()] = username;

return username;
}
},

// current git user.email
email: {
get: function () {
var email = emailCache[process.cwd()];

if (email) { return email; }

email = shell.exec('git config --get user.email', { silent: true }).output.trim();
emailCache[process.cwd()] = email;

return email;
}
}

});
2 changes: 2 additions & 0 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ _.extend(Base.prototype, require('./actions/install'));
_.extend(Base.prototype, require('./actions/string'));
_.extend(Base.prototype, require('./actions/wiring'));
_.extend(Base.prototype, require('./util/common'));
Base.prototype.user = require('./actions/user');
Base.prototype.shell = require('shelljs');
Base.prototype.prompt = require('./actions/prompt');
Base.prototype.invoke = require('./actions/invoke');
Base.prototype.spawnCommand = require('./actions/spawn_command');
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@
"dargs": "~0.1.0",
"async": "~0.2.8",
"inquirer": "~0.2.0",
"iconv-lite": "~0.2.10"
"iconv-lite": "~0.2.10",
"shelljs": "~0.1.4"
},
"devDependencies": {
"mocha": "~1.11.0",
"proxyquire": "~0.4.0"
"proxyquire": "~0.4.0",
"sinon": "~1.7.3"
}
}
10 changes: 10 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var events = require('events');
var assert = require('assert');
var generators = require('..');
var helpers = require('../lib/test/helpers');
var _ = require('lodash');


describe('yeoman.generators.Base', function () {
Expand Down Expand Up @@ -269,4 +270,13 @@ describe('yeoman.generators.Base', function () {
assert.equal(usage, 'yeoman init FOO one two three [options]\n\nA new desc for this generator');
});
});

describe('generator.shell', function () {
it('should extend shelljs module', function () {
_.each(require('shelljs'), function (method, name) {
assert.equal(method, generators.Base.prototype.shell[name]);
});
});
});

});
96 changes: 96 additions & 0 deletions test/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*global describe, before, it, after, before, beforeEach, afterEach */
var shell = require('shelljs');
var assert = require('assert');
var sinon = require('sinon');
var proxyquire = require('proxyquire');
var file = require('../lib/actions/file');

describe('user utility', function () {

it('should be exposed on the Base generator', function () {
assert.equal(require('../lib/actions/user'), require('../lib/base').prototype.user);
});

describe('git methods', function () {

before(function () {
this.cwd = process.cwd();
this.tmp = shell.tempdir();
shell.cd(this.tmp);
file.mkdir('subdir');
shell.exec('git init --quiet');
shell.exec('git config --local user.name Yeoman');
shell.exec('git config --local user.email yo@yeoman.io');
});

after(function () {
shell.cd(this.cwd);
});

beforeEach(function () {
this.shell = shell;
sinon.spy(this.shell, 'exec');

this.user = proxyquire('../lib/actions/user', {
'shelljs': this.shell
});
});

afterEach(function () {
this.shell.exec.restore();
shell.cd(this.tmp);
});

describe('`username`', function () {

it('should be the username used by git', function () {
assert.equal(this.user.git.username, 'Yeoman');
});

it('should be read-only', function () {
this.user.git.username = 'bar';
assert.notEqual(this.user.git.username, 'bar');
});

it('should handle cache', function () {
// Should use cache when used multiple time
this.user.git.username;
this.user.git.username;
assert.equal(this.shell.exec.callCount, 1);

// Cache should be link the CWD, so changing dir rerun the check
shell.cd('subdir');
this.user.git.username;
assert.equal(this.shell.exec.callCount, 2);
});

});

describe('`email`', function () {

it('should be the email used by git', function () {
assert.equal(this.user.git.email, 'yo@yeoman.io');
});

it('should be read-only', function () {
this.user.git.email = 'bar';
assert.notEqual(this.user.git.email, 'bar');
});

it('should handle cache', function () {
// Should use cache when used multiple time
this.user.git.email;
this.user.git.email;
assert.equal(this.shell.exec.callCount, 1);

// Cache should be link the CWD, so changing dir rerun the check
shell.cd('subdir');
this.user.git.email;
assert.equal(this.shell.exec.callCount, 2);
});

});

});

});

0 comments on commit 53402e9

Please sign in to comment.