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

79 lines
2.2 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
require 'action_dispatch/testing/test_request'
require 'fileutils'
module JavaScriptFixturesHelpers
2019-05-18 00:54:41 +05:30
extend ActiveSupport::Concern
2017-08-17 22:00:37 +05:30
include Gitlab::Popen
2019-05-18 00:54:41 +05:30
extend self
2017-08-17 22:00:37 +05:30
2019-05-18 00:54:41 +05:30
included do |base|
2019-02-15 15:39:39 +05:30
base.around do |example|
# pick an arbitrary date from the past, so tests are not time dependent
Timecop.freeze(Time.utc(2015, 7, 3, 10)) { example.run }
end
end
2019-05-18 00:54:41 +05:30
def fixture_root_path
'spec/javascripts/fixtures'
end
2017-08-17 22:00:37 +05:30
# Public: Removes all fixture files from given directory
#
2019-05-18 00:54:41 +05:30
# directory_name - directory of the fixtures (relative to .fixture_root_path)
2017-08-17 22:00:37 +05:30
#
def clean_frontend_fixtures(directory_name)
2019-05-18 00:54:41 +05:30
full_directory_name = File.expand_path(directory_name, fixture_root_path)
Dir[File.expand_path('*.html', full_directory_name)].each do |file_name|
2017-08-17 22:00:37 +05:30
FileUtils.rm(file_name)
end
end
# Public: Store a response object as fixture file
#
# response - string or response object to store
2019-05-18 00:54:41 +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-05-18 00:54:41 +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-05-18 00:54:41 +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
2018-03-17 18:26:18 +05:30
def remove_repository(project)
2018-10-15 14:42:47 +05:30
Gitlab::Shell.new.remove_repository(project.repository_storage, project.disk_path)
2018-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +05:30
private
# 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")
response_mime_type = Mime::Type.lookup(response.content_type)
if response_mime_type.html?
doc = Nokogiri::HTML::DocumentFragment.parse(fixture)
link_tags = doc.css('link')
link_tags.remove
2017-09-10 17:25:29 +05:30
scripts = doc.css("script:not([type='text/template']):not([type='text/x-template'])")
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