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

Handle deleted template files for new module #725

Merged
merged 1 commit into from
Aug 14, 2019
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
3 changes: 2 additions & 1 deletion lib/pdk/generate/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def self.invoke(opts = {})

begin
PDK::Module::TemplateDir.new(template_uri, metadata.data, true) do |templates|
templates.render do |file_path, file_content|
templates.render do |file_path, file_content, file_status|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must be missing something but where does file_status come from?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next if file_status == :delete
file = Pathname.new(temp_target_dir) + file_path
file.dirname.mkpath
write_file(file, file_content)
Expand Down
34 changes: 32 additions & 2 deletions spec/unit/pdk/generate/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
allow(described_class).to receive(:prepare_module_directory).with(temp_target_dir)
allow(File).to receive(:open).with(%r{pdk-test-writable}, anything) { raise Errno::EACCES unless target_parent_writeable }
allow(FileUtils).to receive(:rm_f).with(%r{pdk-test-writable})
allow(test_template_dir).to receive(:render).and_yield('test_file_path', 'test_file_content')
allow(test_template_dir).to receive(:render).and_yield('test_file_path', 'test_file_content', :manage)
end

context 'when the parent directory of the target is not writable' do
Expand All @@ -122,7 +122,7 @@
let(:content) { 'test_file_content' }

before(:each) do
allow(test_template_dir).to receive(:render).and_yield('test_file_path', content)
allow(test_template_dir).to receive(:render).and_yield('test_file_path', content, :manage)
end

it 'writes the rendered files from the template to the temporary directory' do
Expand All @@ -133,6 +133,36 @@
end
end

context 'when the module template contains unmanaged template files' do
let(:content) { 'test_file_content' }

before(:each) do
allow(test_template_dir).to receive(:render).and_yield('test_file_path', content, :unmanage)
end

it 'writes the rendered files from the template to the temporary directory' do
described_class.invoke(invoke_opts)

test_template_file.rewind
expect(test_template_file.read).to eq(content + "\n")
end
end

context 'when the module template contains files with delete option set' do
let(:content) { 'test_file_content' }

before(:each) do
allow(test_template_dir).to receive(:render).and_yield('test_file_path', content, :delete)
end

it 'does not writes the deleted files from the template to the temporary directory' do
described_class.invoke(invoke_opts)

test_template_file.rewind
expect(test_template_file.read).to eq('')
end
end

context 'when the template dir generates metadata about itself' do
let(:template_metadata) do
{
Expand Down