Skipping specific rspec tests in guard - ruby-on-rails

I'm trying to get the guard to skip some tests (those that require selenium to run).
I've added browser_required tag to them and running rspec with "--tag ~browser_required" filters them out.
However I can't get guard not to run them, I've set :cli => "--tag ~browser_required" in guardfile. This is my full guard file http://pastebin.com/pGuWAQm8

In your gist it does have two rspec blocks (one with cli and another without). Perhaps deleting the second one will at least get this working.

Check my config: https://github.com/lucassus/locomotive/blob/master/Guardfile
You could pass cli arguments to rspec command and using --filter options you could reject some specs
guard 'rspec', :version => 2, :cli => "--drb --color --tag ~slow:true" {}
In your example you can use --tag ~js:true

The cli option is deprecated in Rspec 3. Use cmd instead.
For example:
guard :rspec, cmd: "bundle exec rspec --color --tag ~speed:slow", failed_mode: :focus do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)
# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)
watch(%r{\Aapp/(.+)\.rb\z}) { |m| "spec/#{m[1]}_spec.rb" }
end
Read more on Rspec tags here

Related

rubocop fail level and failing build

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

Can I set rspec --format documentation as the default?

Per the Rspec documentation, by default when you run rspec you get the progress formatter (looks like this: ".....").
There is another formatting option rspec --format documentation that goes through each test one by one. My question: how can I enable --format documentation by default without having to type it in the command line every time?
Option 1
Add it to .rspec file (or create one in the project's root directory) - options added to it will be applied to every test run within current project:
# .rspec
--color
--format documentation
Option 2
Add it to RSpec.configure block:
RSpec.configure do |config|
config.formatter = :documentation
end
Option 3
Specify rspec's options globally by adding them to ~/.rspec.
RSpec.configure do |config|
config.color = true
config.formatter = :documentation
config.order = 'default'
end
You can create your own personal RSpec settings by creating a ~/.rspec file:
--color
--format documentation
The project .rspec file should only contain the minimum settings required to run the spec suite to avoid developer wars.

RSpec is Freezing

I have rspec configured installed in my rails app. It was working fine (We are just experimenting with rspec so there's only 2 tests).
They were working fine. Now rspec is freezing when it's going to perform a test using database.
I just freezes. I don't even know were to start looking because there's no error in the output.
Is there a verbose or debugging mode for rspec or someone ever faced this?
I've tried -b but it freezes before can give an error.
Output: (Rspec is configured with --format documentation)
[leandro#machine:~] rspec
User
Than, that's it. It hangs. I has to reset manually the computer twice.
This is the user_spec.rb
require 'spec_helper'
describe User do
let(:user) { User.create({
username: "teste",
email: "teste#teste.com",
name: "Teste",
phone: "123456789",
password: "123456",
notes: "Teste"
}) }
subject { user }
it "is invalid without a username" do
user.username = nil
is_expected.not_to be_valid
end
end
And my spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.color = true
config.tty = true
config.formatter = :documentation #:progress, :html, :textmate
config.expect_with :rspec do |c|
c.syntax = :expect
end
end
SOLUTION
It turns out that delayed_job_active_record gem was causing the hanging.
I don't know why, but as #Greg Malcolm I looked into the log/teste.log and rspec was feeezing right after createing the delayed jobs database and setting up a new user.
I've restricted the gem just for development and production enviroment, and it worked!
I haven't heard of a rake like verbose feature for rspec. But it's probably more useful to look through log/test.log and see what shows up there. It shows database activity which is part of a freeze effect.
You might also want to rails console into test and make sure your database connection is working right.
You have to check the ./log/test.log file. As this file tends to grow big, use the tail command to read the last n lines.
For example, to read the last 5 lines of the log file, execute the following command:
tail -5 ./log/test.log
In my case, my Google Pub/Sub emulator was hanging.
[ActiveJob] Failing with status #<struct Struct::Status code=4, details="Deadline Exceeded", metadata={}>
[ActiveJob] calling pubsub.googleapis.com:443:/google.pubsub.v1.Publisher/GetTopic
It might also be helpful to use tail -f ./log/test.log, to keep track of the logs while RSpec is running. You can read more about it on "Tail Your Test Logs", by thoughtbot.
It's pretty old question but in case anybody else will get this. I've got the same strange Rspec behaviour today because of left --drb option in project .rspec file after removing DRb server (spork or zeus). Just make sure Rspec have DRb disabled if you don't have spork or zeus installed.

Guard: how to run specific tags from w/in Guard's console?

I'm using Spork and Guard in my RSpec test suite. I'm excluding slow tests from running with:
RSpec.configure do |config|
...
config.filter_run_excluding slow: true
...
end
Then when I need to I run the slow tests in a separate shell with: $ rspec . --tag slow
I'm wondering if there's a shortcut to run the slow tags in same shell that Guard is auto-running its tests in?
There's a console > prompt? And after looking at documentation I find that typing >. rspec . --tag slow works...but that's just a little more verbose than switching to another shell. Seems like this would be a fairly common request. Ideas?
You can define groups and have different rspec configurations in each group.
Append the code below to the contents of /Guardfile:
scope group: :fast
group :fast do
guard 'rspec', cli: '--tag ~slow' do
# code for watching
end
end
group :slow do
guard 'rspec', cli: '--tag slow' do
# code for watching
end
end
When you start Guard, it defaults to the fast specs:
$ guard
21:56:35 - INFO - Guard::RSpec is running
21:56:35 - INFO - Guard is now watching at '/Users/michi/testproject'
[1] {Fast} guard(main)>
Pressing enter will run all fast specs:
22:02:00 - INFO - Run Fast
22:02:00 - INFO - Running all specs
Run options: exclude {:slow=>true}
Now you can run just all the slow ones by pressing slow:
[2] {Fast} guard(main)> slow
22:02:50 - INFO - Run Slow
22:02:50 - INFO - Running all specs
Run options: include {:slow=>true}
You can also switch the scope to the slow specs and run them all by pressing enter:
[3] {Fast} guard(main)> scope slow
[4] {Slow} guard(main)>
22:03:30 - INFO - Run Slow
22:03:30 - INFO - Running all specs
Run options: include {:slow=>true}
Hope that helps!
This code will run all the test that are tagged "fast" inside the files we are watching.
guard 'rspec', :version => 2, :cli => "--tag ~fast" do
# code for watching
end
You need to use cli option to run only the tests you need.

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