Skip to content

Commit

Permalink
(PDK-1363) Apply init templates during module convert
Browse files Browse the repository at this point in the history
  • Loading branch information
rodjek committed Aug 14, 2019
1 parent b5b1300 commit 48c2abe
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 5 deletions.
17 changes: 13 additions & 4 deletions lib/pdk/module/convert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def initialize(options = {})
@options = options
end

def convert?
instance_of?(PDK::Module::Convert)
end

def run
stage_changes!

Expand Down Expand Up @@ -68,7 +72,7 @@ def needs_bundle_update?
def stage_changes!
metadata_path = 'metadata.json'

PDK::Module::TemplateDir.new(template_uri, nil, false) do |templates|
PDK::Module::TemplateDir.new(template_uri, nil, true) do |templates|
new_metadata = update_metadata(metadata_path, templates.metadata)
templates.module_metadata = new_metadata.data unless new_metadata.nil?

Expand All @@ -81,11 +85,16 @@ def stage_changes!
end

templates.render do |file_path, file_content, file_status|
if file_status == :unmanage
case file_status
when :unmanage
PDK.logger.debug(_("skipping '%{path}'") % { path: file_path })
elsif file_status == :delete
when :delete
update_manager.remove_file(file_path)
elsif file_status == :manage
when :init
if convert? && !PDK::Util::Filesystem.exist?(file_path)
update_manager.add_file(file_path, file_content)
end
when :manage
if PDK::Util::Filesystem.exist?(file_path)
update_manager.modify_file(file_path, file_content)
else
Expand Down
7 changes: 6 additions & 1 deletion lib/pdk/module/templatedir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ def render
PDK.logger.debug(_("Rendering '%{template}'...") % { template: template_file })
dest_path = template_file.sub(%r{\.erb\Z}, '')
config = config_for(dest_path)
dest_status = :manage

dest_status = if template_loc.start_with?(@moduleroot_init)
:init
else
:manage
end

if config['unmanaged']
dest_status = :unmanage
Expand Down
17 changes: 17 additions & 0 deletions spec/acceptance/convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,21 @@
it { is_expected.not_to be_file }
end
end

context 'when an init-only templated file is missing' do
include_context 'in a new module', 'init_missing', template: template_repo

before(:all) do
FileUtils.rm_f('README.md')
end

describe command("#{pdk_convert_base} --force --skip-interview") do
its(:exit_status) { is_expected.to eq(0) }
its(:stderr) { is_expected.to have_no_output }
its(:stdout) { is_expected.to match(%r{-+files to be added-+\nREADME\.md}mi) }
describe file('README.md') do
it { is_expected.to be_file }
end
end
end
end
16 changes: 16 additions & 0 deletions spec/acceptance/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,21 @@
end
end
end

context 'that is missing an init-only templated file' do
before(:all) do
FileUtils.rm_f('README.md')
end

describe command('pdk update --force') do
its(:exit_status) { is_expected.to eq(0) }
its(:stderr) { is_expected.to have_no_output }
its(:stdout) { is_expected.to match(%r{no changes required}i) }

describe file('README.md') do
it { is_expected.not_to be_file }
end
end
end
end
end
38 changes: 38 additions & 0 deletions spec/unit/pdk/module/convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,38 @@
end
end

context 'when there are init files to add' do
let(:options) { { noop: true } }
let(:template_files) do
{ path: 'a/path/to/file', content: 'file contents', status: :init }
end

context 'and the files already exist' do
include_context 'no changes in the summary'

before(:each) do
allow(PDK::Util::Filesystem).to receive(:exist?).with(template_files[:path]).and_return(true)
allow(update_manager).to receive(:changes?).and_return(false)
end

it 'does not stage the file for addition' do
expect(update_manager).not_to receive(:add_file).with(template_files[:path], anything)
end
end

context 'and the files do not exist' do
before(:each) do
allow(PDK::Util::Filesystem).to receive(:exist?).with(template_files[:path]).and_return(false)
allow(update_manager).to receive(:changes?).and_return(true)
allow(update_manager).to receive(:add_file)
end

it 'stages the file for addition' do
expect(update_manager).to receive(:add_file).with(template_files[:path], template_files[:content])
end
end
end

context 'when there are files to add' do
include_context 'has changes in the summary'
include_context 'added files in the summary'
Expand Down Expand Up @@ -344,6 +376,12 @@
end
end

describe '#convert?' do
subject { described_class.new.convert? }

it { is_expected.to be_truthy }
end

describe '#template_uri' do
subject { described_class.new(options).template_uri }

Expand Down
6 changes: 6 additions & 0 deletions spec/unit/pdk/module/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,10 @@
end
end
end

describe '#convert?' do
subject { described_class.new.convert? }

it { is_expected.to be_falsey }
end
end

0 comments on commit 48c2abe

Please sign in to comment.