Blob Blame History Raw
From 735c110709a8f150c41b48e2423382481c15407d Mon Sep 17 00:00:00 2001
From: Mamoru TASAKA <mtasaka@fedoraproject.org>
Date: Sat, 31 Dec 2022 12:14:33 +0900
Subject: [PATCH] [rb][spec] ruby3.2: redirect stdout directly instead of using
 Logger.reopen

In selenium rb rspec test suite, rspec mocks `File.exist?` method partway.
With ruby3.2, this may confuse `::Logger.new` using `$stdout` as output because of
`IO` class being changed to accept `#path` method.
Then it may close `$stdout` with `::Logger.reopen` via `have_deprecated` rspec
matcher defined in selenium internally.

So when using have_deprecated matcher defined inside selenium, instead of using
Logger.reopen, redirect stdout directly, using `Tempfile` and reopen `$stdout`
directly.

Also, as said above, with ruby 3.2 `IO` also accepts `#path` method, so
`IO#path.exists?` is now valid on ruby3.2. This affects 'uses the given jar file and port'
testsuite in spec/unit/selenium/server_spec.rb: `File` class must be mocked
so that `#exist?` method accepts also '<STDOUT>` argument.

Closes #11498 .
---
 rb/spec/rspec_matchers.rb            | 15 +++++++++------
 rb/spec/unit/selenium/server_spec.rb |  1 +
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/rb/spec/rspec_matchers.rb b/rb/spec/rspec_matchers.rb
index c3df9caf50ca..3893f1648c83 100644
--- a/rb/spec/rspec_matchers.rb
+++ b/rb/spec/rspec_matchers.rb
@@ -20,14 +20,17 @@
 RSpec::Matchers.define :have_deprecated do |deprecation|
   match do |actual|
     # Suppresses logging output to stdout while ensuring that it is still happening
-    default_output = Selenium::WebDriver.logger.io
-    io = StringIO.new
-    Selenium::WebDriver.logger.output = io
+    require "tempfile"
+    Tempfile.create("selenium_rb") do |io|
+      stdout_default = $stdout.dup
+      $stdout.reopen io
 
-    actual.call
+      actual.call
 
-    Selenium::WebDriver.logger.output = default_output
-    @deprecations_found = (io.rewind && io.read).scan(/DEPRECATION\] \[:([^\]]*)\]/).flatten.map(&:to_sym)
+      $stdout.flush
+      $stdout.reopen stdout_default
+      @deprecations_found = (io.rewind && io.read).scan(/DEPRECATION\] \[:([^\]]*)\]/).flatten.map(&:to_sym)
+    end
     expect(Array(deprecation).sort).to eq(@deprecations_found.sort)
   end
 
diff --git a/rb/spec/unit/selenium/server_spec.rb b/rb/spec/unit/selenium/server_spec.rb
index 9384d2fee1cd..053af5f42559 100644
--- a/rb/spec/unit/selenium/server_spec.rb
+++ b/rb/spec/unit/selenium/server_spec.rb
@@ -46,6 +46,7 @@ module Selenium
 
     it 'uses the given jar file and port' do
       allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
+      allow(File).to receive(:exist?).with('<STDOUT>').and_return(false)
       allow(ChildProcess).to receive(:build)
         .with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '1234')
         .and_return(mock_process)