Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
p-lambert committed Jan 16, 2018
1 parent 0a4b7b9 commit ffc8613
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
8 changes: 4 additions & 4 deletions lib/ddtrace/utils.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Datadog
# Utils contains low-level utilities, typically to provide pseudo-random trace IDs.
module Utils
STRING_PLACEHOLDER = ''.encode(Encoding::UTF_8).freeze
STRING_PLACEHOLDER = ''.encode(::Encoding::UTF_8).freeze
# We use a custom random number generator because we want no interference
# with the default one. Using the default prng, we could break code that
# would rely on srand/rand sequences.
Expand Down Expand Up @@ -41,15 +41,15 @@ def self.utf8_encode(str, options = {})
# This option is useful for "gracefully" displaying binary data that
# often contains text such as marshalled objects
str.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
elsif str.encoding?(Encoding::UTF_8)
elsif str.encoding == ::Encoding::UTF_8
str
else
str.encode(Encoding::UTF_8)
str.encode(::Encoding::UTF_8)
end
rescue => e
Tracer.log.error("Error encoding string in UTF-8: #{e}")

options[:placeholder] || STRING_PLACEHOLDER
options.fetch(:placeholder, STRING_PLACEHOLDER)
end
end
end
11 changes: 6 additions & 5 deletions test/utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ def test_forked_process_id_collision
end

def test_utf8_encoding_happy_path
str = 'pristine ✓'
# we can't use utf-8 literals because our tests run against ruby 1.9.3
str = "pristine \U+FFE2".encode(Encoding::UTF_8)

assert_equal('pristine ✓', Datadog::Utils.utf8_encode(str))
assert_equal("pristine \U+FFE2", Datadog::Utils.utf8_encode(str))

assert_equal(::Encoding::UTF_8, Datadog::Utils.utf8_encode(str).encoding)

Expand All @@ -74,7 +75,7 @@ def test_utf8_encoding_invalid_conversion
time_bomb = "\xC2".force_encoding(::Encoding::ASCII_8BIT)

# making sure this is indeed a problem
assert_raises(Encoding::CompatibilityError) do
assert_raises(Encoding::UndefinedConversionError) do
time_bomb.encode(Encoding::UTF_8)
end

Expand All @@ -85,8 +86,8 @@ def test_utf8_encoding_invalid_conversion
end

def test_binary_data
byte_array = "keep what \xC2 is valid".force_encoding(::Encoding::ASCII_8BIT)
byte_array = "keep what\xC2 is valid".force_encoding(::Encoding::ASCII_8BIT)

assert_equal('keep what is valid', Utils.utf8_encode(byte_array, binary: true))
assert_equal('keep what is valid', Datadog::Utils.utf8_encode(byte_array, binary: true))
end
end

0 comments on commit ffc8613

Please sign in to comment.