-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It checks that feature specs do not use `sleep` greater than 1 second.
- Loading branch information
1 parent
0311949
commit 6d55c22
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module OpenProject | ||
# Checks that feature specs do not use `sleep` greater than 1 second. | ||
# | ||
# Relying on `sleep` for synchronization reduces overall performance of | ||
# the test suite. Consider using Capybara `have_*` matchers or | ||
# rspec-wait `wait_for` method instead. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# sleep 20 | ||
# | ||
# # bad | ||
# sleep 1.5 | ||
# | ||
# # bad | ||
# delay = 15 | ||
# sleep delay | ||
# | ||
# # good (use sparingly) | ||
# sleep 1 | ||
# | ||
# # good | ||
# expect(page).not_to have_text("please wait") | ||
# | ||
# # good | ||
# expect(page).to have_text("success") | ||
# | ||
# good | ||
# wait_for { work_package.reload.subject }.to eq("Updated name") | ||
class NoSleepInFeatureSpecs < Base | ||
MSG = "Avoid using `sleep` greater than 1 second in feature specs. " \ | ||
"It will reduce overall performance of the test suite. " \ | ||
"Consider using Capybara `have_*` matchers or rspec-wait " \ | ||
"`wait_for` method instead." | ||
|
||
def_node_matcher :sleep_call?, "(send nil? :sleep $...)" | ||
|
||
def on_send(node) | ||
return unless feature_spec?(processed_source) | ||
|
||
sleep_call?(node) do |args| | ||
add_offense(node, message: MSG) if sleeping_too_much?(args[0]) | ||
end | ||
end | ||
|
||
private | ||
|
||
def sleeping_too_much?(arg) | ||
return false if arg&.numeric_type? && arg.value.between?(0, 1) | ||
|
||
true | ||
end | ||
|
||
def feature_spec?(source) | ||
source.file_path.include?("_spec.rb") && source.file_path.include?("features/") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
spec/rubocop/cop/open_project/no_sleep_in_feature_specs_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::OpenProject::NoSleepInFeatureSpecs, :config do | ||
let(:config) { RuboCop::Config.new } | ||
|
||
it "registers an offense for sleeping more than 1 second in a feature spec" do | ||
expect_offense(<<~RUBY, "spec/features/some_spec.rb") | ||
sleep 1.5 | ||
^^^^^^^^^ OpenProject/NoSleepInFeatureSpecs: #{described_class::MSG} | ||
sleep 20 | ||
^^^^^^^^ OpenProject/NoSleepInFeatureSpecs: #{described_class::MSG} | ||
sleep | ||
^^^^^ OpenProject/NoSleepInFeatureSpecs: #{described_class::MSG} | ||
RUBY | ||
|
||
expect_no_corrections | ||
end | ||
|
||
it "does not register an offense in non-feature specs" do | ||
expect_no_offenses(<<~RUBY, "spec/some_spec.rb") | ||
sleep 1.5 | ||
RUBY | ||
end | ||
|
||
it "registers an offense sleep is called with a non-numeric argument" do | ||
expect_offense(<<~RUBY, "spec/features/some_spec.rb") | ||
delay = 15 | ||
sleep delay | ||
^^^^^^^^^^^ OpenProject/NoSleepInFeatureSpecs: #{described_class::MSG} | ||
RUBY | ||
|
||
expect_no_corrections | ||
end | ||
end |