rubocop fail level and failing build - ruby-on-rails

I have set up Rubocop in my CI Bamboo build, however since there are offenses detected it has an Exit status of 1 which fails this task.
Since I am generating a Rubocop HTML report in the ci task I want the task to pass.
How can I stop rubocop from failing the task, is it something to do with:
--fail-level

in the end:
'--fail-level F'
worked as a rubocop flag, so it will only fail if there are fatal errors. The docs are not clear on this.

If you want to ignore rubocop exit code and continue your CI pipeline you should || true.
So if you are running rubocop like this:
$ rubocop
Then change it to:
$ rubocop || true
which will return exit code 0 and won't stop your pipeline.

I've added this spec to my CI to check Syntax only.
require 'spec_helper'
RSpec.describe 'rubocop analysis' do
subject(:report) { `rubocop --only Lint/Syntax` }
it 'has no offenses' do
expect(report).to match(/no\ offenses\ detected/)
end
end

Related

Rails 6 Tasks with arguments

// This is a question for Ruby on Rails
I have a Rails Tasks that should take arguments from command line.
Looks like this :
task update: :environment do
if (ARGV[1] == "DEBUG")
DEBUG = true
else
DEBUG = false
end
Now I can run the command line :
rails call:update DEBUG
and it works !
But after the task was finished I got also the message :
rails aborted!
Don't know how to build task 'DEBUG' (See the list of available tasks with `rails --tasks`)
I looking already around the community, but all what I found was quite old and seems not to be compatible with Rails 6 ! So thats the reason why I asking here.
I try with the code you shared above I got the same error of rake abort.
its Comming because they way you calling the rake task with the argument it assume the DEBUG is another task so it try to find and give this error.
Here is proper way of doing it
I did some modification in the code to get rid of the error use the following code it will work perfectly
task :update, [:value] => [:environment] do |t, args|
if (args[:value] == "DEBUG")
DEBUG = true
p "Value is True"
else
DEBUG = false
p "Value is False"
end
end
it works perfectly
You can also put this task in the namespace and then call it from there as well
namespace :debug_task do
# task code here what I mentioned above
end
and then call it to form the terminal like this
rake debug_task:update["DEBUG"]

rake aborted! Don't know how to build task 'sandbox'

I'm trying to add the sandbox to my rails spree application and have run into this error
(using windows 8/powershell with Rails 4.1.6). I'm going by this manual: https://github.com/spree/spree/issues/411
This link Use older version of Rake
seems to have a similar issue but I am not sure how to take the necessary steps to achieve it.
When I try:
C:\Ruby193\openUpShop> bundle exec rake sandbox
I get:
rake aborted!
Don't know how to build task 'sandbox'
I'm am new to rails and am still not sure how everything works so a throughout explanation
with step by step instructions would be greatly appreciated! Thanks.
you can use a file sandbox.rb
# use example: rake task:sub_task -- --sandbox
if ARGV.any? {|arg| arg == '--sandbox' }
puts "** << USING SANDBOX!! >> **"
# beginning
$sandbox = -> do
ActiveRecord::Base.connection.begin_transaction
end
# end
at_exit do
ActiveRecord::Base.connection.rollback_transaction
end
end
then only you need add at the beginning of your task.rake file
require_relative 'path_to_your/sandbox.rb'
..and add at the beggining of your task code
desc "description task"
task example_task: :environment do
$sandbox.call if $sandbox.present?
...

Rails extending `rake test` to incorporate custom behaviour

