Permalink
Browse files

rework color-support to work on rspec1 and rspec2

  • Loading branch information...
1 parent 44fb8fe commit 6dd8b031970651b3f859a53bd0944e5839f6f91c @grosser grosser committed Apr 17, 2011
Showing with 38 additions and 20 deletions.
  1. +18 −14 lib/parallel_specs.rb
  2. +20 −6 spec/parallel_specs_spec.rb
View
@@ -2,8 +2,9 @@
class ParallelSpecs < ParallelTests
def self.run_tests(test_files, process_number, options)
- exe = executable # its expensive with bundler, so do not call it twice
- cmd = "#{color} #{exe} #{options} #{spec_opts(exe)} #{test_files*' '}"
+ exe = executable # expensive, so we cache
+ version = (exe =~ /\brspec\b/ ? 2 : 1)
+ cmd = "#{rspec_1_color if version == 1}#{exe} #{options} #{rspec_2_color if version == 2}#{spec_opts(version)} #{test_files*' '}"
execute_command(cmd, process_number)
end
@@ -26,20 +27,23 @@ def self.run(cmd)
`#{cmd}`
end
- def self.spec_opts(executable)
- opts = ['spec/parallel_spec.opts', 'spec/spec.opts'].detect{|f| File.file?(f) }
- return unless opts
- if executable =~ /\brspec\b/
- # RSpec2 does not handle -O, so we inline the options
- File.read(opts).tr("\n", ' ')
- else
- "-O #{opts}"
- end
+ def self.rspec_1_color
+ 'RSPEC_COLOR=1 ; export RSPEC_COLOR ;' if $stdout.tty?
+ end
+
+ def self.rspec_2_color
+ '--tty ' if $stdout.tty?
end
- #display color when we are in a terminal
- def self.color
- ($stdout.tty? ? 'RSPEC_COLOR=1 ; export RSPEC_COLOR ;' : '')
+ def self.spec_opts(rspec_version)
+ options_file = ['spec/parallel_spec.opts', 'spec/spec.opts'].detect{|f| File.file?(f) }
+ return unless options_file
+ if rspec_version == 2
+ # does not handle -O, so we inline the options
+ File.read(options_file).tr("\n", ' ')
+ else
+ "-O #{options_file}"
+ end
end
def self.test_suffix
@@ -22,13 +22,27 @@
end
it "runs with color when called from cmdline" do
- ParallelSpecs.should_receive(:open).with{|x,y| x=~/RSPEC_COLOR=1/}.and_return mocked_process
+ ParallelSpecs.should_receive(:open).with{|x,y| x=~/ --tty /}.and_return mocked_process
$stdout.should_receive(:tty?).and_return true
ParallelSpecs.run_tests(['xxx'],1,'')
end
it "runs without color when not called from cmdline" do
- ParallelSpecs.should_receive(:open).with{|x,y| x !~ /RSPEC_COLOR/}.and_return mocked_process
+ ParallelSpecs.should_receive(:open).with{|x,y| x !~ / --tty /}.and_return mocked_process
+ $stdout.should_receive(:tty?).and_return false
+ ParallelSpecs.run_tests(['xxx'],1,'')
+ end
+
+ it "runs with color for rspec 1 when called for the cmdline" do
+ File.should_receive(:file?).with('script/spec').and_return true
+ ParallelSpecs.should_receive(:open).with{|x,y| x=~/ RSPEC_COLOR=1 /}.and_return mocked_process
+ $stdout.should_receive(:tty?).and_return true
+ ParallelSpecs.run_tests(['xxx'],1,'')
+ end
+
+ it "runs without color for rspec 1 when not called for the cmdline" do
+ File.should_receive(:file?).with('script/spec').and_return true
+ ParallelSpecs.should_receive(:open).with{|x,y| x !~ / RSPEC_COLOR=1 /}.and_return mocked_process
$stdout.should_receive(:tty?).and_return false
ParallelSpecs.run_tests(['xxx'],1,'')
end
@@ -70,14 +84,14 @@
it "uses -O spec/spec.opts when found (with script/spec)" do
File.stub!(:file?).with('script/spec').and_return true
File.stub!(:file?).with('spec/spec.opts').and_return true
- ParallelSpecs.should_receive(:open).with{|x,y| x =~ %r{script/spec\s+-O spec/spec.opts}}.and_return mocked_process
+ ParallelSpecs.should_receive(:open).with{|x,y| x =~ %r{script/spec\s+ -O spec/spec.opts}}.and_return mocked_process
ParallelSpecs.run_tests(['xxx'],1,'')
end
it "uses -O spec/parallel_spec.opts when found (with script/spec)" do
File.stub!(:file?).with('script/spec').and_return true
File.should_receive(:file?).with('spec/parallel_spec.opts').and_return true
- ParallelSpecs.should_receive(:open).with{|x,y| x =~ %r{script/spec\s+-O spec/parallel_spec.opts}}.and_return mocked_process
+ ParallelSpecs.should_receive(:open).with{|x,y| x =~ %r{script/spec\s+ -O spec/parallel_spec.opts}}.and_return mocked_process
ParallelSpecs.run_tests(['xxx'],1,'')
end
@@ -98,7 +112,7 @@
ParallelSpecs.stub!(:bundler_enabled?).and_return true
ParallelSpecs.stub!(:run).with("bundle show rspec").and_return "/foo/bar/rspec-2.0.2"
- ParallelSpecs.should_receive(:open).with{|x,y| x =~ %r{rspec\s+ --foo --bar}}.and_return mocked_process
+ ParallelSpecs.should_receive(:open).with{|x,y| x =~ %r{rspec\s+ --tty --foo --bar}}.and_return mocked_process
ParallelSpecs.run_tests(['xxx'],1,'')
end
@@ -146,4 +160,4 @@
ParallelSpecs.find_results(output).should == ['0 examples, 0 failures, 0 pending','1 examples, 1 failures, 1 pending']
end
end
-end
+end

0 comments on commit 6dd8b03

Please sign in to comment.