<commit_before>require 'erb'

module Fulmar
  module Domain
    module Service
      # Tests the configuration
      class ConfigTestService

        def initialize(config)
          @config = config
          @config.load_user_config = false
          @config.reset
        end

        # Runs all methods beginning with test_ and returns the report
        def run
          @report = []
          tests = self.methods.select { |name| name.to_s[0, 5] == 'test_' }
          tests.each do |test|
            self.send(test)
          end
          @report
        end

        def test_hostnames_exist
          @config.each do |env, target, data|
            if data[:hostname].blank? && !data[:host].blank?
              @report << {
                message: "#{env}:#{target} has a host (#{data[:host]}) but is missing a hostname",
                severity: :warning
              }
            end
          end
        end

        def test_hostnames_in_ssh_config
          hostnames = ssh_hostnames

          @config.each do |env, target, data|
            next if data[:hostname].blank?

            unless hostnames.include? data[:hostname]
              @report << {
                message: "#{env}:#{target} has a hostname (#{data[:hostname]}) which is not found in your ssh config",
                severity: :info
              }
            end
          end
        end

        def test_required_hostnames
          types = %i(rsync rsync_with_version maria)
          @config.each do |env, target, data|
            if types.include? data[:type] and data[:hostname].blank?
              @report << {
                message: "#{env}:#{target} requires a hostname (#{data[:hostname]})",
                severity: :error
              }
            end
          end
        end

        def test_vhost_template_is_set
          vhost_template = false

          @config.each do |_env, _target, data|
            unless data[:vhost_template].blank?
              vhost_template = true
            end
          end

          add_report(
            'The configuration uses the vhost feature but misses a valid configuration for it (missing :vhost_template)',
            :warning
          ) if @config.feature?(:vhost) && !vhost_template
        end

        # Run simple test which only require one configuration
        def test_simple_tests
          @config.each do |env, target, data|
            tests = self.methods.select { |name| name.to_s[0, 12] == 'simple_test_' }
            tests.each do |test|
              self.send(test, env, target, data)
            end
          end
        end

        def simple_test_local_path_exists(env, target, data)
          unless File.exist? data[:local_path]
            @report << {
              message: "#{env}:#{target} has no valid local_path (#{data[:local_path]})",
              severity: :warning
            }
          end
        end

        def simple_test_mariadb_feature_is_set(env, target, data)
          if data[:type] == :maria && !@config.feature?(:database)
            @report << {
              message: "#{env}:#{target} uses mysql/mariadb but your config is missing the database feature",
              severity: :notice
            }
          end
        end

        def simple_test_vhost_feature_is_set(env, target, data)
          if data[:vhost_template] && !@config.feature?(:vhost)
            @report << {
              message: "#{env}:#{target} refers to a vhost_template but your config is missing the vhost feature",
              severity: :warning
            }
          end
        end

        def simple_test_maria_db_config_exists(env, target, data)
          if data[:type] == :maria && data[:maria][:database].blank?
            add_report "#{env}:#{target} is missing a database name in maria:database", :error
          end<commit_msg>[REFACTORING] Use report method<commit_after>require 'erb'

module Fulmar
  module Domain
    module Service
      # Tests the configuration
      class ConfigTestService

        def initialize(config)
          @config = config
          @config.load_user_config = false
          @config.reset
        end

        # Runs all methods beginning with test_ and returns the report
        def run
          @report = []
          tests = self.methods.select { |name| name.to_s[0, 5] == 'test_' }
          tests.each do |test|
            self.send(test)
          end
          @report
        end

        def test_hostnames_exist
          @config.each do |env, target, data|
            if data[:hostname].blank? && !data[:host].blank?
              @report << {
                message: "#{env}:#{target} has a host (#{data[:host]}) but is missing a hostname",
                severity: :warning
              }
            end
          end
        end

        def test_hostnames_in_ssh_config
          hostnames = ssh_hostnames

          @config.each do |env, target, data|
            next if data[:hostname].blank?

            unless hostnames.include? data[:hostname]
              @report << {
                message: "#{env}:#{target} has a hostname (#{data[:hostname]}) which is not found in your ssh config",
                severity: :info
              }
            end
          end
        end

        def test_required_hostnames
          types = %i(rsync rsync_with_version maria)
          @config.each do |env, target, data|
            if types.include? data[:type] and data[:hostname].blank?
              @report << {
                message: "#{env}:#{target} requires a hostname (#{data[:hostname]})",
                severity: :error
              }
            end
          end
        end

        def test_vhost_template_is_set
          vhost_template = false

          @config.each do |_env, _target, data|
            unless data[:vhost_template].blank?
              vhost_template = true
            end
          end

          add_report(
            'The configuration uses the vhost feature but misses a valid configuration for it (missing :vhost_template)',
            :warning
          ) if @config.feature?(:vhost) && !vhost_template
        end

        # Run simple test which only require one configuration
        def test_simple_tests
          @config.each do |env, target, data|
            tests = self.methods.select { |name| name.to_s[0, 12] == 'simple_test_' }
            tests.each do |test|
              self.send(test, env, target, data)
            end
          end
        end

        def simple_test_local_path_exists(env, target, data)
          unless File.exist? data[:local_path]
            add_report("#{env}:#{target} has no valid local_path (#{data[:local_path]})", :warning)
          end
        end

        def simple_test_mariadb_feature_is_set(env, target, data)
          if data[:type] == :maria && !@config.feature?(:database)
            @report << {
              message: "#{env}:#{target} uses mysql/mariadb but your config is missing the database feature",
              severity: :notice
            }
          end
        end

        def simple_test_vhost_feature_is_set(env, target, data)
          if data[:vhost_template] && !@config.feature?(:vhost)
            @report << {
              message: "#{env}:#{target} refers to a vhost_template but your config is missing the vhost feature",
              severity: :warning
            }
          end
        end

        def simple_test_maria_db_config_exists(env, target, data)
          if data[:type] == :maria && data[:maria][:database].blank?
            add_report "#{env}:#{target} is missing a database name in maria:database", :error
          end
{'fraction_non_alphanumeric': 0.0979394887845592, 'fraction_numerical': 0.0013041210224308817, 'mean_word_length': 1.6814685314685314, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3427196', 'n_tokens_mistral': 2103, 'n_tokens_neox': 2067, 'n_words': 585}