Class: PropertyGenerator::ConfigLinter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ConfigLinter

Returns a new instance of ConfigLinter.



7
8
9
# File 'lib/linter/config_linter.rb', line 7

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

Instance Attribute Details

#configsObject

Returns the value of attribute configs.



5
6
7
# File 'lib/linter/config_linter.rb', line 5

def configs
  @configs
end

Instance Method Details

#check_for_config(path) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/linter/config_linter.rb', line 25

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.
  begin
    YAML.load_file(path)
  rescue
    {}
  end
end

#config_file_is_presentObject



35
36
37
38
39
40
41
42
# File 'lib/linter/config_linter.rb', line 35

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



44
45
46
47
48
49
50
51
52
53
# File 'lib/linter/config_linter.rb', line 44

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_matching_region_and_account_valuesObject



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

def 
  status = {status: 'pass', error: ''}
  environments_missmatch_values = []
  any_missmatches = false
  @configs['environment_configs'].keys.each do |environment|
    unless @configs['environments'].include?(@configs['environment_configs'][environment]['region']) && @configs['accounts'].include?(@configs['environment_configs'][environment]['account'])
      environments_missmatch_values << environment
      any_missmatches = true
    end
    if any_missmatches
      status[:status] = 'fail'
      status[:error] = "Environments: #{environments_missmatch_values} in environment_configs have a region or account value not listed in top level."
    end
  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



55
56
57
58
59
60
61
62
# File 'lib/linter/config_linter.rb', line 55

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



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/linter/config_linter.rb', line 11

def run_config_tests
  if @configs == {}
    tests = ['config_file_is_present']
  else
    tests = ['config_has_correct_keys',
             'environment_configs_match_environments_list',
             'environment_configs_have_matching_region_and_account_values',
             'environment_configs_have_well_formatted_interpolations',
             'config_file_is_present']
  end
  results = PropertyGenerator.test_runner(self, tests)
  results
end