From a21c4a38de1b513a00b79cd54bdbbc5a259ac441 Mon Sep 17 00:00:00 2001
From: Peter Leitzen <pleitzen@gitlab.com>
Date: Fri, 20 Jan 2023 12:10:28 +0100
Subject: [PATCH] Improve invalid feature category message for db docs specs

Show which categories are invalid.
---
 .../naming/heredoc_delimiter_naming.yml       |  1 -
 .rubocop_todo/style/if_unless_modifier.yml    |  1 -
 .../style/percent_literal_delimiters.yml      |  1 -
 spec/db/docs_spec.rb                          | 41 +++++++++++--------
 4 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/.rubocop_todo/naming/heredoc_delimiter_naming.yml b/.rubocop_todo/naming/heredoc_delimiter_naming.yml
index d10fac78cef59..3daf50a4e2495 100644
--- a/.rubocop_todo/naming/heredoc_delimiter_naming.yml
+++ b/.rubocop_todo/naming/heredoc_delimiter_naming.yml
@@ -56,7 +56,6 @@ Naming/HeredocDelimiterNaming:
     - 'rubocop/cop/gitlab/module_with_instance_variables.rb'
     - 'rubocop/cop/gitlab/predicate_memoization.rb'
     - 'spec/controllers/projects/pipelines_controller_spec.rb'
-    - 'spec/db/docs_spec.rb'
     - 'spec/deprecation_toolkit_env.rb'
     - 'spec/factories/packages/debian/distribution.rb'
     - 'spec/factories/packages/debian/file_metadatum.rb'
diff --git a/.rubocop_todo/style/if_unless_modifier.yml b/.rubocop_todo/style/if_unless_modifier.yml
index 515a9ddad6a1b..39f4094d40f33 100644
--- a/.rubocop_todo/style/if_unless_modifier.yml
+++ b/.rubocop_todo/style/if_unless_modifier.yml
@@ -1046,7 +1046,6 @@ Style/IfUnlessModifier:
     - 'sidekiq_cluster/cli.rb'
     - 'spec/components/previews/pajamas/alert_component_preview.rb'
     - 'spec/components/previews/pajamas/card_component_preview.rb'
-    - 'spec/db/docs_spec.rb'
     - 'spec/factories/ci/runners.rb'
     - 'spec/factories/clusters/applications/helm.rb'
     - 'spec/factories/container_repositories.rb'
diff --git a/.rubocop_todo/style/percent_literal_delimiters.yml b/.rubocop_todo/style/percent_literal_delimiters.yml
index b2c466353df22..cca0df9cdf8ce 100644
--- a/.rubocop_todo/style/percent_literal_delimiters.yml
+++ b/.rubocop_todo/style/percent_literal_delimiters.yml
@@ -608,7 +608,6 @@ Style/PercentLiteralDelimiters:
     - 'spec/controllers/projects_controller_spec.rb'
     - 'spec/controllers/search_controller_spec.rb'
     - 'spec/controllers/snippets_controller_spec.rb'
-    - 'spec/db/docs_spec.rb'
     - 'spec/experiments/ios_specific_templates_experiment_spec.rb'
     - 'spec/factories/ci/builds.rb'
     - 'spec/factories/ci/runners.rb'
diff --git a/spec/db/docs_spec.rb b/spec/db/docs_spec.rb
index 5960b8bebccc1..8d4cb3ac5efc7 100644
--- a/spec/db/docs_spec.rb
+++ b/spec/db/docs_spec.rb
@@ -2,13 +2,6 @@
 
 require 'spec_helper'
 
-# This list is used to provide temporary exceptions for feature categories
-# that are transitioning and not yet in the feature_categories.yml file
-# any additions here should be accompanied by a link to an issue link
-VALID_FEATURE_CATEGORIES = [
-  'jihu' # https://gitlab.com/gitlab-org/database-team/team-tasks/-/issues/192
-].freeze
-
 RSpec.shared_examples 'validate dictionary' do |objects, directory_path, required_fields|
   context 'for each object' do
     let(:directory_path) {  directory_path }
@@ -32,6 +25,19 @@
       end
     end
 
+    # This list is used to provide temporary exceptions for feature categories
+    # that are transitioning and not yet in the feature_categories.yml file
+    # any additions here should be accompanied by a link to an issue link
+    let(:valid_feature_categories) do
+      [
+        'jihu' # https://gitlab.com/gitlab-org/database-team/team-tasks/-/issues/192
+      ]
+    end
+
+    let(:all_feature_categories) do
+      YAML.load_file(Rails.root.join('config/feature_categories.yml')) + valid_feature_categories
+    end
+
     let(:objects_without_metadata) do
       objects.reject { |t| metadata.has_key?(t) }
     end
@@ -68,9 +74,15 @@
     end
 
     it 'has a valid feature category' do
+      message = <<~TEXT.chomp
+        Please use a category from https://about.gitlab.com/handbook/product/categories/#categories-a-z
+
+        Table metadata files with an invalid feature category
+      TEXT
+
       expect(objects_with_invalid_feature_category).to be_empty, object_metadata_errors(
-        'Table metadata files with an invalid feature category',
-        :error,
+        message,
+        :invalid_feature_category,
         objects_with_invalid_feature_category
       )
     end
@@ -102,11 +114,10 @@ def object_metadata_file_path(object_name)
     Rails.root.join(object_metadata_file(object_name))
   end
 
-  def feature_categories_valid?(object_feature_categories)
+  def invalid_feature_categories(object_feature_categories)
     return false unless object_feature_categories.present?
 
-    all_feature_categories = YAML.load_file(Rails.root.join('config/feature_categories.yml')) + VALID_FEATURE_CATEGORIES
-    object_feature_categories.all? { |category| all_feature_categories.include?(category) }
+    object_feature_categories - all_feature_categories
   end
 
   def load_object_metadata(required_fields, object_name)
@@ -125,10 +136,8 @@ def load_object_metadata(required_fields, object_name)
       if required_fields.include?(:feature_categories)
         object_feature_categories = result.dig(:metadata, :feature_categories)
 
-        unless feature_categories_valid?(object_feature_categories)
-          result[:invalid_feature_category] =
-            "invalid feature category: #{object_feature_categories}" \
-            "Please use a category from https://about.gitlab.com/handbook/product/categories/#categories-a-z"
+        if (invalid = invalid_feature_categories(object_feature_categories)).any?
+          result[:invalid_feature_category] = "invalid feature category: #{invalid.join(', ')}"
         end
       end
     rescue Psych::SyntaxError => ex
-- 
GitLab