diff options
Diffstat (limited to 'gems/gitlab-rspec')
-rw-r--r-- | gems/gitlab-rspec/.gitignore | 11 | ||||
-rw-r--r-- | gems/gitlab-rspec/.gitlab-ci.yml | 4 | ||||
-rw-r--r-- | gems/gitlab-rspec/.rspec | 3 | ||||
-rw-r--r-- | gems/gitlab-rspec/.rubocop.yml | 10 | ||||
-rw-r--r-- | gems/gitlab-rspec/Gemfile | 5 | ||||
-rw-r--r-- | gems/gitlab-rspec/Gemfile.lock | 182 | ||||
-rw-r--r-- | gems/gitlab-rspec/gitlab-rspec.gemspec | 31 | ||||
-rw-r--r-- | gems/gitlab-rspec/lib/gitlab/rspec.rb | 9 | ||||
-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 | ||||
-rw-r--r-- | gems/gitlab-rspec/spec/gitlab/rspec/time_travel_spec.rb | 28 | ||||
-rw-r--r-- | gems/gitlab-rspec/spec/spec_helper.rb | 15 |
14 files changed, 399 insertions, 0 deletions
diff --git a/gems/gitlab-rspec/.gitignore b/gems/gitlab-rspec/.gitignore new file mode 100644 index 00000000000..b04a8c840df --- /dev/null +++ b/gems/gitlab-rspec/.gitignore @@ -0,0 +1,11 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ + +# rspec failure tracking +.rspec_status diff --git a/gems/gitlab-rspec/.gitlab-ci.yml b/gems/gitlab-rspec/.gitlab-ci.yml new file mode 100644 index 00000000000..910b391a0e9 --- /dev/null +++ b/gems/gitlab-rspec/.gitlab-ci.yml @@ -0,0 +1,4 @@ +include: + - local: gems/gem.gitlab-ci.yml + inputs: + gem_name: "gitlab-rspec" diff --git a/gems/gitlab-rspec/.rspec b/gems/gitlab-rspec/.rspec new file mode 100644 index 00000000000..34c5164d9b5 --- /dev/null +++ b/gems/gitlab-rspec/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/gems/gitlab-rspec/.rubocop.yml b/gems/gitlab-rspec/.rubocop.yml new file mode 100644 index 00000000000..38c0c592dad --- /dev/null +++ b/gems/gitlab-rspec/.rubocop.yml @@ -0,0 +1,10 @@ +inherit_from: + - ../config/rubocop.yml + +RSpec/InstanceVariable: + Exclude: + - spec/**/*.rb + +Gitlab/ChangeTimezone: + Exclude: + - spec/gitlab/rspec/time_travel_spec.rb diff --git a/gems/gitlab-rspec/Gemfile b/gems/gitlab-rspec/Gemfile new file mode 100644 index 00000000000..be173b205f7 --- /dev/null +++ b/gems/gitlab-rspec/Gemfile @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +gemspec diff --git a/gems/gitlab-rspec/Gemfile.lock b/gems/gitlab-rspec/Gemfile.lock new file mode 100644 index 00000000000..dcdb4dd009e --- /dev/null +++ b/gems/gitlab-rspec/Gemfile.lock @@ -0,0 +1,182 @@ +PATH + remote: . + specs: + gitlab-rspec (0.1.0) + activesupport (>= 6.1, < 7.1) + rspec (~> 3.0) + +GEM + remote: https://rubygems.org/ + specs: + actionpack (7.0.4.3) + actionview (= 7.0.4.3) + activesupport (= 7.0.4.3) + rack (~> 2.0, >= 2.2.0) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.2.0) + actionview (7.0.4.3) + activesupport (= 7.0.4.3) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.1, >= 1.2.0) + activesupport (7.0.4.3) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) + ast (2.4.2) + benchmark-malloc (0.2.0) + benchmark-perf (0.6.0) + benchmark-trend (0.4.0) + binding_of_caller (1.0.0) + debug_inspector (>= 0.0.1) + builder (3.2.4) + coderay (1.1.3) + concurrent-ruby (1.2.2) + crass (1.0.6) + debug_inspector (1.1.0) + diff-lcs (1.5.0) + erubi (1.12.0) + factory_bot (6.2.0) + activesupport (>= 5.0.0) + factory_bot_rails (6.2.0) + factory_bot (~> 6.2.0) + railties (>= 5.0.0) + gitlab-styles (10.1.0) + rubocop (~> 1.50.2) + rubocop-graphql (~> 0.18) + rubocop-performance (~> 1.15) + rubocop-rails (~> 2.17) + rubocop-rspec (~> 2.22) + i18n (1.13.0) + concurrent-ruby (~> 1.0) + json (2.6.3) + loofah (2.21.3) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) + method_source (1.0.0) + mini_portile2 (2.8.2) + minitest (5.18.0) + nokogiri (1.15.2) + mini_portile2 (~> 2.8.2) + racc (~> 1.4) + parallel (1.23.0) + parser (3.2.2.1) + ast (~> 2.4.1) + proc_to_ast (0.1.0) + coderay + parser + unparser + racc (1.6.2) + rack (2.2.7) + rack-test (2.1.0) + rack (>= 1.3) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.5.0) + loofah (~> 2.19, >= 2.19.1) + railties (7.0.4.3) + actionpack (= 7.0.4.3) + activesupport (= 7.0.4.3) + method_source + rake (>= 12.2) + thor (~> 1.0) + zeitwerk (~> 2.5) + rainbow (3.1.1) + rake (13.0.6) + regexp_parser (2.8.1) + rexml (3.2.5) + rspec (3.12.0) + rspec-core (~> 3.12.0) + rspec-expectations (~> 3.12.0) + rspec-mocks (~> 3.12.0) + rspec-benchmark (0.6.0) + benchmark-malloc (~> 0.2) + benchmark-perf (~> 0.6) + benchmark-trend (~> 0.4) + rspec (>= 3.0) + rspec-core (3.12.0) + rspec-support (~> 3.12.0) + rspec-expectations (3.12.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-mocks (3.12.3) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-parameterized (1.0.0) + rspec-parameterized-core (< 2) + rspec-parameterized-table_syntax (< 2) + rspec-parameterized-core (1.0.0) + parser + proc_to_ast + rspec (>= 2.13, < 4) + unparser + rspec-parameterized-table_syntax (1.0.0) + binding_of_caller + rspec-parameterized-core (< 2) + rspec-rails (6.0.2) + actionpack (>= 6.1) + activesupport (>= 6.1) + railties (>= 6.1) + rspec-core (~> 3.12) + rspec-expectations (~> 3.12) + rspec-mocks (~> 3.12) + rspec-support (~> 3.12) + rspec-support (3.12.0) + rubocop (1.50.2) + json (~> 2.3) + parallel (~> 1.10) + parser (>= 3.2.0.0) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.28.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.29.0) + parser (>= 3.2.1.0) + rubocop-capybara (2.18.0) + rubocop (~> 1.41) + rubocop-factory_bot (2.23.1) + rubocop (~> 1.33) + rubocop-graphql (0.19.0) + rubocop (>= 0.87, < 2) + rubocop-performance (1.18.0) + rubocop (>= 1.7.0, < 2.0) + rubocop-ast (>= 0.4.0) + rubocop-rails (2.20.2) + activesupport (>= 4.2.0) + rack (>= 1.1) + rubocop (>= 1.33.0, < 2.0) + rubocop-rspec (2.22.0) + rubocop (~> 1.33) + rubocop-capybara (~> 2.17) + rubocop-factory_bot (~> 2.22) + ruby-progressbar (1.13.0) + thor (1.2.2) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + unicode-display_width (2.4.2) + unparser (0.6.7) + diff-lcs (~> 1.3) + parser (>= 3.2.0) + zeitwerk (2.6.8) + +PLATFORMS + ruby + +DEPENDENCIES + factory_bot_rails (~> 6.2.0) + gitlab-rspec! + gitlab-styles (~> 10.1.0) + rspec-benchmark (~> 0.6.0) + rspec-parameterized (~> 1.0) + rspec-rails (~> 6.0.1) + rubocop (~> 1.50) + rubocop-rspec (~> 2.22) + +BUNDLED WITH + 2.4.4 diff --git a/gems/gitlab-rspec/gitlab-rspec.gemspec b/gems/gitlab-rspec/gitlab-rspec.gemspec new file mode 100644 index 00000000000..c2c5b6c60b7 --- /dev/null +++ b/gems/gitlab-rspec/gitlab-rspec.gemspec @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require_relative "lib/gitlab/rspec/version" + +Gem::Specification.new do |spec| + spec.name = "gitlab-rspec" + spec.version = Gitlab::Rspec::Version::VERSION + spec.authors = ["group::tenant-scale"] + spec.email = ["engineering@gitlab.com"] + + spec.summary = "GitLab RSpec extensions" + spec.description = "A set of useful helpers to configure RSpec with various stubs and CI configs." + spec.homepage = "https://gitlab.com/gitlab-org/gitlab/-/tree/master/gems/gitlab-rspec" + spec.license = "MIT" + spec.required_ruby_version = ">= 3.0" + spec.metadata["rubygems_mfa_required"] = "true" + + 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" + spec.add_development_dependency "gitlab-styles", "~> 10.1.0" + spec.add_development_dependency "rspec-benchmark", "~> 0.6.0" + spec.add_development_dependency "rspec-parameterized", "~> 1.0" + spec.add_development_dependency "rspec-rails", "~> 6.0.1" + spec.add_development_dependency "rubocop", "~> 1.50" + spec.add_development_dependency "rubocop-rspec", "~> 2.22" +end diff --git a/gems/gitlab-rspec/lib/gitlab/rspec.rb b/gems/gitlab-rspec/lib/gitlab/rspec.rb new file mode 100644 index 00000000000..2d076e99614 --- /dev/null +++ b/gems/gitlab-rspec/lib/gitlab/rspec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require "rspec" +require_relative "rspec/version" + +module Gitlab + module Rspec + end +end 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 diff --git a/gems/gitlab-rspec/spec/gitlab/rspec/time_travel_spec.rb b/gems/gitlab-rspec/spec/gitlab/rspec/time_travel_spec.rb new file mode 100644 index 00000000000..79804a99f70 --- /dev/null +++ b/gems/gitlab-rspec/spec/gitlab/rspec/time_travel_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +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 } + end + end + + describe ':time_travel_to' do + it 'time-travels to the specified date', time_travel_to: '2020-01-01' do + expect(Date.current).to eq(Date.new(2020, 1, 1)) + end + + it 'time-travels to the specified date & time', time_travel_to: '2020-02-02 10:30:45 -0700' do + expect(Time.current).to eq(Time.new(2020, 2, 2, 17, 30, 45, '+00:00')) + end + end +end diff --git a/gems/gitlab-rspec/spec/spec_helper.rb b/gems/gitlab-rspec/spec/spec_helper.rb new file mode 100644 index 00000000000..c694747e094 --- /dev/null +++ b/gems/gitlab-rspec/spec/spec_helper.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'gitlab/rspec/all' + +RSpec.configure do |config| + # Enable flags like --only-failures and --next-failure + config.example_status_persistence_file_path = ".rspec_status" + + # Disable RSpec exposing methods globally on `Module` and `main` + config.disable_monkey_patching! + + config.expect_with :rspec do |c| + c.syntax = :expect + end +end |