Class: PropertyGenerator::ConfigLinter

Inherits:
Object
  • Object
show all
Defined in:
lib/linter/config_linter.rb

Constant Summary collapse

TESTS =
[
  'config_has_correct_keys',
  'environment_configs_match_environments_list',
  'environment_configs_have_valid_region_and_account_values',
  'environment_configs_have_well_formatted_interpolations',
  'config_file_is_present'
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, ignored_tests) ⇒ ConfigLinter

Returns a new instance of ConfigLinter.



14
15
16
17
# File 'lib/linter/config_linter.rb', line 14

def initialize(path, ignored_tests)
  @configs = check_for_config(path)
  @ignored_tests = ignored_tests
end

Instance Attribute Details

#configsObject

Returns the value of attribute configs.



12
13
14
# File 'lib/linter/config_linter.rb', line 12

def configs
  @configs
end

Instance Method Details

#check_for_config(path) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/linter/config_linter.rb', line 30

def check_for_config(path)
  # Tries to load the config file - if is unable to load config.yml it returns an empty hash
  # Empty hash is returned so that the rest of the files are still able to be linted instead of stopping at this point.

  YAML.load_file(path)
rescue StandardError
  {}
end

#config_file_is_presentObject



39
40
41
42
43
44
45
46
# File 'lib/linter/config_linter.rb', line 39

def config_file_is_present
  status = { status: 'pass', error: '' }
  if @configs == {}
    status[:status] = 'fail'
    status[:error] = 'Config.yml file is missing, it is required.'
  end
  status
end

#config_has_correct_keysObject



48
49
50
51
52
53
54
55
56
# File 'lib/linter/config_linter.rb', line 48

def config_has_correct_keys
  status = { status: 'pass', error: '' }
  config_keys = ['environments', 'accounts', 'environment_configs']
  if @configs.keys != config_keys
    status[:status] = 'fail'
    status[:error] = "Config keys should be 'environments', 'accounts', and 'environment_configs'."
  end
  status
end

#environment_configs_have_valid_region_and_account_valuesObject



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/linter/config_linter.rb', line 67

def 
  status = { status: 'pass', error: '' }
  environments_missmatch_values = @configs['environment_configs'].reject do |_, env_config|
    env_config.key?('region') && @configs['accounts'].include?(env_config['account'])
  end.keys

  unless environments_missmatch_values.empty?
    status[:status] = 'fail'
    status[:error] = "Environments: #{environments_missmatch_values} in environment_configs have a region or account value not listed in top level."
  end

  status
end

#environment_configs_have_well_formatted_interpolationsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/linter/config_linter.rb', line 81

def environment_configs_have_well_formatted_interpolations
  status = { status: 'pass', error: '' }
  environments_with_bad_interpolations = []
  any_mistakes = false
  @configs['environment_configs'].keys.each do |environment|
    if @configs['environment_configs'][environment]['interpolations'].class == Hash
      @configs['environment_configs'][environment]['interpolations'].each do |interpolation, value|
        if value.class != String && value.class != Integer && value.class != Float && value.class != Fixnum
          environments_with_bad_interpolations << { environment => interpolation }
          any_mistakes = true
        end
      end
    else
      environments_with_bad_interpolations << environment
      any_mistakes = true
    end
  end
  if any_mistakes
    status[:status] = 'fail'
    status[:error] = "Incorrectly formatted interpolations in Environments: #{environments_with_bad_interpolations}"
  end
  status
end

#environment_configs_match_environments_listObject



58
59
60
61
62
63
64
65
# File 'lib/linter/config_linter.rb', line 58

def environment_configs_match_environments_list
  status = { status: 'pass', error: '' }
  if @configs['environments'] != @configs['environment_configs'].keys
    status[:status] = 'fail'
    status[:error] = 'Environments in environment_configs do not match environments listed in config environments.'
  end
  status
end

#run_config_testsObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/linter/config_linter.rb', line 19

def run_config_tests
  if @configs == {}
    tests = ['config_file_is_present']
  else
    tests = TESTS
  end
  tests -= @ignored_tests

  PropertyGenerator.test_runner(self, tests)
end