From b789bfaeefd9838c33f13c028893424025ade1ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Tue, 12 Mar 2019 18:20:46 +0100 Subject: [PATCH] Simplify the JavaScriptFixturesHelpers module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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: Rémy Coutable --- lib/tasks/karma.rake | 13 ++++++++- spec/javascripts/fixtures/emojis.rb | 20 ------------- spec/javascripts/fixtures/static_fixtures.rb | 20 ++++--------- .../helpers/javascript_fixtures_helpers.rb | 28 +++++++++---------- 4 files changed, 32 insertions(+), 49 deletions(-) delete mode 100644 spec/javascripts/fixtures/emojis.rb diff --git a/lib/tasks/karma.rake b/lib/tasks/karma.rake index 53325d492d1..02987f2beef 100644 --- a/lib/tasks/karma.rake +++ b/lib/tasks/karma.rake @@ -1,13 +1,24 @@ 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| diff --git a/spec/javascripts/fixtures/emojis.rb b/spec/javascripts/fixtures/emojis.rb deleted file mode 100644 index 4dab697e5e2..00000000000 --- a/spec/javascripts/fixtures/emojis.rb +++ /dev/null @@ -1,20 +0,0 @@ -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 diff --git a/spec/javascripts/fixtures/static_fixtures.rb b/spec/javascripts/fixtures/static_fixtures.rb index 852a82587b9..78224471ea9 100644 --- a/spec/javascripts/fixtures/static_fixtures.rb +++ b/spec/javascripts/fixtures/static_fixtures.rb @@ -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 diff --git a/spec/support/helpers/javascript_fixtures_helpers.rb b/spec/support/helpers/javascript_fixtures_helpers.rb index f525b2f945e..cceb179d53e 100644 --- a/spec/support/helpers/javascript_fixtures_helpers.rb +++ b/spec/support/helpers/javascript_fixtures_helpers.rb @@ -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)