Skip to content

Commit

Permalink
Merge pull request #1455 from rspec/command-line-deprecations
Browse files Browse the repository at this point in the history
Deprecate CommandLine in favor of Runner.
  • Loading branch information
myronmarston committed Mar 26, 2014
2 parents 7126d04 + 331f04a commit 35e6e01
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 45 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Expand Up @@ -44,6 +44,8 @@ Deprecations:
`RSpec::Core::Configuration#warnings?`. (Myron Marston)
* Deprecate `share_examples_for` in favor of `shared_examples_for` or
just `shared_examples`. (Myron Marston)
* Deprecate `RSpec::Core::CommandLine` in favor of
`RSpec::Core::Runner`. (Myron Marston)

### 2.99.0.beta2 / 2014-02-17
[full changelog](http://github.com/rspec/rspec-core/compare/v2.99.0.beta1...v2.99.0.beta2)
Expand Down
21 changes: 16 additions & 5 deletions lib/rspec/core/command_line.rb
@@ -1,11 +1,7 @@
module RSpec
module Core
class CommandLine
class Runner
def initialize(options, configuration=RSpec::configuration, world=RSpec::world)
if Array === options
options = ConfigurationOptions.new(options)
options.parse_options
end
@options = options
@configuration = configuration
@world = world
Expand All @@ -32,5 +28,20 @@ def run(err, out)
end
end
end

class CommandLine < Runner
def initialize(options, configuration=RSpec.configuration, world=RSpec.world)
if Array === options
RSpec.deprecate("Instantiating a `RSpec::Core::CommandLine` with an array of options",
:replacement => "Instantiate a `RSpec::Core::Runner` with a `RSpec::Core::ConfigurationOptions` instance")
options = ConfigurationOptions.new(options)
options.parse_options
else
RSpec.deprecate("`RSpec::Core::CommandLine`", :replacement => "`RSpec::Core::Runner`")
end

super
end
end
end
end
4 changes: 2 additions & 2 deletions lib/rspec/core/runner.rb
Expand Up @@ -83,10 +83,10 @@ def self.run(args, err=$stderr, out=$stdout)
DRbCommandLine.new(options).run(err, out)
rescue DRb::DRbConnError
err.puts "No DRb server is running. Running in local process instead ..."
CommandLine.new(options).run(err, out)
new(options).run(err, out)
end
else
CommandLine.new(options).run(err, out)
new(options).run(err, out)
end
ensure
RSpec.reset
Expand Down
76 changes: 47 additions & 29 deletions spec/rspec/core/command_line_spec.rb
Expand Up @@ -3,7 +3,7 @@
require 'tmpdir'

module RSpec::Core
describe CommandLine do
describe Runner do

let(:out) { StringIO.new }
let(:err) { StringIO.new }
Expand All @@ -21,39 +21,33 @@ module RSpec::Core
config.should_receive(:output_stream=).ordered
config.should_receive(:force).at_least(:once).ordered

command_line = build_command_line
command_line.run err, out
end

it "assigns ConfigurationOptions built from Array of options to @options" do
config_options = ConfigurationOptions.new(%w[--color])
command_line = CommandLine.new(%w[--color])
expect(command_line.instance_eval { @options.options }).to eq(config_options.parse_options)
runner = build_runner
runner.run err, out
end

it "assigns submitted ConfigurationOptions to @options" do
config_options = ConfigurationOptions.new(%w[--color])
command_line = CommandLine.new(config_options)
expect(command_line.instance_eval { @options }).to be(config_options)
runner = Runner.new(config_options)
expect(runner.instance_eval { @options }).to be(config_options)
end

describe "#run" do
context "running files" do
include_context "spec files"

it "returns 0 if spec passes" do
command_line = build_command_line passing_spec_filename
expect(command_line.run(err, out)).to eq 0
runner = build_runner passing_spec_filename
expect(runner.run(err, out)).to eq 0
end

it "returns 1 if spec fails" do
command_line = build_command_line failing_spec_filename
expect(command_line.run(err, out)).to eq 1
runner = build_runner failing_spec_filename
expect(runner.run(err, out)).to eq 1
end

it "returns 2 if spec fails and --failure-exit-code is 2" do
command_line = build_command_line failing_spec_filename, "--failure-exit-code", "2"
expect(command_line.run(err, out)).to eq 2
runner = build_runner failing_spec_filename, "--failure-exit-code", "2"
expect(runner.run(err, out)).to eq 2
end
end

Expand All @@ -62,22 +56,22 @@ module RSpec::Core

it "runs before suite hooks" do
config.should_receive(:run_hook).with(:before, :suite)
command_line = build_command_line
command_line.run err, out
runner = build_runner
runner.run err, out
end

it "runs after suite hooks" do
config.should_receive(:run_hook).with(:after, :suite)
command_line = build_command_line
command_line.run err, out
runner = build_runner
runner.run err, out
end

it "runs after suite hooks even after an error" do
config.should_receive(:run_hook).with(:before, :suite).and_raise "this error"
config.should_receive(:run_hook).with(:after , :suite)
expect do
command_line = build_command_line
command_line.run err, out
runner = build_runner
runner.run err, out
end.to raise_error
end
end
Expand All @@ -86,18 +80,18 @@ module RSpec::Core
describe "#run with custom output" do
before { config.stub :files_to_run => [] }

let(:output_file) { File.new("#{Dir.tmpdir}/command_line_spec_output.txt", 'w') }
let(:output_file) { File.new("#{Dir.tmpdir}/runner_spec_output.txt", 'w') }

it "doesn't override output_stream" do
config.output_stream = output_file
command_line = build_command_line
command_line.run err, out
expect(command_line.instance_eval { @configuration.output_stream }).to eq output_file
runner = build_runner
runner.run err, out
expect(runner.instance_eval { @configuration.output_stream }).to eq output_file
end
end

def build_command_line *args
CommandLine.new build_config_options(*args)
def build_runner *args
Runner.new build_config_options(*args)
end

def build_config_options *args
Expand All @@ -106,4 +100,28 @@ def build_config_options *args
options
end
end

describe CommandLine do
it 'is a subclass of `Runner` so that it inherits the same behavior' do
expect(CommandLine.superclass).to be(Runner)
end

it 'prints a deprecation when instantiated' do
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /RSpec::Core::CommandLine/)
CommandLine.new(ConfigurationOptions.new([]))
end

