[前][次][番号順一覧][スレッド一覧]

ruby-changes:72319

From: Peter <ko1@a...>
Date: Sun, 26 Jun 2022 14:41:01 +0900 (JST)
Subject: [ruby-changes:72319] e0bfdb23af (master): [ruby/irb] Ensure stdout is a TTY before calling winsize

https://git.ruby-lang.org/ruby.git/commit/?id=e0bfdb23af

From e0bfdb23af3f182d7605a6ac3c93b07001d9045c Mon Sep 17 00:00:00 2001
From: Peter Jones <pjones@d...>
Date: Thu, 24 Mar 2022 13:48:27 -0700
Subject: [ruby/irb] Ensure stdout is a TTY before calling winsize

When outputting a (possibly truncated) value, IRB will query the
window size.  However, if IRB was piped to another process, stdout
will no longer be a TTY and will not support the `winsize` method.

This fix ensure that stdout is a TTY.

https://github.com/ruby/irb/commit/125de5eeea
---
 lib/irb/input-method.rb  |  2 +-
 test/irb/test_context.rb | 16 ++++++++++++----
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb
index 64276e61be..fd68239ee3 100644
--- a/lib/irb/input-method.rb
+++ b/lib/irb/input-method.rb
@@ -39,7 +39,7 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/input-method.rb#L39
     public :gets
 
     def winsize
-      if instance_variable_defined?(:@stdout)
+      if instance_variable_defined?(:@stdout) && @stdout.tty?
         @stdout.winsize
       else
         [24, 80]
diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb
index 42f82fc37e..ad842c2725 100644
--- a/test/irb/test_context.rb
+++ b/test/irb/test_context.rb
@@ -30,10 +30,6 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_context.rb#L30
       def reset
         @line_no = 0
       end
-
-      def winsize
-        [10, 20]
-      end
     end
 
     def setup
@@ -135,6 +131,18 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_context.rb#L131
         ], out)
     end
 
+    def test_output_to_pipe
+      input = TestInputMethod.new(["n=1"])
+      input.instance_variable_set(:@stdout, StringIO.new)
+      irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
+      irb.context.echo_on_assignment = :truncate
+      out, err = capture_output do
+        irb.eval_input
+      end
+      assert_empty err
+      assert_equal "=> 1\n", out
+    end
+
     def test_eval_object_without_inspect_method
       verbose, $VERBOSE = $VERBOSE, nil
       all_assertions do |all|
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]