Skip to content

Commit

Permalink
(GH-3286) Add file::delete() function
Browse files Browse the repository at this point in the history
Closes #3286

!feature

* **Add `file::delete()` function**
  Delete a file on localhost using ruby's `File.delete`. This will only
  delete files on the machine you run Bolt on.
  • Loading branch information
jay7x committed Mar 30, 2024
1 parent 4b30c39 commit 0381bd6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
22 changes: 22 additions & 0 deletions bolt-modules/file/lib/puppet/functions/file/delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

# Delete a file on localhost using ruby's `File.delete`. This will only delete
# files on the machine you run Bolt on.
Puppet::Functions.create_function(:'file::delete') do
# @param filename Absolute path.
# @example Delete a file from disk
# file::delete('C:/Users/me/report')
dispatch :delete do
required_param 'String', :filename
return_type 'Undef'
end

def delete(filename)
# Send Analytics Report
Puppet.lookup(:bolt_executor) {}&.report_function_call(self.class.name)

File.delete(filename)
nil
end
end

15 changes: 15 additions & 0 deletions bolt-modules/file/spec/functions/file/delete_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'spec_helper'
require 'tempfile'

describe 'file::delete' do
it {
Dir.mktmpdir do |dir|
file = File.join(dir, 'file_delete')
File.write(file, 'file_delete_contents')
is_expected.to run.with_params(file)
expect(File.exist?(file)).to eq(false)
end
}
end

0 comments on commit 0381bd6

Please sign in to comment.