debian-mirror-gitlab/spec/support/helpers/javascript_fixtures_helpers.rb

107 lines
3.4 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'action_dispatch/testing/test_request'
require 'fileutils'
2021-11-18 22:05:49 +05:30
require_relative '../../../lib/gitlab/popen'
2017-08-17 22:00:37 +05:30
module JavaScriptFixturesHelpers
2019-07-07 11:18:12 +05:30
extend ActiveSupport::Concern
2017-08-17 22:00:37 +05:30
include Gitlab::Popen
2019-07-07 11:18:12 +05:30
extend self
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
included do |base|
2019-02-15 15:39:39 +05:30
base.around do |example|
2022-01-26 12:08:38 +05:30
# Don't actually run the example when we're only interested in the `test file -> JSON frontend fixture` mapping
if ENV['GENERATE_FRONTEND_FIXTURES_MAPPING'] == 'true'
$fixtures_mapping[example.metadata[:file_path].delete_prefix('./')] << File.join(fixture_root_path, example.description) # rubocop:disable Style/GlobalVars
next
end
2019-02-15 15:39:39 +05:30
# pick an arbitrary date from the past, so tests are not time dependent
2021-04-17 20:07:23 +05:30
# Also see spec/frontend/__helpers__/fake_date/jest.js
2019-02-15 15:39:39 +05:30
Timecop.freeze(Time.utc(2015, 7, 3, 10)) { example.run }
2019-09-04 21:01:54 +05:30
raise NoMethodError.new('You need to set `response` for the fixture generator! This will automatically happen with `type: :controller` or `type: :request`.', 'response') unless respond_to?(:response)
store_frontend_fixture(response, example.description)
2019-02-15 15:39:39 +05:30
end
end
2019-07-07 11:18:12 +05:30
def fixture_root_path
2019-10-12 21:52:04 +05:30
'tmp/tests/frontend/fixtures' + (Gitlab.ee? ? '-ee' : '')
2019-07-07 11:18:12 +05:30
end
2019-09-04 21:01:54 +05:30
def remove_repository(project)
Gitlab::Shell.new.remove_repository(project.repository_storage, project.disk_path)
end
2021-01-03 14:25:43 +05:30
# Public: Reads a GraphQL query from the filesystem as a string
#
2021-11-11 11:23:49 +05:30
# query_path - file path to the GraphQL query, relative to `app/assets/javascripts`.
# ee - boolean, when true `query_path` will be looked up in `/ee`.
def get_graphql_query_as_string(query_path, ee: false)
base = (ee ? 'ee/' : '') + 'app/assets/javascripts'
path = Rails.root / base / query_path
2021-09-30 23:02:18 +05:30
queries = Gitlab::Graphql::Queries.find(path)
if queries.length == 1
queries.first.text(mode: Gitlab.ee? ? :ee : :ce )
else
raise "Could not find query file at #{path}, please check your query_path" % path
end
2021-01-03 14:25:43 +05:30
end
2019-09-04 21:01:54 +05:30
private
# Private: Store a response object as fixture file
2017-08-17 22:00:37 +05:30
#
# response - string or response object to store
2019-07-07 11:18:12 +05:30
# fixture_file_name - file name to store the fixture in (relative to .fixture_root_path)
2017-08-17 22:00:37 +05:30
#
def store_frontend_fixture(response, fixture_file_name)
2019-07-07 11:18:12 +05:30
full_fixture_path = File.expand_path(fixture_file_name, fixture_root_path)
2017-08-17 22:00:37 +05:30
fixture = response.respond_to?(:body) ? parse_response(response) : response
2019-07-07 11:18:12 +05:30
FileUtils.mkdir_p(File.dirname(full_fixture_path))
File.write(full_fixture_path, fixture)
2017-08-17 22:00:37 +05:30
end
2021-09-04 01:27:46 +05:30
def parse_html(fixture)
if respond_to?(:use_full_html) && public_send(:use_full_html)
Nokogiri::HTML::Document.parse(fixture)
else
Nokogiri::HTML::DocumentFragment.parse(fixture)
end
end
2017-08-17 22:00:37 +05:30
# Private: Prepare a response object for use as a frontend fixture
#
# response - response object to prepare
#
def parse_response(response)
fixture = response.body
fixture.force_encoding("utf-8")
2020-03-13 15:44:24 +05:30
response_mime_type = Mime::Type.lookup(response.media_type)
2017-08-17 22:00:37 +05:30
if response_mime_type.html?
2021-09-04 01:27:46 +05:30
doc = parse_html(fixture)
2017-08-17 22:00:37 +05:30
link_tags = doc.css('link')
link_tags.remove
2019-12-21 20:55:43 +05:30
scripts = doc.css("script:not([type='text/template']):not([type='text/x-template']):not([type='application/json'])")
2017-08-17 22:00:37 +05:30
scripts.remove
fixture = doc.to_html
# replace relative links
test_host = ActionDispatch::TestRequest::DEFAULT_ENV['HTTP_HOST']
fixture.gsub!(%r{="/}, "=\"http://#{test_host}/")
end
fixture
end
end