From fe78eff3894bf706d694c30cb92c2f3aecc495bd Mon Sep 17 00:00:00 2001
From: Stan Hu <stanhu@gmail.com>
Date: Tue, 21 May 2019 14:17:24 -0700
Subject: [PATCH] Upgrade shoulda-matchers to 4.0.1

This removes the warning when using Ruby 2.6:

BigDecimal.new is deprecated; use BigDecimal() method instead.

This also adds Rails 5 support, which eliminates the need for the monkey
patch to handle
https://github.com/thoughtbot/shoulda-matchers/issues/913.

CE port: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/28443
---
 Gemfile                                       |  2 +-
 Gemfile.lock                                  |  6 ++---
 app/models/merge_request.rb                   |  2 ++
 ee/app/models/ee/board.rb                     |  2 +-
 ee/spec/models/group_spec.rb                  |  5 +++-
 ee/spec/models/prometheus_alert_event_spec.rb |  2 +-
 ee/spec/models/prometheus_alert_spec.rb       |  4 +--
 spec/models/ci/pipeline_schedule_spec.rb      |  2 ++
 spec/models/clusters/cluster_spec.rb          |  2 ++
 spec/models/deployment_spec.rb                |  4 +--
 spec/models/environment_spec.rb               |  2 +-
 spec/support/shoulda/matchers/rails_shim.rb   | 27 -------------------
 12 files changed, 21 insertions(+), 39 deletions(-)
 delete mode 100644 spec/support/shoulda/matchers/rails_shim.rb

diff --git a/Gemfile b/Gemfile
index d7c1c3576f08d..8e2fd0633b880 100644
--- a/Gemfile
+++ b/Gemfile
@@ -394,7 +394,7 @@ group :development, :test do
 end
 
 group :test do
-  gem 'shoulda-matchers', '~> 3.1.2', require: false
+  gem 'shoulda-matchers', '~> 4.0.1', require: false
   gem 'email_spec', '~> 2.2.0'
   gem 'json-schema', '~> 2.8.0'
   gem 'webmock', '~> 3.5.1'
diff --git a/Gemfile.lock b/Gemfile.lock
index 9d36470028a3c..de54d5fc8d037 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -888,8 +888,8 @@ GEM
     sexp_processor (4.12.0)
     sham_rack (1.3.6)
       rack
-    shoulda-matchers (3.1.2)
-      activesupport (>= 4.0.0)
+    shoulda-matchers (4.0.1)
+      activesupport (>= 4.2.0)
     sidekiq (5.2.7)
       connection_pool (~> 2.2, >= 2.2.2)
       rack (>= 1.5.0)
@@ -1241,7 +1241,7 @@ DEPENDENCIES
   sentry-raven (~> 2.7)
   settingslogic (~> 2.0.9)
   sham_rack (~> 1.3.6)
-  shoulda-matchers (~> 3.1.2)
+  shoulda-matchers (~> 4.0.1)
   sidekiq (~> 5.2.7)
   sidekiq-cron (~> 1.0)
   simple_po_parser (~> 1.1.2)
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 0e6ad26bc1c2a..6168345047d8a 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -583,6 +583,8 @@ def branch_merge_base_sha
   end
 
   def validate_branches
+    return unless target_project && source_project
+
     if target_project == source_project && target_branch == source_branch
       errors.add :branch_conflict, "You can't use same project/branch for source and target"
       return
diff --git a/ee/app/models/ee/board.rb b/ee/app/models/ee/board.rb
index c00ff823cde76..162c10a74dfbf 100644
--- a/ee/app/models/ee/board.rb
+++ b/ee/app/models/ee/board.rb
@@ -35,7 +35,7 @@ def scoped?
     end
 
     def milestone
-      return unless parent.feature_available?(:scoped_issue_board)
+      return unless parent&.feature_available?(:scoped_issue_board)
 
       case milestone_id
       when ::Milestone::Upcoming.id
diff --git a/ee/spec/models/group_spec.rb b/ee/spec/models/group_spec.rb
index b22abd5e55d67..6bb94b5d3121a 100644
--- a/ee/spec/models/group_spec.rb
+++ b/ee/spec/models/group_spec.rb
@@ -7,7 +7,10 @@
 
   describe 'associations' do
     it { is_expected.to have_many(:audit_events).dependent(false) }
-    it { is_expected.to belong_to(:file_template_project) }
+    # shoulda-matchers attempts to set the association to nil to ensure
+    # the presence check works, but since this is a private method that
+    # method can't be called with a public_send.
+    it { is_expected.to belong_to(:file_template_project).class_name('Project').without_validating_presence }
     it { is_expected.to have_many(:dependency_proxy_blobs) }
     it { is_expected.to have_one(:dependency_proxy_setting) }
   end
