From deda92ff7888c7b2e22e8c944812a0ed9107208e Mon Sep 17 00:00:00 2001 From: Dan Funk Date: Thu, 15 Mar 2012 13:12:23 -0400 Subject: [PATCH] adding in some explanitory error messages, rather than dying on a stack trace that can be obscure at times --- README.markdown | 6 ++++- .../machinist/install/install_generator.rb | 27 +++++++++++-------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/README.markdown b/README.markdown index 671ae64..4a305bb 100644 --- a/README.markdown +++ b/README.markdown @@ -62,7 +62,11 @@ In your app's `Gemfile`, in the `group :test` section, add: Then run: bundle - rails generate machinist:install + rails generate machinist:install + +Or (if you are using rspec) + bundle + rails generate machinist:install -t rspec If you want Machinist to automatically add a blueprint to your blueprints file whenever you generate a model, add the following to your `config/application.rb` diff --git a/lib/generators/machinist/install/install_generator.rb b/lib/generators/machinist/install/install_generator.rb index 3840062..675e7a7 100644 --- a/lib/generators/machinist/install/install_generator.rb +++ b/lib/generators/machinist/install/install_generator.rb @@ -2,22 +2,27 @@ module Machinist module Generators #:nodoc: class InstallGenerator < Rails::Generators::Base #:nodoc: source_root File.expand_path('../templates', __FILE__) - - class_option :test_framework, :type => :string, :aliases => "-t", :desc => "Test framework to use Machinist with" - class_option :cucumber, :type => :boolean, :desc => "Set up access to Machinist from Cucumber" + class_option :test_framework, :type => :string, :default => 'test_unit', :aliases => "-t", :desc => "Test framework to use Machinist with" + class_option :cucumber, :type => :boolean, :default => false, :desc => "Set up access to Machinist from Cucumber" def blueprints_file if rspec? copy_file "blueprints.rb", "spec/support/blueprints.rb" - else + elsif test_unit? copy_file "blueprints.rb", "test/blueprints.rb" + else + say_status(:error, "No test framework found. Please specify either 'rspec' or 'test_unit' with the -t option.", :red) end end - + def test_helper - if test_unit? - inject_into_file("test/test_helper.rb", :after => "require 'rails/test_help'\n") do - "require File.expand_path(File.dirname(__FILE__) + '/blueprints')\n" + if test_unit? + if File.exist?("test/test_helper.rb") + inject_into_file("test/test_helper.rb", :after => "require 'rails/test_help'\n") do + "require File.expand_path(File.dirname(__FILE__) + '/blueprints')\n" + end + else + say_status(:warning, "Unable to modify the test_helper file. It does not exist.", :red) end end end @@ -31,11 +36,11 @@ def cucumber_support private def rspec? - options[:test_framework].to_sym == :rspec + options[:test_framework] and options[:test_framework].to_sym == :rspec end - def test_unit? - options[:test_framework].to_sym == :test_unit + def test_unit? + options[:test_framework] and options[:test_framework].to_sym == :test_unit end def cucumber?