How can I get color rendition in an autotest using Test / Unit, MiniTest?

Rails 3.2.1 application using minitest and autotest-rails gems.

If I run "rake test", the output will be in color. But if I run an autotest, the output is not in color.

How to get color information when using autotest?

Here is my test_helper.rb:

ENV["RAILS_ENV"] = "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require 'turn/autorun' Turn.config do |c| # use one of output formats: # :outline - turn original case/test outline mode [default] # :progress - indicates progress with progress bar # :dotted - test/unit traditional dot-progress mode # :pretty - new pretty reporter # :marshal - dump output as YAML (normal run mode only) # :cue - interactive testing c.format = :pretty # turn on invoke/execute tracing, enable full backtrace c.trace = true # use humanized test names (works only with :outline format) c.natural = true end class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. # # Note: You'll currently still have to declare fixtures explicitly in integration tests # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... end 
+6
source share
1 answer

If I run "rake test", the output will be in color.

This is due to the fact that in the autotest mode the "terminal" in which your test procedure runs is not tty , but when launched directly.

First, how it works, color codes are defined in escape sequences which, if you wrote them down, would look like \E[48mPRINT ME IN RED\E[0m ( details ).

Your terminal understands these escape sequences (usually), replacing them with colors, improving the appearance of the output.

Using the environment variables defined by the terminal emulator and looking at it, it enters, and the output streams (i.e. $stdin , $stdout and $stderr ) process (s) (s), can determine the color support and it is connected to the terminal ( tty ) or to a file or other process, etc.

When one process starts another process, your process, and not the terminal, is the owner, so your test output does not talk to the terminal, which understands the color escape sequences, it talks to the autotest, and this is not so.

The same behavior will happen by running tests, but redirecting the output to a file, escape codes and sequences will be meaningless.

The relationship is as follows:

 # rake test Terminal Application \- Bash \- rake # Rake $stdout.tty? => true # (Bash is a terminal emulator) # autotest Terminal Application \- Bash \- autotest \- rake # Rake $stdout.tty? => false # (Autotest is not a terminal emulator) 

There are several ways to fake support, so autotest works in color, one of the methods described here will turn out to be the most reliable without checking it yourself.

Another way is simply to short-circuit the “color support check” using this method

The #tty? method #tty? in streams is not only useful for the above, but also consider the case when you run Ruby-debug or some other “interactive” command, when the control process is not tty, there is no way that ruby-debug can ask the user if he is connected to another application, which may not understand the prompts, therefore, when streaming output to a file or executing one process inside another, intelligent software always checks first to see if the parent process can be confused by calling and typing or sending non-standard output.

If you want to do extra reading, take a look at $stdin.tty? from the Ruby documentation, it explains the difference between inbound and outbound process flows, which are considered ttys, and the effect that is related to how this is done.

+7
source

Source: https://habr.com/ru/post/909016/


All Articles