Skip to content

Commit 2d75a98

Browse files
committed
[GR-14806] Update specs.
PullRequest: truffleruby/2690
2 parents 87000a0 + f2a8dc7 commit 2d75a98

File tree

225 files changed

+4881
-4162
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

225 files changed

+4881
-4162
lines changed

spec/mspec/.rspec

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

spec/mspec/lib/mspec/helpers/ruby_exe.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
#
1616
# `#{RUBY_EXE} 'path/to/some/file.rb'`
1717
#
18-
# The ruby_exe helper also accepts an options hash with three
19-
# keys: :options, :args and :env. For example:
18+
# The ruby_exe helper also accepts an options hash with four
19+
# keys: :options, :args :env and :exception.
20+
#
21+
# For example:
2022
#
2123
# ruby_exe('file.rb', :options => "-w",
2224
# :args => "arg1 arg2",
@@ -28,6 +30,9 @@
2830
#
2931
# with access to ENV["FOO"] with value "bar".
3032
#
33+
# When `exception: false` and Ruby command fails then exception will not be
34+
# raised.
35+
#
3136
# If +nil+ is passed for the first argument, the command line
3237
# will be built only from the options hash.
3338
#
@@ -52,6 +57,8 @@
5257
# (with -T on the command line or in the config with set :flags)
5358
# will be appended to RUBY_EXE so that the interpreter
5459
# is always called with those flags.
60+
#
61+
# Failure of a Ruby command leads to raising exception by default.
5562

5663
def ruby_exe_options(option)
5764
case option
@@ -128,9 +135,19 @@ def ruby_exe(code = :not_given, opts = {})
128135
code = tmpfile
129136
end
130137

138+
expected_exit_status = opts.fetch(:exit_status, 0)
139+
131140
begin
132141
platform_is_not :opal do
133-
`#{ruby_cmd(code, opts)}`
142+
command = ruby_cmd(code, opts)
143+
output = `#{command}`
144+
145+
last_status = Process.last_status
146+
if last_status.exitstatus != expected_exit_status
147+
raise "Expected exit status is #{expected_exit_status.inspect} but actual is #{last_status.exitstatus.inspect} for command ruby_exe(#{command.inspect})"
148+
end
149+
150+
output
134151
end
135152
ensure
136153
saved_env.each { |key, value| ENV[key] = value }

spec/mspec/lib/mspec/matchers/match_yaml.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ def clean_yaml(yaml)
3030
def valid_yaml?(obj)
3131
require 'yaml'
3232
begin
33-
YAML.load(obj)
33+
if YAML.respond_to?(:unsafe_load)
34+
YAML.unsafe_load(obj)
35+
else
36+
YAML.load(obj)
37+
end
3438
rescue
3539
false
3640
else

spec/mspec/spec/helpers/ruby_exe_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ class RubyExeSpecs
146146

147147
@script = RubyExeSpecs.new
148148
allow(@script).to receive(:`)
149+
150+
status_successful = double(Process::Status, exitstatus: 0)
151+
allow(Process).to receive(:last_status).and_return(status_successful)
152+
end
153+
154+
it "returns command STDOUT when given command" do
155+
code = "code"
156+
options = {}
157+
output = "output"
158+
allow(@script).to receive(:`).and_return(output)
159+
160+
expect(@script.ruby_exe(code, options)).to eq output
149161
end
150162

151163
it "returns an Array containing the interpreter executable and flags when given no arguments" do
@@ -160,6 +172,30 @@ class RubyExeSpecs
160172
@script.ruby_exe(code, options)
161173
end
162174

