summaryrefslogtreecommitdiff
path: root/tooling/lib/tooling/predictive_tests.rb
blob: 503426b552096b65c8ccc22cc631b2d468989a9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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(
        from: :api,
        changed_files_pathname: 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(
        from: :changed_files,
        changed_files_pathname: rspec_changed_files_path,
        predictive_tests_pathname: rspec_matching_tests_path,
        frontend_fixtures_mapping_pathname: 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