Commit ed0e0329 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'ali/timecop-travel-autocorrection' into 'master'

Enable Rspec/TimecopTravel cop [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!45082
parents c8e6d741 8f6bcd01
......@@ -301,7 +301,7 @@ RSpec/TimecopFreeze:
- 'qa/spec/**/*.rb'
RSpec/TimecopTravel:
Enabled: false
Enabled: true
AutoCorrect: true
Include:
- 'spec/**/*.rb'
......
......@@ -1246,6 +1246,29 @@ RSpec/TimecopFreeze:
- 'spec/workers/concerns/reenqueuer_spec.rb'
- 'spec/workers/metrics/dashboard/prune_old_annotations_worker_spec.rb'
# Offense count: 54
# Cop supports --auto-correct.
RSpec/TimecopTravel:
Exclude:
- 'ee/spec/lib/gitlab/geo/event_gap_tracking_spec.rb'
- 'ee/spec/lib/gitlab/geo/git_push_http_spec.rb'
- 'ee/spec/lib/gitlab/geo/jwt_request_decoder_spec.rb'
- 'ee/spec/lib/gitlab/geo/log_cursor/daemon_spec.rb'
- 'ee/spec/models/broadcast_message_spec.rb'
- 'ee/spec/models/burndown_spec.rb'
- 'qa/spec/support/repeater_spec.rb'
- 'spec/features/users/terms_spec.rb'
- 'spec/lib/feature_spec.rb'
- 'spec/models/broadcast_message_spec.rb'
- 'spec/models/concerns/issuable_spec.rb'
- 'spec/requests/api/ci/runner/jobs_trace_spec.rb'
- 'spec/requests/api/issues/put_projects_issues_spec.rb'
- 'spec/support/shared_contexts/cache_allowed_users_in_namespace_shared_context.rb'
- 'spec/support/shared_examples/requests/api/time_tracking_shared_examples.rb'
- 'spec/support/shared_examples/workers/concerns/reenqueuer_shared_examples.rb'
- 'spec/workers/concerns/reenqueuer_spec.rb'
- 'spec/lib/gitlab/analytics/cycle_analytics/median_spec.rb'
# Offense count: 43
Graphql/IDType:
Exclude:
......
......@@ -98,7 +98,7 @@ RSpec.describe 'Analytics (JavaScript fixtures)', :sidekiq_inline do
issue = create(:issue, project: project, created_at: 20.days.ago, author: user)
Timecop.travel(5.days.ago) do
travel_to(5.days.ago) do
Issues::UpdateService.new(
project,
user,
......@@ -106,7 +106,7 @@ RSpec.describe 'Analytics (JavaScript fixtures)', :sidekiq_inline do
).execute(issue)
end
Timecop.travel(2.days.ago) do
travel_to(2.days.ago) do
Issues::UpdateService.new(
project,
user,
......
......@@ -24,29 +24,29 @@ RSpec.describe Gitlab::Analytics::CycleAnalytics::DataCollector do
before do
# takes 10 days
resource1 = Timecop.travel(Time.new(2019, 3, 5)) do
resource1 = travel_to(Time.new(2019, 3, 5)) do
create_data_for_start_event(self)
end
Timecop.travel(Time.new(2019, 3, 15)) do
travel_to(Time.new(2019, 3, 15)) do
create_data_for_end_event(resource1, self)
end
# takes 5 days
resource2 = Timecop.travel(Time.new(2019, 3, 5)) do
resource2 = travel_to(Time.new(2019, 3, 5)) do
create_data_for_start_event(self)
end
Timecop.travel(Time.new(2019, 3, 10)) do
travel_to(Time.new(2019, 3, 10)) do
create_data_for_end_event(resource2, self)
end
# takes 15 days
resource3 = Timecop.travel(Time.new(2019, 3, 5)) do
resource3 = travel_to(Time.new(2019, 3, 5)) do
create_data_for_start_event(self)
end
Timecop.travel(Time.new(2019, 3, 20)) do
travel_to(Time.new(2019, 3, 20)) do
create_data_for_end_event(resource3, self)
end
end
......@@ -478,7 +478,7 @@ RSpec.describe Gitlab::Analytics::CycleAnalytics::DataCollector do
before do
group.add_user(user, GroupMember::MAINTAINER)
Timecop.travel(Time.new(2019, 6, 1)) do
travel_to(Time.new(2019, 6, 1)) do
mr = create(:merge_request, source_project: project1)
mr.metrics.update!(merged_at: 1.hour.from_now)
......
......@@ -42,7 +42,7 @@ RSpec.describe Gitlab::Analytics::CycleAnalytics::RequestParams do
end
it 'is valid' do
Timecop.travel '2019-03-01' do
travel_to '2019-03-01' do
expect(subject).to be_valid
end
end
......
......@@ -19,12 +19,12 @@ RSpec.describe Gitlab::Geo::EventGapTracking, :clean_gitlab_redis_cache do
end
it 'returns the lowest gap id' do
Timecop.travel(50.minutes.ago) do
travel_to(50.minutes.ago) do
gap_tracking.previous_id = 18
gap_tracking.send(:track_gaps, 20)
end
Timecop.travel(40.minutes.ago) do
travel_to(40.minutes.ago) do
gap_tracking.previous_id = 12
gap_tracking.send(:track_gaps, 14)
end
......@@ -116,7 +116,7 @@ RSpec.describe Gitlab::Geo::EventGapTracking, :clean_gitlab_redis_cache do
yielded << event_log
end
Timecop.travel(13.minutes.ago) do
travel_to(13.minutes.ago) do
gap_tracking.check!(event_id_with_gap)
end
create(:geo_event_log, :updated_event, id: gap_id)
......@@ -125,7 +125,7 @@ RSpec.describe Gitlab::Geo::EventGapTracking, :clean_gitlab_redis_cache do
expect { gap_tracking.fill_gaps(&blk) }.to change { yielded.count }.by(1)
end.count
Timecop.travel(12.minutes.ago) do
travel_to(12.minutes.ago) do
gap_tracking.check!(event_id_with_gap + 3)
end
create(:geo_event_log, :updated_event, id: event_id_with_gap + 1)
......
......@@ -45,7 +45,7 @@ RSpec.describe Gitlab::Geo::JwtRequestDecoder do
it 'successfully decodes when clocks are off by IAT leeway' do
subject
Timecop.travel(30.seconds.ago) { expect(subject.decode).to eq(data) }
travel_to(30.seconds.ago) { expect(subject.decode).to eq(data) }
end
it 'raises InvalidSignatureTimeError after expiring' do
......@@ -57,7 +57,7 @@ RSpec.describe Gitlab::Geo::JwtRequestDecoder do
it 'raises InvalidSignatureTimeError to decode when clocks are not in sync' do
subject
Timecop.travel(2.minutes.ago) { expect { subject.decode }.to raise_error(Gitlab::Geo::InvalidSignatureTimeError) }
travel_to(2.minutes.ago) { expect { subject.decode }.to raise_error(Gitlab::Geo::InvalidSignatureTimeError) }
end
it 'raises invalid decryption key error' do
......
......@@ -152,7 +152,7 @@ RSpec.describe EE::Namespace::RootStorageSize do
subject { model.enforce_limit? }
around do |example|
Timecop.travel(current_date) { example.run }
travel_to(current_date) { example.run }
end
context 'when current date is before enforcement date' do
......
......@@ -35,7 +35,7 @@ RSpec.describe Gitlab::SeatLinkData do
end
around do |example|
Timecop.travel(utc_time) { example.run }
travel_to(utc_time) { example.run }
end
context 'when passing no params' do
......
......@@ -55,7 +55,7 @@ RSpec.describe 'Rack Attack EE throttles' do
expect_rejection { post(path, **post_args) }
Timecop.travel(period.from_now) do
travel_to(period.from_now) do
requests_per_period.times do
post(path, **post_args)
expect(response).to have_gitlab_http_status(:ok)
......
......@@ -323,7 +323,7 @@ RSpec.shared_examples 'cycle analytics data endpoint examples' do
end
it 'succeeds' do
Timecop.travel '2019-04-01' do
travel_to '2019-04-01' do
subject
expect(response).to be_successful
......
......@@ -22,7 +22,7 @@ RSpec.describe SyncSeatLinkWorker, type: :worker do
end
it 'executes the SyncSeatLinkRequestWorker with expected params' do
Timecop.travel(utc_time) do
travel_to(utc_time) do
subject.perform
expect(SyncSeatLinkRequestWorker).to have_received(:perform_async)
......@@ -41,7 +41,7 @@ RSpec.describe SyncSeatLinkWorker, type: :worker do
end
it 'executes the SyncSeatLinkRequestWorker with expected params' do
Timecop.travel(utc_time) do
travel_to(utc_time) do
expect(Date.current.to_s).to eql('2020-03-13')
subject.perform
......@@ -62,7 +62,7 @@ RSpec.describe SyncSeatLinkWorker, type: :worker do
end
it 'executes the SyncSeatLinkRequestWorker with expected params' do
Timecop.travel(utc_time.beginning_of_day) do
travel_to(utc_time.beginning_of_day) do
expect(Date.current.to_s).to eql('2020-03-11')
subject.perform
......
......@@ -48,7 +48,7 @@ RSpec.describe 'Admin Mode Login', :clean_gitlab_redis_shared_state, :do_not_moc
it 'allows login with valid code' do
# Cannot reuse the TOTP
Timecop.travel(30.seconds.from_now) do
travel_to(30.seconds.from_now) do
enter_code(user.current_otp)
expect(current_path).to eq admin_root_path
......@@ -58,7 +58,7 @@ RSpec.describe 'Admin Mode Login', :clean_gitlab_redis_shared_state, :do_not_moc
it 'blocks login with invalid code' do
# Cannot reuse the TOTP
Timecop.travel(30.seconds.from_now) do
travel_to(30.seconds.from_now) do
enter_code('foo')
expect(page).to have_content('Invalid two-factor code')
......@@ -67,7 +67,7 @@ RSpec.describe 'Admin Mode Login', :clean_gitlab_redis_shared_state, :do_not_moc
it 'allows login with invalid code, then valid code' do
# Cannot reuse the TOTP
Timecop.travel(30.seconds.from_now) do
travel_to(30.seconds.from_now) do
enter_code('foo')
expect(page).to have_content('Invalid two-factor code')
......@@ -163,7 +163,7 @@ RSpec.describe 'Admin Mode Login', :clean_gitlab_redis_shared_state, :do_not_moc
expect(page).to have_content('Two-Factor Authentication')
# Cannot reuse the TOTP
Timecop.travel(30.seconds.from_now) do
travel_to(30.seconds.from_now) do
enter_code(user.current_otp)
expect(current_path).to eq admin_root_path
......@@ -215,7 +215,7 @@ RSpec.describe 'Admin Mode Login', :clean_gitlab_redis_shared_state, :do_not_moc
expect(page).to have_content('Two-Factor Authentication')
# Cannot reuse the TOTP
Timecop.travel(30.seconds.from_now) do
travel_to(30.seconds.from_now) do
enter_code(user.current_otp)
expect(current_path).to eq admin_root_path
......
......@@ -23,7 +23,7 @@ RSpec.describe 'Navigation bar counter', :use_clean_rails_memory_store_caching d
user.invalidate_cache_counts
Timecop.travel(3.minutes.from_now) do
travel_to(3.minutes.from_now) do
visit issues_path
expect_counters('issues', '0')
......@@ -39,7 +39,7 @@ RSpec.describe 'Navigation bar counter', :use_clean_rails_memory_store_caching d
user.invalidate_cache_counts
Timecop.travel(3.minutes.from_now) do
travel_to(3.minutes.from_now) do
visit merge_requests_path
expect_counters('merge_requests', '0')
......
......@@ -7,7 +7,7 @@ RSpec.describe Forever do
subject { described_class.date }
it 'returns Postgresql future date' do
Timecop.travel(Date.new(2999, 12, 31)) do
travel_to(Date.new(2999, 12, 31)) do
is_expected.to be > Date.today
end
end
......
......@@ -26,7 +26,7 @@ RSpec.describe Gitlab::Auth::UniqueIpsLimiter, :clean_gitlab_redis_shared_state
expect(described_class.update_and_return_ips_count(user.id, 'ip2')).to eq(1)
expect(described_class.update_and_return_ips_count(user.id, 'ip3')).to eq(2)
Timecop.travel(Time.now.utc + described_class.config.unique_ips_limit_time_window) do
travel_to(Time.now.utc + described_class.config.unique_ips_limit_time_window) do
expect(described_class.update_and_return_ips_count(user.id, 'ip4')).to eq(1)
expect(described_class.update_and_return_ips_count(user.id, 'ip5')).to eq(2)
end
......
......@@ -1035,7 +1035,7 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
def for_defined_days_back(days: [31, 3])
days.each do |n|
Timecop.travel(n.days.ago) do
travel_to(n.days.ago) do
yield
end
end
......
......@@ -241,7 +241,7 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do
end
def update_job_after_time(update_interval = 20.minutes, state = 'running')
Timecop.travel(job.updated_at + update_interval) do
travel_to(job.updated_at + update_interval) do
update_job(job.token, state: state)
end
end
......
......@@ -68,7 +68,7 @@ RSpec.describe 'Rack Attack global throttles' do
expect_rejection { get url_that_does_not_require_authentication }
Timecop.travel(period.from_now) do
travel_to(period.from_now) do
requests_per_period.times do
get url_that_does_not_require_authentication
expect(response).to have_gitlab_http_status(:ok)
......
......@@ -259,14 +259,14 @@ RSpec.shared_examples 'Pipeline Processing Service' do
expect(builds_names_and_statuses).to eq({ 'build': 'success', 'rollout10%': 'scheduled' })
Timecop.travel 2.minutes.from_now do
travel_to 2.minutes.from_now do
enqueue_scheduled('rollout10%')
end
succeed_pending
expect(builds_names_and_statuses).to eq({ 'build': 'success', 'rollout10%': 'success', 'rollout100%': 'scheduled' })
Timecop.travel 2.minutes.from_now do
travel_to 2.minutes.from_now do
enqueue_scheduled('rollout100%')
end
succeed_pending
......@@ -330,7 +330,7 @@ RSpec.shared_examples 'Pipeline Processing Service' do
expect(builds_names_and_statuses).to eq({ 'build': 'success', 'rollout10%': 'scheduled' })
Timecop.travel 2.minutes.from_now do
travel_to 2.minutes.from_now do
enqueue_scheduled('rollout10%')
end
fail_running_or_pending
......@@ -398,7 +398,7 @@ RSpec.shared_examples 'Pipeline Processing Service' do
expect(process_pipeline).to be_truthy
expect(builds_names_and_statuses).to eq({ 'delayed1': 'scheduled', 'delayed2': 'scheduled' })
Timecop.travel 2.minutes.from_now do
travel_to 2.minutes.from_now do
enqueue_scheduled('delayed1')
end
......@@ -419,7 +419,7 @@ RSpec.shared_examples 'Pipeline Processing Service' do
expect(process_pipeline).to be_truthy
expect(builds_names_and_statuses).to eq({ 'delayed': 'scheduled' })
Timecop.travel 2.minutes.from_now do
travel_to 2.minutes.from_now do
enqueue_scheduled('delayed')
end
fail_running_or_pending
......
......@@ -258,7 +258,7 @@ RSpec.describe Deployments::UpdateEnvironmentService do
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(deployment.finished_at)
# Current deploy
Timecop.travel(12.hours.from_now) do
travel_to(12.hours.from_now) do
service.execute
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(deployment.finished_at)
......
......@@ -236,7 +236,7 @@ module UsageDataHelpers
def for_defined_days_back(days: [31, 3])
days.each do |n|
Timecop.travel(n.days.ago) do
travel_to(n.days.ago) do
yield
end
end
......
......@@ -48,7 +48,7 @@ RSpec.shared_examples 'rate-limited token-authenticated requests' do
expect_rejection { make_request(request_args) }
Timecop.travel(period.from_now) do
travel_to(period.from_now) do
requests_per_period.times do
make_request(request_args)
expect(response).not_to have_gitlab_http_status(:too_many_requests)
......@@ -175,7 +175,7 @@ RSpec.shared_examples 'rate-limited web authenticated requests' do
expect_rejection { request_authenticated_web_url }
Timecop.travel(period.from_now) do
travel_to(period.from_now) do
requests_per_period.times do
request_authenticated_web_url
expect(response).not_to have_gitlab_http_status(:too_many_requests)
......
......@@ -131,7 +131,7 @@ RSpec.shared_examples 'issues move service' do |group|
updated_at1 = issue1.updated_at
updated_at2 = issue2.updated_at
Timecop.travel(1.minute.from_now) do
travel_to(1.minute.from_now) do
described_class.new(parent, user, params).execute(issue)
end
......
......@@ -17,7 +17,7 @@ RSpec.describe Metrics::Dashboard::PruneOldAnnotationsWorker do
# is idempotent in the scope of 24h
expect { described_class.new.perform }.not_to change { Metrics::Dashboard::Annotation.all.to_a }
Timecop.travel(24.hours.from_now) do
travel_to(24.hours.from_now) do
described_class.new.perform
expect(Metrics::Dashboard::Annotation.all).to match_array([one_day_old_annotation])
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment