Skip to content

Commit

Permalink
Prepare io/console for move to gem.
Browse files Browse the repository at this point in the history
This commit makes the following changes:

* Move JRuby-specific console logic to io/console/jruby
* Add console.rb to load appropriate impl for the current engine
* Adjust tags to match passing tests
* Add test_io_console to MRI stdlib suite

See ruby/io-console#11
  • Loading branch information
headius committed Jan 13, 2020
1 parent d0addf2 commit 9af94cb
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 85 deletions.
Expand Up @@ -95,8 +95,8 @@ public static void cacheCallSite(SkinnyMethodAdapter method, String className, S
method.ldc(call.getId());
if (refined) {
siteClass = RefinedCachingCallSite.class;
signature = sig(siteClass, String.class, IRScope.class, String.class);
method.getstatic(className, scopeFieldName, ci(IRScope.class));
signature = sig(siteClass, String.class, StaticScope.class, String.class);
load
method.ldc(callType.name());
} else {
switch (callType) {
Expand Down
70 changes: 4 additions & 66 deletions lib/ruby/stdlib/io/console.rb
@@ -1,67 +1,5 @@
# This implementation of io/console is a little hacky. It shells out to `stty`
# for most operations, which does not work on Windows, in secured environments,
# and so on. In addition, because on Java 6 we can't actually launch
# subprocesses with tty control, stty will not actually manipulate the
# controlling terminal.
#
# For platforms where shelling to stty does not work, most operations will
# just be pass-throughs. This allows them to function, but does not actually
# change any tty flags.
#
# Finally, since we're using stty to shell out, we can only manipulate stdin/
# stdout tty rather than manipulating whatever terminal is actually associated
# with the IO we're calling against. This will produce surprising results if
# anyone is actually using io/console against non-stdio ttys...but that case
# seems like it would be pretty rare.
#
# Note: we are incorporating this into 1.7.0 since RubyGems uses io/console
# when pushing gems, in order to mask the password entry. Worst case is that
# we don't actually disable echo and the password is shown...we will try to
# do a better version of this in 1.7.1.

require 'rbconfig'

require 'io/console/common'

# If Windows, always use the stub version
if RbConfig::CONFIG['host_os'] =~ /(mswin)|(win32)|(ming)/
require 'io/console/stub_console'
if RUBY_ENGINE == 'jruby'
require 'io/console/jruby'
else

# If Linux or BSD, try to load the native version
if RbConfig::CONFIG['host_os'].downcase =~ /darwin|openbsd|freebsd|netbsd|linux/
begin

# Attempt to load the native Linux and BSD console logic
require 'io/console/native_console'
ready = true

rescue Exception => ex

warn "failed to load native console support: #{ex}" if $VERBOSE
ready = false

end
end

# Native failed, try to use stty
if !ready
begin

require 'io/console/stty_console'
ready = true

rescue Exception

warn "failed to load stty console support: #{ex}" if $VERBOSE
ready = false

end
end

# If still not ready, just use stubbed version
if !ready
require 'io/console/stub_console'
end

end
require 'io/console.so'
end
67 changes: 67 additions & 0 deletions lib/ruby/stdlib/io/console/jruby.rb
@@ -0,0 +1,67 @@
# This implementation of io/console is a little hacky. It shells out to `stty`
# for most operations, which does not work on Windows, in secured environments,
# and so on. In addition, because on Java 6 we can't actually launch
# subprocesses with tty control, stty will not actually manipulate the
# controlling terminal.
#
# For platforms where shelling to stty does not work, most operations will
# just be pass-throughs. This allows them to function, but does not actually
# change any tty flags.
#
# Finally, since we're using stty to shell out, we can only manipulate stdin/
# stdout tty rather than manipulating whatever terminal is actually associated
# with the IO we're calling against. This will produce surprising results if
# anyone is actually using io/console against non-stdio ttys...but that case
# seems like it would be pretty rare.
#
# Note: we are incorporating this into 1.7.0 since RubyGems uses io/console
# when pushing gems, in order to mask the password entry. Worst case is that
# we don't actually disable echo and the password is shown...we will try to
# do a better version of this in 1.7.1.

require 'rbconfig'

require_relative 'jruby/common'

# If Windows, always use the stub version
if RbConfig::CONFIG['host_os'] =~ /(mswin)|(win32)|(ming)/
require_relative 'jruby/stub_console'
else

# If Linux or BSD, try to load the native version
if RbConfig::CONFIG['host_os'].downcase =~ /darwin|openbsd|freebsd|netbsd|linux/
begin

# Attempt to load the native Linux and BSD console logic
require_relative 'jruby/native_console'
ready = true

rescue Exception => ex

warn "failed to load native console support: #{ex}" if $VERBOSE
ready = false

end
end

# Native failed, try to use stty
if !ready
begin

require_relative 'jruby/stty_console'
ready = true

rescue Exception

warn "failed to load stty console support: #{ex}" if $VERBOSE
ready = false

end
end

# If still not ready, just use stubbed version
if !ready
require_relative 'jruby/stub_console'
end

end
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,9 +1,9 @@
# Load appropriate native bits for BSD or Linux
case RbConfig::CONFIG['host_os'].downcase
when /darwin|openbsd|freebsd|netbsd/
require 'io/console/bsd_console'
require_relative 'bsd_console'
when /linux/
require 'io/console/linux_console'
require_relative 'linux_console'
else
raise LoadError.new("no native io/console support")
end
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/mri.stdlib.index
Expand Up @@ -83,7 +83,7 @@ fileutils/test_verbose.rb

io/wait/test_io_wait.rb
io/nonblock/test_flush.rb
#io/console/test_io_console.rb
io/console/test_io_console.rb

json/json_addition_test.rb
json/json_common_interface_test.rb
Expand Down
27 changes: 14 additions & 13 deletions test/mri/excludes/TestIO_Console.rb
@@ -1,17 +1,18 @@
# frozen_string_literal: false
exclude :test_cooked, 'depends on unimplemented PTY functionality'
exclude :test_echo, 'depends on unimplemented PTY functionality'
# exclude :test_cooked, 'depends on unimplemented PTY functionality'
# exclude :test_echo, 'depends on unimplemented PTY functionality'
exclude :test_getpass, 'not portable'
exclude :test_iflush, 'depends on unimplemented PTY functionality'
exclude :test_ioflush, 'depends on unimplemented PTY functionality'
exclude :test_ioflush2, 'depends on unimplemented PTY functionality'
exclude :test_noecho, 'depends on unimplemented PTY functionality'
exclude :test_noecho2, 'depends on unimplemented PTY functionality'
exclude :test_oflush, 'depends on unimplemented PTY functionality'
exclude :test_raw, 'depends on unimplemented PTY functionality'
exclude :test_raw!, 'depends on unimplemented PTY functionality'
# exclude :test_iflush, 'depends on unimplemented PTY functionality'
# exclude :test_ioflush, 'depends on unimplemented PTY functionality'
# exclude :test_ioflush2, 'depends on unimplemented PTY functionality'
# exclude :test_noecho, 'depends on unimplemented PTY functionality'
# exclude :test_noecho2, 'depends on unimplemented PTY functionality'
# exclude :test_oflush, 'depends on unimplemented PTY functionality'
# exclude :test_raw, 'depends on unimplemented PTY functionality'
# exclude :test_raw!, 'depends on unimplemented PTY functionality'
exclude :test_raw_minchar, 'depends on unimplemented PTY functionality'
exclude :test_raw_timeout, 'depends on unimplemented PTY functionality'
exclude :test_setecho, 'depends on unimplemented PTY functionality'
exclude :test_setecho2, 'depends on unimplemented PTY functionality'
exclude :test_winsize, 'depends on unimplemented PTY functionality'
exclude :test_set_winsize_invalid_dev, 'depends on unimplemented PTY functionality'
# exclude :test_setecho, 'depends on unimplemented PTY functionality'
# exclude :test_setecho2, 'depends on unimplemented PTY functionality'
# exclude :test_winsize, 'depends on unimplemented PTY functionality'
2 changes: 1 addition & 1 deletion test/mri/io/console/test_io_console.rb
Expand Up @@ -355,7 +355,7 @@ def test_sync
noctty = [EnvUtil.rubybin, "-e", "Process.daemon(true)"]
when !(rubyw = RbConfig::CONFIG["RUBYW_INSTALL_NAME"]).empty?
dir, base = File.split(EnvUtil.rubybin)
noctty = [File.join(dir, base.sub(/ruby/, rubyw))]
noctty = [File.join(dir, base.sub(RUBY_ENGINE, rubyw))]
end

if noctty
Expand Down

0 comments on commit 9af94cb

Please sign in to comment.