2014-12-31 15:59:32 -05:00
|
|
|
require 'spec_helper'
|
2011-11-08 17:50:16 -05:00
|
|
|
|
2014-12-31 15:59:32 -05:00
|
|
|
RSpec.describe "AwesomePrint" do
|
2011-11-08 17:50:16 -05:00
|
|
|
def stub_tty!(output = true, stream = STDOUT)
|
|
|
|
if output
|
|
|
|
stream.instance_eval { def tty?; true; end }
|
|
|
|
else
|
|
|
|
stream.instance_eval { def tty?; false; end }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "colorization" do
|
|
|
|
PLAIN = '[ 1, :two, "three", [ nil, [ true, false ] ] ]'
|
|
|
|
COLORIZED = "[ \e[1;34m1\e[0m, \e[0;36m:two\e[0m, \e[0;33m\"three\"\e[0m, [ \e[1;31mnil\e[0m, [ \e[1;32mtrue\e[0m, \e[1;31mfalse\e[0m ] ] ]"
|
|
|
|
|
|
|
|
before do
|
|
|
|
ENV['TERM'] = "xterm-colors"
|
|
|
|
ENV.delete('ANSICON')
|
|
|
|
@arr = [ 1, :two, "three", [ nil, [ true, false] ] ]
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "default settings (no forced colors)" do
|
|
|
|
before do
|
|
|
|
AwesomePrint.force_colors! false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "colorizes tty processes by default" do
|
|
|
|
stub_tty!
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
|
2011-11-08 17:50:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "colorizes processes with ENV['ANSICON'] by default" do
|
|
|
|
begin
|
|
|
|
stub_tty!
|
|
|
|
term, ENV['ANSICON'] = ENV['ANSICON'], "1"
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
|
2011-11-08 17:50:16 -05:00
|
|
|
ensure
|
|
|
|
ENV['ANSICON'] = term
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not colorize tty processes running in dumb terminals by default" do
|
|
|
|
begin
|
|
|
|
stub_tty!
|
|
|
|
term, ENV['TERM'] = ENV['TERM'], "dumb"
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(@arr.ai(:multiline => false)).to eq(PLAIN)
|
2011-11-08 17:50:16 -05:00
|
|
|
ensure
|
|
|
|
ENV['TERM'] = term
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not colorize subprocesses by default" do
|
|
|
|
begin
|
|
|
|
stub_tty! false
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(@arr.ai(:multiline => false)).to eq(PLAIN)
|
2011-11-08 17:50:16 -05:00
|
|
|
ensure
|
|
|
|
stub_tty!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "forced colors override" do
|
|
|
|
before do
|
|
|
|
AwesomePrint.force_colors!
|
|
|
|
end
|
2016-08-27 09:43:32 -04:00
|
|
|
|
2011-11-08 17:50:16 -05:00
|
|
|
it "still colorizes tty processes" do
|
|
|
|
stub_tty!
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
|
2011-11-08 17:50:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "colorizes processes with ENV['ANSICON'] set to 0" do
|
|
|
|
begin
|
|
|
|
stub_tty!
|
|
|
|
term, ENV['ANSICON'] = ENV['ANSICON'], "1"
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
|
2011-11-08 17:50:16 -05:00
|
|
|
ensure
|
|
|
|
ENV['ANSICON'] = term
|
|
|
|
end
|
|
|
|
end
|
2016-08-27 09:43:32 -04:00
|
|
|
|
2011-11-08 17:50:16 -05:00
|
|
|
it "colorizes dumb terminals" do
|
|
|
|
begin
|
|
|
|
stub_tty!
|
|
|
|
term, ENV['TERM'] = ENV['TERM'], "dumb"
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
|
2011-11-08 17:50:16 -05:00
|
|
|
ensure
|
|
|
|
ENV['TERM'] = term
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "colorizes subprocess" do
|
|
|
|
begin
|
|
|
|
stub_tty! false
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
|
2011-11-08 17:50:16 -05:00
|
|
|
ensure
|
|
|
|
stub_tty!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|