Skip to content

Commit 291d316

Browse files
committed
Generate autotest discover file.
1 parent 11089ad commit 291d316

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Feature: configure
2+
3+
Use --configure to generate configuration files.
4+
5+
Currently, the only supported argument is "autotest", which creates
6+
a autotest/discover.rb file in your project root directory.
7+
8+
Background:
9+
Given a directory named "rspec_project"
10+
And I cd to "rspec_project"
11+
12+
Scenario: generate autotest directory and discover file
13+
When I run "rspec --configure autotest"
14+
Then the following directories should exist:
15+
| autotest |
16+
And the following files should exist:
17+
| autotest/discover.rb |
18+
And the file "autotest/discover.rb" should contain "Autotest.add_discovery"
19+
And the stdout should contain "autotest/discover.rb has been added"

lib/rspec/core.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
require 'rspec/core/around_proxy'
1414
require 'rspec/core/world'
1515
require 'rspec/core/configuration'
16+
require 'rspec/core/command_line_configuration'
1617
require 'rspec/core/option_parser'
1718
require 'rspec/core/configuration_options'
1819
require 'rspec/core/command_line'
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module RSpec
2+
module Core
3+
class CommandLineConfiguration
4+
attr_reader :command
5+
6+
def initialize(cmd)
7+
@command = cmd
8+
end
9+
10+
def run
11+
case @command
12+
when 'autotest' then Autotest.generate
13+
else raise ArgumentError, "#{@command} is not a valid option"
14+
end
15+
end
16+
17+
class Autotest
18+
def self.generate
19+
create_autotest_directory
20+
create_discover_file
21+
puts "autotest/discover.rb has been added"
22+
end
23+
24+
def self.create_autotest_directory
25+
Dir.mkdir('autotest') unless File.exist?('autotest')
26+
end
27+
28+
def self.create_discover_file
29+
optionally_remove_discover_file if discover_file_exists?
30+
31+
File.open(discover_file_path, 'w') do |file|
32+
file << 'Autotest.add_discovery { "rspec2" }'
33+
end
34+
end
35+
36+
def optionally_remove_discover_file
37+
print "Discover file already exists, overwrite [Y/n]? "
38+
exit if gets != 'Y'
39+
FileUtils.rm_rf(discover_file_path)
40+
end
41+
42+
def self.discover_file_exists?
43+
File.exist?(discover_file_path)
44+
end
45+
46+
def self.discover_file_path
47+
File.join('autotest', 'discover.rb')
48+
end
49+
end
50+
end
51+
end

lib/rspec/core/option_parser.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,15 @@ def parser(options)
8585
parser.on('-X', '--drb', 'Run examples via DRb') do |o|
8686
options[:drb] = true
8787
end
88+
89+
parser.on('--configure COMMAND', 'Generate configuration files') do |cmd|
90+
CommandLineConfiguration.new(cmd).run
91+
exit
92+
end
8893

8994
parser.on('--drb-port [PORT]', 'Port to connect to on the DRb server') do |o|
9095
options[:drb_port] = o.to_i
9196
end
92-
9397
end
9498
end
9599
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'spec_helper'
2+
3+
module RSpec::Core
4+
describe CommandLineConfiguration do
5+
describe '#run' do
6+
context 'given autotest command' do
7+
let(:config) { described_class.new('autotest') }
8+
9+
it 'calls Autotest.generate' do
10+
described_class::Autotest.should_receive(:generate)
11+
config.run
12+
end
13+
end
14+
15+
context 'given unsupported command' do
16+
let(:config) { described_class.new('unsupported') }
17+
18+
it 'raises ArgumentError' do
19+
lambda { config.run }.should(
20+
raise_error(ArgumentError, 'unsupported is not a valid option')
21+
)
22+
end
23+
end
24+
end
25+
end
26+
end

0 commit comments

Comments
 (0)