Skip to content

Commit

Permalink
(PDK-668) Templatedir now reads .sync.yml for config when rendering t…
Browse files Browse the repository at this point in the history
…emplates.
  • Loading branch information
Helen Campbell committed Nov 16, 2017
1 parent 5945270 commit 2cd9a3a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/pdk/module/templatedir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ def self.files_in_template(dirs)
#
# @api private
def config_for(dest_path)
sync_config_path = File.join(@path, '/.sync.yml')

if @config.nil?
config_path = File.join(@path, 'config_defaults.yml')

Expand All @@ -249,11 +251,23 @@ def config_for(dest_path)
else
@config = {}
end

if File.file?(sync_config_path) && File.readable?(sync_config_path)
begin
@sync_config = YAML.safe_load(File.read(sync_config_path), [], [], true)
rescue StandardError => e
PDK.logger.warn(_("'%{file}' is not a valid YAML file: %{message}") % { file: config_path, message: e.message })
@sync_config = {}
end
else
@sync_config = {}
end
end

file_config = @config.fetch(:global, {})
file_config['module_metadata'] = @module_metadata
file_config.merge!(@config.fetch(dest_path, {})) unless dest_path.nil?
file_config.merge!(@sync_config)
file_config
end
end
Expand Down
44 changes: 43 additions & 1 deletion spec/unit/pdk/module/template_dir_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'yaml'

describe PDK::Module::TemplateDir do
subject(:template_dir) do
Expand All @@ -18,7 +19,6 @@

let(:config_defaults) do
<<-EOS
---
foo:
attr:
- val: 1
Expand Down Expand Up @@ -174,4 +174,46 @@
end
end
end

describe '.config_for(dest_path)' do
before(:each) do
allow(File).to receive(:directory?).with(anything).and_return(true)
allow(PDK::Util).to receive(:make_tmpdir_name).with('pdk-module-template').and_return('/tmp/path')
allow(PDK::CLI::Exec).to receive(:git).with('clone', path_or_url, '/tmp/path').and_return(exit_code: 0)
allow(File).to receive(:file?).with(anything).and_return(File.join(path_or_url, 'config_defaults.yml')).and_return(true)
allow(File).to receive(:read).with(File.join(path_or_url, 'config_defaults.yml')).and_return(config_defaults)
end
context 'when the module has a .sync.yml file' do
let(:yaml_text) do
<<-EOF
appveyor.yml:
delete: true
.travis.yml:
extras:
- rvm: 2.1.9
foo:
attr:
- val: 3
EOF
end
let(:yaml_hash) do
YAML.load(yaml_text)
end
let(:config_hash) do
YAML.load(config_defaults)
end
before(:each) do
allow(File).to receive(:readable?).with('/path/to/templates/config_defaults.yml').and_return true
allow(File).to receive(:file?).with('/path/to/templates/.sync.yml').and_return true
allow(File).to receive(:readable?).with('/path/to/templates/.sync.yml').and_return true
allow(File).to receive(:read).with('/path/to/templates/.sync.yml').and_return yaml_text
allow(YAML).to receive(:safe_load).with(yaml_text, [], [], true).and_return yaml_hash
allow(YAML).to receive(:safe_load).with(config_defaults, [], [], true).and_return config_hash
end

it 'absorbs config' do
expect(template_dir.config_for("/path/to/templates/")).to eq({"module_metadata"=>{"name"=>"foo-bar", "version"=>"0.1.0"}, "appveyor.yml"=>{"delete"=>true}, ".travis.yml"=>{"extras"=>[{"rvm"=>"2.1.9"}]}, "foo"=>{"attr"=>[{"val"=>3}]}})
end
end
end
end

0 comments on commit 2cd9a3a

Please sign in to comment.