Skip to content

Commit

Permalink
[GR-20446] Fix recently imported specs
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4309
  • Loading branch information
andrykonchin committed Jul 4, 2024
2 parents ceac0cf + 0d4e18f commit 79b5c20
Show file tree
Hide file tree
Showing 23 changed files with 122 additions and 78 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ Compatibility:
* Joni has been updated from 2.1.44 to 2.2.1 (@andrykonchin).
* Fix `Hash#to_h` called with a block and pass key and value to the block as separate arguments (#3607, @andrykonchin).
* Fix `StringIO#initialize` and preserve initial string's encoding when mode is `w` so the initial string is truncated (#3599, @andrykonchin).
* Fix `IO#{autoclose=,autoclose?}` and raise `IOError` when io is closed (@andrykonchin).
* Fix `Thread#{thread_variable_get,thread_variable_set,thread_variable?,key?,[],[]=,fetch}` and convert a non-String/Symbol thread-local variable name to String using `#to_str` (@andrykonchin).
* Fix formatting in `Exception#full_message` when `RuntimeError` is not handled and `highlight` option is specified (@andrykonchin).
* Fix `String#encode` and convert fallback values into String using `#to_str` (@andrykonchin).
* Fix `Kernel.warn` and don't call `Warning#warn` if a specified category is disabled (@andrykonchin).

Performance:

Expand Down
18 changes: 18 additions & 0 deletions spec/ruby/core/exception/full_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@
err.full_message(highlight: true).should !~ /unhandled exception/
err.full_message(highlight: false).should !~ /unhandled exception/
end

it "adds escape sequences to highlight some strings if the message is not specified and :highlight option is specified" do
e = RuntimeError.new("")

full_message = e.full_message(highlight: true, order: :top).lines
full_message[0].should.end_with? "\e[1;4munhandled exception\e[m\n"

full_message = e.full_message(highlight: true, order: :bottom).lines
full_message[0].should == "\e[1mTraceback\e[m (most recent call last):\n"
full_message[-1].should.end_with? "\e[1;4munhandled exception\e[m\n"

full_message = e.full_message(highlight: false, order: :top).lines
full_message[0].should.end_with? "unhandled exception\n"

full_message = e.full_message(highlight: false, order: :bottom).lines
full_message[0].should == "Traceback (most recent call last):\n"
full_message[-1].should.end_with? "unhandled exception\n"
end
end

describe "generic Error" do
Expand Down
11 changes: 11 additions & 0 deletions spec/ruby/core/thread/element_reference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@
t2["value"].should == 2
end

it "converts a key that is neither String nor Symbol with #to_str" do
key = mock('value')
key.should_receive(:to_str).and_return('value')

th = Thread.new do
Thread.current[:value] = 1
end.join

th[key].should == 1
end

it "raises exceptions on the wrong type of keys" do
-> { Thread.current[nil] }.should raise_error(TypeError)
-> { Thread.current[5] }.should raise_error(TypeError)
Expand Down
25 changes: 24 additions & 1 deletion spec/ruby/core/thread/element_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,33 @@
th.freeze
-> {
th[:foo] = "bar"
}.should raise_error(FrozenError, /frozen/)
}.should raise_error(FrozenError, "can't modify frozen thread locals")
end.join
end

it "accepts Strings and Symbols" do
t1 = Thread.new do
Thread.current[:value] = 1
end.join
t2 = Thread.new do
Thread.current["value"] = 2
end.join

t1[:value].should == 1
t2[:value].should == 2
end

it "converts a key that is neither String nor Symbol with #to_str" do
key = mock('value')
key.should_receive(:to_str).and_return('value')

th = Thread.new do
Thread.current[key] = 1
end.join

th[:value].should == 1
end

it "raises exceptions on the wrong type of keys" do
-> { Thread.current[nil] = true }.should raise_error(TypeError)
-> { Thread.current[5] = true }.should raise_error(TypeError)
Expand Down
7 changes: 7 additions & 0 deletions spec/ruby/core/thread/key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
@th.key?(:stanley.to_s).should == false
end

it "converts a key that is neither String nor Symbol with #to_str" do
key = mock('key')
key.should_receive(:to_str).and_return('oliver')

