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

(GH-3286) Add file::delete() function #3287

Merged
merged 1 commit into from
Apr 16, 2024
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
21 changes: 21 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,21 @@
# 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[1]', :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
Copy link
Contributor

Choose a reason for hiding this comment

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

why nil here, do you always want to return nil and not the result of delete()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was copied from file::write() actually.. In our case delete() will always return 1 (number of filenames passed as the arguments). Not sure how useful it is.

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
Loading