base.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*global describe, before, beforeEach, after, afterEach, it */
  2. var fs = require('fs');
  3. var path = require('path');
  4. var util = require('util');
  5. var events = require('events');
  6. var assert = require('assert');
  7. var sinon = require('sinon');
  8. var generators = require('..');
  9. var helpers = require('../lib/test/helpers');
  10. var _ = require('lodash');
  11. describe('yeoman.generators.Base', function () {
  12. // TODO(mklabs): generate generator about to be tested, or add it in fixtures.
  13. before(generators.test.before(path.join(__dirname, 'temp.dev')));
  14. before(function () {
  15. var env = this.env = generators();
  16. function Dummy() {
  17. generators.Base.apply(this, arguments);
  18. }
  19. util.inherits(Dummy, generators.Base);
  20. Dummy.prototype.test = function () {
  21. this.shouldRun = true;
  22. };
  23. env.register(Dummy, 'ember:all');
  24. env.register(Dummy, 'hook1:ember');
  25. env.register(Dummy, 'hook2:ember:all');
  26. env.register(Dummy, 'hook3');
  27. env.register(function () {
  28. this.write('app/scripts/models/application-model.js', '// ...');
  29. }, 'hook4');
  30. this.Dummy = Dummy;
  31. this.dummy = new Dummy(['bar', 'baz', 'bom'], {
  32. foo: false,
  33. something: 'else',
  34. // mandatory options, created by the env#create() helper
  35. resolved: 'ember:all',
  36. env: env,
  37. });
  38. this.dummy
  39. .hookFor('hook1')
  40. .hookFor('hook2')
  41. .hookFor('hook3')
  42. .hookFor('hook4');
  43. });
  44. describe('generator.appname', function () {
  45. it('should be set with the project directory name without non-alphanums', function () {
  46. assert.equal(this.dummy.appname, "temp dev");
  47. });
  48. });
  49. describe('generator.run(args, cb)', function () {
  50. it('should run all methods in the given generator', function () {
  51. this.dummy.run();
  52. });
  53. it('should have the _running flag turned on', function () {
  54. assert.ok(this.dummy._running);
  55. });
  56. });
  57. describe('generator.run(args, cb) regression', function () {
  58. var events = [];
  59. var resolveCalled = 0;
  60. var resolveExpected = 0;
  61. before(function () {
  62. var Unicorn = function () {
  63. generators.Base.apply(this, arguments);
  64. };
  65. util.inherits(Unicorn, generators.Base);
  66. Unicorn.prototype.test1 = function () {
  67. this.async()();
  68. };
  69. Unicorn.prototype.test2 = function () {
  70. // Nothing
  71. };
  72. Unicorn.prototype.test3 = function () {
  73. this.async()('mostlyn\'t');
  74. };
  75. Unicorn.prototype.test4 = function () {
  76. // Nothing again
  77. };
  78. this.unicorn = helpers.createGenerator('unicorn:app', [
  79. [Unicorn, 'unicorn:app']
  80. ]);
  81. helpers.stub(this.unicorn, 'emit', function (type, err) {
  82. events.push({
  83. type: type,
  84. err: err
  85. });
  86. if (type === 'method') {
  87. resolveExpected++;
  88. }
  89. });
  90. helpers.decorate(this.unicorn.conflicter, 'resolve', function () {
  91. resolveCalled++;
  92. });
  93. });
  94. after(helpers.restore);
  95. afterEach(function () {
  96. events = [];
  97. resolveCalled = 0;
  98. resolveExpected = 0;
  99. });
  100. it('should call `done` only once', function (done) {
  101. // Mocha will fail if done was called more than once.
  102. this.unicorn.run({}, done);
  103. });
  104. it('should emit an error from async', function (done) {
  105. this.unicorn.run({}, function () {
  106. assert.ok(JSON.stringify(events).indexOf('{"type":"error","err":"mostlyn\'t"}') > -1);
  107. done();
  108. });
  109. });
  110. it('should resolve conflicts after each method is invoked', function (done) {
  111. this.unicorn.run({}, function () {
  112. assert.equal(resolveCalled, resolveExpected);
  113. done();
  114. });
  115. });
  116. });
  117. describe('generator.runHooks(cb)', function () {
  118. it('should go through all registered hooks, and invoke them in series', function (done) {
  119. this.dummy.runHooks(function (err) {
  120. if (err) {
  121. return err;
  122. }
  123. fs.stat('app/scripts/models/application-model.js', done);
  124. });
  125. });
  126. });
  127. describe('generator.argument(name, config)', function () {
  128. it('should add a new argument to the generator instance', function () {
  129. assert.equal(this.dummy._arguments.length, 0);
  130. this.dummy.argument('foo');
  131. assert.equal(this.dummy._arguments.length, 1);
  132. });
  133. it('should create the property specified with value from positional args', function () {
  134. assert.equal(this.dummy.foo, 'bar');
  135. });
  136. it('should slice positional arguments when config.type is Array', function () {
  137. this.dummy.argument('bar', {
  138. type: Array
  139. });
  140. assert.deepEqual(this.dummy.bar, ['baz', 'bom']);
  141. });
  142. it('should raise an error if required arguments are not provided', function (done) {
  143. var dummy = new generators.Base([], {
  144. env: this.env,
  145. resolved: 'dummy:all'
  146. }).on('error', function (ev) {
  147. done();
  148. });
  149. dummy.argument('foo', {
  150. required: true
  151. });
  152. });
  153. });
  154. describe('generator.option(name, config)', function () {
  155. it('should add a new option to the set of generator expected options', function () {
  156. // every generator have the --help options
  157. var generator = new this.Dummy([], {
  158. env: this.env,
  159. resolved: 'test'
  160. });
  161. assert.equal(generator._options.length, 1);
  162. generator.option('foo');
  163. assert.equal(generator._options.length, 2);
  164. assert.deepEqual(generator._options.pop(), {
  165. desc: 'Description for foo',
  166. name: 'foo',
  167. type: Boolean,
  168. defaults: false,
  169. hide: false
  170. });
  171. });
  172. });
  173. describe('generator.hookFor(name, config)', function () {
  174. it('should emit errors if called when running', function () {
  175. try {
  176. this.dummy.hookFor('maoow');
  177. } catch (err) {
  178. assert.equal(err.message, 'hookFor must be used within the constructor only');
  179. }
  180. });
  181. it('should create the macthing option', function () {
  182. this.dummy._running = false;
  183. this.dummy.hookFor('something');
  184. assert.deepEqual(this.dummy._options.pop(), {
  185. desc: 'Something to be invoked',
  186. name: 'something',
  187. type: Boolean,
  188. defaults: 'else',
  189. hide: false
  190. });
  191. });
  192. it('should update the internal hooks holder', function () {
  193. assert.deepEqual(this.dummy._hooks.pop(), {
  194. name: 'something'
  195. });
  196. });
  197. });
  198. describe('generator.defaultFor(config)', function () {
  199. it('should return the value for the option name, doing lookup in options and Grunt config', function () {
  200. var name = this.dummy.defaultFor('something');
  201. assert.equal(name, 'else');
  202. });
  203. });
  204. describe('generator.desc(decription)', function () {
  205. it('should update the internal description', function () {
  206. this.dummy.desc('A new desc for this generator');
  207. assert.equal(this.dummy.description, 'A new desc for this generator');
  208. });
  209. });
  210. describe('generator.help()', function () {
  211. it('should return the expected help / usage output', function () {
  212. this.dummy.option('ooOoo');
  213. var help = this.dummy.help();
  214. assert.ok(help.match('Usage:'));
  215. assert.ok(help.match('yeoman init FOO one two three \\[options\\]'));
  216. assert.ok(help.match('A new desc for this generator'));
  217. assert.ok(help.match('Options:'));
  218. assert.ok(help.match('--help # Print generator\'s options and usage'));
  219. assert.ok(help.match('--ooOoo # Description for ooOoo'));
  220. });
  221. });
  222. describe('generator.usage()', function () {
  223. it('should return the expected help / usage output', function () {
  224. var usage = this.dummy.usage();
  225. assert.equal(usage, 'yeoman init FOO one two three [options]\n\nA new desc for this generator');
  226. });
  227. });
  228. describe('generator.shell', function () {
  229. it('should extend shelljs module', function () {
  230. _.each(require('shelljs'), function (method, name) {
  231. assert.equal(method, generators.Base.prototype.shell[name]);
  232. });
  233. });
  234. });
  235. describe('generator.storage()', function () {
  236. it('should provide a storage instance', function () {
  237. assert.ok(this.dummy.config instanceof require('../lib/util/storage'));
  238. });
  239. it('should set the CWD where `.yo-rc.json` is found', function () {
  240. var projectDir = path.join(__dirname, 'fixtures/dummy-project');
  241. process.chdir(path.join(projectDir, 'subdir'));
  242. var dummy = new this.Dummy([ 'foo' ], {
  243. resolved: 'ember:all',
  244. env: this.env
  245. });
  246. assert.equal(process.cwd(), projectDir);
  247. });
  248. it('should update storage when destinationRoot change', function () {
  249. sinon.spy(this.Dummy.prototype, '_setStorage');
  250. this.dummy.destinationRoot('foo');
  251. assert.equal(this.Dummy.prototype._setStorage.callCount, 1);
  252. this.dummy.destinationRoot();
  253. assert.equal(this.Dummy.prototype._setStorage.callCount, 1);
  254. this.dummy.destinationRoot('foo');
  255. assert.equal(this.Dummy.prototype._setStorage.callCount, 2);
  256. this.Dummy.prototype._setStorage.restore();
  257. });
  258. });
  259. });