Skip to content

Commit 7daa863

Browse files
committed
[GR-20046] Implement exception option for Kernel#system.
PullRequest: truffleruby/1255
2 parents 7cc786e + 8f40e3b commit 7daa863

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Compatibility:
9797
* Implemented `rb_module_new`, `rb_define_class_id`, `rb_define_module_id`, (#1876, @chrisseaton).
9898
* Implemented `ENV.slice`.
9999
* Support for the Darkfish theme for RDoc generation has been added back.
100+
* Implemented `Kernel#system` `exception: true` option.
100101

101102
Performance:
102103

spec/tags/core/kernel/system_tags.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ slow:Kernel.system executes with `sh` if the command contains shell characters
1616
slow:Kernel.system ignores SHELL env var and always uses `sh`
1717
slow:Kernel.system expands shell variables when given a single string argument
1818
slow:Kernel.system does not expand shell variables when given multiples arguments
19-
fails:Kernel#system raises RuntimeError when `exception: true` is given and the command exits with a non-zero exit status
20-
fails:Kernel#system raises Errno::ENOENT when `exception: true` is given and the specified command does not exist
21-
fails:Kernel.system raises RuntimeError when `exception: true` is given and the command exits with a non-zero exit status
22-
fails:Kernel.system raises Errno::ENOENT when `exception: true` is given and the specified command does not exist
19+
slow:Kernel#system raises RuntimeError when `exception: true` is given and the command exits with a non-zero exit status
20+
slow:Kernel#system raises Errno::ENOENT when `exception: true` is given and the specified command does not exist
21+
slow:Kernel.system raises RuntimeError when `exception: true` is given and the command exits with a non-zero exit status
22+
slow:Kernel.system raises Errno::ENOENT when `exception: true` is given and the specified command does not exist

src/main/ruby/truffleruby/core/kernel.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,30 @@ def syscall(*args)
505505
module_function :syscall
506506

507507
def system(*args)
508+
options = Truffle::Type.try_convert(args.last, Hash, :to_hash)
509+
exception = if options
510+
args[-1] = options
511+
options.delete(:exception)
512+
else
513+
false
514+
end
515+
508516
begin
509517
pid = Process.spawn(*args)
510-
rescue SystemCallError
518+
rescue SystemCallError => e
519+
raise e if exception
511520
return nil
512521
end
513522

514523
Process.waitpid pid
515-
$?.exitstatus == 0
524+
result = $?.exitstatus == 0
525+
return true if result
526+
if exception
527+
# TODO (bjfish, 9 Jan 2020): refactoring needed for more descriptive errors
528+
raise RuntimeError, 'command failed'
529+
else
530+
return false
531+
end
516532
end
517533
module_function :system
518534

0 commit comments

Comments
 (0)