1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
require "spec_helper"
require "stringio"
require 'tmpdir'
module RSpec::Core
describe CommandLine do
let(:out) { StringIO.new }
let(:err) { StringIO.new }
let(:config) { RSpec::configuration }
let(:world) { RSpec::world }
before { config.stub :run_hook }
it "configures streams before command line options" do
config.stub :load_spec_files
old_env = ENV['SPEC_OPTS']
ENV['SPEC_OPTS'] = '--default_path spec'
# this is necessary to ensure that color works correctly on windows
config.should_receive(:error_stream=).ordered
config.should_receive(:output_stream=).ordered
config.should_receive(:force).at_least(:once).ordered
command_line = build_command_line
command_line.run err, out
ENV['SPEC_OPTS'] = old_env
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)
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)
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
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
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
end
end
context "running hooks" do
before { config.stub :load_spec_files }
it "runs before suite hooks" do
config.should_receive(:run_hook).with(:before, :suite)
command_line = build_command_line
command_line.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
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
end.to raise_error
end
end
end
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') }
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
end
end
def build_command_line *args
CommandLine.new build_config_options(*args)
end
def build_config_options *args
options = ConfigurationOptions.new args
options.parse_options
options
end
end
end
|