diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
commit | 6ed4ec3e0b1340f96b7c043ef51d1b33bbe85fde (patch) | |
tree | dc4d20fe6064752c0bd323187252c77e0a89144b /scripts/lib/glfm/render_static_html.rb | |
parent | 9868dae7fc0655bd7ce4a6887d4e6d487690eeed (diff) |
Add latest changes from gitlab-org/gitlab@15-4-stable-eev15.4.0-rc42
Diffstat (limited to 'scripts/lib/glfm/render_static_html.rb')
-rw-r--r-- | scripts/lib/glfm/render_static_html.rb | 113 |
1 files changed, 53 insertions, 60 deletions
diff --git a/scripts/lib/glfm/render_static_html.rb b/scripts/lib/glfm/render_static_html.rb index a0bda05b41e..8d72aec7c3b 100644 --- a/scripts/lib/glfm/render_static_html.rb +++ b/scripts/lib/glfm/render_static_html.rb @@ -1,80 +1,73 @@ # frozen_string_literal: true +require 'spec_helper' require_relative 'constants' require_relative 'shared' # Purpose: # - Reads a set of markdown examples from a hash which has been serialized to disk -# - Converts each example to static HTML using the `markdown` helper +# - Sets up the appropriate fixture data for the markdown examples +# - Converts each example to static HTML using the appropriate API markdown endpoint # - Writes the HTML for each example to a hash which is serialized to disk # -# It should be invoked via `rails runner` from the Rails root directory. -# It is intended to be invoked from the `update_example_snapshots.rb` script class. -module Glfm - class RenderStaticHtml - include Constants - include Shared - - def process - markdown_yml_path = ARGV[0] - markdown_hash = YAML.load_file(markdown_yml_path) +# Requirements: +# The input and output files are specified via these environment variables: +# - INPUT_MARKDOWN_YML_PATH +# - OUTPUT_STATIC_HTML_TEMPFILE_PATH +# +# Although it is implemented as an RSpec test, it is not a unit test. We use +# RSpec because that is the simplest environment in which we can use the +# Factorybot factory methods to create persisted model objects with stable +# and consistent data values, to ensure consistent example snapshot HTML +# across various machines and environments. RSpec also makes it easy to invoke +# the API # and obtain the response. +# +# It is intended to be invoked as a helper subprocess from the `update_example_snapshots.rb` +# script class. It's not intended to be run or used directly. This usage is also reinforced +# by not naming the file with a `_spec.rb` ending. +RSpec.describe 'Render Static HTML', :api, type: :request do # rubocop:disable RSpec/TopLevelDescribePath + include Glfm::Constants + include Glfm::Shared - context = build_context + # noinspection RailsParamDefResolve (RubyMine can't find the shared context from this file location) + include_context 'with GLFM example snapshot fixtures' - # NOTE: We COULD parallelize this loop like the Javascript WYSIWYG example generation does, - # but it wouldn't save much time. Most of the time is spent loading the Rails environment - # via `rails runner`. In initial testing, this loop only took ~7 seconds while the entire - # script took ~20 seconds. Unfortunately, there's no easy way to execute - # `Banzai.render_and_post_process` without using `rails runner` - static_html_hash = markdown_hash.transform_values do |markdown| - Banzai.render_and_post_process(markdown, context) - end + it 'can create a project dependency graph using factories' do + markdown_hash = YAML.safe_load(File.open(ENV.fetch('INPUT_MARKDOWN_YML_PATH')), symbolize_names: true) + metadata_hash = YAML.safe_load(File.open(ENV.fetch('INPUT_METADATA_YML_PATH')), symbolize_names: true) - static_html_tempfile_path = Dir::Tmpname.create(STATIC_HTML_TEMPFILE_BASENAME) do |path| - tmpfile = File.open(path, 'w') - YAML.dump(static_html_hash, tmpfile) - tmpfile.close - end + # NOTE: We cannot parallelize this loop like the Javascript WYSIWYG example generation does, + # because the rspec `post` API cannot be parallized (it is not thread-safe, it can't find + # the controller). + static_html_hash = markdown_hash.transform_values.with_index do |markdown, index| + name = markdown_hash.keys[index] + api_url = metadata_hash.dig(name, :api_request_override_path) || (api "/markdown") - # Write the path to the output file to stdout - print static_html_tempfile_path - end + post api_url, params: { text: markdown, gfm: true } + # noinspection RubyResolve + expect(response).to be_successful - private + returned_html_value = + begin + parsed_response = Gitlab::Json.parse(response.body, symbolize_names: true) + # Some responses have the HTML in the `html` key, others in the `body` key. + parsed_response[:body] || parsed_response[:html] + rescue JSON::ParserError + # if we got a parsing error, just return the raw response body for debugging purposes. + response.body + end - def build_context - user_username = 'glfm_user_username' - user = User.find_by_username(user_username) || - User.create!( - email: "glfm_user_email@example.com", - name: "glfm_user_name", - password: "glfm_user_password", - username: user_username - ) + returned_html_value + end - # Ensure that we never try to hit Gitaly, even if we - # reload the project - Project.define_method(:skip_disk_validation) do - true - end + write_output_file(static_html_hash) + end - project_name = 'glfm_project_name' - project = Project.find_by_name(project_name) || - Project.create!( - creator: user, - description: "glfm_project_description", - name: project_name, - namespace: user.namespace, - path: 'glfm_project_path' - ) + private - { - only_path: false, - current_user: user, - project: project - } - end + def write_output_file(static_html_hash) + tmpfile = File.open(ENV.fetch('OUTPUT_STATIC_HTML_TEMPFILE_PATH'), 'w') + yaml_string = dump_yaml_with_formatting(static_html_hash) + write_file(tmpfile, yaml_string) end end - -Glfm::RenderStaticHtml.new.process |