From 36317404bee8edece2247ffc99b2f97e2a34ada5 Mon Sep 17 00:00:00 2001 From: Eddie Monge Date: Fri, 16 May 2014 15:30:17 -0700 Subject: [PATCH] fix(gen): fix more appPath, other changes Views and decorators weren't using appPath correctly. The code is ugly and not DRY at all but will be cleaned up later. gitIgnore also needed some appPath love. Moved the gitignore creation to the package section of app/index.js Also did more work with tests, cleaning them up and testing more things. They are going to have to have a full rewrite so it makes me sad doing these small changes. --- .gitignore | 2 +- app/index.js | 2 + common/index.js | 1 - decorator/index.js | 26 +- script-base.js | 1 + templates/common/root/gitignore | 2 +- test/test-appname-substitution.js | 70 +++--- test/test-apppath.js | 384 ++++++++++++++---------------- test/test-file-creation.js | 205 +++++++--------- test/test-route-creation.js | 153 ++++++------ view/index.js | 13 +- 11 files changed, 392 insertions(+), 467 deletions(-) diff --git a/.gitignore b/.gitignore index 6218a0b95..547373185 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ .DS_Store node_modules -test/temp +test/tmp diff --git a/app/index.js b/app/index.js index a8d7287fe..c05794c5d 100644 --- a/app/index.js +++ b/app/index.js @@ -37,6 +37,7 @@ var Generator = module.exports = function Generator(args, options) { } catch (e) {} } this.env.options.appPath = this.env.options.appPath || 'app'; + this.options.appPath = this.env.options.appPath; } this.appPath = this.env.options.appPath; @@ -268,6 +269,7 @@ Generator.prototype.packageFiles = function packageFiles() { this.coffee = this.env.options.coffee; this.template('root/_bower.json', 'bower.json'); this.template('root/_bowerrc', '.bowerrc'); + this.template('root/gitignore', '.gitignore'); this.template('root/_package.json', 'package.json'); this.template('root/_Gruntfile.js', 'Gruntfile.js'); }; diff --git a/common/index.js b/common/index.js index 526c45c2c..fab748b55 100644 --- a/common/index.js +++ b/common/index.js @@ -17,7 +17,6 @@ Generator.prototype.setupEnv = function setupEnv() { this.copy('.editorconfig'); this.copy('.gitattributes'); this.copy('.jshintrc'); - this.copy('gitignore', '.gitignore'); this.directory('test'); this.sourceRoot(join(__dirname, '../templates/common')); diff --git a/decorator/index.js b/decorator/index.js index 9ea86fbd2..ea72c0ef3 100644 --- a/decorator/index.js +++ b/decorator/index.js @@ -2,14 +2,27 @@ var util = require('util'); var ScriptBase = require('../script-base.js'); var fs = require('fs'); +var path = require('path'); function buildRelativePath(fileName){ - return 'decorators/' + fileName + "Decorator"; + return path.join('decorators', fileName + "Decorator"); } var Generator = module.exports = function Generator(args, options) { ScriptBase.apply(this, arguments); this.fileName = this.name; + + if (typeof this.env.options.appPath === 'undefined') { + this.env.options.appPath = this.options.appPath; + + if (!this.env.options.appPath) { + try { + this.env.options.appPath = require(path.join(process.cwd(), 'bower.json')).appPath; + } catch (e) {} + } + this.env.options.appPath = this.env.options.appPath || 'app'; + this.options.appPath = this.env.options.appPath; + } }; util.inherits(Generator, ScriptBase); @@ -18,8 +31,10 @@ Generator.prototype.askForOverwrite = function askForOverwrite() { var cb = this.async(); // TODO: Any yeoman.util function to handle this? - var fileExists = fs.existsSync(this.env.cwd + '/app/scripts/' + buildRelativePath(this.fileName) + ".js"); - if (fileExists) { + if (fs.existsSync(path.join( + this.env.cwd, this.env.options.appPath, + 'scripts', buildRelativePath(this.fileName) + ".js" + ))) { var prompts = [{ type: 'confirm', name: 'overwriteDecorator', @@ -62,6 +77,9 @@ Generator.prototype.askForNewName = function askForNewName() { }; Generator.prototype.createDecoratorFiles = function createDecoratorFiles() { - this.appTemplate('decorator', 'scripts/' + buildRelativePath(this.fileName)); + this.appTemplate( + 'decorator', + path.join('scripts', buildRelativePath(this.fileName)) + ); this.addScriptToIndex(buildRelativePath(this.fileName)); }; diff --git a/script-base.js b/script-base.js index 7a78c9518..e674711a4 100644 --- a/script-base.js +++ b/script-base.js @@ -28,6 +28,7 @@ var Generator = module.exports = function Generator() { } catch (e) {} } this.env.options.appPath = this.env.options.appPath || 'app'; + this.options.appPath = this.env.options.appPath; } if (typeof this.env.options.testPath === 'undefined') { diff --git a/templates/common/root/gitignore b/templates/common/root/gitignore index 7911b28d5..3f13bd974 100644 --- a/templates/common/root/gitignore +++ b/templates/common/root/gitignore @@ -2,4 +2,4 @@ node_modules dist .tmp .sass-cache -app/bower_components +<%= appPath %>/bower_components diff --git a/test/test-appname-substitution.js b/test/test-appname-substitution.js index 60c95d814..c3dea9037 100644 --- a/test/test-appname-substitution.js +++ b/test/test-appname-substitution.js @@ -1,11 +1,7 @@ /*global describe, before, it, beforeEach */ 'use strict'; -var fs = require('fs'); -var assert = require('assert'); var path = require('path'); -var util = require('util'); -var generators = require('yeoman-generator'); var helpers = require('yeoman-generator').test; describe('Angular generator template mechanism', function () { @@ -22,56 +18,54 @@ describe('Angular generator template mechanism', function () { 'karma:app' ] ]; - helpers.testDirectory(path.join(__dirname, 'temp', appName), function (err) { + helpers.testDirectory(path.join(__dirname, 'tmp', appName), function (err) { if (err) { done(err); } + angular = helpers.createGenerator('angular:app', deps, [appName], { 'skip-welcome-message': true, 'skip-install': true, 'skip-message': true }); + + helpers.mockPrompt(angular, { + compass: true, + bootstrap: true, + compassBootstrap: true, + modules: [] + }); + done(); }); }); it('should generate the same appName in every file', function (done) { - var expected = [ - 'app/scripts/app.js', - 'app/scripts/controllers/main.js', - 'app/index.html', - 'test/spec/controllers/main.js' - ]; - - helpers.mockPrompt(angular, { - compass: true, - bootstrap: true, - compassBootstrap: true, - modules: [] - }); - angular.run({}, function () { - // Check if all files are created for the test - helpers.assertFile(expected); - - // read JS Files - var app_js = fs.readFileSync('app/scripts/app.js', 'utf8'); - var main_js = fs.readFileSync('app/scripts/controllers/main.js', 'utf8'); - var main_test_js = fs.readFileSync('test/spec/controllers/main.js', 'utf8'); - - // Test JS Files - var regex_js = new RegExp('module\\(\'' + appName + 'App\''); - - assert.ok(regex_js.test(app_js), 'app.js template using a wrong appName'); - assert.ok(regex_js.test(main_js), 'main.js template using a wrong appName'); - assert.ok(regex_js.test(main_test_js), 'controller spec template using a wrong appName'); + helpers.assertFile([ + 'app/scripts/app.js', + 'app/scripts/controllers/main.js', + 'app/index.html', + 'test/spec/controllers/main.js' + ]); - // read HTML file - var index_html = fs.readFileSync('app/index.html', 'utf8'); + helpers.assertFileContent( + 'app/scripts/app.js', + new RegExp('module\\(\'' + appName + 'App\'') + ); + helpers.assertFileContent( + 'app/scripts/controllers/main.js', + new RegExp('module\\(\'' + appName + 'App\'') + ); + helpers.assertFileContent( + 'test/spec/controllers/main.js', + new RegExp('module\\(\'' + appName + 'App\'') + ); - // Test HTML File - var regex_html = new RegExp('ng-app=\"' + appName + 'App\"'); - assert.ok(regex_html.test(index_html), 'index.html template using a wrong appName'); + helpers.assertFileContent( + 'app/index.html', + new RegExp('ng-app=\"' + appName + 'App\"') + ); done(); }); }); diff --git a/test/test-apppath.js b/test/test-apppath.js index 7ea566d6f..a92ca3dc9 100644 --- a/test/test-apppath.js +++ b/test/test-apppath.js @@ -1,210 +1,174 @@ -// /*global describe, before, it, beforeEach */ -// 'use strict'; -// -// var fs = require('fs'); -// var assert = require('assert'); -// var path = require('path'); -// var util = require('util'); -// var generators = require('yeoman-generator'); -// var helpers = require('yeoman-generator').test; -// var _ = require('underscore.string'); -// -// describe('Angular generator appPath option', function () { -// var angular; -// var appPath = 'customAppPath'; -// var expected = [ -// appPath + '/.htaccess', -// appPath + '/404.html', -// appPath + '/favicon.ico', -// appPath + '/robots.txt', -// appPath + '/styles/main.scss', -// appPath + '/views/main.html', -// appPath + '/index.html', -// '.bowerrc', -// '.editorconfig', -// '.gitignore', -// '.jshintrc', -// 'Gruntfile.js', -// 'package.json', -// 'bower.json' -// ]; -// var mockPrompts = { -// compass: true, -// bootstrap: true, -// compassBootstrap: true, -// modules: [] -// }; -// var genOptions = { -// 'app-path': appPath, -// 'skip-install': true, -// 'skip-welcome-message': true, -// 'skip-message': true -// }; -// -// beforeEach(function (done) { -// var deps = [ -// '../../app', -// '../../common', -// '../../controller', -// '../../main', [ -// helpers.createDummyGenerator(), -// 'karma:app' -// ] -// ]; -// helpers.testDirectory(path.join(__dirname, 'temp'), function (err) { -// if (err) { -// done(err); -// } -// angular = helpers.createGenerator('angular:app', deps, false, genOptions); -// done(); -// }); -// }); -// -// it('should generate dotfiles', function (done) { -// helpers.mockPrompt(angular, mockPrompts); -// -// angular.run({}, function () { -// helpers.assertFile(expected); -// done(); -// }); -// }); -// -// it.only('creates expected JS files', function (done) { -// helpers.mockPrompt(angular, mockPrompts); -// -// angular.run({}, function() { -// helpers.assertFile([].concat(expected, [ -// appPath + '/scripts/app.js', -// appPath + '/scripts/controllers/main.js', -// 'test/spec/controllers/main.js' -// ])); -// done(); -// }); -// }); -// -// it('creates CoffeeScript files', function (done) { -// helpers.mockPrompt(angular, mockPrompts); -// -// angular.env.options.coffee = true; -// angular.run([], function () { -// helpers.assertFile([].concat(expected, [ -// appPath + '/scripts/app.coffee', -// appPath + '/scripts/controllers/main.coffee', -// 'test/spec/controllers/main.coffee' -// ])); -// done(); -// }); -// }); -// -// /** -// * Generic test function that can be used to cover the scenarios where a generator is creating both a source file -// * and a test file. The function will run the respective generator, and then check for the existence of the two -// * generated files. A RegExp check is done on each file, checking for the generated content with a pattern. -// * -// * The number of parameters is quite huge due to the many options in which the generated files differ, -// * e.g. Services start with an upper case letter, whereas filters, directives or constants start with a lower case -// * letter. -// * -// * The generated items all use the dummy name 'foo'. -// * -// * @param generatorType The type of generator to run, e.g. 'filter'. -// * @param specType The type of the generated spec file, e.g. 'service' - all service types (constant, value, ...) -// * use the same Service spec template. -// * @param targetDirectory The directory into which the files are generated, e.g. 'directives' - this will be -// * located under 'customAppPath/scripts' for the sources and 'test/spec' for the tests. -// * @param scriptNameFn The function used to create the name of the created item, e.g. _.classify to generate 'Foo', -// * or _.camelize to generate 'foo'. -// * @param specNameFn Same as scriptNameFn, but for the describe text used in the Spec file. Some generators use -// * _.classify, others use _.camelize. -// * @param suffix An optional suffix to be appended to the generated item name, e.g. 'Ctrl' for controllers, which -// * will generate 'FooCtrl'. -// * @param done The done function. -// */ -// function generatorTest(generatorType, specType, targetDirectory, scriptNameFn, specNameFn, suffix, done) { -// var angularGenerator; -// var name = 'foo'; -// var deps = [path.join('../..', generatorType)]; -// angularGenerator = helpers.createGenerator('angular:' + generatorType, deps, [name], genOptions); -// -// helpers.mockPrompt(angular, mockPrompts); -// angular.run([], function () { -// angularGenerator.run([], function () { -// assert.fileContent([ -// [ -// path.join(appPath + '/scripts', targetDirectory, name + '.js'), -// new RegExp( -// generatorType + '\\(\'' + scriptNameFn(name) + suffix + '\'', -// 'g' -// ) -// ] -// ]); -// done(); -// }); -// }); -// } -// -// describe('Controller', function () { -// it('should generate a new controller', function (done) { -// generatorTest('controller', 'controller', 'controllers', _.classify, _.classify, 'Ctrl', done); -// }); -// }); -// -// describe('Directive', function () { -// it('should generate a new directive', function (done) { -// generatorTest('directive', 'directive', 'directives', _.camelize, _.camelize, '', done); -// }); -// }); -// -// describe('Filter', function () { -// it('should generate a new filter', function (done) { -// generatorTest('filter', 'filter', 'filters', _.camelize, _.camelize, '', done); -// }); -// }); -// -// describe('Service', function () { -// function serviceTest (generatorType, nameFn, done) { -// generatorTest(generatorType, 'service', 'services', nameFn, nameFn, '', done); -// } -// -// ['constant', 'factory', 'provider', 'value'].forEach(function(t) { -// it('should generate a new ' + t, function (done) { -// serviceTest(t, _.camelize, done); -// }); -// }); -// -// -// it('should generate a new service', function (done) { -// serviceTest('service', _.capitalize, done); -// }); -// }); -// -// describe('View', function () { -// it('should generate a new view', function (done) { -// var angularView; -// var deps = ['../../view']; -// angularView = helpers.createGenerator('angular:view', deps, ['foo'], genOptions); -// -// helpers.mockPrompt(angular, mockPrompts); -// angular.run([], function () { -// angularView.run([], function () { -// helpers.assertFile([appPath + '/views/foo.html']); -// done(); -// }); -// }); -// }); -// -// it('should generate a new view in subdirectories', function (done) { -// var angularView; -// var deps = ['../../view']; -// angularView = helpers.createGenerator('angular:view', deps, ['foo/bar'], genOptions); -// -// helpers.mockPrompt(angular, mockPrompts); -// angular.run([], function () { -// angularView.run([], function () { -// helpers.assertFile([appPath + '/views/foo/bar.html']); -// done(); -// }); -// }); -// }); -// }); -// }); +/*global describe, before, it, beforeEach */ +'use strict'; + +var path = require('path'); +var helpers = require('yeoman-generator').test; +var _ = require('underscore.string'); + +describe('Angular generator appPath option', function () { + var angular; + var appPath = 'customAppPath'; + var expected = [ + appPath + '/.htaccess', + appPath + '/404.html', + appPath + '/favicon.ico', + appPath + '/robots.txt', + appPath + '/styles/main.scss', + appPath + '/views/main.html', + appPath + '/index.html', + '.bowerrc', + '.editorconfig', + '.gitignore', + '.jshintrc', + 'Gruntfile.js', + 'package.json', + 'bower.json' + ]; + var mockPrompts = { + compass: true, + bootstrap: true, + compassBootstrap: true, + modules: [] + }; + var genOptions = { + 'appPath': appPath, + 'skip-install': true, + 'skip-welcome-message': true, + 'skip-message': true + }; + + beforeEach(function (done) { + helpers.testDirectory(path.join(__dirname, 'tmp'), function (err) { + if (err) { + done(err); + } + + angular = helpers.createGenerator( + 'angular:app', + [ + '../../app', + '../../common', + '../../controller', + '../../main', [ + helpers.createDummyGenerator(), + 'karma:app' + ] + ], + false, + genOptions + ); + helpers.mockPrompt(angular, mockPrompts); + + done(); + }); + }); + + describe('App files', function () { + it('should generate dotfiles for apppath', function (done) { + angular.run({}, function () { + helpers.assertFile(expected); + console.log('a') + done(); + }); + }); + + it('creates expected JS files', function (done) { + angular.run({}, function() { + helpers.assertFile([].concat(expected, [ + appPath + '/scripts/app.js', + appPath + '/scripts/controllers/main.js', + 'test/spec/controllers/main.js' + ])); + done(); + }); + }); + + it('creates CoffeeScript files', function (done) { + angular.env.options.coffee = true; + angular.run([], function () { + helpers.assertFile([].concat(expected, [ + appPath + '/scripts/app.coffee', + appPath + '/scripts/controllers/main.coffee', + 'test/spec/controllers/main.coffee' + ])); + done(); + }); + }); + }); + + describe('Service Subgenerators', function () { + var generatorTest = function (generatorType, specType, targetDirectory, scriptNameFn, specNameFn, suffix, done) { + var angularGenerator; + var name = 'foo'; + var deps = [path.join('../..', generatorType)]; + angularGenerator = helpers.createGenerator('angular:' + generatorType, deps, [name], genOptions); + + angular.run([], function () { + angularGenerator.run([], function () { + helpers.assertFileContent([ + [ + path.join(appPath + '/scripts', targetDirectory, name + '.js'), + new RegExp( + generatorType + '\\(\'' + scriptNameFn(name) + suffix + '\'', + 'g' + ) + ] + ]); + done(); + }); + }); + }; + + it('should generate a new controller', function (done) { + generatorTest('controller', 'controller', 'controllers', _.classify, _.classify, 'Ctrl', done); + }); + + it('should generate a new directive', function (done) { + generatorTest('directive', 'directive', 'directives', _.camelize, _.camelize, '', done); + }); + + it('should generate a new filter', function (done) { + generatorTest('filter', 'filter', 'filters', _.camelize, _.camelize, '', done); + }); + + ['constant', 'factory', 'provider', 'value'].forEach(function(t) { + it('should generate a new ' + t, function (done) { + generatorTest(t, 'service', 'services', _.camelize, _.camelize, '', done) + }); + }); + + it('should generate a new service', function (done) { + generatorTest('service', 'service', 'services', _.capitalize, _.capitalize, '', done) + }); + }); + + describe('View', function () { + it('should generate a new view', function (done) { + var angularView; + var deps = ['../../view']; + angularView = helpers.createGenerator('angular:view', deps, ['foo'], genOptions); + + helpers.mockPrompt(angular, mockPrompts); + angular.run([], function () { + angularView.run([], function () { + helpers.assertFile([appPath + '/views/foo.html']); + done(); + }); + }); + }); + + it('should generate a new view in subdirectories', function (done) { + var angularView; + var deps = ['../../view']; + angularView = helpers.createGenerator('angular:view', deps, ['foo/bar'], genOptions); + + helpers.mockPrompt(angular, mockPrompts); + angular.run([], function () { + angularView.run([], function () { + helpers.assertFile([appPath + '/views/foo/bar.html']); + done(); + }); + }); + }); + }); +}); diff --git a/test/test-file-creation.js b/test/test-file-creation.js index e48d03431..bc00b444c 100644 --- a/test/test-file-creation.js +++ b/test/test-file-creation.js @@ -1,11 +1,7 @@ /*global describe, before, it, beforeEach */ 'use strict'; -var fs = require('fs'); -var assert = require('assert'); var path = require('path'); -var util = require('util'); -var generators = require('yeoman-generator'); var helpers = require('yeoman-generator').test; var _ = require('underscore.string'); @@ -18,14 +14,14 @@ describe('Angular generator', function () { 'app/robots.txt', 'app/styles/main.scss', 'app/views/main.html', + 'app/index.html', '.bowerrc', '.editorconfig', '.gitignore', '.jshintrc', 'Gruntfile.js', 'package.json', - 'bower.json', - 'app/index.html' + 'bower.json' ]; var mockPrompts = { compass: true, @@ -40,146 +36,111 @@ describe('Angular generator', function () { }; beforeEach(function (done) { - var deps = [ - '../../app', - '../../common', - '../../controller', - '../../main', [ - helpers.createDummyGenerator(), - 'karma:app' - ] - ]; - helpers.testDirectory(path.join(__dirname, 'temp'), function (err) { + helpers.testDirectory(path.join(__dirname, 'tmp'), function (err) { if (err) { done(err); } - angular = helpers.createGenerator('angular:app', deps, false, genOptions); - done(); - }); - }); - - it('should generate dotfiles', function (done) { - helpers.mockPrompt(angular, mockPrompts); + angular = helpers.createGenerator( + 'angular:app', + [ + '../../app', + '../../common', + '../../controller', + '../../main', [ + helpers.createDummyGenerator(), + 'karma:app' + ] + ], + false, + genOptions + ); + helpers.mockPrompt(angular, mockPrompts); - angular.run({}, function () { - helpers.assertFile(expected); done(); }); }); - it('creates expected JS files', function (done) { - helpers.mockPrompt(angular, mockPrompts); - - angular.run({}, function() { - helpers.assertFile([].concat(expected, [ - 'app/scripts/app.js', - 'app/scripts/controllers/main.js', - 'test/spec/controllers/main.js' - ])); - done(); + describe('App files', function () { + it('should generate dotfiles', function (done) { + angular.run({}, function () { + helpers.assertFile(expected); + done(); + }); }); - }); - - it('creates CoffeeScript files', function (done) { - helpers.mockPrompt(angular, mockPrompts); - angular.env.options.coffee = true; - angular.run([], function () { - helpers.assertFile([].concat(expected, [ - 'app/scripts/app.coffee', - 'app/scripts/controllers/main.coffee', - 'test/spec/controllers/main.coffee' - ])); - done(); + it('creates expected JS files', function (done) { + angular.run({}, function() { + helpers.assertFile([].concat(expected, [ + 'app/scripts/app.js', + 'app/scripts/controllers/main.js', + 'test/spec/controllers/main.js' + ])); + done(); + }); }); - }); - /** - * Generic test function that can be used to cover the scenarios where a generator is creating both a source file - * and a test file. The function will run the respective generator, and then check for the existence of the two - * generated files. A RegExp check is done on each file, checking for the generated content with a pattern. - * - * The number of parameters is quite huge due to the many options in which the generated files differ, - * e.g. Services start with an upper case letter, whereas filters, directives or constants start with a lower case - * letter. - * - * The generated items all use the dummy name 'foo'. - * - * @param generatorType The type of generator to run, e.g. 'filter'. - * @param specType The type of the generated spec file, e.g. 'service' - all service types (constant, value, ...) - * use the same Service spec template. - * @param targetDirectory The directory into which the files are generated, e.g. 'directives' - this will be - * located under 'app/scripts' for the sources and 'test/spec' for the tests. - * @param scriptNameFn The function used to create the name of the created item, e.g. _.classify to generate 'Foo', - * or _.camelize to generate 'foo'. - * @param specNameFn Same as scriptNameFn, but for the describe text used in the Spec file. Some generators use - * _.classify, others use _.camelize. - * @param suffix An optional suffix to be appended to the generated item name, e.g. 'Ctrl' for controllers, which - * will generate 'FooCtrl'. - * @param done The done function. - */ - function generatorTest(generatorType, specType, targetDirectory, scriptNameFn, specNameFn, suffix, done) { - var angularGenerator; - var name = 'foo'; - var deps = [path.join('../..', generatorType)]; - angularGenerator = helpers.createGenerator('angular:' + generatorType, deps, [name], genOptions); - - helpers.mockPrompt(angular, mockPrompts); - angular.run([], function () { - angularGenerator.run([], function () { - assert.fileContent([ - [ - path.join('app/scripts', targetDirectory, name + '.js'), - new RegExp( - generatorType + '\\(\'' + scriptNameFn(name) + suffix + '\'', - 'g' - ) - ], - [ - path.join('test/spec', targetDirectory, name + '.js'), - new RegExp( - 'describe\\(\'' + _.classify(specType) + ': ' + specNameFn(name) + suffix + '\'', - 'g' - ) - ] - ]); + it('creates CoffeeScript files', function (done) { + angular.env.options.coffee = true; + angular.run([], function () { + helpers.assertFile([].concat(expected, [ + 'app/scripts/app.coffee', + 'app/scripts/controllers/main.coffee', + 'test/spec/controllers/main.coffee' + ])); done(); }); }); - } + }); + + describe('Service Subgenerators', function () { + var generatorTest = function (generatorType, specType, targetDirectory, scriptNameFn, specNameFn, suffix, done) { + var name = 'foo'; + var deps = [path.join('../..', generatorType)]; + var genTester = helpers.createGenerator('angular:' + generatorType, deps, [name], genOptions); + + angular.run([], function () { + genTester.run([], function () { + helpers.assertFileContent([ + [ + path.join('app/scripts', targetDirectory, name + '.js'), + new RegExp( + generatorType + '\\(\'' + scriptNameFn(name) + suffix + '\'', + 'g' + ) + ], + [ + path.join('test/spec', targetDirectory, name + '.js'), + new RegExp( + 'describe\\(\'' + _.classify(specType) + ': ' + specNameFn(name) + suffix + '\'', + 'g' + ) + ] + ]); + done(); + }); + }); + }; - describe('Controller', function () { it('should generate a new controller', function (done) { generatorTest('controller', 'controller', 'controllers', _.classify, _.classify, 'Ctrl', done); }); - }); - describe('Directive', function () { it('should generate a new directive', function (done) { generatorTest('directive', 'directive', 'directives', _.camelize, _.camelize, '', done); }); - }); - describe('Filter', function () { it('should generate a new filter', function (done) { generatorTest('filter', 'filter', 'filters', _.camelize, _.camelize, '', done); }); - }); - - describe('Service', function () { - function serviceTest (generatorType, nameFn, done) { - generatorTest(generatorType, 'service', 'services', nameFn, nameFn, '', done); - } ['constant', 'factory', 'provider', 'value'].forEach(function(t) { it('should generate a new ' + t, function (done) { - serviceTest(t, _.camelize, done); + generatorTest(t, 'service', 'services', _.camelize, _.camelize, '', done) }); }); - it('should generate a new service', function (done) { - serviceTest('service', _.capitalize, done); + generatorTest('service', 'service', 'services', _.capitalize, _.capitalize, '', done) }); }); @@ -189,12 +150,10 @@ describe('Angular generator', function () { var deps = ['../../view']; angularView = helpers.createGenerator('angular:view', deps, ['foo'], genOptions); - helpers.mockPrompt(angular, mockPrompts); - angular.run([], function () { - angularView.run([], function () { - helpers.assertFile(['app/views/foo.html']); - done(); - }); + helpers.mockPrompt(angularView, mockPrompts); + angularView.run([], function () { + helpers.assertFile(['app/views/foo.html']); + done(); }); }); @@ -203,12 +162,10 @@ describe('Angular generator', function () { var deps = ['../../view']; angularView = helpers.createGenerator('angular:view', deps, ['foo/bar'], genOptions); - helpers.mockPrompt(angular, mockPrompts); - angular.run([], function () { - angularView.run([], function () { - helpers.assertFile(['app/views/foo/bar.html']); - done(); - }); + helpers.mockPrompt(angularView, mockPrompts); + angularView.run([], function () { + helpers.assertFile(['app/views/foo/bar.html']); + done(); }); }); }); diff --git a/test/test-route-creation.js b/test/test-route-creation.js index 2772efdf4..b260c9e25 100644 --- a/test/test-route-creation.js +++ b/test/test-route-creation.js @@ -1,110 +1,95 @@ /*global describe, before, it, beforeEach */ 'use strict'; -var fs = require('fs'); -var assert = require('assert'); var path = require('path'); -var util = require('util'); -var generators = require('yeoman-generator'); var helpers = require('yeoman-generator').test; -var _ = require('underscore.string'); describe('Angular generator route mechanism', function () { var angular; + var route = 'simpleroute'; + var expected = [ + 'app/scripts/controllers/' + route + '.js', + 'test/spec/controllers/' + route + '.js', + 'app/views/' + route + '.html' + ]; + var genOptions = { + 'skip-install': true, + 'skip-welcome-message': true, + 'skip-message': true + }; + var mockPrompts = { + compass: true, + bootstrap: true, + compassBootstrap: true, + modules: ['routeModule'] + }; beforeEach(function (done) { - var deps = [ - '../../app', - '../../common', - '../../controller', - '../../route', - '../../view', - '../../main', [ - helpers.createDummyGenerator(), - 'karma:app' - ] - ]; - helpers.testDirectory(path.join(__dirname, 'temp'), function (err) { + helpers.testDirectory(path.join(__dirname, 'tmp'), function (err) { if (err) { done(err); } - angular = helpers.createGenerator('angular:app', deps); - angular.options['skip-install'] = true; - angular.options['skip-welcome-message'] = true; - - helpers.mockPrompt(angular, { - compass: true, - bootstrap: true, - compassBootstrap: true, - modules: ['routeModule'] - }); - - angular.run({}, function() { + angular = helpers.createGenerator( + 'angular:app', + [ + '../../app', + '../../common', + '../../controller', + '../../main', + '../../route', + '../../view', [ + helpers.createDummyGenerator(), + 'karma:app' + ] + ], + false, + genOptions + ); + helpers.mockPrompt(angular, mockPrompts); + angular.run({}, function () { + angular = helpers.createGenerator( + 'angular:route', + [ + '../../controller', + '../../route', + '../../view' + ], + [route], + genOptions + ); + helpers.mockPrompt(angular, mockPrompts); done(); }); }); }); - it('should generate routes, controllers and views', function(done){ - var route = 'simpleroute'; - var expected = [ - 'app/scripts/controllers/' + route + '.js', - 'test/spec/controllers/' + route + '.js', - 'app/views/' + route + '.html' - ]; - var deps = [ - '../../app', - '../../common', - '../../controller', - '../../route', - '../../view' - ]; - - var angularRouteGenerator = helpers.createGenerator('angular:route', deps, [route]); - - angularRouteGenerator.run({}, function() { - - // Check if new files are created for the route - helpers.assertFile(expected); - - var app_js = fs.readFileSync('app/scripts/app.js', 'utf8'); - var route_regex = new RegExp('when\\(\'/' + route + '\''); - - assert.ok(route_regex.test(app_js), 'app.js does not have the route ' + route + ' added'); + describe('create routes', function () { + it('should generate default route items', function(done){ + angular.run({}, function(e) { + helpers.assertFile(expected); + helpers.assertFileContent( + 'app/scripts/app.js', + new RegExp('when\\(\'/' + route + '\'') + ); - done(); + done(); + }); }); - }); - - // Test with URI specified explicitly - it('should generate routes, controllers and views with the route uri given', function(done){ - var route = 'complexroute'; - var uri = 'segment1/segment2/:parameter' - var expected = [ - 'app/scripts/controllers/' + route + '.js', - 'test/spec/controllers/' + route + '.js', - 'app/views/' + route + '.html' - ]; - var deps = [ - '../../app', - '../../common', - '../../controller', - '../../route', - '../../view' - ]; - - angular = helpers.createGenerator('angular:route', deps, [route], { uri: uri }); - angular.run({}, function() { - // Check if new files are created for the route - helpers.assertFile(expected); + // Test with URI specified explicitly + it('should generate route items with the route uri given', function(done){ + var uri = 'segment1/segment2/:parameter' - var app_js = fs.readFileSync('app/scripts/app.js', 'utf8'); - var route_regex = new RegExp('when\\(\'/' + uri + '\''); - - assert.ok(route_regex.test(app_js), 'app.js does not have the route ' + uri + ' added'); + angular.options.uri = uri; + angular.run({}, function() { + helpers.assertFile(expected); + helpers.assertFileContent( + 'app/scripts/app.js', + new RegExp('when\\(\'/' + uri + '\'') + ); - done(); + done(); + }); }); }); }); diff --git a/view/index.js b/view/index.js index 0ff28e25a..aaf29d31c 100644 --- a/view/index.js +++ b/view/index.js @@ -3,16 +3,21 @@ var path = require('path'); var util = require('util'); var yeoman = require('yeoman-generator'); - var Generator = module.exports = function Generator() { yeoman.generators.NamedBase.apply(this, arguments); + this.sourceRoot(path.join(__dirname, '../templates/common')); if (typeof this.env.options.appPath === 'undefined') { - try { - this.env.options.appPath = require(path.join(process.cwd(), 'bower.json')).appPath; - } catch (e) {} + this.env.options.appPath = this.options.appPath; + + if (!this.env.options.appPath) { + try { + this.env.options.appPath = require(path.join(process.cwd(), 'bower.json')).appPath; + } catch (e) {} + } this.env.options.appPath = this.env.options.appPath || 'app'; + this.options.appPath = this.env.options.appPath; } };