@th.key?(key).should == true
end

it "raises exceptions on the wrong type of keys" do
-> { Thread.current.key? nil }.should raise_error(TypeError)
-> { Thread.current.key? 5 }.should raise_error(TypeError)
Expand Down
18 changes: 10 additions & 8 deletions spec/ruby/core/thread/thread_variable_get_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@
@t.thread_variable_get(:a).should be_nil
end

it "does not raise a TypeError if the key is neither Symbol nor String, nor responds to #to_str" do
@t.thread_variable_get(123).should be_nil
end

it "does not try to convert the key with #to_sym" do
key = mock('key')
key.should_not_receive(:to_sym)
@t.thread_variable_get(key).should be_nil
ruby_bug "#20606", ""..."3.4" do
it "raises a TypeError if the key is neither Symbol nor String, nor responds to #to_str" do
-> { @t.thread_variable_get(123) }.should raise_error(TypeError, /123 is not a symbol/)
end

it "does not try to convert the key with #to_sym" do
key = mock('key')
key.should_not_receive(:to_sym)
-> { @t.thread_variable_get(key) }.should raise_error(TypeError)
end
end
end
4 changes: 2 additions & 2 deletions spec/ruby/core/thread/thread_variable_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
end

it "raises a TypeError if the key is neither Symbol nor String, nor responds to #to_str" do
-> { @t.thread_variable_set(123, 1) }.should raise_error(TypeError, '123 is not a symbol')
-> { @t.thread_variable_set(123, 1) }.should raise_error(TypeError, /123 is not a symbol/)
end

