Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

merge --configure and --init into a single command

- #430

- generates .rspec if not present
- generates spec/spec_helper if not present
- offers to delete obsolete autotest/discover.rb if present
- offers to delete obsolete lib/tasks/rspec.rake if present
  • Loading branch information...
commit 3eff7990aa01d0cf5e02044c4b710f39932daa1a 1 parent 3f482b7
@dchelimsky dchelimsky authored
View
2  Gemfile
@@ -16,7 +16,7 @@ gem "cucumber", "1.0.1"
gem "aruba", "0.4.2"
gem "ZenTest", "4.6.2"
gem "nokogiri", "1.5.0"
-gem "fakefs", "0.4.0", :require => false
+gem "fakefs", "0.4.0", :require => "fakefs/safe"
platforms :jruby do
gem "jruby-openssl"
View
22 features/command_line/configure.feature
@@ -1,22 +0,0 @@
-Feature: --configure option
-
- Use the --configure option on the command line to generate configuration
- files.
-
- The only supported argument, so far, is "autotest", which creates a .rspec
- file in your project root directory. When autotest sees this file, it knows
- to load RSpec's Autotest subclass.
-
- Scenario: generate .rspec file for autotest
- When I run `rspec --configure autotest`
- Then the following files should exist:
- | .rspec |
- And the output should contain ".rspec file did not exist, so it was created."
-
- Scenario: .rspec file already exists
- Given a file named ".rspec" with:
- """
- --color
- """
- When I run `rspec --configure autotest`
- Then the output should contain ".rspec file already exists, so nothing was changed."
View
18 features/command_line/init.feature
@@ -0,0 +1,18 @@
+Feature: --init option
+
+ Use the --init option on the command line to generate conventional
+ files for an rspec project.
+
+ Scenario: generate .rspec
+ When I run `rspec --init`
+ Then the following files should exist:
+ | .rspec |
+ And the output should contain "create .rspec"
+
+ Scenario: .rspec file already exists
+ Given a file named ".rspec" with:
+ """
+ --color
+ """
+ When I run `rspec --init`
+ Then the output should contain "exist .rspec"
View
1  lib/rspec/core.rb
@@ -15,7 +15,6 @@
require 'rspec/core/world'
require 'rspec/core/configuration'
-require 'rspec/core/command_line_configuration'
require 'rspec/core/project_initializer'
require 'rspec/core/option_parser'
require 'rspec/core/drb_options'
View
62 lib/rspec/core/command_line_configuration.rb
@@ -1,62 +0,0 @@
-module RSpec
- module Core
- class CommandLineConfiguration
- attr_reader :command
-
- def initialize(cmd)
- @command = cmd
- end
-
- def run
- case @command
- when 'autotest' then Autotest.generate
- else raise ArgumentError, <<-MESSAGE
-
-#{"*"*50}
-"#{@command}" is not valid a valid argument to "rspec --configure".
-Supported options are:
-
- rspec --configure autotest # generates .rspec file
-
-#{"*"*50}
-MESSAGE
- end
- end
-
- class Autotest
- class << self
- def generate
- create_dot_rspec_file
- remove_autotest_dir_if_present
- end
-
- def create_dot_rspec_file
- puts "Autotest loads RSpec's Autotest subclass when there is a .rspec file in the project's root directory."
- if File.exist?('./.rspec')
- puts ".rspec file already exists, so nothing was changed."
- else
- FileUtils.touch('./.rspec')
- puts ".rspec file did not exist, so it was created."
- end
- end
-
- def remove_autotest_dir_if_present
- if discover_file_exists?
- print "Delete obsolete autotest/discover.rb [y/n]? "
- exit if gets !~ /y/i
- FileUtils.rm_rf(discover_file_path)
- end
- end
-
- def discover_file_exists?
- File.exist?(discover_file_path)
- end
-
- def discover_file_path
- File.join('autotest', 'discover.rb')
- end
- end
- end
- end
- end
-end
View
14 lib/rspec/core/option_parser.rb
@@ -28,7 +28,7 @@ def parser(options)
OptionParser.new do |parser|
parser.banner = "Usage: rspec [options] [files or directories]\n\n"
- parser.on('-I DIRECTORY', 'specify $LOAD_PATH directory (may be used more than once)') do |dir|
+ parser.on('-I PATH', 'specify PATH to add to $LOAD_PATH (may be used more than once)') do |dir|
options[:libs] ||= []
options[:libs] << dir
end
@@ -69,13 +69,12 @@ def parser(options)
options[:drb] = o
end
- parser.on('--drb-port [PORT]', 'Port to connect to on the DRb server') do |o|
+ parser.on('--drb-port PORT', 'Port to connect to on the DRb server') do |o|
options[:drb_port] = o.to_i
end
- parser.on('--configure COMMAND', 'Generate configuration files') do |cmd|
- CommandLineConfiguration.new(cmd).run
- exit
+ parser.on("--tty", "Used internally by rspec when sending commands to other processes") do |o|
+ options[:tty] = true
end
parser.on('--init', 'Initialize your project with RSpec.') do |cmd|
@@ -83,8 +82,9 @@ def parser(options)
exit
end
- parser.on("--tty", "Used internally by rspec when sending commands to other processes") do |o|
- options[:tty] = true
+ parser.on('--configure', 'Deprecated. Use --init instead.') do |cmd|
+ warn "--configure is deprecated with no effect. Use --init instead."
+ exit
end
parser.separator("\n **** Output formatting ****\n\n")
View
79 lib/rspec/core/project_initializer.rb
@@ -1,49 +1,62 @@
module RSpec
module Core
class ProjectInitializer
- attr_reader :command
-
- def initialize(cmd)
- @command = cmd
+ def initialize(arg=nil)
+ @arg = arg
end
def run
- FileUtils.mkdir_p 'spec'
- File.open 'spec/spec_helper.rb', 'w+' do |file|
- file.write <<-FILE
-require 'rspec'
+ warn "The --configure option no longer needs any arguments, so #{@arg} was ignored." if @arg
+ create_spec_helper_file
+ create_dot_rspec_file
+ delete_if_confirmed("autotest/discover.rb", <<-MESSAGE)
+ RSpec registers its own discover.rb with Autotest, so autotest/discover.rb is
+ no longer needed.
+ MESSAGE
+
+ delete_if_confirmed("lib/tasks/rspec.rake", <<-MESSAGE)
+ If the file in lib/tasks/rspec.rake is the one generated by rspec-rails-1x,
+ you can get rid of it, as it is no longer needed with rspec-2.
+ MESSAGE
+ end
-RSpec.configure do |c|
- c.mock_with :rspec
-end
- FILE
+ def create_dot_rspec_file
+ if File.exist?('.rspec')
+ report_exists('.rspec')
+ else
+ report_creating('.rspec')
+ FileUtils.touch('.rspec')
end
- File.open 'spec/example_spec.rb', 'w+' do |file|
- file.write <<-FILE
-require 'spec_helper'
-
-describe 'example' do
+ end
- # let(:test_data) { 'testo' }
+ def create_spec_helper_file
+ if File.exist?('spec/spec_helper.rb')
+ report_exists("spec/spec_helper.rb")
+ else
+ report_creating("spec/spec_helper.rb")
+ FileUtils.mkdir('spec') unless File.exist?('spec') && File.directory?('spec')
+ FileUtils.touch('spec/spec_helper.rb')
+ end
+ end
- # before do
- # some_setup_stuff
- # end
+ def delete_if_confirmed(path, message)
+ if File.exist?(path)
+ puts
+ puts message
+ puts
+ puts " delete #{path}? [y/n]"
+ FileUtils.rm_rf(path) if gets =~ /y/i
+ end
+ end
- # context 'doing something' do
- # it 'should behave like i want to' do
- # it.should be_true
- # end
- # end
-
- pending 'please remove this example and add your own spec!'
+ def report_exists(file)
+ puts " exist #{file}"
+ end
-end
- FILE
- end
-
- puts 'now run: rspec spec'
+ def report_creating(file)
+ puts " create #{file}"
end
+
end
end
end
View
26 spec/rspec/core/command_line_configuration_spec.rb
@@ -1,26 +0,0 @@
-require 'spec_helper'
-
-module RSpec::Core
- describe CommandLineConfiguration do
- describe '#run' do
- context 'given autotest command' do
- let(:config) { CommandLineConfiguration.new('autotest') }
-
- it 'calls Autotest.generate' do
- CommandLineConfiguration::Autotest.should_receive(:generate)
- config.run
- end
- end
-
- context 'given unsupported command' do
- let(:config) { CommandLineConfiguration.new('unsupported') }
-
- it 'raises ArgumentError' do
- lambda { config.run }.should(
- raise_error(ArgumentError, /"unsupported" is not valid/)
- )
- end
- end
- end
- end
-end
View
130 spec/rspec/core/project_initializer_spec.rb
@@ -0,0 +1,130 @@
+require "spec_helper"
+
+module RSpec::Core
+ describe ProjectInitializer, :fakefs do
+ describe "#run" do
+ context "with no args" do
+ let(:command_line_config) { ProjectInitializer.new }
+
+ before do
+ command_line_config.stub(:warn)
+ command_line_config.stub(:puts)
+ command_line_config.stub(:gets => 'no')
+ end
+
+ context "with no .rspec file" do
+ it "says it's creating .rspec " do
+ command_line_config.should_receive(:puts).with(/create\s+\.rspec/)
+ command_line_config.run
+ end
+
+ it "generates a .rspec" do
+ command_line_config.run
+ File.read('.rspec').should eq('')
+ end
+ end
+
+ context "with a .rspec file" do
+ it "says .rspec exists" do
+ FileUtils.touch('.rspec')
+ command_line_config.should_receive(:puts).with(/exist\s+\.rspec/)
+ command_line_config.run
+ end
+
+ it "doesn't create a new one" do
+ File.open('.rspec', 'w') {|f| f << '--color'}
+ command_line_config.run
+ File.read('.rspec').should eq('--color')
+ end
+ end
+
+ context "with no spec/spec_helper.rb file" do
+ it "says it's creating spec/spec_helper.rb " do
+ command_line_config.should_receive(:puts).with(/create\s+spec\/spec_helper.rb/)
+ command_line_config.run
+ end
+
+ it "generates a spec/spec_helper.rb" do
+ command_line_config.run
+ File.read('spec/spec_helper.rb').should eq('')
+ end
+ end
+
+ context "with a spec/spec_helper.rb file" do
+ before { FileUtils.mkdir('spec') }
+
+ it "says spec/spec_helper.rb exists" do
+ FileUtils.touch('spec/spec_helper.rb')
+ command_line_config.should_receive(:puts).with(/exist\s+spec\/spec_helper.rb/)
+ command_line_config.run
+ end
+
+ it "doesn't create a new one" do
+ random_content = "content #{rand}"
+ File.open('spec/spec_helper.rb', 'w') {|f| f << random_content}
+ command_line_config.run
+ File.read('spec/spec_helper.rb').should eq(random_content)
+ end
+ end
+
+ context "with autotest/discover.rb" do
+ before do
+ FileUtils.mkdir('autotest')
+ FileUtils.touch 'autotest/discover.rb'
+ end
+
+ it "asks whether to delete the file" do
+ command_line_config.should_receive(:puts).with(/delete/)
+ command_line_config.run
+ end
+
+ it "removes it if confirmed" do
+ command_line_config.stub(:gets => 'yes')
+ command_line_config.run
+ File.exist?('autotest/discover.rb').should be_false
+ end
+
+ it "leaves it if not confirmed" do
+ command_line_config.stub(:gets => 'no')
+ command_line_config.run
+ File.exist?('autotest/discover.rb').should be_true
+ end
+ end
+
+ context "with lib/tasks/rspec.rake" do
+ before do
+ FileUtils.mkdir_p('lib/tasks')
+ FileUtils.touch 'lib/tasks/rspec.rake'
+ end
+
+ it "asks whether to delete the file" do
+ command_line_config.should_receive(:puts).with(/delete/)
+ command_line_config.run
+ end
+
+ it "removes it if confirmed" do
+ command_line_config.stub(:gets => 'yes')
+ command_line_config.run
+ File.exist?('lib/tasks/rspec.rake').should be_false
+ end
+
+ it "leaves it if not confirmed" do
+ command_line_config.stub(:gets => 'no')
+ command_line_config.run
+ File.exist?('lib/tasks/rspec.rake').should be_true
+ end
+ end
+ end
+
+ context "given an arg" do
+ it "warns if arg received (no longer necessary)" do
+ config = ProjectInitializer.new("another_arg")
+ config.stub(:puts)
+ config.stub(:gets => 'no')
+ config.should_receive(:warn).with(/no longer.*another_arg was ignored/)
+ config.run
+ end
+ end
+ end
+ end
+end
View
2  spec/spec_helper.rb
@@ -18,6 +18,7 @@ def self.each_run
require 'rspec/autorun'
require 'autotest/rspec2'
require 'aruba/api'
+ require 'fakefs/spec_helpers'
Dir['./spec/support/**/*.rb'].map {|f| require f}
@@ -78,6 +79,7 @@ def in_editor?
c.color = !in_editor?
c.filter_run :focus
c.filter_run :foo
+ c.include FakeFS::SpecHelpers, :fakefs
c.run_all_when_everything_filtered = true
c.filter_run_excluding :ruby => lambda {|version|
case version.to_s
Please sign in to comment.
Something went wrong with that request. Please try again.