Skip to content

Commit

Permalink
(PDK-430) Do not cache template-url answer if using the default template
Browse files Browse the repository at this point in the history
  • Loading branch information
rodjek committed Aug 28, 2017
1 parent bafb86f commit 714bfe8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
18 changes: 16 additions & 2 deletions lib/pdk/generators/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ class Module
def self.default_template_url
if !PDK.answers['template-url'].nil?
PDK.answers['template-url']
elsif PDK::Util.package_install?
else
puppetlabs_template_url
end
end

def self.puppetlabs_template_url
if PDK::Util.package_install?
'file://' + File.join(PDK::Util.package_cachedir, 'pdk-module-template.git')
else
'https://github.com/puppetlabs/pdk-module-template'
Expand Down Expand Up @@ -68,7 +74,15 @@ def self.invoke(opts = {})
end
end

PDK.answers.update!('template-url' => template_url)
if template_url == puppetlabs_template_url
# If the user specifies our template via the command line, remove the
# saved template-url answer.
PDK.answers.update!('template-url' => nil) if opts.key?(:'template-url')
else
# Save the template-url answer if the module was generated using
# a template other than ours.
PDK.answers.update!('template-url' => template_url)
end

begin
FileUtils.mv(temp_target_dir, target_dir)
Expand Down
13 changes: 12 additions & 1 deletion spec/unit/pdk/generate/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,20 @@
described_class.invoke(invoke_opts.merge(:'template-url' => 'cli-template'))
end

it 'saves the template-url to the answer file' do
it 'saves the template-url to the answer file if it is not the puppetlabs template' do
expect(PDK.answers).to receive(:update!).with('template-url' => 'cli-template')

described_class.invoke(invoke_opts.merge(:'template-url' => 'cli-template'))
end

it 'clears the saved template-url answer if it is the puppetlabs template' do
PDK.answers.update!('template-url' => 'custom-url')
expect(PDK.answers).to receive(:update!).with('template-url' => nil).and_call_original
allow(described_class).to receive(:puppetlabs_template_url).and_return('puppetlabs-url')

described_class.invoke(invoke_opts.merge(:'template-url' => 'puppetlabs-url'))
expect(PDK.answers['template-url']).to eq(nil)
end
end

context 'when a template-url is not supplied on the command line' do
Expand All @@ -191,6 +200,7 @@

it 'uses the vendored template url' do
expect(PDK::Module::TemplateDir).to receive(:new).with('file:///tmp/package/cache/pdk-module-template.git', anything).and_yield(test_template_dir)
expect(PDK.answers).not_to receive(:update!).with(:'template-url' => anything)

described_class.invoke(invoke_opts)
end
Expand All @@ -199,6 +209,7 @@
context 'and pdk is not installed from packages' do
it 'uses the default template to generate the module' do
expect(PDK::Module::TemplateDir).to receive(:new).with(described_class.default_template_url, anything).and_yield(test_template_dir)
expect(PDK.answers).not_to receive(:update!).with(:'template-url' => anything)

described_class.invoke(invoke_opts)
end
Expand Down

0 comments on commit 714bfe8

Please sign in to comment.