How do I globally configure RSpec to keep the '--color' and '--format specdoc' options turned on

RubyColorsRspecConfiguration Files

Ruby Problem Overview


How do I set global configuration for RSpec in Ubuntu.

Specifically so, --color and --format specdoc stay turned on, across all my projects (ie every time I run rspec anywhere).

Ruby Solutions


Solution 1 - Ruby

As you can see in the docs here, the intended use is creating ~/.rspec and in it putting your options, such as --color.

To quickly create an ~/.rspec file with the --color option, just run:

echo '--color' >> ~/.rspec 

Solution 2 - Ruby

One can also use a spec_helper.rb file in all projects. The file should include the following:

RSpec.configure do |config|
  # Use color in STDOUT
  config.color = true

  # Use color not only in STDOUT but also in pagers and files
  config.tty = true

  # Use the specified formatter
  config.formatter = :documentation # :progress, :html,
                                    # :json, CustomFormatterClass
end

Any example file must require the helper to be able to use that options.

Solution 3 - Ruby

In your spec_helper.rb file, include the following option:

RSpec.configure do |config|
  config.color_enabled = true
end

You then must require in each *_spec.rb file that should use that option.

Solution 4 - Ruby

If you use rake to run rspec tests then you can edit spec/spec.opts

http://rspec.info/rails/runners.html

Solution 5 - Ruby

Or simply add alias spec=spec --color --format specdoc to your ~/.bashrc file like me.

Solution 6 - Ruby

One thing to be aware of is the impact of the different ways of running RSpec.

I was trying to turn on the option with the following code in spec/spec_helper.rb -

Rspec.configure do |config|
  config.tty = $stdout.tty?
end
  1. calling the 'rspec' binary directly - or as 'bundle exec rspec' and checking $stdout.tty? will return true.
  2. invoking the 'rake spec' task - or as 'bundle exec rake spec' - Rake will invoke rspec in a separate process, and $stdout.tty? will return false.

In the end I used the ~/.rspec option, with just --tty as its contents. Works well for me and keeps our CI server output clean.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEvolveView Question on Stackoverflow
Solution 1 - RubyabyxView Answer on Stackoverflow
Solution 2 - RubyShamaokeView Answer on Stackoverflow
Solution 3 - RubyChristoph PetschnigView Answer on Stackoverflow
Solution 4 - RubyfernybView Answer on Stackoverflow
Solution 5 - RubyzzerooView Answer on Stackoverflow
Solution 6 - RubyLeifView Answer on Stackoverflow