Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off warnings, 'ruby -w', in 'rake test'?

I have inherited a huge Rails project with hundreds of warnings, due to other people's sloppy coding habits, none of which I can fix.

When I run an individual test suite, with ruby test/function/my_controller_test.rb, I get a clean run with no warnings. But when I run rake test, something deep in Rails's rake tasks turns on ruby -w, activating Ruby's warning system. How do I deactivate that line? I will edit the source and erase the -w if I must, but where is it?

The answer is not rake -q - that turns off rake's own spew.

like image 924
Phlip Avatar asked Mar 03 '18 21:03

Phlip


People also ask

How do I run a rake test from the command line?

If rake is invoked with a “TEST=filename” command line option, then the list of test files will be overridden to include only the filename specified on the command line. This provides an easy way to run just one test.

How to manage warnings in Ruby?

But proper way to manage warnings is to fix them and updating the gems which fix them. Want to know more about Ruby 2.7? Subscribe to my mailing list. I am looking for remote work related to Ruby/Rails.

How to silence deprecation warnings in Ruby on rails?

Even running rails server prints all of these warnings before showing actual output. Fortunately, we can silence these deprecation warnings easily. Along with introducing lot of deprecation warnings, Ruby 2.7 has also provided a way out by enhancing the command line W flag.

Can a rake task be unit tested?

A rake task is just Ruby code. Therefore it can be unit tested just like any other Ruby code. (It can even be developed in a test-first way. Just saying.)


2 Answers

If you want to suppress this you can add this to your shell config if you're using a POSIX-type shell:

export RUBYOPT=-W0

Or you can prepend that to any command:

RUBYOPT=-W0 ruby ...

For other shells you'll need to set that environment variable somehow.

like image 101
tadman Avatar answered Sep 20 '22 15:09

tadman


I'm sure this has long been resolved, but for any future folks finding this, there is a clean way to do with with in the rakefile. Just add t.warning = false to your raketask config. So for example, a super generic one would be:

Rake::TestTask.new do |t|
  t.libs << "test"
  t.test_files = FileList['source_code/*_test.rb']
  t.verbose = true
  t.warning = false 
end

Fixing the warnings is still the best solution, but if needed, here ya go :)

like image 25
jenn Avatar answered Sep 16 '22 15:09

jenn