context "when given an array as the first arg" do
it 'parses it into configuration options' do
cl = CommandLine.new(%w[ --require foo ])
options = cl.instance_eval { @options }
expect(options.options).to include(:requires => ['foo'])
end

it 'issues a deprecation about the array arg' do
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /array/)
CommandLine.new(%w[ --require foo ])
end
end
end
end
6 changes: 3 additions & 3 deletions spec/rspec/core/formatters/html_formatter_spec.rb
Expand Up @@ -23,14 +23,14 @@ module Formatters
err.set_encoding("utf-8") if err.respond_to?(:set_encoding)
out.set_encoding("utf-8") if out.respond_to?(:set_encoding)

command_line = RSpec::Core::CommandLine.new(options)
configuration = command_line.instance_variable_get(:@configuration)
runner = RSpec::Core::Runner.new(options)
configuration = runner.instance_variable_get(:@configuration)

configuration.backtrace_cleaner.inclusion_patterns = []
configuration.output_stream = out
configuration.deprecation_stream = err

command_line.run(err, out)
runner.run(err, out)

html = out.string.gsub(/\d+\.\d+(s| seconds)/, "n.nnnn\\1")

Expand Down
6 changes: 3 additions & 3 deletions spec/rspec/core/formatters/text_mate_formatter_spec.rb
Expand Up @@ -23,14 +23,14 @@ module Formatters
err.set_encoding("utf-8") if err.respond_to?(:set_encoding)
out.set_encoding("utf-8") if out.respond_to?(:set_encoding)

command_line = RSpec::Core::CommandLine.new(options)
configuration = command_line.instance_variable_get(:@configuration)
runner = RSpec::Core::Runner.new(options)
configuration = runner.instance_variable_get(:@configuration)

configuration.backtrace_cleaner.inclusion_patterns = []
configuration.output_stream = out
configuration.deprecation_stream = err

command_line.run(err, out)
runner.run(err, out)
html = out.string.gsub(/\d+\.\d+(s| seconds)/, "n.nnnn\\1")

actual_doc = Nokogiri::HTML(html)
Expand Down
6 changes: 3 additions & 3 deletions spec/rspec/core/runner_spec.rb
Expand Up @@ -67,11 +67,11 @@ def run_specs
run_specs
end

it "builds a CommandLine and runs the specs" do
process_proxy = double(RSpec::Core::CommandLine, :run => 0)
it "builds a Runner and runs the specs" do
process_proxy = double(RSpec::Core::Runner, :run => 0)
process_proxy.should_receive(:run).with(err, out)

RSpec::Core::CommandLine.should_receive(:new).and_return(process_proxy)
RSpec::Core::Runner.should_receive(:new).and_return(process_proxy)

run_specs
end
Expand Down

0 comments on commit 35e6e01

Please sign in to comment.