Skip to content

Commit

Permalink
Fix error when attempting to format an unlinked tempfile (#3164)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdebono authored Jan 14, 2025
1 parent 115ea8f commit 6af904f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 2 additions & 0 deletions gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Issue - Fixed error when attempting to log an unlinked tempfile.

3.215.0 (2025-01-10)
------------------

Expand Down
10 changes: 7 additions & 3 deletions gems/aws-sdk-core/lib/aws-sdk-core/log/param_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ def summarize_value(value)
when String then summarize_string(value)
when Hash then '{' + summarize_hash(value) + '}'
when Array then summarize_array(value)
when File then summarize_file(value.path)
when Pathname then summarize_file(value)
when File then summarize_file(value)
when Pathname then summarize_filepath(value)
else value.inspect
end
end

def summarize_file(path)
def summarize_file(file)
"#<File:#{file.path} (#{file.size} bytes)>"
end

def summarize_filepath(path)
"#<File:#{path} (#{File.size(path)} bytes)>"
end

Expand Down
15 changes: 13 additions & 2 deletions gems/aws-sdk-core/spec/aws/log/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def format(pattern, options = {})
end

it 'provides a :request_params replacement' do
file = Tempfile.create
# This is not available on windows, see:
# - https://docs.ruby-lang.org/en/3.4/Tempfile.html#class-Tempfile-label-Unlink+after+creation
# - https://github.com/aws/aws-sdk-ruby/issues/3163
File.unlink(file.path) unless RUBY_DESCRIPTION.match?(/mswin|ming|cygwin/)
file.write('foo=bar')

response.context.params = {
foo: 'bar',
attributes: {
Expand All @@ -43,15 +50,19 @@ def format(pattern, options = {})
config: {
nested: true,
path: Pathname.new(__FILE__),
tmpfile: file,
complex: double('obj', inspect: '"inspected"')
},
huge: '-' * 1000
huge: '-' * 1000,
list: ['one', 'two']
}
formatted = format('{:request_params}', max_string_size: 20)
size = File.size(__FILE__)
expect(formatted).to eq(<<-FORMATTED.strip)
{foo:"bar",attributes:{"color"=>"red","size"=>"large"},config:{nested:true,path:#<File:#{__FILE__} (#{size} bytes)>,complex:"inspected"},huge:#<String "--------------------" ... (1000 bytes)>}
{foo:"bar",attributes:{"color"=>"red","size"=>"large"},config:{nested:true,path:#<File:#{__FILE__} (#{size} bytes)>,tmpfile:#<File:#{file.path} (#{file.size} bytes)>,complex:"inspected"},huge:#<String "--------------------" ... (1000 bytes)>,list:["one","two"]}
FORMATTED
ensure
file.close
end

it 'provides a :time replacement' do
Expand Down

0 comments on commit 6af904f

Please sign in to comment.