storage.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*global describe, it, before, after, beforeEach, afterEach */
  2. var path = require('path');
  3. var fs = require('fs');
  4. var assert = require('assert');
  5. var _ = require('lodash');
  6. var shell = require('shelljs');
  7. var sinon = require('sinon');
  8. var Storage = require('../lib/util/storage');
  9. describe('Generator storage', function () {
  10. before(function () {
  11. this.storePath = path.join(__dirname, './fixtures/config.json');
  12. });
  13. it('should require a name parameter', function () {
  14. assert.throws(function () { new Storage(); });
  15. });
  16. it('constructor should take a path parameter', function () {
  17. var store = new Storage('test', this.storePath);
  18. assert.equal(store.get('testFramework'), 'mocha');
  19. assert.ok(store.existed);
  20. });
  21. it('should create a new config file on `set`', function (done) {
  22. var storePath = path.join(shell.tempdir(), 'new-config.json');
  23. var store = new Storage('test', storePath);
  24. store.once('save', function () {
  25. var fileContent = JSON.parse(fs.readFileSync(storePath));
  26. shell.rm('-f', storePath);
  27. assert.equal(fileContent.test.foo, 'bar');
  28. assert.ok(!store.existed);
  29. done();
  30. }.bind(this));
  31. store.set('foo', 'bar');
  32. });
  33. it('should default to `.yo-rc.json` file', function () {
  34. var cwd = process.cwd();
  35. var tmp = shell.tempdir();
  36. process.chdir(tmp);
  37. var store = new Storage('yo');
  38. store.on('save', function () {
  39. var fileContent = JSON.parse(fs.readFileSync(path.join(tmp, '.yo-rc.json')));
  40. assert.equal(fileContent.yo.foo, 'bar');
  41. process.chdir(cwd);
  42. });
  43. store.set('foo', 'bar');
  44. });
  45. describe('instance methods', function () {
  46. beforeEach(function () {
  47. this.store = new Storage('test', this.storePath);
  48. this.initialState = _.cloneDeep(this.store._fullStore);
  49. });
  50. afterEach(function (done) {
  51. this.store._fullStore = this.initialState;
  52. this.store.on('save', done);
  53. this.store.save();
  54. });
  55. it('should get values', function () {
  56. assert.equal(this.store.get('testFramework'), 'mocha');
  57. assert.equal(this.store.get('name'), 'test');
  58. });
  59. it('should set values', function () {
  60. this.store.set('name', 'Yeoman!');
  61. assert.equal(this.store.get('name'), 'Yeoman!');
  62. });
  63. it('should set multipe values at once', function () {
  64. this.store.set({
  65. foo: 'bar',
  66. john: 'doe'
  67. });
  68. assert.equal(this.store.get('foo'), 'bar');
  69. assert.equal(this.store.get('john'), 'doe');
  70. });
  71. it('should get all values', function () {
  72. var val = this.store.getAll();
  73. assert.notEqual(val, this.store._store); // must not return the reference
  74. _.each(this.store._store, function (method, name) {
  75. assert(method === val[name]);
  76. });
  77. });
  78. it('should delete value', function () {
  79. assert.equal(this.store.get('name'), 'test');
  80. this.store.delete('name');
  81. assert.equal(this.store.get('name'), undefined);
  82. });
  83. it('should prevent saving `function` value', function () {
  84. assert.throws(function () {
  85. this.store.set('foo', function () {});
  86. }.bind(this));
  87. });
  88. it('should init new name config', function () {
  89. var store = new Storage('foobar', this.storePath);
  90. store.set('foo', 'bar');
  91. assert.equal(store.get('foo'), 'bar');
  92. assert.ok(this.store.get('foo') == null);
  93. });
  94. it('should initialize storage file on `save`', function (done) {
  95. var storePath = path.join(shell.tempdir(), 'save.json');
  96. var store = new Storage('test', storePath);
  97. store.once('save', function () {
  98. var fileContent = JSON.parse(fs.readFileSync(storePath));
  99. assert.ok(fileContent);
  100. assert.ok(!store.existed);
  101. shell.rm('-f', storePath);
  102. done()
  103. });
  104. store.save();
  105. });
  106. it('should debounce save method', function (done) {
  107. var spy = sinon.spy(Storage.prototype, 'forceSave');
  108. var store = new Storage('name', path.join(shell.tempdir(), 'foo.json'));
  109. var saveSpy = sinon.spy(store, 'save');
  110. store.once('save', function () {
  111. assert.equal(spy.callCount, 1);
  112. assert.equal(saveSpy.callCount, 3);
  113. spy.restore();
  114. done();
  115. });
  116. store.save();
  117. store.save();
  118. store.save();
  119. });
  120. it('should allow setting defaults', function () {
  121. this.store.set('val1', 1);
  122. this.store.defaults({
  123. 'val1': 3,
  124. 'val2': 4
  125. });
  126. assert.equal(this.store.get('val1'), 1);
  127. assert.equal(this.store.get('val2'), 4);
  128. assert.throws(function () {
  129. this.store.defaults('foo');
  130. }.bind(this));
  131. });
  132. });
  133. });