160

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).

skaffman
  • 398,947
  • 96
  • 818
  • 769
Evolve
  • 8,939
  • 12
  • 51
  • 63

6 Answers6

242

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 
Nakilon
  • 34,866
  • 14
  • 107
  • 142
abyx
  • 69,862
  • 18
  • 95
  • 117
154

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.

Shamaoke
  • 6,222
  • 8
  • 34
  • 40
  • 1
    The original question asks for a global configuration, this is a project-specific one. Useful, but not the correct answer, unlike abyx's which points to the `.rspec` file. – Olivier Lacan Jun 19 '12 at 00:11
  • 8
    A search for info about the RSpec config and formatters brings one to this page, so I appreciated this answer, even if it was for the wrong question :-) – phatmann Aug 01 '12 at 12:01
  • 3
    `color_enabled` is now `color` – elado Jul 11 '14 at 02:48
12

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.

Christoph Petschnig
  • 4,047
  • 1
  • 37
  • 46
  • Hi thank you - would you be able to elaborate on the following: "You then must require in each *_spec.rb file that should use that option." i don't understand. – BenKoshy May 11 '16 at 01:26
  • Each of your *_spec.rb file usually starts with require "spec_helper", you should not forget this. – Christoph Petschnig May 11 '16 at 07:00
6

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

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

fernyb
  • 973
  • 1
  • 8
  • 11
2

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

zzeroo
  • 5,466
  • 4
  • 33
  • 49
  • 5
    This solution is not very portable. Correct answer is @abyx with using `.rspec`, as when its checked in with the project, anyone else getting it will get the same settings. – Ian Vaughan Dec 11 '11 at 21:35
  • 1
    But the question was about "global configuration for RSpec in Ubuntu", "across all my projects" not portable nor coop mode. – zzeroo Dec 21 '11 at 18:20
  • 3
    Humm, I guess your right, your answer does relate directly to the OP. I was thinking bigger picture, but I still think the better answer was @abyx, if the OP gets used to config projects correctly then others will benefit, maybe not now as he might be workin solo, but good practices are just that. Sorry, bit ranty, just my way of thinking. – Ian Vaughan Dec 24 '11 at 08:13
  • 1
    @zzeroo @Ian : Do note that putting the `.rspec` file in the user's home directory (as I mentioned in my answer) works globally for all of the user's invocations of rspec. That is in fact more solid than using aliases, as some gems/other aliases/tools the user might use would not necessarily use the alias – abyx Apr 29 '12 at 07:22
  • 1
    Now coming back to this question and being a little older and wiser, I would agree that setting the .rspec dot file is now a better option. Updating my approved answer to abyx, thanks so much zzero for answering my question best back in 2010. :) – Evolve Jan 18 '13 at 23:31
1

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.

Leif
  • 1,129
  • 1
  • 12
  • 18