Commit 06c5b41a authored by Rémy Coutable's avatar Rémy Coutable

Simplify the JavaScriptFixturesHelpers module

- Only storing fixtures in one place
- This place changes whether we are in CE or EE

We discovered with @winh that only fixtures located under
spec/javascripts/fixtures are used, even in EE so there's no need to
clean/create fixtures in ee/spec/javascripts/fixtures.
Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 90c1702b
unless Rails.env.production?
namespace :karma do
desc 'GitLab | Karma | Generate fixtures for JavaScript tests'
RSpec::Core::RakeTask.new(:fixtures, [:pattern]) do |t, args|
task fixtures: ['karma:copy_emojis_from_public_folder', 'karma:rspec_fixtures']
desc 'GitLab | Karma | Generate fixtures using RSpec'
RSpec::Core::RakeTask.new(:rspec_fixtures, [:pattern]) do |t, args|
args.with_defaults(pattern: '{spec,ee/spec}/javascripts/fixtures/*.rb')
ENV['NO_KNAPSACK'] = 'true'
t.pattern = args[:pattern]
t.rspec_opts = '--format documentation'
end
desc 'GitLab | Karma | Copy emojis file'
task :copy_emojis_from_public_folder do
# Copying the emojis.json from the public folder
fixture_file_name = Rails.root.join('spec/javascripts/fixtures/emojis/emojis.json')
FileUtils.mkdir_p(File.dirname(fixture_file_name))
FileUtils.cp(Rails.root.join('public/-/emojis/1/emojis.json'), fixture_file_name)
end
desc 'GitLab | Karma | Run JavaScript tests'
task tests: ['yarn:check'] do
sh "yarn run karma" do |ok, res|
......
require 'spec_helper'
describe 'Emojis (JavaScript fixtures)' do
include JavaScriptFixturesHelpers
before(:all) do
clean_frontend_fixtures('emojis/')
end
it 'emojis/emojis.json' do |example|
JavaScriptFixturesHelpers::FIXTURE_PATHS.each do |fixture_path|
next unless File.directory?(fixture_path)
# Copying the emojis.json from the public folder
fixture_file_name = File.expand_path('emojis/emojis.json', fixture_path)
FileUtils.mkdir_p(File.dirname(fixture_file_name))
FileUtils.cp(Rails.root.join('public/-/emojis/1/emojis.json'), fixture_file_name)
end
end
end
......@@ -7,25 +7,17 @@ describe ApplicationController, '(Static JavaScript fixtures)', type: :controlle
clean_frontend_fixtures('static/')
end
JavaScriptFixturesHelpers::FIXTURE_PATHS.each do |fixture_path|
fixtures_path = File.expand_path(fixture_path, Rails.root)
Dir.glob(File.expand_path('**/*.haml', fixtures_path)).map do |file_path|
template_file_name = file_path.sub(/\A#{fixtures_path}#{File::SEPARATOR}/, '')
it "static/#{template_file_name.sub(/\.haml\z/, '.raw')}" do |example|
fixture_file_name = example.description
rendered = render_template(fixture_path, template_file_name)
store_frontend_fixture(rendered, fixture_file_name)
end
Dir.glob('{,ee/}spec/javascripts/fixtures/**/*.haml').map do |file_path|
it "static/#{file_path.sub(%r{\A(ee/)?spec/javascripts/fixtures/}, '').sub(/\.haml\z/, '.raw')}" do |example|
store_frontend_fixture(render_template(file_path), example.description)
end
end
private
def render_template(fixture_path, template_file_name)
def render_template(template_file_name)
controller = ApplicationController.new
controller.prepend_view_path(fixture_path)
controller.render_to_string(template: template_file_name, layout: false)
controller.prepend_view_path(File.dirname(template_file_name))
controller.render_to_string(template: File.basename(template_file_name), layout: false)
end
end
......@@ -5,7 +5,7 @@ module JavaScriptFixturesHelpers
extend ActiveSupport::Concern
include Gitlab::Popen
FIXTURE_PATHS = %w[spec/javascripts/fixtures ee/spec/javascripts/fixtures].freeze
extend self
included do |base|
base.around do |example|
......@@ -14,32 +14,32 @@ module JavaScriptFixturesHelpers
end
end
def fixture_root_path
'spec/javascripts/fixtures'
end
# Public: Removes all fixture files from given directory
#
# directory_name - directory of the fixtures (relative to FIXTURE_PATHS)
# directory_name - directory of the fixtures (relative to .fixture_root_path)
#
def clean_frontend_fixtures(directory_name)
FIXTURE_PATHS.each do |fixture_path|
directory_name = File.expand_path(directory_name, fixture_path)
Dir[File.expand_path('*.html.raw', directory_name)].each do |file_name|
FileUtils.rm(file_name)
end
full_directory_name = File.expand_path(directory_name, fixture_root_path)
Dir[File.expand_path('*.html.raw', full_directory_name)].each do |file_name|
FileUtils.rm(file_name)
end
end
# Public: Store a response object as fixture file
#
# response - string or response object to store
# fixture_file_name - file name to store the fixture in (relative to FIXTURE_PATHS)
# fixture_file_name - file name to store the fixture in (relative to .fixture_root_path)
#
def store_frontend_fixture(response, fixture_file_name)
FIXTURE_PATHS.each do |fixture_path|
fixture_file_name = File.expand_path(fixture_file_name, fixture_path)
fixture = response.respond_to?(:body) ? parse_response(response) : response
full_fixture_path = File.expand_path(fixture_file_name, fixture_root_path)
fixture = response.respond_to?(:body) ? parse_response(response) : response
FileUtils.mkdir_p(File.dirname(fixture_file_name))
File.write(fixture_file_name, fixture)
end
FileUtils.mkdir_p(File.dirname(full_fixture_path))
File.write(full_fixture_path, fixture)
end
def remove_repository(project)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment