Testing Rake tasks that contain "exit"
Description
We have Rake tasks that contain calls to exit when some conditions fail. When testing these Rake tasks there's a risk of incorrectly reporting errors.
Example
- Apply this change:
diff --git a/lib/tasks/gitlab/ldap.rake b/lib/tasks/gitlab/ldap.rake
index c66a2a2..b73ffc2 100644
--- a/lib/tasks/gitlab/ldap.rake
+++ b/lib/tasks/gitlab/ldap.rake
@@ -8,7 +8,7 @@ namespace :gitlab do
prompt('What is the new provider ID? Ex. \'ldapcustom\': '.color(:blue))
puts '' # Add some separation in the output
- identities = Identity.where(provider: old_provider)
+ identities = Identity.where(provider: new_provider)
identity_count = identities.count
if identities.empty?
- Run
rspec spec/tasks/gitlab/ldap_rake_spec.rb spec/models/note_spec.rb
The output will be:
0/88 |> | ETA: ??:??:??
Finished in 1.42 seconds (files took 18.02 seconds to load)
1 example, 0 failures
That's because the call to exit in ldap.rake causes RSpec to exit. Fortunately (for CI) the exit status is still non-zero, but the output is confusing.
This was originally reported by @dbernheisel in https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/8178#note_28987190.