175+
it "raises exception when command exit status is not successful" do
176+
code = "code"
177+
options = {}
178+
179+
status_failed = double(Process::Status, exitstatus: 4)
180+
allow(Process).to receive(:last_status).and_return(status_failed)
181+
182+
expect {
183+
@script.ruby_exe(code, options)
184+
}.to raise_error(%r{Expected exit status is 0 but actual is 4 for command ruby_exe\(.+\)})
185+
end
186+
187+
it "shows in the exception message if exitstatus is nil (e.g., signal)" do
188+
code = "code"
189+
options = {}
190+
191+
status_failed = double(Process::Status, exitstatus: nil)
192+
allow(Process).to receive(:last_status).and_return(status_failed)
193+
194+
expect {
195+
@script.ruby_exe(code, options)
196+
}.to raise_error(%r{Expected exit status is 0 but actual is nil for command ruby_exe\(.+\)})
197+
end
198+
163199
describe "with :dir option" do
164200
it "is deprecated" do
165201
expect {
@@ -197,4 +233,24 @@ class RubyExeSpecs
197233
end.to raise_error(Exception)
198234
end
199235
end
236+
237+
describe "with :exit_status option" do
238+
before do
239+
status_failed = double(Process::Status, exitstatus: 4)
240+
allow(Process).to receive(:last_status).and_return(status_failed)
241+
end
242+
243+
it "raises exception when command ends with not expected status" do
244+
expect {
245+
@script.ruby_exe("path", exit_status: 1)
246+
}.to raise_error(%r{Expected exit status is 1 but actual is 4 for command ruby_exe\(.+\)})
247+
end
248+
249+
it "does not raise exception when command ends with expected status" do
250+
output = "output"
251+
allow(@script).to receive(:`).and_return(output)
252+
253+
expect(@script.ruby_exe("path", exit_status: 4)).to eq output
254+
end
255+
end
200256
end

spec/ruby/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ruby/spec is known to be tested in these implementations for every commit:
3030
* [Opal](https://github.com/opal/opal/tree/master/spec)
3131

3232
ruby/spec describes the behavior of Ruby 2.5 and more recent Ruby versions.
33-
More precisely, every latest stable MRI release should [pass](https://travis-ci.org/ruby/spec) all specs of ruby/spec (2.5.x, 2.6.x, 2.7.x, etc), and those are tested in TravisCI.
33+
More precisely, every latest stable MRI release should [pass](https://github.com/ruby/spec/actions/workflows/ci.yml) all specs of ruby/spec (2.5.x, 2.6.x, 2.7.x, etc), and those are tested in CI.
3434

3535
### Synchronization with Ruby Implementations
3636

spec/ruby/command_line/dash_encoding_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
end
2626

2727
it "does not accept a third encoding" do
28-
ruby_exe(@test_string, options: "--disable-gems --encoding big5:#{@enc2}:utf-32le", args: "2>&1").should =~ /extra argument for --encoding: utf-32le/
28+
options = {
29+
options: "--disable-gems --encoding big5:#{@enc2}:utf-32le",
30+
args: "2>&1",
31+
exit_status: 1
32+
}
33+
34+
ruby_exe(@test_string, options).should =~ /extra argument for --encoding: utf-32le/
2935
end
3036
end

spec/ruby/command_line/dash_r_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
end
1414

1515
it "requires the file before parsing the main script" do
16-
out = ruby_exe(fixture(__FILE__, "bad_syntax.rb"), options: "-r #{@test_file}", args: "2>&1")
16+
out = ruby_exe(fixture(__FILE__, "bad_syntax.rb"), options: "-r #{@test_file}", args: "2>&1", exit_status: 1)
1717
$?.should_not.success?
1818
out.should include("REQUIRED")
1919
out.should include("syntax error")

spec/ruby/command_line/dash_upper_e_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
it "raises a RuntimeError if used with -U" do
3232
ruby_exe("p 1",
3333
options: '-Eascii:ascii -U',
34-
args: '2>&1').should =~ /RuntimeError/
34+
args: '2>&1',
35+
exit_status: 1).should =~ /RuntimeError/
3536
end
3637
end

spec/ruby/command_line/dash_upper_s_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
end
2222

2323
it "runs launcher found in PATH and sets the exit status to 1 if it fails" do
24-
result = ruby_exe(nil, options: '-S dash_s_fail', env: { 'PATH' => @path }, args: '2>&1')
24+
result = ruby_exe(nil, options: '-S dash_s_fail', env: { 'PATH' => @path }, args: '2>&1', exit_status: 1)
2525
result.should =~ /\bdie\b/
2626
$?.exitstatus.should == 1
2727
end

spec/ruby/command_line/dash_upper_u_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
it "raises a RuntimeError if used with -Eext:int" do
3333
ruby_exe("p 1",
3434
options: '-U -Eascii:ascii',
35-
args: '2>&1').should =~ /RuntimeError/
35+
args: '2>&1',
36+
exit_status: 1).should =~ /RuntimeError/
3637
end
3738

3839
it "raises a RuntimeError if used with -E:int" do
3940
ruby_exe("p 1",
4041
options: '-U -E:ascii',
41-
args: '2>&1').should =~ /RuntimeError/
42+
args: '2>&1',
43+
exit_status: 1).should =~ /RuntimeError/
4244
end
4345
end

0 commit comments

Comments
 (0)