I've got a bunch of files in test/policies, and I've tried to enhance rake test like so:
# lib/tasks/test.rake
namespace :test do
desc "Test Pundit policies"
Rake::TestTask.new(:policies) do |t|
t.libs << 'test'
t.pattern = 'test/policies/*_test.rb'
end
end
Rake::Task["test"].enhance do
Rake::Task["test:policies"].invoke
end
It works great if I run bin/rake test:all, but bin/rake test now only runs the policy tests, and none of my others.
Can anyone advise what I am doing wrong here? In case it's not clear, I want rake test to run all my tests, just like it used to.
UPDATE
Actually, it's kind of working now, but I've noticed that my functional, unit and integration tests do run now, but only if the policy tests all pass. If any of the policy tests fail, then the rest of my test suite fails to run.
And I don't like the output, notice how there are two blocks of test output below:
[vagrant#vagrant-centos-6-4 vagrant]$ bin/rake test
Run options: --seed 54880
# Running:
.........................
Finished in 0.584530s, 42.7694 runs/s, 56.4556 assertions/s.
25 runs, 33 assertions, 0 failures, 0 errors, 0 skips
Run options: --seed 19900
# Running:
.........................................................
Finished in 4.132299s, 51.0612 runs/s, 128.9839 assertions/s.
I'd prefer if the policy tests output was merged into the same output block as that from my other tests. Any ideas, or is this as good as it's going to get?
Someone else asked the same question and I answered it for them. The solution in my case was to change my code from:
Rake::TestTask.new(:policies) do |t|
to:
Rails::TestTask.new(:policies) do |t|
It just works, and fixes all the issues I was having.

Autotest: "Could not run tests". But nothing in the log

Autotest broke with rspec-rails 2.2.1 update. Now I can't get it working again.. .even after rolling back to rspec-rais 2.2.0.
Only info I'm able to find is a Growl popup saying "App: Could not run tests". Nothing is being written to log/test.log.
this is output in console:
/Users/meltemi/.rvm/rubies/ruby-1.9.2-p0/bin/ruby -I.:lib:test -rubygems -e "%w[test/unit test/test_helper.rb].each { |f| require f }" | unit_diff -u
Loaded suite -e
Started
Finished in 0.003428 seconds.
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 38767
Any ideas where to start troubleshooting this? I'd look in an autotest.log...if i could find one!?!
Edit: tried running autotest with autotest -v and get a looong list of "No tests matched..." in the console (20± examples below):
...
No tests matched .git/objects/fe/41f5cc24d667635c46a533fd09bf7bb514e032
No tests matched .git/objects/fe/4577696b2811818fe7439990be0d2f65a892c5
No tests matched .git/objects/fe/e16b09c5e782035a347ed9b257ff9b2b3fa173
No tests matched .git/refs/heads/MenuModel
No tests matched .git/refs/heads/master
No tests matched .git/refs/remotes/origin/master
No tests matched .git/refs/tags/v0.0.1
No tests matched .git/refs/tags/v0.0.1.1
No tests matched .gitignore
No tests matched .rspec
No tests matched Capfile
No tests matched Gemfile
No tests matched Gemfile.lock
No tests matched README
No tests matched Rakefile
No tests matched app/helpers/application_helper.rb
No tests matched autotest/discover.rb
No tests matched config/application.rb
No tests matched config/boot.rb
No tests matched config/database.yml
No tests matched config/deploy.rb
No tests matched config/environment.rb
...
here's the config (~/.autotest):
# ~/.autotest
# Include plugins
require 'autotest/fsevent'
require 'autotest/growl'
# ./.autotest
# exceptions (files to ignore)
# mappings (which files to specifically monitor for changes)
Autotest.add_hook(:initialize) {|at|
at.add_exception %r{^\.git} # ignore Version Control System
at.add_exception %r{^./tmp} # ignore temp files, lest autotest will run again, and again...
# at.clear_mappings # take out the default (test/test*rb)
at.add_mapping(%r{^lib/.*\.rb$}) {|f, _|
Dir['spec/**/*.rb']
}
nil
}
autotest -s rspec2 tells autotest to use the "rspec2" style, which will make it look for spec files and run the examples therein.
This is just a workaround though. Autotest is supposed to discover the specs based on the entries in your project's ./autotest/discover.rb. And as you found, a recent code update broke the autodiscovery. I ran into this myself and was unable to track down the source.

How can I tell if Rails code is being run via rake or script/generate?

I've got a plugin that is a bit heavy-weight. (Bullet, configured with Growl notifications.) I'd like to not enable it if I'm just running a rake task or a generator, since it's not useful in those situations. Is there any way to tell if that's the case?
It's as simple as that:
if $rails_rake_task
puts 'Guess what, I`m running from Rake'
else
puts 'No; this is not a Rake task'
end
Rails 4+
Instead of $rails_rake_task, use:
File.basename($0) == 'rake'
I like NickMervin's answer better, because it does not depend on the internal implementation of Rake (e.g. on Rake's global variable).
This is even better - no regexp needed
File.split($0).last == 'rake'
File.split() is needed, because somebody could start rake with it's full path, e.g.:
/usr/local/bin/rake taskname
$0 holds the current ruby program being run, so this should work:
$0 =~ /rake$/
It appears that running rake will define a global variable $rakefile, but in my case it gets set to nil; so you're better off just checking if $rakefile has been defined... seeing as __FILE__ and $FILENAME don't get defined to anything special.
$ cat test.rb
puts(global_variables.include? "$rakefile")
puts __FILE__
puts $FILENAME
$ cat Rakefile
task :default do
load 'test.rb'
end
$ ruby test.rb
false
test.rb
-
$ rake
(in /tmp)
true
./test.rb
-
Not sure about script/generator, though.
The most stable option is to add $is_rake = true at the beginning of Rakefile and use it from your code.
Use of $0 or $PROGRAM_NAME sometimes will fail, for example when using spring and checking variables from config/initializers
You can disable the plugin using environment variable:
$ DISABLE_BULLET= 1 rake some:task
And then in your code:
unless ENV['DISABLE_BULLET']
end
We could ask this
Rake.application.top_level_tasks
In a rails application, this is an empty array, whereas in a Rake task, the array has the task name in it.
top_level_tasks probably isn't a public API, so it's subject to changes. But this is the only thing I have found.

Resources