Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PDK-430) Do not cache template-url answer if using the default template #265

Merged
merged 1 commit into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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