Skip to content

Commit

Permalink
Generate autotest configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinko committed Jul 17, 2010
1 parent d8b649a commit ff5770e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
19 changes: 19 additions & 0 deletions features/command_line/configure.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Feature: configure

Use --configure to generate configuration files.

Currently, the only supported argument is "autotest", which creates
a autotest/discover.rb file in your project root directory.

Background:
Given a directory named "rspec_project"
And I cd to "rspec_project"

Scenario: generate autotest directory and discover file
When I run "rspec --configure autotest"
Then the following directories should exist:
| autotest |
And the following files should exist:
| autotest/discover.rb |
And the file "autotest/discover.rb" should contain "Autotest.add_discovery"
And the stdout should contain "autotest/discover.rb has been added"
1 change: 1 addition & 0 deletions lib/rspec/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'rspec/core/around_proxy'
require 'rspec/core/world'
require 'rspec/core/configuration'
require 'rspec/core/command_line_configuration'
require 'rspec/core/option_parser'
require 'rspec/core/configuration_options'
require 'rspec/core/command_line'
Expand Down
53 changes: 53 additions & 0 deletions lib/rspec/core/command_line_configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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, "#{@command} is not a valid option"
end
end

class Autotest
class << self
def generate
create_autotest_directory
create_discover_file
puts "autotest/discover.rb has been added"
end

def create_autotest_directory
Dir.mkdir('autotest') unless File.exist?('autotest')
end

def create_discover_file
optionally_remove_discover_file if discover_file_exists?
File.open(discover_file_path, 'w') do |file|
file << 'Autotest.add_discovery { "rspec2" }'
end
end

def optionally_remove_discover_file
print "Discover file already exists, overwrite [y/N]? "
exit if gets !~ /y/i
FileUtils.rm_rf(discover_file_path)
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
6 changes: 5 additions & 1 deletion lib/rspec/core/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ def parser(options)
parser.on('-X', '--drb', 'Run examples via DRb') do |o|
options[:drb] = true
end

parser.on('--configure COMMAND', 'Generate configuration files') do |cmd|
CommandLineConfiguration.new(cmd).run
exit
end

parser.on('--drb-port [PORT]', 'Port to connect to on the DRb server') do |o|
options[:drb_port] = o.to_i
end

end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/rspec/core/command_line_configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

module RSpec::Core
describe CommandLineConfiguration do
describe '#run' do
context 'given autotest command' do
let(:config) { described_class.new('autotest') }

it 'calls Autotest.generate' do
described_class::Autotest.should_receive(:generate)
config.run
end
end

context 'given unsupported command' do
let(:config) { described_class.new('unsupported') }

it 'raises ArgumentError' do
lambda { config.run }.should(
raise_error(ArgumentError, 'unsupported is not a valid option')
)
end
end
end
end
end

0 comments on commit ff5770e

Please sign in to comment.