user.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*global describe, before, it, after, before, beforeEach, afterEach */
  2. var shell = require('shelljs');
  3. var assert = require('assert');
  4. var sinon = require('sinon');
  5. var proxyquire = require('proxyquire');
  6. var file = require('../lib/actions/file');
  7. describe('user utility', function () {
  8. it('should be exposed on the Base generator', function () {
  9. assert.equal(require('../lib/actions/user'), require('../lib/base').prototype.user);
  10. });
  11. describe('git methods', function () {
  12. before(function () {
  13. this.cwd = process.cwd();
  14. this.tmp = shell.tempdir();
  15. shell.cd(this.tmp);
  16. file.mkdir('subdir');
  17. shell.exec('git init --quiet');
  18. shell.exec('git config --local user.name Yeoman');
  19. shell.exec('git config --local user.email [email protected]');
  20. });
  21. after(function () {
  22. shell.cd(this.cwd);
  23. });
  24. beforeEach(function () {
  25. this.shell = shell;
  26. sinon.spy(this.shell, 'exec');
  27. this.user = proxyquire('../lib/actions/user', {
  28. 'shelljs': this.shell
  29. });
  30. });
  31. afterEach(function () {
  32. this.shell.exec.restore();
  33. shell.cd(this.tmp);
  34. });
  35. describe('`username`', function () {
  36. it('should be the username used by git', function () {
  37. assert.equal(this.user.git.username, 'Yeoman');
  38. });
  39. it('should be read-only', function () {
  40. this.user.git.username = 'bar';
  41. assert.notEqual(this.user.git.username, 'bar');
  42. });
  43. it('should handle cache', function () {
  44. // Should use cache when used multiple time
  45. this.user.git.username;
  46. this.user.git.username;
  47. assert.equal(this.shell.exec.callCount, 1);
  48. // Cache should be link the CWD, so changing dir rerun the check
  49. shell.cd('subdir');
  50. this.user.git.username;
  51. assert.equal(this.shell.exec.callCount, 2);
  52. });
  53. });
  54. describe('`email`', function () {
  55. it('should be the email used by git', function () {
  56. assert.equal(this.user.git.email, '[email protected]');
  57. });
  58. it('should be read-only', function () {
  59. this.user.git.email = 'bar';
  60. assert.notEqual(this.user.git.email, 'bar');
  61. });
  62. it('should handle cache', function () {
  63. // Should use cache when used multiple time
  64. this.user.git.email;
  65. this.user.git.email;
  66. assert.equal(this.shell.exec.callCount, 1);
  67. // Cache should be link the CWD, so changing dir rerun the check
  68. shell.cd('subdir');
  69. this.user.git.email;
  70. assert.equal(this.shell.exec.callCount, 2);
  71. });
  72. });
  73. });
  74. });