diff --git a/ee/spec/models/prometheus_alert_event_spec.rb b/ee/spec/models/prometheus_alert_event_spec.rb
index a56525d2b722c..d867da41a27e2 100644
--- a/ee/spec/models/prometheus_alert_event_spec.rb
+++ b/ee/spec/models/prometheus_alert_event_spec.rb
@@ -7,7 +7,7 @@
   let(:alert) { subject.prometheus_alert }
 
   describe 'associations' do
-    it { is_expected.to belong_to(:prometheus_alert) }
+    it { is_expected.to belong_to(:prometheus_alert).required }
   end
 
   describe 'validations' do
diff --git a/ee/spec/models/prometheus_alert_spec.rb b/ee/spec/models/prometheus_alert_spec.rb
index cfc48e3639cfc..34dc325e5e227 100644
--- a/ee/spec/models/prometheus_alert_spec.rb
+++ b/ee/spec/models/prometheus_alert_spec.rb
@@ -24,8 +24,8 @@
   end
 
   describe 'associations' do
-    it { is_expected.to belong_to(:project) }
-    it { is_expected.to belong_to(:environment) }
+    it { is_expected.to belong_to(:project).required }
+    it { is_expected.to belong_to(:environment).required }
   end
 
   describe 'project validations' do
diff --git a/spec/models/ci/pipeline_schedule_spec.rb b/spec/models/ci/pipeline_schedule_spec.rb
index 1bfc14d2839de..42d4769a921e8 100644
--- a/spec/models/ci/pipeline_schedule_spec.rb
+++ b/spec/models/ci/pipeline_schedule_spec.rb
@@ -3,6 +3,8 @@
 require 'spec_helper'
 
 describe Ci::PipelineSchedule do
+  subject { build(:ci_pipeline_schedule) }
+
   it { is_expected.to belong_to(:project) }
   it { is_expected.to belong_to(:owner) }
 
diff --git a/spec/models/clusters/cluster_spec.rb b/spec/models/clusters/cluster_spec.rb
index 58203da5b224b..f9d8ffd06e0d2 100644
--- a/spec/models/clusters/cluster_spec.rb
+++ b/spec/models/clusters/cluster_spec.rb
@@ -5,6 +5,8 @@
 describe Clusters::Cluster do
   it_behaves_like 'having unique enum values'
 
+  subject { build(:cluster) }
+
   it { is_expected.to belong_to(:user) }
   it { is_expected.to have_many(:cluster_projects) }
   it { is_expected.to have_many(:projects) }
diff --git a/spec/models/deployment_spec.rb b/spec/models/deployment_spec.rb
index f51322e14040b..1dceef3fc0065 100644
--- a/spec/models/deployment_spec.rb
+++ b/spec/models/deployment_spec.rb
@@ -5,8 +5,8 @@
 describe Deployment do
   subject { build(:deployment) }
 
-  it { is_expected.to belong_to(:project) }
-  it { is_expected.to belong_to(:environment) }
+  it { is_expected.to belong_to(:project).required }
+  it { is_expected.to belong_to(:environment).required }
   it { is_expected.to belong_to(:user) }
   it { is_expected.to belong_to(:deployable) }
 
diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb
index 17246f238e0a5..7233d2454c627 100644
--- a/spec/models/environment_spec.rb
+++ b/spec/models/environment_spec.rb
@@ -6,7 +6,7 @@
   let(:project) { create(:project, :stubbed_repository) }
   subject(:environment) { create(:environment, project: project) }
 
-  it { is_expected.to belong_to(:project) }
+  it { is_expected.to belong_to(:project).required }
   it { is_expected.to have_many(:deployments) }
 
   it { is_expected.to delegate_method(:stop_action).to(:last_deployment) }
diff --git a/spec/support/shoulda/matchers/rails_shim.rb b/spec/support/shoulda/matchers/rails_shim.rb
deleted file mode 100644
index 8d70598beb568..0000000000000
--- a/spec/support/shoulda/matchers/rails_shim.rb
+++ /dev/null
@@ -1,27 +0,0 @@
-# monkey patch which fixes serialization matcher in Rails 5
-# https://github.com/thoughtbot/shoulda-matchers/issues/913
-# This can be removed when a new version of shoulda-matchers
-# is released
-module Shoulda
-  module Matchers
-    class RailsShim
-      def self.serialized_attributes_for(model)
-        if defined?(::ActiveRecord::Type::Serialized)
-          # Rails 5+
-          serialized_columns = model.columns.select do |column|
-            model.type_for_attribute(column.name).is_a?(
-              ::ActiveRecord::Type::Serialized
-            )
-          end
-
-          serialized_columns.inject({}) do |hash, column| # rubocop:disable Style/EachWithObject
-            hash[column.name.to_s] = model.type_for_attribute(column.name).coder
-            hash
-          end
-        else
-          model.serialized_attributes
-        end
-      end
-    end
-  end
-end
-- 
GitLab