2016-11-19 16:59:32 -05:00
|
|
|
require 'action_dispatch/testing/test_request'
|
2016-08-29 12:18:59 -04:00
|
|
|
require 'fileutils'
|
|
|
|
require 'gitlab/popen'
|
|
|
|
|
|
|
|
module JavaScriptFixturesHelpers
|
|
|
|
include Gitlab::Popen
|
|
|
|
|
2017-02-21 18:32:18 -05:00
|
|
|
FIXTURE_PATH = 'spec/javascripts/fixtures'.freeze
|
2016-08-29 12:18:59 -04:00
|
|
|
|
|
|
|
# Public: Removes all fixture files from given directory
|
|
|
|
#
|
|
|
|
# directory_name - directory of the fixtures (relative to FIXTURE_PATH)
|
|
|
|
#
|
|
|
|
def clean_frontend_fixtures(directory_name)
|
|
|
|
directory_name = File.expand_path(directory_name, FIXTURE_PATH)
|
2016-12-30 16:27:40 -05:00
|
|
|
Dir[File.expand_path('*.html.raw', directory_name)].each do |file_name|
|
2016-08-29 12:18:59 -04:00
|
|
|
FileUtils.rm(file_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Public: Store a response object as fixture file
|
|
|
|
#
|
2016-12-30 15:46:41 -05:00
|
|
|
# response - string or response object to store
|
2016-08-29 12:18:59 -04:00
|
|
|
# fixture_file_name - file name to store the fixture in (relative to FIXTURE_PATH)
|
|
|
|
#
|
|
|
|
def store_frontend_fixture(response, fixture_file_name)
|
|
|
|
fixture_file_name = File.expand_path(fixture_file_name, FIXTURE_PATH)
|
2016-12-30 15:46:41 -05:00
|
|
|
fixture = response.respond_to?(:body) ? parse_response(response) : response
|
|
|
|
|
|
|
|
FileUtils.mkdir_p(File.dirname(fixture_file_name))
|
|
|
|
File.write(fixture_file_name, fixture)
|
|
|
|
end
|
|
|
|
|
2017-08-22 19:19:35 -04:00
|
|
|
def remove_repository(project)
|
|
|
|
Gitlab::Shell.new.remove_repository(project.repository_storage_path, project.disk_path)
|
|
|
|
end
|
|
|
|
|
2016-12-30 15:46:41 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
# Private: Prepare a response object for use as a frontend fixture
|
|
|
|
#
|
|
|
|
# response - response object to prepare
|
|
|
|
#
|
|
|
|
def parse_response(response)
|
2016-08-29 12:18:59 -04:00
|
|
|
fixture = response.body
|
2017-01-03 16:26:14 -05:00
|
|
|
fixture.force_encoding("utf-8")
|
2016-08-29 12:18:59 -04:00
|
|
|
|
|
|
|
response_mime_type = Mime::Type.lookup(response.content_type)
|
|
|
|
if response_mime_type.html?
|
|
|
|
doc = Nokogiri::HTML::DocumentFragment.parse(fixture)
|
|
|
|
|
2016-11-23 15:36:34 -05:00
|
|
|
link_tags = doc.css('link')
|
|
|
|
link_tags.remove
|
|
|
|
|
2017-06-02 12:25:18 -04:00
|
|
|
scripts = doc.css("script:not([type='text/template']):not([type='text/x-template'])")
|
2016-08-29 12:18:59 -04:00
|
|
|
scripts.remove
|
|
|
|
|
|
|
|
fixture = doc.to_html
|
|
|
|
|
|
|
|
# replace relative links
|
2016-11-19 16:59:32 -05:00
|
|
|
test_host = ActionDispatch::TestRequest::DEFAULT_ENV['HTTP_HOST']
|
|
|
|
fixture.gsub!(%r{="/}, "=\"http://#{test_host}/")
|
2016-08-29 12:18:59 -04:00
|
|
|
end
|
|
|
|
|
2016-12-30 15:46:41 -05:00
|
|
|
fixture
|
2016-08-29 12:18:59 -04:00
|
|
|
end
|
|
|
|
end
|