How to Enable Colors with Rspec When Using Jruby or Bundle Exec

How to enable colors with rspec when using JRuby or bundle exec?

Adding --tty to the call fixes the problem for me:

jruby -S bundle exec rspec --tty --color spec/foo_spec.rb

The option tells RSpec that the output is not written to a file (in which case you wouldn't want colorized output), but send to another process instead.

How do I get the color from the backticks command in ruby?

From reading the code it looks like RSpec calls IO#isatty on the output stream to decide whether or not to colorize the output. The backquote method must work differently from system in this respect.

EDIT

This works if you add the option --tty to the rspec command:

`rspec --color --tty file_spec.rb`

as mentioned in this SO question.

How do I keep the *colorized* output result of a command when printing the result of a command in Ruby?

Take a look at this and this questions, they are about just the same thing but with rspec instead of cucumber.

How do I get colour with Windows command prompt using RSpec in Ruby?

UPDATE:
Win32Console no longer works with rspec. ANSICON recommended.
https://github.com/rspec/rspec-rails/issues/487#issuecomment-3556806

Using guard with JRuby on Windows fails

I think it's a bundler bug as I get the same error if I run:

bundle exec rspec spec

I manage to make it work commenting and adding some code to guard-rspec source file.

Open file:

C:\jruby-1.6.7\lib\ruby\gems\1.8\gems\guard-rspec-0.6.0\lib\guard\rspec\runner.rb

Obs: Note that this path might be diferent on you machine. Anyway, just go to the source of the guard-rspec gem and open the runner.rb file.

And change the rspec_command to this:

  def rspec_command(paths, options={})
warn_deprectation(options)

cmd_parts = []
cmd_parts << "rvm #{options[:rvm].join(',')} exec" if options[:rvm].is_a?(Array)
cmd_parts << "bundle exec" if (bundler? && options[:binstubs] == true && options[:bundler] != false) || (bundler? && options[:bundler] != false)
cmd_parts << "'"
cmd_parts << rspec_exec(options)
cmd_parts << options[:cli] if options[:cli]
cmd_parts << "-f progress" if options[:cli].nil? || !options[:cli].split(/[\s=]/).any? { |w| %w[-f --format].include?(w) }
#cmd_parts << "-r #{File.dirname(__FILE__)}/formatters/notification_#{rspec_class.downcase}.rb -f Guard::RSpec::Formatter::Notification#{rspec_class}#{rspec_version == 1 ? ":" : " --out "}/dev/null" if options[:notification] != false
cmd_parts << "--failure-exit-code #{failure_exit_code}" if failure_exit_code_supported?(options)

cmd_parts << paths.join(' ')
cmd_parts << "'"

cmd_parts.join(' ')
end

The changes I made:

 1. commented on of the lines
2. Added 2 code line to add "quotes" to the string builder.

It works here, so it should work on your machine as well.
I open an issue on bundler https://github.com/carlhuda/bundler/issues/1689 for the issue.



Related Topics



Leave a reply



Submit