diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-19 17:16:28 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-19 17:16:28 +0300 |
commit | e4384360a16dd9a19d4d2d25d0ef1f2b862ed2a6 (patch) | |
tree | 2fcdfa7dcdb9db8f5208b2562f4b4e803d671243 /gems/gitlab-rspec/lib/gitlab/rspec | |
parent | ffda4e7bcac36987f936b4ba515995a6698698f0 (diff) |
Add latest changes from gitlab-org/gitlab@16-2-stable-eev16.2.0-rc42
Diffstat (limited to 'gems/gitlab-rspec/lib/gitlab/rspec')
-rw-r--r-- | gems/gitlab-rspec/lib/gitlab/rspec/all.rb | 8 | ||||
-rw-r--r-- | gems/gitlab-rspec/lib/gitlab/rspec/configurations/time_travel.rb | 32 | ||||
-rw-r--r-- | gems/gitlab-rspec/lib/gitlab/rspec/stub_env.rb | 52 | ||||
-rw-r--r-- | gems/gitlab-rspec/lib/gitlab/rspec/version.rb | 9 |
4 files changed, 101 insertions, 0 deletions
diff --git a/gems/gitlab-rspec/lib/gitlab/rspec/all.rb b/gems/gitlab-rspec/lib/gitlab/rspec/all.rb new file mode 100644 index 00000000000..091d2ba0287 --- /dev/null +++ b/gems/gitlab-rspec/lib/gitlab/rspec/all.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +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 00000000000..b30aa1cde0d --- /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/gems/gitlab-rspec/lib/gitlab/rspec/stub_env.rb b/gems/gitlab-rspec/lib/gitlab/rspec/stub_env.rb new file mode 100644 index 00000000000..f8775d9f7b5 --- /dev/null +++ b/gems/gitlab-rspec/lib/gitlab/rspec/stub_env.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +# Inspired by https://github.com/ljkbennett/stub_env/blob/master/lib/stub_env/helpers.rb +module StubENV + # Stub ENV variables + # + # You can provide either a key and value as separate params or both in a Hash format + # + # Keys and values will always be converted to String, to comply with how ENV behaves + # + # @param key_or_hash [String, Hash<String,String>] + # @param value [String] + def stub_env(key_or_hash, value = nil) + init_stub unless env_stubbed? + + if key_or_hash.is_a? Hash + key_or_hash.each do |key, value| + add_stubbed_value(key, ensure_env_type(value)) + end + else + add_stubbed_value key_or_hash, ensure_env_type(value) + end + end + + private + + STUBBED_KEY = '__STUBBED__' + + def add_stubbed_value(key, value) + allow(ENV).to receive(:[]).with(key).and_return(value) + allow(ENV).to receive(:key?).with(key).and_return(true) + allow(ENV).to receive(:fetch).with(key).and_return(value) + allow(ENV).to receive(:fetch).with(key, anything) do |_, default_val| + value || default_val + end + end + + def env_stubbed? + ENV.fetch(STUBBED_KEY, false) + end + + def init_stub + allow(ENV).to receive(:[]).and_call_original + allow(ENV).to receive(:key?).and_call_original + allow(ENV).to receive(:fetch).and_call_original + add_stubbed_value(STUBBED_KEY, true) + end + + def ensure_env_type(value) + value.nil? ? value : value.to_s + end +end diff --git a/gems/gitlab-rspec/lib/gitlab/rspec/version.rb b/gems/gitlab-rspec/lib/gitlab/rspec/version.rb new file mode 100644 index 00000000000..34c51245012 --- /dev/null +++ b/gems/gitlab-rspec/lib/gitlab/rspec/version.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Gitlab + module Rspec + module Version + VERSION = "0.1.0" + end + end +end |