Permalink
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...
1 parent 3f482b7 commit 3eff7990aa01d0cf5e02044c4b710f39932daa1a @dchelimsky dchelimsky committed Nov 10, 2011
View
@@ -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"
@@ -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."
@@ -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
@@ -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'
@@ -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
@@ -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,22 +69,22 @@ 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|
ProjectInitializer.new(cmd).run
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")
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit 3eff799

Please sign in to comment.