-
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility methods for getting git info
Closes #314
- Loading branch information
1 parent
720b23b
commit 53402e9
Showing
6 changed files
with
161 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); |