Skip to content

Commit

Permalink
Run Rails console test against IRB with Reline instead of Readline
Browse files Browse the repository at this point in the history
1. By removing the `--singleline` flag, IRB will use Reline by default.
2. By assigning `TERM=dumb`, Reline will skip east-asian width detection,
   which was what caused the test to hang.

I need to stress that the east-asian width detection is not a bug but an
improvement in Reline to help rendering east-asian characters correctly.
Readline actually can't do this well. Please see @tompng's great explanation
in ruby/irb#582 (comment)

However, this detection should not happen when the terminal is running in
PTY (usually used in test environment). The problem is that in Ruby we
don't have a way to detect if the terminal is running in TTY or PTY.

But by passing `TERM=dumb`, Reline will assume that the terminal is not
capable of several advanced features, including this east-asian width
detection.
  • Loading branch information
st0012 committed Jun 1, 2023
1 parent 3438316 commit cf45394
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions railties/test/application/console_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def write_prompt(command, expected_output = nil)

def spawn_console(options, wait_for_prompt: true)
pid = Process.spawn(
{ "TERM" => "dumb" },
"#{app_path}/bin/rails console #{options}",
in: @replica, out: @replica, err: @replica
)
Expand All @@ -137,7 +138,7 @@ def spawn_console(options, wait_for_prompt: true)
end

def test_sandbox
options = "--sandbox -- --singleline --nocolorize"
options = "--sandbox -- --nocolorize"
spawn_console(options)

write_prompt "Post.count", "=> 0"
Expand Down Expand Up @@ -165,7 +166,7 @@ def test_sandbox_when_sandbox_is_disabled
end

def test_environment_option_and_irb_option
options = "-e test -- --verbose --singleline --nocolorize"
options = "-e test -- --verbose --nocolorize"
spawn_console(options)

write_prompt "a = 1", "a = 1"
Expand Down
8 changes: 4 additions & 4 deletions railties/test/engine/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def test_runner_command_work_inside_engine
if available_pty?
def test_console_command_work_inside_engine
primary, replica = PTY.open
cmd = "console --singleline"
spawn_command(cmd, replica)
cmd = "console"
spawn_command(cmd, replica, env: { "TERM" => "dumb" })
assert_output(">", primary)
ensure
primary.puts "quit"
Expand Down Expand Up @@ -64,9 +64,9 @@ def plugin_path
"#{@destination_root}/bukkits"
end

def spawn_command(command, fd)
def spawn_command(command, fd, env: {})
in_plugin_context(plugin_path) do
Process.spawn("bin/rails #{command}", in: fd, out: fd, err: fd)
Process.spawn(env, "bin/rails #{command}", in: fd, out: fd, err: fd)
end
end

Expand Down

0 comments on commit cf45394

Please sign in to comment.