Skip to content

Commit

Permalink
(PDK-395) Use vendored pdk-module-template repo when available
Browse files Browse the repository at this point in the history
  • Loading branch information
scotje committed Aug 15, 2017
1 parent 671df45 commit a3918a1
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/pdk/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def self.run(args)
end

def self.template_url_option(dsl)
dsl.option nil, 'template-url', _('Specifies the URL to the template to use when creating new modules, and other parts.'), argument: :required, default: PDK::Generate::Module::DEFAULT_TEMPLATE
dsl.option nil, 'template-url', _('Specifies the URL to the template to use when creating new modules, and other parts.'), argument: :required, default: PDK::Generate::Module.default_template_url
end

@base_cmd = Cri::Command.define do
Expand Down
14 changes: 12 additions & 2 deletions lib/pdk/generators/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
module PDK
module Generate
class Module
DEFAULT_TEMPLATE = 'https://github.com/puppetlabs/pdk-module-template'.freeze
def self.default_template_url
PDK::Util.package_install? ? vendored_template_url : 'https://github.com/puppetlabs/pdk-module-template'
end

def self.invoke(opts = {})
target_dir = File.expand_path(opts[:target_dir])
Expand All @@ -42,7 +44,7 @@ def self.invoke(opts = {})

prepare_module_directory(temp_target_dir)

template_url = opts.fetch(:'template-url', PDK.answers['template-url'] || DEFAULT_TEMPLATE)
template_url = opts.fetch(:'template-url', PDK.answers['template-url'] || default_template_url)

PDK::Module::TemplateDir.new(template_url, metadata.data) do |templates|
templates.render do |file_path, file_content|
Expand Down Expand Up @@ -241,6 +243,14 @@ def self.module_interview(metadata, opts = {})
'license' => answers['license'],
)
end

class << self
private

def vendored_template_url
'file://' + File.join(PDK::Util.package_cachedir, 'pdk-module-template.git')
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pdk/generators/puppet_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def templates
@templates ||= [
{ type: 'CLI', url: @options[:'template-url'], allow_fallback: false },
{ type: 'metadata', url: module_metadata.data['template-url'], allow_fallback: true },
{ type: 'default', url: PDK::Generate::Module::DEFAULT_TEMPLATE, allow_fallback: false },
{ type: 'default', url: PDK::Generate::Module.default_template_url, allow_fallback: false },
]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pdk/module/templatedir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def initialize(path_or_url, module_metadata = {})
def metadata
return {} unless @repo

ref_result = PDK::CLI::Exec.git('--git-dir', File.join(@path, '.git'), 'describe', '--all', '--long')
ref_result = PDK::CLI::Exec.git('--git-dir', File.join(@path, '.git'), 'describe', '--all', '--long', '--always')
if ref_result[:exit_code].zero?
{ 'template-url' => @repo, 'template-ref' => ref_result[:stdout].strip }
else
Expand Down
7 changes: 4 additions & 3 deletions package-testing/pre/000_install_package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
# For most platforms, beaker will install the dev repo from the build server then 'install_package('pdk')' can simply be used
pkg ||= 'pdk'

if ENV['SHA']
if ENV['LOCAL_PKG']
workstation.install_local_package(pkg)
else
install_puppetlabs_dev_repo(workstation, 'pdk', ENV['SHA'], 'repo-config')
workstation.install_package(pkg)
end

workstation.install_package(pkg)
end
end
end
6 changes: 5 additions & 1 deletion package-testing/tests/validate_a_new_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
end

step 'Create a module' do
on(workstation, pdk_command(workstation, "new module #{module_name} --skip-interview"))
on(workstation, pdk_command(workstation, "new module #{module_name} --skip-interview")) do
on(workstation, %(cat #{module_name}/metadata.json | egrep '"template-url":'), accept_all_exit_codes: true) do |template_result|
assert_match(%r{"file://.*pdk-module-template\.git",?$}, template_result.stdout.strip, "New module's metadata should refer to vendored pdk-module-template repo")
end
end
end

step 'Validate the module' do
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper_acceptance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def execute_script(script)
RSpec.configure do |c|
c.before(:suite) do
RSpec.configuration.template_dir = Dir.mktmpdir
output, status = Open3.capture2e('git', 'clone', '--bare', PDK::Generate::Module::DEFAULT_TEMPLATE, RSpec.configuration.template_dir)
output, status = Open3.capture2e('git', 'clone', '--bare', PDK::Generate::Module.default_template_url, RSpec.configuration.template_dir)
raise "Failed to cache module template: #{output}" unless status.success?

tempdir = Dir.mktmpdir
Expand Down
21 changes: 18 additions & 3 deletions spec/unit/pdk/generate/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,25 @@
end

context 'and no template-url answer exists' do
it 'uses the default template to generate the module' do
expect(PDK::Module::TemplateDir).to receive(:new).with(PDK::Generate::Module::DEFAULT_TEMPLATE, anything).and_yield(test_template_dir)
context 'and pdk is installed from packages' do
before(:each) do
allow(PDK::Util).to receive(:package_install?).and_return(true)
allow(PDK::Util).to receive(:package_cachedir).and_return('/tmp/package/cache')
end

described_class.invoke(invoke_opts)
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)

described_class.invoke(invoke_opts)
end
end

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)

described_class.invoke(invoke_opts)
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/unit/pdk/generate/puppet_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
let(:module_metadata) { '{"name": "testuser-test_module"}' }

before(:each) do
allow(PDK::Util).to receive(:package_install?).and_return(false)
allow(File).to receive(:file?).with(metadata_path).and_return(true)
allow(File).to receive(:readable?).with(metadata_path).and_return(true)
allow(File).to receive(:read).with(metadata_path).and_return(module_metadata)
Expand Down Expand Up @@ -139,7 +140,7 @@
allow(default_templatedir).to receive(:object_config).and_return(configs_hash)
allow(cli_templatedir).to receive(:object_config).and_return(configs_hash)
allow(metadata_templatedir).to receive(:object_config).and_return(configs_hash)
allow(PDK::Module::TemplateDir).to receive(:new).with(PDK::Generate::Module::DEFAULT_TEMPLATE).and_yield(default_templatedir)
allow(PDK::Module::TemplateDir).to receive(:new).with(PDK::Generate::Module.default_template_url).and_yield(default_templatedir)
end

context 'when a template-url is provided on the CLI' do
Expand Down

0 comments on commit a3918a1

Please sign in to comment.