Commit b789bfae 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 68aacd65
unless Rails.env.production? unless Rails.env.production?
namespace :karma do namespace :karma do
desc 'GitLab | Karma | Generate fixtures for JavaScript tests' 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') args.with_defaults(pattern: '{spec,ee/spec}/javascripts/fixtures/*.rb')
ENV['NO_KNAPSACK'] = 'true' ENV['NO_KNAPSACK'] = 'true'
t.pattern = args[:pattern] t.pattern = args[:pattern]
t.rspec_opts = '--format documentation' t.rspec_opts = '--format documentation'
end 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' desc 'GitLab | Karma | Run JavaScript tests'
task tests: ['yarn:check'] do task tests: ['yarn:check'] do
sh "yarn run karma" do |ok, res| 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 ...@@ -7,25 +7,17 @@ describe ApplicationController, '(Static JavaScript fixtures)', type: :controlle
clean_frontend_fixtures('static/') clean_frontend_fixtures('static/')
end end
JavaScriptFixturesHelpers::FIXTURE_PATHS.each do |fixture_path| Dir.glob('{,ee/}spec/javascripts/fixtures/**/*.haml').map do |file_path|
fixtures_path = File.expand_path(fixture_path, Rails.root) 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)
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
end end
end end
private private
def render_template(fixture_path, template_file_name) def render_template(template_file_name)
controller = ApplicationController.new controller = ApplicationController.new
controller.prepend_view_path(fixture_path) controller.prepend_view_path(File.dirname(template_file_name))
controller.render_to_string(template: template_file_name, layout: false) controller.render_to_string(template: File.basename(template_file_name), layout: false)
end end
end end
...@@ -5,7 +5,7 @@ module JavaScriptFixturesHelpers ...@@ -5,7 +5,7 @@ module JavaScriptFixturesHelpers
extend ActiveSupport::Concern extend ActiveSupport::Concern
include Gitlab::Popen include Gitlab::Popen
FIXTURE_PATHS = %w[spec/javascripts/fixtures ee/spec/javascripts/fixtures].freeze extend self
included do |base| included do |base|
base.around do |example| base.around do |example|
...@@ -14,32 +14,32 @@ module JavaScriptFixturesHelpers ...@@ -14,32 +14,32 @@ module JavaScriptFixturesHelpers
end end
end end
def fixture_root_path
'spec/javascripts/fixtures'
end
# Public: Removes all fixture files from given directory # 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) def clean_frontend_fixtures(directory_name)
FIXTURE_PATHS.each do |fixture_path| full_directory_name = File.expand_path(directory_name, fixture_root_path)
directory_name = File.expand_path(directory_name, fixture_path) Dir[File.expand_path('*.html.raw', full_directory_name)].each do |file_name|
Dir[File.expand_path('*.html.raw', directory_name)].each do |file_name| FileUtils.rm(file_name)
FileUtils.rm(file_name)
end
end end
end end
# Public: Store a response object as fixture file # Public: Store a response object as fixture file
# #
# response - string or response object to store # 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) def store_frontend_fixture(response, fixture_file_name)
FIXTURE_PATHS.each do |fixture_path| full_fixture_path = File.expand_path(fixture_file_name, fixture_root_path)
fixture_file_name = File.expand_path(fixture_file_name, fixture_path) fixture = response.respond_to?(:body) ? parse_response(response) : response
fixture = response.respond_to?(:body) ? parse_response(response) : response
FileUtils.mkdir_p(File.dirname(fixture_file_name)) FileUtils.mkdir_p(File.dirname(full_fixture_path))
File.write(fixture_file_name, fixture) File.write(full_fixture_path, fixture)
end
end end
def remove_repository(project) 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