diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-04-11 21:08:31 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-04-11 21:08:31 +0300 |
commit | 1a2f754734eb189e371e25e685413808f69a7f2c (patch) | |
tree | 2c97884971f36d9026600897b74364d2e212a109 /tooling/lib | |
parent | f1ce71c88c407709987dd4a7b40bdb7596b6baa2 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'tooling/lib')
-rwxr-xr-x | tooling/lib/tooling/find_changes.rb | 45 | ||||
-rw-r--r-- | tooling/lib/tooling/find_tests.rb | 10 | ||||
-rw-r--r-- | tooling/lib/tooling/helpers/file_handler.rb | 12 | ||||
-rw-r--r-- | tooling/lib/tooling/mappings/graphql_base_type_mappings.rb | 10 | ||||
-rw-r--r-- | tooling/lib/tooling/mappings/js_to_system_specs_mappings.rb | 16 | ||||
-rw-r--r-- | tooling/lib/tooling/mappings/partial_to_views_mappings.rb | 12 | ||||
-rw-r--r-- | tooling/lib/tooling/mappings/view_to_js_mappings.rb | 14 | ||||
-rw-r--r-- | tooling/lib/tooling/mappings/view_to_system_specs_mappings.rb | 12 | ||||
-rw-r--r-- | tooling/lib/tooling/predictive_tests.rb | 53 |
9 files changed, 126 insertions, 58 deletions
diff --git a/tooling/lib/tooling/find_changes.rb b/tooling/lib/tooling/find_changes.rb index e151734e41c..b4439bd4abe 100755 --- a/tooling/lib/tooling/find_changes.rb +++ b/tooling/lib/tooling/find_changes.rb @@ -2,28 +2,35 @@ # frozen_string_literal: true require 'gitlab' +require_relative 'helpers/file_handler' module Tooling class FindChanges - def initialize(output_file: nil, matched_tests_file: nil, frontend_fixtures_mapping_path: nil) - @gitlab_token = ENV['PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE'] || '' - @gitlab_endpoint = ENV['CI_API_V4_URL'] - @mr_project_path = ENV['CI_MERGE_REQUEST_PROJECT_PATH'] - @mr_iid = ENV['CI_MERGE_REQUEST_IID'] - @output_file = output_file - @matched_tests_file = matched_tests_file - @frontend_fixtures_mapping_path = frontend_fixtures_mapping_path + include Helpers::FileHandler + + def initialize( + changed_files_pathname = nil, predictive_tests_pathname = nil, frontend_fixtures_mapping_pathname = nil + ) + @gitlab_token = ENV['PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE'] || '' + @gitlab_endpoint = ENV['CI_API_V4_URL'] + @mr_project_path = ENV['CI_MERGE_REQUEST_PROJECT_PATH'] + @mr_iid = ENV['CI_MERGE_REQUEST_IID'] + @changed_files_pathname = changed_files_pathname + @predictive_tests_pathname = predictive_tests_pathname + @frontend_fixtures_mapping_pathname = frontend_fixtures_mapping_pathname end def execute - raise ArgumentError, "An path to an output file must be given as first argument." if output_file.nil? + if changed_files_pathname.nil? + raise ArgumentError, "A path to the changed files file must be given as first argument." + end add_frontend_fixture_files! - File.write(output_file, file_changes.join(' ')) + write_array_to_file(changed_files_pathname, file_changes, overwrite: true) end def only_js_files_changed - @output_file = nil # We ensure that we'll get the diff from the MR directly, not from a file. + @changed_files_pathname = nil # We ensure that we'll get the diff from the MR directly, not from a file. file_changes.any? && file_changes.all? { |file| file.end_with?('.js') } end @@ -31,7 +38,7 @@ module Tooling private attr_reader :gitlab_token, :gitlab_endpoint, :mr_project_path, - :mr_iid, :output_file, :matched_tests_file, :frontend_fixtures_mapping_path + :mr_iid, :changed_files_pathname, :predictive_tests_pathname, :frontend_fixtures_mapping_pathname def gitlab @gitlab ||= begin @@ -45,7 +52,7 @@ module Tooling end def add_frontend_fixture_files? - matched_tests_file && frontend_fixtures_mapping_path + predictive_tests_pathname && frontend_fixtures_mapping_pathname end def add_frontend_fixture_files! @@ -61,8 +68,8 @@ module Tooling def file_changes @file_changes ||= - if output_file && File.exist?(output_file) - File.read(output_file).split(' ') + if changed_files_pathname && File.exist?(changed_files_pathname) + read_array_from_file(changed_files_pathname) else mr_changes.changes.flat_map do |change| change.to_h.values_at('old_path', 'new_path') @@ -75,15 +82,15 @@ module Tooling end def test_files - return [] if !matched_tests_file || !File.exist?(matched_tests_file) + return [] if !predictive_tests_pathname || !File.exist?(predictive_tests_pathname) - File.read(matched_tests_file).split(' ') + read_array_from_file(predictive_tests_pathname) end def frontend_fixtures_mapping - return {} if !frontend_fixtures_mapping_path || !File.exist?(frontend_fixtures_mapping_path) + return {} if !frontend_fixtures_mapping_pathname || !File.exist?(frontend_fixtures_mapping_pathname) - JSON.parse(File.read(frontend_fixtures_mapping_path)) # rubocop:disable Gitlab/Json + JSON.parse(File.read(frontend_fixtures_mapping_pathname)) # rubocop:disable Gitlab/Json end end end diff --git a/tooling/lib/tooling/find_tests.rb b/tooling/lib/tooling/find_tests.rb index b63b207c58b..f26c1eacdc7 100644 --- a/tooling/lib/tooling/find_tests.rb +++ b/tooling/lib/tooling/find_tests.rb @@ -7,9 +7,9 @@ module Tooling class FindTests include Helpers::FileHandler - def initialize(changes_file, matching_tests_paths) - @matching_tests_paths = matching_tests_paths - @changed_files = read_array_from_file(changes_file) + def initialize(changed_files_pathname, predictive_tests_pathname) + @predictive_tests_pathname = predictive_tests_pathname + @changed_files = read_array_from_file(changed_files_pathname) end def execute @@ -21,11 +21,11 @@ module Tooling end end - write_array_to_file(matching_tests_paths, tff.test_files.uniq) + write_array_to_file(predictive_tests_pathname, tff.test_files.uniq) end private - attr_reader :changed_files, :matching_tests, :matching_tests_paths + attr_reader :changed_files, :matching_tests, :predictive_tests_pathname end end diff --git a/tooling/lib/tooling/helpers/file_handler.rb b/tooling/lib/tooling/helpers/file_handler.rb index ec4f42ea363..2778bb1ffbc 100644 --- a/tooling/lib/tooling/helpers/file_handler.rb +++ b/tooling/lib/tooling/helpers/file_handler.rb @@ -11,10 +11,18 @@ module Tooling File.read(file).split(' ') end - def write_array_to_file(file, content_array) + def write_array_to_file(file, content_array, overwrite: false) FileUtils.touch file - output_content = (File.read(file).split(' ') + content_array).join(' ') + # We sort the array to make it easier to read the output file + content_array.sort! + + output_content = + if overwrite + content_array.join(' ') + else + (File.read(file).split(' ') + content_array).join(' ') + end File.write(file, output_content) end diff --git a/tooling/lib/tooling/mappings/graphql_base_type_mappings.rb b/tooling/lib/tooling/mappings/graphql_base_type_mappings.rb index cd8b55d5820..569a8278163 100644 --- a/tooling/lib/tooling/mappings/graphql_base_type_mappings.rb +++ b/tooling/lib/tooling/mappings/graphql_base_type_mappings.rb @@ -25,9 +25,9 @@ module Tooling 'jh' => GRAPHQL_TYPES_FOLDERS_JH }.freeze - def initialize(changes_file, matching_tests_paths) - @matching_tests_paths = matching_tests_paths - @changed_files = read_array_from_file(changes_file) + def initialize(changed_files_pathname, predictive_tests_pathname) + @predictive_tests_pathname = predictive_tests_pathname + @changed_files = read_array_from_file(changed_files_pathname) end def execute @@ -46,7 +46,7 @@ module Tooling end end.compact.uniq - write_array_to_file(matching_tests_paths, matching_graphql_tests) + write_array_to_file(predictive_tests_pathname, matching_graphql_tests) end def filter_files @@ -113,7 +113,7 @@ module Tooling private - attr_reader :changed_files, :matching_tests_paths + attr_reader :changed_files, :predictive_tests_pathname end end end diff --git a/tooling/lib/tooling/mappings/js_to_system_specs_mappings.rb b/tooling/lib/tooling/mappings/js_to_system_specs_mappings.rb index 0c61b921f83..b2fca3a765a 100644 --- a/tooling/lib/tooling/mappings/js_to_system_specs_mappings.rb +++ b/tooling/lib/tooling/mappings/js_to_system_specs_mappings.rb @@ -10,13 +10,13 @@ module Tooling module Mappings class JsToSystemSpecsMappings < Base def initialize( - changes_file, matching_tests_paths, + changed_files_pathname, predictive_tests_pathname, js_base_folder: 'app/assets/javascripts', system_specs_base_folder: 'spec/features') - @changed_files = read_array_from_file(changes_file) - @matching_tests_paths = matching_tests_paths - @js_base_folder = js_base_folder - @js_base_folders = folders_for_available_editions(js_base_folder) - @system_specs_base_folder = system_specs_base_folder + @changed_files = read_array_from_file(changed_files_pathname) + @predictive_tests_pathname = predictive_tests_pathname + @js_base_folder = js_base_folder + @js_base_folders = folders_for_available_editions(js_base_folder) + @system_specs_base_folder = system_specs_base_folder # Cannot be extracted to a constant, as it depends on a variable @first_js_folder_extract_regexp = %r{ @@ -36,7 +36,7 @@ module Tooling end end - write_array_to_file(matching_tests_paths, matching_system_tests) + write_array_to_file(predictive_tests_pathname, matching_system_tests) end # Keep the files that are in the @js_base_folders folders @@ -66,7 +66,7 @@ module Tooling private - attr_reader :changed_files, :matching_tests_paths + attr_reader :changed_files, :predictive_tests_pathname end end end diff --git a/tooling/lib/tooling/mappings/partial_to_views_mappings.rb b/tooling/lib/tooling/mappings/partial_to_views_mappings.rb index 3109da685f1..8b0a5ed4ecd 100644 --- a/tooling/lib/tooling/mappings/partial_to_views_mappings.rb +++ b/tooling/lib/tooling/mappings/partial_to_views_mappings.rb @@ -7,10 +7,10 @@ require_relative '../../../../lib/gitlab_edition' module Tooling module Mappings class PartialToViewsMappings < Base - def initialize(changes_file, output_file, view_base_folder: 'app/views') - @output_file = output_file - @changed_files = read_array_from_file(changes_file) - @view_base_folders = folders_for_available_editions(view_base_folder) + def initialize(changed_files_pathname, views_with_partials_pathname, view_base_folder: 'app/views') + @views_with_partials_pathname = views_with_partials_pathname + @changed_files = read_array_from_file(changed_files_pathname) + @view_base_folders = folders_for_available_editions(view_base_folder) end def execute @@ -28,7 +28,7 @@ module Tooling end end - write_array_to_file(output_file, views_including_modified_partials) + write_array_to_file(views_with_partials_pathname, views_including_modified_partials) end def filter_files @@ -99,7 +99,7 @@ module Tooling private - attr_reader :changed_files, :output_file, :view_base_folders + attr_reader :changed_files, :views_with_partials_pathname, :view_base_folders end end end diff --git a/tooling/lib/tooling/mappings/view_to_js_mappings.rb b/tooling/lib/tooling/mappings/view_to_js_mappings.rb index fd2db6b6d7e..f2098d6acd5 100644 --- a/tooling/lib/tooling/mappings/view_to_js_mappings.rb +++ b/tooling/lib/tooling/mappings/view_to_js_mappings.rb @@ -14,12 +14,12 @@ module Tooling RAILS_PARTIAL_INVOCATION_REGEXP = %r{(?:render|render_if_exists)(?: |\()(?:partial: ?)?['"]([\w/-]+)['"]}.freeze def initialize( - changes_file, matching_tests_paths, + changed_files_pathname, predictive_tests_pathname, view_base_folder: 'app/views', js_base_folder: 'app/assets/javascripts') - @changed_files = read_array_from_file(changes_file) - @matching_tests_paths = matching_tests_paths - @view_base_folders = folders_for_available_editions(view_base_folder) - @js_base_folders = folders_for_available_editions(js_base_folder) + @changed_files = read_array_from_file(changed_files_pathname) + @predictive_tests_pathname = predictive_tests_pathname + @view_base_folders = folders_for_available_editions(view_base_folder) + @js_base_folders = folders_for_available_editions(js_base_folder) end def execute @@ -40,7 +40,7 @@ module Tooling end end - write_array_to_file(matching_tests_paths, matching_js_files) + write_array_to_file(predictive_tests_pathname, matching_js_files) end # Keep the files that are in the @view_base_folders folder @@ -76,7 +76,7 @@ module Tooling private - attr_reader :changed_files, :matching_tests_paths + attr_reader :changed_files, :predictive_tests_pathname end end end diff --git a/tooling/lib/tooling/mappings/view_to_system_specs_mappings.rb b/tooling/lib/tooling/mappings/view_to_system_specs_mappings.rb index 227f2c6e62b..6d840dcbd71 100644 --- a/tooling/lib/tooling/mappings/view_to_system_specs_mappings.rb +++ b/tooling/lib/tooling/mappings/view_to_system_specs_mappings.rb @@ -7,10 +7,10 @@ require_relative '../../../../lib/gitlab_edition' module Tooling module Mappings class ViewToSystemSpecsMappings < Base - def initialize(changes_file, output_file, view_base_folder: 'app/views') - @output_file = output_file - @changed_files = read_array_from_file(changes_file) - @view_base_folders = folders_for_available_editions(view_base_folder) + def initialize(changed_files_pathname, predictive_tests_pathname, view_base_folder: 'app/views') + @predictive_tests_pathname = predictive_tests_pathname + @changed_files = read_array_from_file(changed_files_pathname) + @view_base_folders = folders_for_available_editions(view_base_folder) end def execute @@ -27,12 +27,12 @@ module Tooling end end - write_array_to_file(output_file, found_system_specs.compact.uniq.sort) + write_array_to_file(predictive_tests_pathname, found_system_specs.compact.uniq.sort) end private - attr_reader :changed_files, :output_file, :view_base_folders + attr_reader :changed_files, :predictive_tests_pathname, :view_base_folders # Keep the views files that are in the @view_base_folders folder def filter_files diff --git a/tooling/lib/tooling/predictive_tests.rb b/tooling/lib/tooling/predictive_tests.rb new file mode 100644 index 00000000000..2691e1ba56d --- /dev/null +++ b/tooling/lib/tooling/predictive_tests.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require_relative 'find_changes' +require_relative 'find_tests' +require_relative 'mappings/graphql_base_type_mappings' +require_relative 'mappings/js_to_system_specs_mappings' +require_relative 'mappings/partial_to_views_mappings' +require_relative 'mappings/view_to_js_mappings' +require_relative 'mappings/view_to_system_specs_mappings' + +module Tooling + class PredictiveTests + REQUIRED_ENV_VARIABLES = %w[ + RSPEC_CHANGED_FILES_PATH + RSPEC_MATCHING_TESTS_PATH + RSPEC_VIEWS_INCLUDING_PARTIALS_PATH + FRONTEND_FIXTURES_MAPPING_PATH + RSPEC_MATCHING_JS_FILES_PATH + ].freeze + + def initialize + missing_env_variables = REQUIRED_ENV_VARIABLES.select { |key| ENV[key.to_s] == '' } + unless missing_env_variables.empty? + raise "[predictive tests] Missing ENV variable(s): #{missing_env_variables.join(',')}." + end + + @rspec_changed_files_path = ENV['RSPEC_CHANGED_FILES_PATH'] + @rspec_matching_tests_path = ENV['RSPEC_MATCHING_TESTS_PATH'] + @rspec_views_including_partials_path = ENV['RSPEC_VIEWS_INCLUDING_PARTIALS_PATH'] + @frontend_fixtures_mapping_path = ENV['FRONTEND_FIXTURES_MAPPING_PATH'] + @rspec_matching_js_files_path = ENV['RSPEC_MATCHING_JS_FILES_PATH'] + end + + def execute + Tooling::FindChanges.new(rspec_changed_files_path).execute + Tooling::FindTests.new(rspec_changed_files_path, rspec_matching_tests_path).execute + Tooling::Mappings::PartialToViewsMappings.new( + rspec_changed_files_path, rspec_views_including_partials_path).execute + Tooling::FindTests.new(rspec_views_including_partials_path, rspec_matching_tests_path).execute + Tooling::Mappings::JsToSystemSpecsMappings.new(rspec_changed_files_path, rspec_matching_tests_path).execute + Tooling::Mappings::GraphqlBaseTypeMappings.new(rspec_changed_files_path, rspec_matching_tests_path).execute + Tooling::Mappings::ViewToSystemSpecsMappings.new(rspec_changed_files_path, rspec_matching_tests_path).execute + Tooling::FindChanges.new( + rspec_changed_files_path, rspec_matching_tests_path, frontend_fixtures_mapping_path).execute + Tooling::Mappings::ViewToJsMappings.new(rspec_changed_files_path, rspec_matching_js_files_path).execute + end + + private + + attr_reader :rspec_changed_files_path, :rspec_matching_tests_path, :rspec_views_including_partials_path, + :frontend_fixtures_mapping_path, :rspec_matching_js_files_path + end +end |