Skip to content

Commit

Permalink
Merge pull request #725 from Secure-24/new_module_with_deleted
Browse files Browse the repository at this point in the history
Handle deleted template files for new module
  • Loading branch information
rodjek committed Aug 14, 2019
2 parents d19fdda + 61641f2 commit 8cc51d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
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|
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

0 comments on commit 8cc51d5

Please sign in to comment.