Blob Blame History Raw
From cf45394d104b00679c900e9d2dd09154cadcbe11 Mon Sep 17 00:00:00 2001
From: Stan Lo <stan001212@gmail.com>
Date: Thu, 1 Jun 2023 18:00:31 +0100
Subject: [PATCH] Run Rails console test against IRB with Reline instead of
 Readline

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 https://github.com/ruby/irb/pull/582#issuecomment-1550057805

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.
---
 railties/test/application/console_test.rb | 5 +++--
 railties/test/engine/commands_test.rb     | 8 ++++----
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/railties/test/application/console_test.rb b/railties/test/application/console_test.rb
index 4ef1ef012038..11e01f3c73ba 100644
--- a/railties/test/application/console_test.rb
+++ b/railties/test/application/console_test.rb
@@ -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
     )
@@ -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"
@@ -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"
diff --git a/railties/test/engine/commands_test.rb b/railties/test/engine/commands_test.rb
index 925b4b32f7ce..bc9211ce967d 100644
--- a/railties/test/engine/commands_test.rb
+++ b/railties/test/engine/commands_test.rb
@@ -34,8 +34,8 @@ def test_runner_command_work_inside_engine
     skip "PTY unavailable" unless available_pty?
 
     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"
@@ -66,8 +66,9 @@ def plugin_path
       "#{@destination_root}/bukkits"
     end
 
-    def spawn_command(command, fd)
+    def spawn_command(command, fd, env: {})
       Process.spawn(
+        env,
         "#{plugin_path}/bin/rails #{command}",
         in: fd, out: fd, err: fd
       )