Skip to content

Commit 0450d7d

Browse files
committed
1 parent 81ea0ef commit 0450d7d

File tree

13 files changed

+103
-55
lines changed

13 files changed

+103
-55
lines changed

spec/ruby/core/binding/fixtures/irbrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# empty configuration

spec/ruby/core/binding/irb_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
describe "Binding#irb" do
55
it "creates an IRB session with the binding in scope" do
66
irb_fixture = fixture __FILE__, "irb.rb"
7+
irbrc_fixture = fixture __FILE__, "irbrc"
78

8-
out = IO.popen([*ruby_exe, irb_fixture], "r+") do |pipe|
9+
out = IO.popen([{"IRBRC"=>irbrc_fixture}, *ruby_exe, irb_fixture], "r+") do |pipe|
910
pipe.puts "a ** 2"
1011
pipe.puts "exit"
1112
pipe.readlines.map(&:chomp)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require_relative '../../spec_helper'
2+
require_relative 'fixtures/classes'
3+
4+
ruby_version_is "2.7" do
5+
describe "Enumerable#tally" do
6+
before :each do
7+
ScratchPad.record []
8+
end
9+
10+
it "returns a hash with counts according to the value" do
11+
enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz')
12+
enum.tally.should == { 'foo' => 2, 'bar' => 1, 'baz' => 1}
13+
end
14+
15+
it "returns a hash without default" do
16+
hash = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz').tally
17+
hash.default_proc.should be_nil
18+
hash.default.should be_nil
19+
end
20+
21+
it "returns an empty hash for empty enumerables" do
22+
EnumerableSpecs::Empty.new.tally.should == {}
23+
end
24+
25+
it "counts values as gathered array when yielded with multiple arguments" do
26+
EnumerableSpecs::YieldsMixed2.new.tally.should == EnumerableSpecs::YieldsMixed2.gathered_yields.group_by(&:itself).transform_values(&:size)
27+
end
28+
29+
it "does not call given block" do
30+
enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz')
31+
enum.tally { |v| ScratchPad << v }
32+
ScratchPad.recorded.should == []
33+
end
34+
end
35+
end

spec/ruby/core/file/fixtures/file_types.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def self.symlink
5252
end
5353

5454
def self.socket
55-
require 'socket'
56-
name = tmp("ftype_socket.socket")
57-
rm_r name
55+
require_relative '../../../library/socket/fixtures/classes.rb'
56+
57+
name = SocketSpecs.socket_path
5858
socket = UNIXServer.new name
5959
begin
6060
yield name

spec/ruby/core/file/stat/ftype_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@
5555
end
5656
end
5757

58-
# This will silently not execute the block if no socket
59-
# can be found. However, if you are running X, there is
60-
# a good chance that if nothing else, at least the X
61-
# Server socket exists.
6258
it "returns 'socket' when the file is a socket" do
6359
FileSpecs.socket do |socket|
6460
File.lstat(socket).ftype.should == 'socket'

spec/ruby/core/process/fixtures/clocks.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def self.clock_constants
1111
clocks -= [:CLOCK_BOOTTIME_ALARM, :CLOCK_REALTIME_ALARM]
1212
end
1313

