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-672) List files changed from convert #363

Merged
merged 4 commits into from
Nov 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
2 changes: 1 addition & 1 deletion lib/pdk/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 2 additions & 18 deletions lib/pdk/generate/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = _(
Expand Down Expand Up @@ -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|
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion lib/pdk/generate/puppet_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions lib/pdk/module/convert.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'pdk/generate/module'
require 'pdk/module/update_manager'
require 'pdk/util'

module PDK
module Module
Expand All @@ -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

Expand Down
18 changes: 18 additions & 0 deletions lib/pdk/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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_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
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pdk/cli/convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/pdk/generate/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pdk/generate/puppet_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
112 changes: 102 additions & 10 deletions spec/unit/pdk/module/convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) { {} }
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -67,20 +87,14 @@
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!)
end
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
Expand Down Expand Up @@ -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
Expand Down