diff --git a/lib/pdk/cli.rb b/lib/pdk/cli.rb index d0ed4114c..690e61be5 100644 --- a/lib/pdk/cli.rb +++ b/lib/pdk/cli.rb @@ -38,7 +38,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 or classes.'), argument: :required, default: PDK::Generate::Module.default_template_url + dsl.option nil, 'template-url', _('Specifies the URL to the template to use when creating new modules or classes.'), argument: :required, default: PDK::Util.default_template_url end def self.skip_interview_option(dsl) diff --git a/lib/pdk/generate/module.rb b/lib/pdk/generate/module.rb index 22da45e2f..66f90401f 100644 --- a/lib/pdk/generate/module.rb +++ b/lib/pdk/generate/module.rb @@ -17,22 +17,6 @@ module PDK module Generate class Module - def self.default_template_url - if !PDK.answers['template-url'].nil? - PDK.answers['template-url'] - 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' - end - end - def self.validate_options(opts) unless PDK::CLI::Util::OptionValidator.valid_module_name?(opts[:module_name]) error_msg = _( @@ -68,7 +52,7 @@ def self.invoke(opts = {}) prepare_module_directory(temp_target_dir) - template_url = opts.fetch(:'template-url', default_template_url) + template_url = opts.fetch(:'template-url', PDK::Util.default_template_url) PDK::Module::TemplateDir.new(template_url, metadata.data, true) do |templates| templates.render do |file_path, file_content| @@ -84,7 +68,7 @@ def self.invoke(opts = {}) metadata.write!(File.join(temp_target_dir, 'metadata.json')) end - if template_url == puppetlabs_template_url + if template_url == PDK::Util.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') diff --git a/lib/pdk/generate/puppet_object.rb b/lib/pdk/generate/puppet_object.rb index 4fefa85eb..d8a2cb5b1 100644 --- a/lib/pdk/generate/puppet_object.rb +++ b/lib/pdk/generate/puppet_object.rb @@ -223,7 +223,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_url, allow_fallback: false }, + { type: 'default', url: PDK::Util.default_template_url, allow_fallback: false }, ] end diff --git a/lib/pdk/module/convert.rb b/lib/pdk/module/convert.rb index df5e14566..2233182b5 100644 --- a/lib/pdk/module/convert.rb +++ b/lib/pdk/module/convert.rb @@ -1,5 +1,6 @@ require 'pdk/generate/module' require 'pdk/module/update_manager' +require 'pdk/util' module PDK module Module @@ -8,12 +9,33 @@ def self.invoke(options) # TODO: Dummy template metadata, replace with TemplateDir#metadata template_metadata = {} update_manager = PDK::Module::UpdateManager.new + template_url = options.fetch(:'template-url', PDK::Util.default_template_url) update_manager.modify_file('metadata.json', update_metadata('metadata.json', template_metadata)) + PDK::Module::TemplateDir.new(template_url, nil, false) do |templates| + templates.render do |file_path, file_content| + if File.exist? file_path + update_manager.modify_file(file_path, file_content) + else + update_manager.add_file(file_path, file_content) + end + end + end + return unless update_manager.changes? + [:added, :removed].each do |category| + PDK.logger.info(_('Files to be %{category}:') % { category: category }) + update_manager.changes[category].each do |file| + # This is where we add the entries to the report + puts file[:path] + end + end + + PDK.logger.info(_('Files to be modified:')) update_manager.changes[:modified].each do |_, diff| + # This is where we add the entries to the report puts diff end diff --git a/lib/pdk/util.rb b/lib/pdk/util.rb index b76f85d96..6e45ad480 100644 --- a/lib/pdk/util.rb +++ b/lib/pdk/util.rb @@ -166,5 +166,23 @@ def targets_relative_to_pwd(targets) end end module_function :targets_relative_to_pwd + + def default_template_url + if !PDK.answers['template-url'].nil? + PDK.answers['template-url'] + else + puppetlabs_template_url + end + end + module_function :default_template_url + + def puppetlabs_template_url + if package_install? + 'file://' + File.join(package_cachedir, 'pdk-module-template.git') + else + 'https://github.com/puppetlabs/pdk-module-template' + end + end + module_function :puppetlabs_template_url end end diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index d0aa0df3c..98038c24a 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -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_url, RSpec.configuration.template_dir) + output, status = Open3.capture2e('git', 'clone', '--bare', PDK::Util.default_template_url, RSpec.configuration.template_dir) raise "Failed to cache module template: #{output}" unless status.success? tempdir = Dir.mktmpdir diff --git a/spec/unit/pdk/cli/convert_spec.rb b/spec/unit/pdk/cli/convert_spec.rb index ac3818da0..e241f3ed0 100644 --- a/spec/unit/pdk/cli/convert_spec.rb +++ b/spec/unit/pdk/cli/convert_spec.rb @@ -51,7 +51,7 @@ end it 'invokes the converter with the default template if the user chooses to continue' do - expect(PDK::Module::Convert).to receive(:invoke).with(:'template-url' => PDK::Generate::Module.default_template_url) + expect(PDK::Module::Convert).to receive(:invoke).with(:'template-url' => PDK::Util.default_template_url) PDK::CLI.run(['convert']) end diff --git a/spec/unit/pdk/generate/module_spec.rb b/spec/unit/pdk/generate/module_spec.rb index 3e9f568da..480a6463b 100644 --- a/spec/unit/pdk/generate/module_spec.rb +++ b/spec/unit/pdk/generate/module_spec.rb @@ -191,7 +191,7 @@ 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') + allow(PDK::Util).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) @@ -231,7 +231,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, anything).and_yield(test_template_dir) + expect(PDK::Module::TemplateDir).to receive(:new).with(PDK::Util.default_template_url, anything, anything).and_yield(test_template_dir) expect(PDK.answers).not_to receive(:update!).with(:'template-url' => anything) described_class.invoke(invoke_opts) diff --git a/spec/unit/pdk/generate/puppet_object_spec.rb b/spec/unit/pdk/generate/puppet_object_spec.rb index 1639007dc..1b666261f 100644 --- a/spec/unit/pdk/generate/puppet_object_spec.rb +++ b/spec/unit/pdk/generate/puppet_object_spec.rb @@ -140,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_url).and_yield(default_templatedir) + allow(PDK::Module::TemplateDir).to receive(:new).with(PDK::Util.default_template_url).and_yield(default_templatedir) end context 'when a template-url is provided on the CLI' do diff --git a/spec/unit/pdk/module/convert_spec.rb b/spec/unit/pdk/module/convert_spec.rb index 8f1e5ef54..7ef59873e 100644 --- a/spec/unit/pdk/module/convert_spec.rb +++ b/spec/unit/pdk/module/convert_spec.rb @@ -14,9 +14,17 @@ end end + shared_context 'prompt to continue' do |value| + before(:each) do + allow(PDK::CLI::Util).to receive(:prompt_for_yes).with(anything).and_return(value) + end + end + describe '.invoke' do let(:options) { {} } let(:update_manager) { instance_double(PDK::Module::UpdateManager, sync_changes!: true) } + let(:template_dir) { instance_double(PDK::Module::TemplateDir, metadata: {}) } + let(:template_files) { { path: 'a/path/to/file', content: 'file contents' } } let(:added_files) { [] } let(:removed_files) { [] } let(:modified_files) { {} } @@ -25,9 +33,10 @@ changes = { added: added_files, removed: removed_files, modified: modified_files } allow(PDK::Module::UpdateManager).to receive(:new).and_return(update_manager) - allow(update_manager).to receive(:modify_file).with(any_args) + allow(described_class).to receive(:update_metadata).with(anything, anything).and_return('') + allow(PDK::Module::TemplateDir).to receive(:new).with(anything, anything, anything).and_yield(template_dir) + allow(template_dir).to receive(:render).and_yield(template_files[:path], template_files[:content]) allow(update_manager).to receive(:changes).and_return(changes) - allow(described_class).to receive(:update_metadata).with(any_args).and_return('') end after(:each) do @@ -36,7 +45,12 @@ context 'when there are no changes to apply' do before(:each) do + allow(File).to receive(:exist?).with('a/path/to/file').and_return(true) allow(update_manager).to receive(:changes?).and_return(false) + allow(template_dir).to receive(:render) + allow(PDK::Module::TemplateDir).to receive(:files_in_template).and_return({}) + + allow(update_manager).to receive(:modify_file).with('metadata.json', anything) end it 'returns without syncing the changes' do @@ -46,8 +60,13 @@ context 'when there are changes to apply' do before(:each) do + allow(File).to receive(:exist?).with('a/path/to/file').and_return(true) + allow(update_manager).to receive(:modify_file).with(any_args) allow(update_manager).to receive(:changes?).and_return(true) allow($stdout).to receive(:puts).with('a diff') + + allow(update_manager).to receive(:modify_file).with('metadata.json', anything) + allow(update_manager).to receive(:modify_file).with(template_files[:path], template_files[:content]) end let(:modified_files) do @@ -57,8 +76,9 @@ end context 'and run normally' do + include_context 'prompt to continue', false + it 'prints a diff of the changed files' do - allow(PDK::CLI::Util).to receive(:prompt_for_yes).with(anything).and_return(false) expect($stdout).to receive(:puts).with('a diff') end @@ -67,9 +87,7 @@ end context 'if the user chooses to continue' do - before(:each) do - allow(PDK::CLI::Util).to receive(:prompt_for_yes).with(anything).and_return(true) - end + include_context 'prompt to continue', true it 'syncs the pending changes' do expect(update_manager).to receive(:sync_changes!) @@ -77,10 +95,6 @@ end context 'if the user chooses not to continue' do - before(:each) do - allow(PDK::CLI::Util).to receive(:prompt_for_yes).with(anything).and_return(false) - end - it 'does not sync the changes' do expect(update_manager).not_to receive(:sync_changes!) end @@ -119,6 +133,84 @@ end end end + + context 'when there are files to add' do + let(:added_files) do + [ + { + path: 'path/to/file', + content: 'file contents', + }, + ] + end + + before(:each) do + allow(File).to receive(:exist?).with('a/path/to/file').and_return(false) + allow(update_manager).to receive(:changes?).and_return(true) + allow($stdout).to receive(:puts).with('path/to/file') + + allow(update_manager).to receive(:modify_file).with('metadata.json', anything) + allow(update_manager).to receive(:add_file).with(template_files[:path], template_files[:content]) + end + + context 'and run normally' do + include_context 'prompt to continue', false + + it 'prints a path of the added files' do + expect($stdout).to receive(:puts).with('path/to/file') + end + + it 'prompts the user to continue' do + expect(PDK::CLI::Util).to receive(:prompt_for_yes).with(anything).and_return(false) + end + + context 'if the user chooses to continue' do + include_context 'prompt to continue', true + + it 'syncs the pending changes' do + expect(update_manager).to receive(:sync_changes!) + end + end + + context 'if the user chooses not to continue' do + it 'does not sync the changes' do + expect(update_manager).not_to receive(:sync_changes!) + end + end + end + + context 'and run in noop mode' do + let(:options) { { noop: true } } + + it 'prints a path of the added files' do + expect($stdout).to receive(:puts).with('path/to/file') + end + + it 'does not prompt the user to continue' do + expect(PDK::CLI::Util).not_to receive(:prompt_for_yes) + end + + it 'does not sync the changes' do + expect(update_manager).not_to receive(:sync_changes!) + end + end + + context 'and run in force mode' do + let(:options) { { force: true } } + + it 'prints a path of the added files' do + expect($stdout).to receive(:puts).with('path/to/file') + end + + it 'does not prompt the user to continue' do + expect(PDK::CLI::Util).not_to receive(:prompt_for_yes) + end + + it 'syncs the pending changes' do + expect(update_manager).to receive(:sync_changes!) + end + end + end end describe '.update_metadata' do