about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <bofh@yhbt.net>2020-07-26 01:57:58 +0000
committerEric Wong <bofh@yhbt.net>2020-07-30 02:58:30 +0000
commitb51542b6e1c16457748c553f49d4f847556c6137 (patch)
tree1b7203fc2d831cd9bb54c1c9fb079f54a1588fd6
parentba58f35a75d264ac90b7f099e9f19a922beaf4da (diff)
downloadunicorn-b51542b6e1c16457748c553f49d4f847556c6137.tar.gz
This can be useful for diagnosing failures, especially since
GNU tail supports inotify these days.
-rw-r--r--test/test_helper.rb21
1 files changed, 17 insertions, 4 deletions
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 94a5b1b..e3c6ad4 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -37,13 +37,25 @@ end
 def redirect_test_io
   orig_err = STDERR.dup
   orig_out = STDOUT.dup
-  STDERR.reopen("test_stderr.#{$$}.log", "a")
-  STDOUT.reopen("test_stdout.#{$$}.log", "a")
+  new_out = File.open("test_stdout.#$$.log", "a")
+  new_err = File.open("test_stderr.#$$.log", "a")
+  new_out.sync = new_err.sync = true
+
+  if tail = ENV['TAIL'] # "tail -F" if GNU, "tail -f" otherwise
+    require 'shellwords'
+    cmd = tail.shellsplit
+    cmd << new_out.path
+    cmd << new_err.path
+    pid = Process.spawn(*cmd, { 1 => 2, :pgroup => true })
+    sleep 0.1 # wait for tail(1) to startup
+  end
+  STDERR.reopen(new_err)
+  STDOUT.reopen(new_out)
   STDERR.sync = STDOUT.sync = true
 
   at_exit do
-    File.unlink("test_stderr.#{$$}.log") rescue nil
-    File.unlink("test_stdout.#{$$}.log") rescue nil
+    File.unlink(new_out.path) rescue nil
+    File.unlink(new_err.path) rescue nil
   end
 
   begin
@@ -51,6 +63,7 @@ def redirect_test_io
   ensure
     STDERR.reopen(orig_err)
     STDOUT.reopen(orig_out)
+    Process.kill(:TERM, pid) if pid
   end
 end