diff options
Diffstat (limited to 'spec/support/shared_contexts/glfm')
-rw-r--r-- | spec/support/shared_contexts/glfm/api_markdown_snapshot_shared_context.rb | 62 | ||||
-rw-r--r-- | spec/support/shared_contexts/glfm/example_snapshot_fixtures.rb | 27 |
2 files changed, 89 insertions, 0 deletions
diff --git a/spec/support/shared_contexts/glfm/api_markdown_snapshot_shared_context.rb b/spec/support/shared_contexts/glfm/api_markdown_snapshot_shared_context.rb new file mode 100644 index 00000000000..3623fa0850d --- /dev/null +++ b/spec/support/shared_contexts/glfm/api_markdown_snapshot_shared_context.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require_relative '../../../../scripts/lib/glfm/constants' + +# See https://docs.gitlab.com/ee/development/gitlab_flavored_markdown/specification_guide/#markdown-snapshot-testing +# for documentation on this spec. +RSpec.shared_context 'with API::Markdown Snapshot shared context' do |ee_only: false| + include_context 'with GLFM example snapshot fixtures' + + include ApiHelpers + + markdown_examples, html_examples, normalizations_by_example_name, metadata_by_example_name = [ + Glfm::Constants::ES_MARKDOWN_YML_PATH, + Glfm::Constants::ES_HTML_YML_PATH, + Glfm::Constants::GLFM_EXAMPLE_NORMALIZATIONS_YML_PATH, + Glfm::Constants::GLFM_EXAMPLE_METADATA_YML_PATH + ].map { |path| YAML.safe_load(File.open(path), symbolize_names: true, aliases: true) } + + if (focused_markdown_examples_string = ENV['FOCUSED_MARKDOWN_EXAMPLES']) + focused_markdown_examples = focused_markdown_examples_string.split(',').map(&:strip).map(&:to_sym) + markdown_examples.select! { |example_name| focused_markdown_examples.include?(example_name) } + end + + markdown_examples.select! { |example_name| !!metadata_by_example_name&.dig(example_name, :ee) == ee_only } + + markdown_examples.each do |name, markdown| + context "for #{name}" do + let(:html) { html_examples.fetch(name).fetch(:static) } + let(:normalizations) { normalizations_by_example_name.dig(name, :html, :static, :snapshot) } + + it "verifies conversion of GLFM to HTML", :unlimited_max_formatted_output_length do + # noinspection RubyResolve + normalized_html = normalize_html(html, normalizations) + api_url = metadata_by_example_name&.dig(name, :api_request_override_path) || (api "/markdown") + + post api_url, params: { text: markdown, gfm: true } + expect(response).to be_successful + parsed_response = Gitlab::Json.parse(response.body, symbolize_names: true) + # Some responses have the HTML in the `html` key, others in the `body` key. + response_html = parsed_response[:body] || parsed_response[:html] + normalized_response_html = normalize_html(response_html, normalizations) + + expect(normalized_response_html).to eq(normalized_html) + end + + def normalize_html(html, normalizations) + return html unless normalizations + + normalized_html = html.dup + normalizations.each_value do |normalization_entry| + normalization_entry.each do |normalization| + regex = normalization.fetch(:regex) + replacement = normalization.fetch(:replacement) + normalized_html.gsub!(%r{#{regex}}, replacement) + end + end + + normalized_html + end + end + end +end diff --git a/spec/support/shared_contexts/glfm/example_snapshot_fixtures.rb b/spec/support/shared_contexts/glfm/example_snapshot_fixtures.rb new file mode 100644 index 00000000000..22b401bc841 --- /dev/null +++ b/spec/support/shared_contexts/glfm/example_snapshot_fixtures.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +RSpec.shared_context 'with GLFM example snapshot fixtures' do + let_it_be(:user) { create(:user) } + let_it_be(:group) { create(:group, name: 'glfm_group').tap { |group| group.add_owner(user) } } + + let_it_be(:project) do + # NOTE: We hardcode the IDs on all fixtures to prevent variability in the + # rendered HTML/Prosemirror JSON, and to minimize the need for normalization: + # https://docs.gitlab.com/ee/development/gitlab_flavored_markdown/specification_guide/#normalization + create(:project, :repository, creator: user, group: group, name: 'glfm_project', id: 77777) + end + + let_it_be(:project_snippet) { create(:project_snippet, title: 'glfm_project_snippet', id: 88888, project: project) } + let_it_be(:personal_snippet) { create(:snippet, id: 99999) } + + before do + # Set 'GITLAB_TEST_FOOTNOTE_ID' in order to override random number generation in + # Banzai::Filter::FootnoteFilter#random_number, and thus avoid the need to + # perform normalization on the value. See: + # https://docs.gitlab.com/ee/development/gitlab_flavored_markdown/specification_guide/#normalization + stub_env('GITLAB_TEST_FOOTNOTE_ID', 42) + + stub_licensed_features(group_wikis: true) + sign_in(user) + end +end |