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-739) Fall back to default template if necessary #400

Merged
merged 2 commits into from
Jan 9, 2018
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
6 changes: 6 additions & 0 deletions lib/pdk/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ def default_template_url
return puppetlabs_template_url if answer_file_url == 'https://github.com/puppetlabs/pdk-module-template'
return puppetlabs_template_url if answer_file_url == puppetlabs_template_url

unless PDK::Util::Git.repo_exists?(answer_file_url)
PDK.logger.warn(_("Unable to access the previously used template '%{template}', using the default template instead.") % { template: answer_file_url })
PDK.answers.update!('template-url' => nil)
return puppetlabs_template_url
end

answer_file_url
end
module_function :default_template_url
Expand Down
6 changes: 6 additions & 0 deletions lib/pdk/util/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def self.git(*args)

PDK::CLI::Exec.execute(git_bin, *args)
end

def self.repo_exists?(repo, ref = nil)
args = ['ls-remote', '--exit-code', repo, ref].compact

git(*args)[:exit_code].zero?
end
end
end
end
5 changes: 5 additions & 0 deletions spec/unit/pdk/generate/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
context 'when a template-url is supplied on the command line' do
before(:each) do
allow(FileUtils).to receive(:mv).with(temp_target_dir, target_dir).and_return(0)
allow(PDK::Util).to receive(:default_template_url).and_return('https://github.com/puppetlabs/pdk-templates')
end

it 'uses that template to generate the module' do
Expand Down Expand Up @@ -204,6 +205,10 @@
end

context 'and a template-url answer exists' do
before(:each) do
allow(PDK::Util::Git).to receive(:repo_exists?).with('answer-template').and_return(true)
end

it 'uses the template-url from the answer file to generate the module' do
PDK.answers.update!('template-url' => 'answer-template')
expect(PDK::Module::TemplateDir).to receive(:new).with('answer-template', anything, anything).and_yield(test_template_dir)
Expand Down
29 changes: 29 additions & 0 deletions spec/unit/pdk/util/git_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'spec_helper'

describe PDK::Util::Git do
describe '.repo_exists?' do
subject { described_class.repo_exists?(repo) }

let(:repo) { 'pdk-templates' }

before(:each) do
allow(described_class).to receive(:git).with('ls-remote', '--exit-code', repo).and_return(result)
end

context 'when git ls-remote finds refs in the repository' do
let(:result) do
{ exit_code: 0 }
end

it { is_expected.to be_truthy }
end

context 'when git ls-remote can not find any refs in the repository' do
let(:result) do
{ exit_code: 2 }
end

it { is_expected.to be_falsey }
end
end
end
22 changes: 20 additions & 2 deletions spec/unit/pdk/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,26 @@
allow(PDK).to receive(:answers).and_return('template-url' => 'custom_template_url')
end

it 'returns custom url' do
is_expected.to eq('custom_template_url')
context 'and the template is a valid repo' do
before(:each) do
allow(PDK::Util::Git).to receive(:repo_exists?).with('custom_template_url').and_return(true)
end

it 'returns custom url' do
is_expected.to eq('custom_template_url')
end
end

context 'and the template is not a valid repo' do
before(:each) do
allow(PDK::Util::Git).to receive(:repo_exists?).with('custom_template_url').and_return(false)
allow(PDK.answers).to receive(:update!).with('template-url' => nil)
end

it 'returns the puppetlabs template url' do
expect(logger).to receive(:warn).with(a_string_matching(%r{using the default template}))
is_expected.to eq('puppetlabs_template_url')
end
end
end
end
Expand Down