diff --git a/.rubocop_todo/rspec/missing_feature_category.yml b/.rubocop_todo/rspec/missing_feature_category.yml index 0392c1feb40e17443a5dbfce3408443bb5f00c1a..bc050a40a737f57362e2972d5680e6c864ea1564 100644 --- a/.rubocop_todo/rspec/missing_feature_category.yml +++ b/.rubocop_todo/rspec/missing_feature_category.yml @@ -5565,7 +5565,6 @@ RSpec/MissingFeatureCategory: - 'spec/support_specs/helpers/stub_method_calls_spec.rb' - 'spec/support_specs/matchers/be_sorted_spec.rb' - 'spec/support_specs/matchers/exceed_query_limit_helpers_spec.rb' - - 'spec/support_specs/time_travel_spec.rb' - 'spec/tasks/admin_mode_spec.rb' - 'spec/tasks/config_lint_rake_spec.rb' - 'spec/tasks/dev_rake_spec.rb' diff --git a/Gemfile.lock b/Gemfile.lock index 984bceae521ca26a9e0747e0332c02aef8072abf..364cccdcf643590e130d2b48b509c0c9d1999353 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -16,6 +16,7 @@ PATH remote: gems/gitlab-rspec specs: gitlab-rspec (0.1.0) + activesupport (>= 6.1, < 7.1) rspec (~> 3.0) PATH diff --git a/doc/development/testing_guide/best_practices.md b/doc/development/testing_guide/best_practices.md index f4829a1e2397466058d237bfa77712084618ed46..7785819d1e04f382c4455b1819728fe2bb5e387b 100644 --- a/doc/development/testing_guide/best_practices.md +++ b/doc/development/testing_guide/best_practices.md @@ -1000,7 +1000,7 @@ describe 'specs which require time to be frozen to a specific date and/or time', end ``` -[Under the hood](https://gitlab.com/gitlab-org/gitlab/-/blob/master/spec/support/time_travel.rb), these helpers use the `around(:each)` hook and the block syntax of the +[Under the hood](https://gitlab.com/gitlab-org/gitlab/-/blob/master/gems/gitlab-rspec/lib/gitlab/rspec/configurations/time_travel.rb), these helpers use the `around(:each)` hook and the block syntax of the [`ActiveSupport::Testing::TimeHelpers`](https://api.rubyonrails.org/classes/ActiveSupport/Testing/TimeHelpers.html) methods: diff --git a/gems/gitlab-rspec/.rubocop.yml b/gems/gitlab-rspec/.rubocop.yml index 8c670b439d3eb02d2ce5f3847696991962de7614..a2bdc5735c4d9ae2a8aea956777aa1fdc8d95b4b 100644 --- a/gems/gitlab-rspec/.rubocop.yml +++ b/gems/gitlab-rspec/.rubocop.yml @@ -1,2 +1,14 @@ inherit_from: - ../config/rubocop.yml + +RSpec/InstanceVariable: + Exclude: + - spec/**/*.rb + +Gitlab/ChangeTimezone: + Exclude: + - spec/gitlab/rspec/time_travel_spec.rb + +# FIXME +Gitlab/RSpec/AvoidSetup: + Enabled: false diff --git a/gems/gitlab-rspec/Gemfile.lock b/gems/gitlab-rspec/Gemfile.lock index 56c4b01e76441ac36d31e24fbaa7eedaff3e63e2..dcdb4dd009e8fc316661427154110e4899f19b03 100644 --- a/gems/gitlab-rspec/Gemfile.lock +++ b/gems/gitlab-rspec/Gemfile.lock @@ -2,6 +2,7 @@ PATH remote: . specs: gitlab-rspec (0.1.0) + activesupport (>= 6.1, < 7.1) rspec (~> 3.0) GEM diff --git a/gems/gitlab-rspec/gitlab-rspec.gemspec b/gems/gitlab-rspec/gitlab-rspec.gemspec index 60bc84fbe3620a239d17decf66b90265cf7cff6b..c2c5b6c60b78dcaefcc6cbd92aae8040c4c718ea 100644 --- a/gems/gitlab-rspec/gitlab-rspec.gemspec +++ b/gems/gitlab-rspec/gitlab-rspec.gemspec @@ -18,6 +18,7 @@ Gem::Specification.new do |spec| spec.files = Dir["lib/**/*.rb"] spec.require_paths = ["lib"] + spec.add_runtime_dependency "activesupport", ">= 6.1", "< 7.1" spec.add_runtime_dependency "rspec", "~> 3.0" spec.add_development_dependency "factory_bot_rails", "~> 6.2.0" diff --git a/gems/gitlab-rspec/lib/gitlab/rspec/all.rb b/gems/gitlab-rspec/lib/gitlab/rspec/all.rb index 47beb70d23e7b09038a239af4051c96241d8719d..091d2ba02879ef6c6de9e1aa7f97216cb14ebc97 100644 --- a/gems/gitlab-rspec/lib/gitlab/rspec/all.rb +++ b/gems/gitlab-rspec/lib/gitlab/rspec/all.rb @@ -2,3 +2,7 @@ require_relative "../rspec" require_relative "stub_env" + +require_relative "configurations/time_travel" + +Gitlab::Rspec::Configurations::TimeTravel.configure! diff --git a/gems/gitlab-rspec/lib/gitlab/rspec/configurations/time_travel.rb b/gems/gitlab-rspec/lib/gitlab/rspec/configurations/time_travel.rb new file mode 100644 index 0000000000000000000000000000000000000000..b30aa1cde0d84c8e0afb8181dcca47cf4bb154d0 --- /dev/null +++ b/gems/gitlab-rspec/lib/gitlab/rspec/configurations/time_travel.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'active_support/all' +require 'active_support/testing/time_helpers' + +module Gitlab + module Rspec + module Configurations + class TimeTravel + def self.configure! + RSpec.configure do |config| + config.include ActiveSupport::Testing::TimeHelpers + + config.around(:example, :freeze_time) do |example| + freeze_time { example.run } + end + + config.around(:example, :time_travel_to) do |example| + date_or_time = example.metadata[:time_travel_to] + + unless date_or_time.respond_to?(:to_time) && date_or_time.to_time.present? + raise 'The time_travel_to RSpec metadata must have a Date or Time value.' + end + + travel_to(date_or_time) { example.run } + end + end + end + end + end + end +end diff --git a/spec/support_specs/time_travel_spec.rb b/gems/gitlab-rspec/spec/gitlab/rspec/time_travel_spec.rb similarity index 78% rename from spec/support_specs/time_travel_spec.rb rename to gems/gitlab-rspec/spec/gitlab/rspec/time_travel_spec.rb index 8fa51c0c1f0e27a3393d864d35b1d1abe516e4ef..79804a99f70766cefc49c45bd1d0d127843a99aa 100644 --- a/spec/support_specs/time_travel_spec.rb +++ b/gems/gitlab-rspec/spec/gitlab/rspec/time_travel_spec.rb @@ -1,8 +1,15 @@ # frozen_string_literal: true -require 'spec_helper' - RSpec.describe 'time travel' do + before(:all) do + @original_time_zone = Time.zone + Time.zone = 'Eastern Time (US & Canada)' + end + + after(:all) do + Time.zone = @original_time_zone + end + describe ':freeze_time' do it 'freezes time around a spec example', :freeze_time do expect { sleep 0.1 }.not_to change { Time.now.to_f } diff --git a/spec/support/rspec_order_todo.yml b/spec/support/rspec_order_todo.yml index 0dda40d30f8dd715cab868c44a9d22077c0afe59..0f91ff5f12cf903699559853a8c08401e9080718 100644 --- a/spec/support/rspec_order_todo.yml +++ b/spec/support/rspec_order_todo.yml @@ -9680,7 +9680,6 @@ - './spec/support_specs/helpers/stub_method_calls_spec.rb' - './spec/support_specs/matchers/be_sorted_spec.rb' - './spec/support_specs/matchers/exceed_query_limit_helpers_spec.rb' -- './spec/support_specs/time_travel_spec.rb' - './spec/tasks/admin_mode_spec.rb' - './spec/tasks/cache/clear/redis_spec.rb' - './spec/tasks/config_lint_spec.rb' diff --git a/spec/support/time_travel.rb b/spec/support/time_travel.rb deleted file mode 100644 index 9dfbfd205242f1fbd97ad7529435ee1653666ad7..0000000000000000000000000000000000000000 --- a/spec/support/time_travel.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -require 'active_support/testing/time_helpers' - -RSpec.configure do |config| - config.include ActiveSupport::Testing::TimeHelpers - - config.around(:example, :freeze_time) do |example| - freeze_time { example.run } - end - - config.around(:example, :time_travel_to) do |example| - date_or_time = example.metadata[:time_travel_to] - - unless date_or_time.respond_to?(:to_time) && date_or_time.to_time.present? - raise 'The time_travel_to RSpec metadata must have a Date or Time value.' - end - - travel_to(date_or_time) { example.run } - end -end diff --git a/spec/tooling/rspec_flaky/flaky_example_spec.rb b/spec/tooling/rspec_flaky/flaky_example_spec.rb index 68e40bc1050300390773b9f4d196a9541a96cfb0..c100756d7342665586ba11522840ccfd9b0adefe 100644 --- a/spec/tooling/rspec_flaky/flaky_example_spec.rb +++ b/spec/tooling/rspec_flaky/flaky_example_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'gitlab/rspec/all' -require_relative '../../support/time_travel' require_relative '../../../tooling/rspec_flaky/flaky_example' diff --git a/spec/tooling/rspec_flaky/flaky_examples_collection_spec.rb b/spec/tooling/rspec_flaky/flaky_examples_collection_spec.rb index 9d75c97febe2afb581f77fbaa030c7b078c0cc02..8304bcb426ea7ef10ee306ecbc4c6482cf413daa 100644 --- a/spec/tooling/rspec_flaky/flaky_examples_collection_spec.rb +++ b/spec/tooling/rspec_flaky/flaky_examples_collection_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require_relative '../../support/time_travel' +require 'gitlab/rspec/all' require_relative '../../../tooling/rspec_flaky/flaky_examples_collection' diff --git a/spec/tooling/rspec_flaky/listener_spec.rb b/spec/tooling/rspec_flaky/listener_spec.rb index c0b7140334795c40959207f75468a1bfa0bdc7bd..d1ed84dad76415e3a47cd72cea67178988af16ca 100644 --- a/spec/tooling/rspec_flaky/listener_spec.rb +++ b/spec/tooling/rspec_flaky/listener_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'gitlab/rspec/all' -require_relative '../../support/time_travel' require_relative '../../../tooling/rspec_flaky/listener' diff --git a/spec/tooling/rspec_flaky/report_spec.rb b/spec/tooling/rspec_flaky/report_spec.rb index e7365c1e150d1a60afb9944bb1f228cceee74960..543a69cd8ee1379f476eb0e4e93edfa4dd7b2ff9 100644 --- a/spec/tooling/rspec_flaky/report_spec.rb +++ b/spec/tooling/rspec_flaky/report_spec.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true require 'tempfile' - -require_relative '../../support/time_travel' +require 'gitlab/rspec/all' require_relative '../../../tooling/rspec_flaky/report'