it "does not try to convert the key with #to_sym" do
key = mock('key')
key.should_not_receive(:to_sym)
-> { @t.thread_variable_set(key, 42) }.should raise_error(TypeError, "#{key.inspect} is not a symbol")
-> { @t.thread_variable_set(key, 42) }.should raise_error(TypeError, /#{key.inspect} is not a symbol/)
end
end
18 changes: 10 additions & 8 deletions spec/ruby/core/thread/thread_variable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@
@t.thread_variable?(:a).should be_false
end

it "does not raise a TypeError if the key is neither Symbol nor String, nor responds to #to_str" do
@t.thread_variable?(123).should be_false
end

it "does not try to convert the key with #to_sym" do
key = mock('key')
key.should_not_receive(:to_sym)
@t.thread_variable?(key).should be_false
ruby_bug "#20606", ""..."3.4" do
it "raises a TypeError if the key is neither Symbol nor String, nor responds to #to_str" do
-> { @t.thread_variable?(123) }.should raise_error(TypeError, /123 is not a symbol/)
end

it "does not try to convert the key with #to_sym" do
key = mock('key')
key.should_not_receive(:to_sym)
-> { @t.thread_variable?(key) }.should raise_error(TypeError)
end
end
end
2 changes: 1 addition & 1 deletion spec/ruby/core/warning/warn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def Warning.warn(msg)
end
end

ruby_bug '#19530', ''...'3.4' do
ruby_bug '#20573', ''...'3.4' do
it "isn't called by Kernel.warn when category is :deprecated but Warning[:deprecated] is false" do
warn_deprecated = Warning[:deprecated]
begin
Expand Down
2 changes: 0 additions & 2 deletions spec/tags/core/exception/detailed_message_tags.txt

This file was deleted.

2 changes: 0 additions & 2 deletions spec/tags/core/io/autoclose_tags.txt

This file was deleted.

16 changes: 0 additions & 16 deletions spec/tags/core/string/encode_tags.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
fails:String#encode given the fallback option given a hash calls to_str on the returned value
fails:String#encode given the fallback option given a hash does not call to_s on the returned value
fails:String#encode given the fallback option given a hash raises an error if the key is not present in the hash
fails:String#encode given the fallback option given a hash raises an error if the value is itself invalid
fails:String#encode given the fallback option given an object not responding to [] raises an error
fails:String#encode given the fallback option given a proc calls to_str on the returned value
fails:String#encode given the fallback option given a proc does not call to_s on the returned value
fails:String#encode given the fallback option given a proc raises an error if the returned value is itself invalid
fails:String#encode given the fallback option given a lambda calls to_str on the returned value
fails:String#encode given the fallback option given a lambda does not call to_s on the returned value
fails:String#encode given the fallback option given a lambda raises an error if the returned value is itself invalid
fails:String#encode given the fallback option given a method calls to_str on the returned value
fails:String#encode given the fallback option given a method does not call to_s on the returned value
fails:String#encode given the fallback option given a method raises an error if the returned value is itself invalid
fails:String#encode when passed options normalizes newlines with cr_newline option
fails:String#encode when passed options normalizes newlines with crlf_newline option
fails:String#encode! given the fallback option given a hash calls to_str on the returned value
fails:String#encode! given the fallback option given a hash does not call to_s on the returned value
fails:String#encode! given the fallback option given a hash raises an error if the key is not present in the hash
fails:String#encode! given the fallback option given a hash raises an error if the value is itself invalid
fails:String#encode! given the fallback option given an object not responding to [] raises an error
fails:String#encode! given the fallback option given a proc calls to_str on the returned value
fails:String#encode! given the fallback option given a proc does not call to_s on the returned value
fails:String#encode! given the fallback option given a proc raises an error if the returned value is itself invalid
fails:String#encode! given the fallback option given a lambda calls to_str on the returned value
fails:String#encode! given the fallback option given a lambda does not call to_s on the returned value
fails:String#encode! given the fallback option given a lambda raises an error if the returned value is itself invalid
fails:String#encode! given the fallback option given a method calls to_str on the returned value
fails:String#encode! given the fallback option given a method does not call to_s on the returned value
fails:String#encode! given the fallback option given a method raises an error if the returned value is itself invalid
3 changes: 0 additions & 3 deletions spec/tags/core/thread/thread_variable_get_tags.txt

This file was deleted.

5 changes: 0 additions & 5 deletions spec/tags/core/thread/thread_variable_set_tags.txt

This file was deleted.

3 changes: 0 additions & 3 deletions spec/tags/core/thread/thread_variable_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/tags/core/thread/thread_variables_tags.txt

This file was deleted.

2 changes: 0 additions & 2 deletions spec/tags/core/warning/warn_tags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ slow:Warning.warn has Warning as the method owner
slow:Warning.warn can be overridden
slow:Warning.warn does not add a newline
slow:Warning.warn returns nil
fails:Warning.warn isn't called by Kernel.warn when category is :deprecated but Warning[:deprecated] is false
fails:Warning.warn isn't called by Kernel.warn when category is :experimental but Warning[:experimental] is false
4 changes: 3 additions & 1 deletion src/main/ruby/truffleruby/core/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -908,10 +908,12 @@ def advise(advice, offset = 0, len = 0)
# which is not really the owner of the fd should not actually close
# the fd.
def autoclose?
ensure_open
@autoclose
end

def autoclose=(autoclose)
ensure_open
@autoclose = Primitive.as_boolean(autoclose)
end

Expand Down Expand Up @@ -2417,7 +2419,7 @@ def close
if fd >= 0
# Need to set even if the instance is frozen
Primitive.io_set_fd(self, -1)
if fd >= 3 && autoclose?
if fd >= 3 && @autoclose
ret = Truffle::POSIX.close(fd)
Errno.handle if ret < 0
end
Expand Down
8 changes: 6 additions & 2 deletions src/main/ruby/truffleruby/core/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,12 @@ def untrace_var(name, cmd = undefined)

def warn(*messages, uplevel: undefined, category: nil)
if !Primitive.nil?($VERBOSE) && !messages.empty?
unless Primitive.nil?(category)
category = Truffle::Type.rb_convert_type(category, Symbol, :to_sym)
Truffle::WarningOperations.check_category(category)
return nil unless Warning[category]
end

prefix = if Primitive.undefined?(uplevel)
''
else
Expand Down Expand Up @@ -662,15 +668,13 @@ def warn(*messages, uplevel: undefined, category: nil)
unless message.encoding.ascii_compatible?
raise Encoding::CompatibilityError, "ASCII incompatible encoding: #{message.encoding}"
end
Truffle::WarningOperations.check_category(category) unless Primitive.nil?(category)

$stderr.write message
else
warning_warn = Warning.method(:warn)
if warning_warn.arity == 1
warning_warn.call(message)
else
category = Truffle::Type.rb_convert_type(category, Symbol, :to_sym) unless Primitive.nil?(category)
warning_warn.call(message, category: category)
end
end
Expand Down
1 change: 1 addition & 0 deletions src/main/ruby/truffleruby/core/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ def encode!(to = undefined, from = undefined, **options)
(_, fallback_enc_from, fallback_enc_to, error_bytes, _) = ec.primitive_errinfo
rep = fallback[error_bytes.force_encoding(fallback_enc_from)]
raise ec.last_error unless rep
rep = Truffle::Type.rb_convert_type rep, String, :to_str
dest << rep.encode(fallback_enc_to)
status = ec.primitive_convert src, dest, nil, nil
end
Expand Down
41 changes: 22 additions & 19 deletions src/main/ruby/truffleruby/core/thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,10 @@ def report_on_exception=(val)

# Fiber-local variables

private def convert_to_local_name(name)
if Primitive.is_a?(name, Symbol)
name
elsif Primitive.is_a?(name, String)
name.to_sym
else
Kernel.raise TypeError, "#{name.inspect} is not a symbol nor a string"
end
end

def fetch(name, default = undefined)
Primitive.warn_block_supersedes_default_value_argument if !Primitive.undefined?(default) && block_given?

key = convert_to_local_name(name)
key = Truffle::Type.coerce_to_symbol(name)
locals = Primitive.thread_get_fiber_locals self
if Primitive.object_ivar_defined? locals, key
return Primitive.object_ivar_get locals, key
Expand All @@ -241,20 +231,23 @@ def fetch(name, default = undefined)
end

def [](name)
var = convert_to_local_name(name)
var = Truffle::Type.coerce_to_symbol(name)
locals = Primitive.thread_get_fiber_locals self
Primitive.object_ivar_get locals, var
end

def []=(name, value)
var = convert_to_local_name(name)
Primitive.check_frozen self
if frozen?
raise FrozenError, "can't modify frozen thread locals"
end

var = Truffle::Type.coerce_to_symbol(name)
locals = Primitive.thread_get_fiber_locals self
Primitive.object_ivar_set locals, var, value
end

def key?(name)
var = convert_to_local_name(name)
var = Truffle::Type.coerce_to_symbol(name)
locals = Primitive.thread_get_fiber_locals self
Primitive.object_ivar_defined? locals, var
end
Expand All @@ -267,17 +260,27 @@ def keys
# Thread-local variables

def thread_variable_get(name)
var = convert_to_local_name(name)
var = Truffle::Type.coerce_to_symbol(name)
TruffleRuby.synchronized(self) { Primitive.thread_local_variables(self)[var] }
end

def thread_variable_set(name, value)
var = convert_to_local_name(name)
TruffleRuby.synchronized(self) { Primitive.thread_local_variables(self)[var] = value }
if frozen?
Kernel.raise FrozenError, "can't modify frozen thread locals"
end

var = Truffle::Type.coerce_to_symbol(name)
TruffleRuby.synchronized(self) do
if Primitive.nil?(value)
Primitive.thread_local_variables(self).delete(var)
else
Primitive.thread_local_variables(self)[var] = value
end
end
end

def thread_variable?(name)
var = convert_to_local_name(name)
var = Truffle::Type.coerce_to_symbol(name)
TruffleRuby.synchronized(self) { Primitive.thread_local_variables(self).key? var }
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def self.detailed_message(exception, highlight)

if message.empty?
message = Primitive.equal?(exception_class, RuntimeError) ? 'unhandled exception' : class_name
message = "\n\e[1m#{message}\e[m" if highlight
message = "\e[1;4m#{message}\e[m" if highlight
return message
end

Expand Down
2 changes: 1 addition & 1 deletion src/main/ruby/truffleruby/core/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def self.coerce_to_symbol(object)

string = Truffle::Type.rb_check_convert_type(object, String, :to_str)
if Primitive.nil?(string)
raise TypeError, "#{object} is not a symbol nor a string"
raise TypeError, "#{object.inspect} is not a symbol nor a string"
end

string.to_sym
Expand Down

0 comments on commit 79b5c20

Please sign in to comment.