2019-07-25 01:27:42 -04:00
# frozen_string_literal: true
2016-11-19 16:59:32 -05:00
require 'action_dispatch/testing/test_request'
2016-08-29 12:18:59 -04:00
require 'fileutils'
module JavaScriptFixturesHelpers
2019-02-26 12:11:26 -05:00
extend ActiveSupport :: Concern
2016-08-29 12:18:59 -04:00
include Gitlab :: Popen
2019-03-12 13:20:46 -04:00
extend self
2016-08-29 12:18:59 -04:00
2019-02-26 12:11:26 -05:00
included do | base |
2018-10-29 17:36:28 -04:00
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 }
2019-04-21 06:00:13 -04:00
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 )
2018-10-29 17:36:28 -04:00
end
end
2019-03-12 13:20:46 -04:00
def fixture_root_path
2019-07-17 18:47:33 -04:00
'tmp/tests/frontend/fixtures' + ( Gitlab . ee? ? '-ee' : '' )
2019-03-12 13:20:46 -04:00
end
2016-08-29 12:18:59 -04:00
# Public: Removes all fixture files from given directory
#
2019-03-12 13:20:46 -04:00
# directory_name - directory of the fixtures (relative to .fixture_root_path)
2016-08-29 12:18:59 -04:00
#
def clean_frontend_fixtures ( directory_name )
2019-03-12 13:20:46 -04:00
full_directory_name = File . expand_path ( directory_name , fixture_root_path )
2019-03-26 12:03:28 -04:00
Dir [ File . expand_path ( '*.html' , full_directory_name ) ] . each do | file_name |
2019-03-12 13:20:46 -04:00
FileUtils . rm ( file_name )
2016-08-29 12:18:59 -04:00
end
end
2019-04-21 06:00:13 -04:00
def remove_repository ( project )
Gitlab :: Shell . new . remove_repository ( project . repository_storage , project . disk_path )
end
private
# Private: Store a response object as fixture file
2016-08-29 12:18:59 -04:00
#
2016-12-30 15:46:41 -05:00
# response - string or response object to store
2019-03-12 13:20:46 -04:00
# fixture_file_name - file name to store the fixture in (relative to .fixture_root_path)
2016-08-29 12:18:59 -04:00
#
def store_frontend_fixture ( response , fixture_file_name )
2019-03-12 13:20:46 -04:00
full_fixture_path = File . expand_path ( fixture_file_name , fixture_root_path )
fixture = response . respond_to? ( :body ) ? parse_response ( response ) : response
2016-12-30 15:46:41 -05:00
2019-03-12 13:20:46 -04:00
FileUtils . mkdir_p ( File . dirname ( full_fixture_path ) )
File . write ( full_fixture_path , fixture )
2016-12-30 15:46:41 -05:00
end
# 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