Class: Kitchen::PC::CLI

Inherits:
CLI
  • Object
show all
Defined in:
lib/kitchen/pc/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Intercept the CLI call and merge project and parent configs before firing off the proper CLI intialization



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
# File 'lib/kitchen/pc/cli.rb', line 13

def initialize(*args)
  # Read the configs as provided
  loader = Kitchen::Loader::YAML.new(
    project_config: ENV["KITCHEN_YAML"],
    local_config: ENV["KITCHEN_LOCAL_YAML"],
    global_config: ENV["KITCHEN_GLOBAL_YAML"]
  )
  # For any configs that have parents, merge in the parents
  # and write out to a temporary file, then set the associated
  # environment variable for Test Kitchen to use that file
  tmp_files = []
  configs = [
    { :path_fn => :config_file, :yaml_fn => :yaml, :env_var => 'KITCHEN_YAML' },
    { :path_fn => :local_config_file, :yaml_fn => :local_yaml, :env_var => 'KITCHEN_LOCAL_YAML' },
    { :path_fn => :global_config_file, :yaml_fn => :global_yaml, :env_var => 'KITCHEN_GLOBAL_YAML' },
  ].each do |params|
    config_path = loader.send params[:path_fn]
    config = loader.send params[:yaml_fn]
    if config.key?('parent')
      merged_file = self.class.merge_parent(config)
      ENV[params[:env_var]] = merged_file
      tmp_files.push(merged_file)
    else
      ENV[params[:env_var]] = config_path
    end
  end

  # Cleanup the temp config files on exit
  at_exit do
    begin
      File.unlink(*tmp_files)
    rescue
      # ignore cleanup errors
    end
  end

  # Finally delegate to super
  super
end

Class Method Details

.merge_configs(parent, child) ⇒ Object



81
82
83
# File 'lib/kitchen/pc/cli.rb', line 81

def self.merge_configs(parent, child)
  normalize(parent).rmerge(normalize(child))
end

.merge_parent(config) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kitchen/pc/cli.rb', line 53

def self.merge_parent(config)
  # If the config doesn't have a parent, except
  if !config.key?('parent')
    raise "Bad Programmer! merge_parent called on a config without a parent: #{config.to_s}"
  end
  # Create a temp file to write the merged config
  file = Tempfile.new(['test-kitchen-config', '.yml'])
  begin
    parent = parse_config(File.expand_path(config['parent']))
    merged = merge_configs(parent, config)
    merged.delete('parent')
    file.write merged.to_yaml
  rescue
    file.unlink # Only unlink here if an error occurred,
                #  otherwise we'll leave that up to the caller
                #  to unlink eonce finished processing the file
    raise       # Re-raise the original exception
  ensure
    file.close
  end
  return file.path
end

.normalize(obj) ⇒ Object



85
86
87
# File 'lib/kitchen/pc/cli.rb', line 85

def self.normalize(obj)
  Kitchen::Loader::YAML.new.send(:normalize, obj)
end

.parse_config(config_path) ⇒ Object



76
77
78
79
# File 'lib/kitchen/pc/cli.rb', line 76

def self.parse_config(config_path)
  str = Kitchen::Loader::YAML.new.send(:yaml_string, config_path)
  Kitchen::Loader::YAML.new.send(:parse_yaml_string, str, config_path)
end