14-
clocks.map { |c|
14+
clocks.sort.map { |c|
1515
[c, Process.const_get(c)]
1616
}
1717
end

spec/ruby/core/process/getrlimit_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
end
4242

4343
context "when passed a Symbol" do
44-
Process.constants.grep(/\ARLIMIT_/) do |fullname|
45-
short = $'
46-
it "coerces :#{short} into #{fullname}" do
44+
it "coerces the short name into the full RLIMIT_ prefixed name" do
45+
Process.constants.grep(/\ARLIMIT_/) do |fullname|
46+
short = fullname[/\ARLIMIT_(.+)/, 1]
4747
Process.getrlimit(short.to_sym).should == Process.getrlimit(Process.const_get(fullname))
4848
end
4949
end
@@ -54,9 +54,9 @@
5454
end
5555

5656
context "when passed a String" do
57-
Process.constants.grep(/\ARLIMIT_/) do |fullname|
58-
short = $'
59-
it "coerces '#{short}' into #{fullname}" do
57+
it "coerces the short name into the full RLIMIT_ prefixed name" do
58+
Process.constants.grep(/\ARLIMIT_/) do |fullname|
59+
short = fullname[/\ARLIMIT_(.+)/, 1]
6060
Process.getrlimit(short).should == Process.getrlimit(Process.const_get(fullname))
6161
end
6262
end
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
reserved_signals = ARGV
1+
cannot_be_trapped = %w[KILL STOP] # See man 2 signal
22

3-
(Signal.list.keys - reserved_signals).each do |signal|
4-
Signal.trap(signal, -> {})
5-
Signal.trap(signal, "DEFAULT")
3+
(Signal.list.keys - cannot_be_trapped).each do |signal|
4+
begin
5+
Signal.trap(signal, -> {})
6+
rescue ArgumentError => e
7+
unless /can't trap reserved signal|Signal already used by VM or OS/ =~ e.message
8+
raise e
9+
end
10+
else
11+
Signal.trap(signal, "DEFAULT")
12+
end
613
end
714

815
puts "OK"

spec/ruby/core/signal/trap_spec.rb

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,8 @@
115115
end
116116

117117
describe "Signal.trap" do
118-
cannot_be_trapped = %w[KILL STOP] # See man 2 signal
119-
reserved_signals = %w[VTALRM]
120-
121-
if PlatformGuard.implementation?(:ruby)
122-
reserved_signals += %w[SEGV ILL FPE BUS]
123-
end
124-
125-
if PlatformGuard.implementation?(:truffleruby)
126-
if !TruffleRuby.native?
127-
reserved_signals += %w[SEGV ILL FPE USR1 QUIT]
128-
end
129-
end
130-
131-
if PlatformGuard.implementation?(:jruby)
132-
reserved_signals += %w[SEGV ILL FPE BUS USR1 QUIT]
133-
end
134-
135-
cannot_be_trapped.each do |signal|
118+
# See man 2 signal
119+
%w[KILL STOP].each do |signal|
136120
it "raises ArgumentError or Errno::EINVAL for SIG#{signal}" do
137121
-> {
138122
trap(signal, -> {})
@@ -143,17 +127,8 @@
143127
end
144128
end
145129

146-
reserved_signals.each do |signal|
147-
it "raises ArgumentError for reserved signal: SIG#{signal}" do
148-
-> {
149-
trap(signal, -> {})
150-
}.should raise_error(ArgumentError, /can't trap reserved signal|Signal already used by VM or OS/)
151-
end
152-
end
153-
154-
it "allows to register a handler for all known signals, except reserved signals" do
155-
excluded = cannot_be_trapped + reserved_signals
156-
out = ruby_exe(fixture(__FILE__, "trap_all.rb"), args: [*excluded, "2>&1"])
130+
it "allows to register a handler for all known signals, except reserved signals for which it raises ArgumentError" do
131+
out = ruby_exe(fixture(__FILE__, "trap_all.rb"), args: "2>&1")
157132
out.should == "OK\n"
158133
$?.exitstatus.should == 0
159134
end

spec/ruby/library/socket/fixtures/classes.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,33 @@ def log(message)
132132
@logger.puts message if @logger
133133
end
134134
end
135+
136+
# We need to find a free port for Socket.tcp_server_loop and Socket.udp_server_loop,
137+
# and the only reliable way to do that is to pass 0 as the port, but then we need to
138+
# find out which one was chosen and the API doesn't let us find what it is. So we
139+
# intercept one of the public API methods called by these methods.
140+
class ServerLoopPortFinder < Socket
141+
def self.tcp_server_sockets(*args)
142+
super(*args) { |sockets|
143+
@port = sockets.first.local_address.ip_port
144+
yield(sockets)
145+
}
146+
end
147+
148+
def self.udp_server_sockets(*args, &block)
149+
super(*args) { |sockets|
150+
@port = sockets.first.local_address.ip_port
151+
yield(sockets)
152+
}
153+
end
154+
155+
def self.cleanup
156+
@port = nil
157+
end
158+
159+
def self.port
160+
sleep 0.001 until @port
161+
@port
162+
end
163+
end
135164
end

0 commit comments

Comments
 (0)