Skip to content

Instantly share code, notes, and snippets.

@wincent
Created June 22, 2010 23:49
Show Gist options
  • Save wincent/449281 to your computer and use it in GitHub Desktop.
Save wincent/449281 to your computer and use it in GitHub Desktop.
From 4c3574c21d09efbdd649f93f3db3af7455f9369e Mon Sep 17 00:00:00 2001
From: Wincent Colaiuta <win@wincent.com>
Date: Wed, 23 Jun 2010 01:39:29 +0200
Subject: [PATCH] Add the ability to override default "spec/acceptance" dir
Some users may wish to keep their acceptance specs in a directory that
is completely separate from their existing "specs" directory (for
example, because they don't want to run the acceptance specs whenever
they do "rake spec").
This commit modifies the "steak:install" generator to allow the user to
provide a custom "spec_dir" and override the default "spec/acceptance"
destination.
Signed-off-by: Wincent Colaiuta <win@wincent.com>
---
README.rdoc | 6 +++
lib/generators/steak/install_generator.rb | 33 ++++++++++++-----
lib/generators/steak/spec_generator.rb | 29 +++++++++++++--
.../steak/templates/acceptance_helper.rb | 5 ++-
lib/generators/steak/templates/steak.rake | 8 ++--
lib/generators/steak/templates/steak.yml | 1 +
spec/acceptance/steak_install_generator_spec.rb | 39 ++++++++++++++++++++
spec/acceptance/steak_spec_generator_spec.rb | 10 +++++
8 files changed, 112 insertions(+), 19 deletions(-)
create mode 100644 lib/generators/steak/templates/steak.yml
diff --git a/README.rdoc b/README.rdoc
index c5f6db8..52c88ba 100644
--- a/README.rdoc
+++ b/README.rdoc
@@ -104,6 +104,12 @@ That will create some basic helper files and directory structure under the +spec
$ rails generate steak:install --webrat
+If you prefer to keep your acceptance specs completely separate from your other specs you can override the default +spec/acceptance+ directory by passing in an additional parameter to the generator; for example:
+
+ $ rails generate steak:install features
+
+Will create a +features+ directory with an appropriate +acceptance_helper.rb+ file, and customize the "spec:acceptance" Rake task to use that directory.
+
Spend one minute on getting familiar with the structure and files you've got.
Now you may want to create your first acceptance spec:
diff --git a/lib/generators/steak/install_generator.rb b/lib/generators/steak/install_generator.rb
index 68bcbce..e0e7e3b 100644
--- a/lib/generators/steak/install_generator.rb
+++ b/lib/generators/steak/install_generator.rb
@@ -1,14 +1,17 @@
require 'rails/generators'
+require 'yaml'
module Steak
class InstallGenerator < Rails::Generators::Base
class_option :webrat, :desc => 'Use Webrat.', :type => :boolean
class_option :capybara, :desc => 'Use Capybara.', :type => :boolean
+ argument :spec_dir, :type => :string, :default => 'spec/acceptance'
desc <<DESC
Description:
- Sets up Steak in your Rails project. This will generate the
- spec/acceptance directory and the necessary files.
+ Sets up Steak in your Rails project. This will generate a
+ directory for acceptance specs ("spec/acceptance" by default)
+ and the necessary files.
If you haven't already, You should also run
`rails generate rspec:install` to complete the set up.
@@ -18,24 +21,36 @@ Examples:
DESC
def initialize(args=[], options={}, config={})
- puts "Defaulting to Capybara..." if options.empty?
+ unless options.any? { |o| o =~ /--(webrat|capybara)/ }
+ puts "Defaulting to Capybara..."
+ end
super
end
def manifest
- empty_directory 'spec/controllers'
- empty_directory 'spec/acceptance/support'
empty_directory 'lib/tasks'
- template "acceptance_helper.rb", "spec/acceptance/acceptance_helper.rb"
- copy_file "helpers.rb", "spec/acceptance/support/helpers.rb"
- copy_file "paths.rb", "spec/acceptance/support/paths.rb"
- copy_file "steak.rake", "lib/tasks/steak.rake"
+ empty_directory support_dir
+ template "steak.yml", "steak.yml"
+ template "steak.rake", "lib/tasks/steak.rake"
+ template "acceptance_helper.rb", (spec_dir + "/acceptance_helper.rb")
+ copy_file "helpers.rb", (support_dir + "/helpers.rb")
+ copy_file "paths.rb", (support_dir + "/paths.rb")
end
+ private
+
def driver
@driver = options.webrat? ? 'webrat' : 'capybara'
end
+ def support_dir
+ spec_dir + '/support'
+ end
+
+ def config_yaml
+ { 'spec_dir' => spec_dir }.to_yaml
+ end
+
def self.source_root
File.join(File.dirname(__FILE__), 'templates')
end
diff --git a/lib/generators/steak/spec_generator.rb b/lib/generators/steak/spec_generator.rb
index 19f762b..5c56fa1 100644
--- a/lib/generators/steak/spec_generator.rb
+++ b/lib/generators/steak/spec_generator.rb
@@ -1,23 +1,44 @@
require 'rails/generators'
+require 'yaml'
module Steak
class SpecGenerator < Rails::Generators::NamedBase
+ def self.yaml_config
+ @yaml_config ||= YAML.load_file 'steak.yml'
+ rescue ArgumentError, Errno::ENOENT
+ @yaml_config = {}
+ end
+
+ def self.spec_dir
+ if yaml_config && yaml_config.has_key?('spec_dir')
+ yaml_config['spec_dir']
+ else
+ 'spec/acceptance'
+ end
+ end
+
desc <<DESC
Description:
Create an acceptance spec for the feature NAME in the
- 'spec/acceptance' folder.
+ '#{spec_dir}' folder.
Example:
`rails generate steak:spec checkout`
Creates an acceptance spec for the "checkout" feature:
- spec/acceptance/checkout_spec.rb
+ #{spec_dir}/checkout_spec.rb
DESC
def manifest
- empty_directory File.join('spec/acceptance', class_path)
+ empty_directory File.join(spec_dir, class_path)
file_name.gsub!(/_spec$/,"")
- template 'acceptance_spec.rb', File.join('spec/acceptance', class_path, "#{file_name}_spec.rb")
+ template 'acceptance_spec.rb', File.join(spec_dir, class_path, "#{file_name}_spec.rb")
+ end
+
+ private
+
+ def spec_dir
+ self.class.spec_dir
end
def self.source_root
diff --git a/lib/generators/steak/templates/acceptance_helper.rb b/lib/generators/steak/templates/acceptance_helper.rb
index 5ca8c37..b20a8d9 100644
--- a/lib/generators/steak/templates/acceptance_helper.rb
+++ b/lib/generators/steak/templates/acceptance_helper.rb
@@ -1,4 +1,5 @@
-require File.dirname(__FILE__) + "/../spec_helper"
+require "<%= File.expand_path 'spec/spec_helper' -%>"
+
require "steak"
<%- if driver == 'webrat' %>
require "webrat"
@@ -27,5 +28,5 @@ RSpec.configure do |config|
end
<%- end -%>
-# Put your acceptance spec helpers inside /spec/acceptance/support
+# Put your acceptance spec helpers inside <%= support_dir %>
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
diff --git a/lib/generators/steak/templates/steak.rake b/lib/generators/steak/templates/steak.rake
index 502e628..69c8474 100644
--- a/lib/generators/steak/templates/steak.rake
+++ b/lib/generators/steak/templates/steak.rake
@@ -1,14 +1,14 @@
require 'rspec/core/rake_task'
namespace :spec do
- desc "Run the code examples in spec/acceptance"
+ desc "Run the code examples in <%= spec_dir -%>"
RSpec::Core::RakeTask.new(:acceptance => "db:test:prepare") do |t|
- t.pattern = "spec/acceptance/**/*_spec.rb"
+ t.pattern = "<%= spec_dir -%>/**/*_spec.rb"
end
task :statsetup do
require 'rails/code_statistics'
- ::STATS_DIRECTORIES << %w(Acceptance\ specs spec/acceptance) if File.exist?('spec/acceptance')
- ::CodeStatistics::TEST_TYPES << "Acceptance specs" if File.exist?('spec/acceptance')
+ ::STATS_DIRECTORIES << %w(Acceptance\ specs <%= spec_dir -%>) if File.exist?('<%= spec_dir -%>')
+ ::CodeStatistics::TEST_TYPES << "Acceptance specs" if File.exist?('<%= spec_dir -%>')
end
end
diff --git a/lib/generators/steak/templates/steak.yml b/lib/generators/steak/templates/steak.yml
new file mode 100644
index 0000000..140e94b
--- /dev/null
+++ b/lib/generators/steak/templates/steak.yml
@@ -0,0 +1 @@
+<%= config_yaml %>
diff --git a/spec/acceptance/steak_install_generator_spec.rb b/spec/acceptance/steak_install_generator_spec.rb
index 5ea6069..253d74f 100644
--- a/spec/acceptance/steak_install_generator_spec.rb
+++ b/spec/acceptance/steak_install_generator_spec.rb
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + "/acceptance_helper.rb"
+require 'yaml'
feature "Steak generator for rails", %q{
In order to quickly start to hack my rails project with steak
@@ -18,7 +19,9 @@ feature "Steak generator for rails", %q{
File.exist?(rails_app + "/spec/acceptance/support/helpers.rb").should be_true
File.exist?(rails_app + "/spec/acceptance/support/paths.rb").should be_true
File.exist?(rails_app + "/lib/tasks/steak.rake").should be_true
+ File.exist?(rails_app + "/steak.yml").should be_true
+ YAML.load_file(rails_app + '/steak.yml')['spec_dir'].should == 'spec/acceptance'
end
scenario "Running generator with capybara by default" do
@@ -64,6 +67,42 @@ feature "Steak generator for rails", %q{
output.should =~ /1 example, 0 failures/
end
+ scenario "Running generator with custom spec_dir" do
+ rails_app = create_rails_app(:setup_steak => false, :scaffold => :anything)
+
+ Dir.chdir rails_app do
+ `rails generate steak:install features`
+ end
+
+ File.exist?(rails_app + '/features/acceptance_helper.rb').should be_true
+ File.exist?(rails_app + '/features/support/helpers.rb').should be_true
+ File.exist?(rails_app + '/features/support/paths.rb').should be_true
+ File.exist?(rails_app + '/lib/tasks/steak.rake').should be_true
+
+ YAML.load_file(rails_app + '/steak.yml')['spec_dir'].should == 'features'
+
+ helper = File.open(rails_app + '/features/acceptance_helper.rb').read
+ helper.should match(%r{Put your acceptance spec helpers inside features/support})
+
+ tasks = File.open(rails_app + '/lib/tasks/steak.rake').read
+ tasks.should match(/features/)
+ tasks.should_not match(%r{spec/acceptance})
+
+ spec_file = create_spec :path => rails_app + "/features",
+ :content => <<-SPEC
+ require File.dirname(__FILE__) + "/acceptance_helper.rb"
+ feature "First feature" do
+ scenario "First scenario" do
+ 1.should == 1
+ end
+ end
+ SPEC
+ output = Dir.chdir rails_app do
+ `rake spec:acceptance`
+ end
+ output.should =~ /1 example, 0 failures/
+ end
+
scenario "Running rake stats" do
rails_app = create_rails_app
diff --git a/spec/acceptance/steak_spec_generator_spec.rb b/spec/acceptance/steak_spec_generator_spec.rb
index 8ec64a5..1afa7b8 100644
--- a/spec/acceptance/steak_spec_generator_spec.rb
+++ b/spec/acceptance/steak_spec_generator_spec.rb
@@ -41,4 +41,14 @@ feature "Acceptance spec generator for rails", %q{
File.exist?(@rails_app + "/spec/acceptance/document_creation_spec.rb").should be_true
end
+
+ scenario "Adding new acceptance spec with custom spec_dir" do
+ rails_app = create_rails_app(:setup_steak => false)
+ Dir.chdir rails_app do
+ `rails generate steak:install acceptance_specs`
+ `rails generate steak:spec document_creation`
+ end
+
+ File.exist?(rails_app + "/acceptance_specs/document_creation_spec.rb").should be_true
+ end
end
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment