Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Add support for bold colours #2914

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion features/formatters/configurable_colors.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Feature: Configurable colors
* `detail_color`: Color used for miscellaneous test details (default: `:cyan`)

Colors are specified as symbols. Options are `:black`, `:red`, `:green`,
`:yellow`, `:blue`, `:magenta`, `:cyan`, and `:white`.
`:yellow`, `:blue`, `:magenta`, `:cyan`, `:white`, `:bold_black`, `:bold_red`,
`:bold_green`, `:bold_yellow`, `:bold_blue`, `:bold_magenta`, `:bold_cyan`,
and `:bold_white`,

@keep-ansi-escape-sequences
Scenario: Customizing the failure color
Expand All @@ -29,3 +31,21 @@ Feature: Configurable colors
"""
When I run `rspec custom_failure_color_spec.rb --format progress`
Then the failing example is printed in magenta

@keep-ansi-escape-sequences
Scenario: Customizing the failure color with a custom console code
Given a file named "custom_failure_color_spec.rb" with:
"""ruby
RSpec.configure do |config|
config.failure_color = "1;32"
config.color_mode = :on
end

RSpec.describe "failure" do
it "fails and uses the custom color" do
expect(2).to eq(4)
end
end
"""
When I run `rspec custom_failure_color_spec.rb --format progress`
Then the failing example is printed wrapped in "1;32"
10 changes: 8 additions & 2 deletions features/step_definitions/additional_cli_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@
end

# This step can be generalized if it's ever used to test other colors
Then /^the failing example is printed in magenta$/ do
Then /^the failing example is printed (?:wrapped )?in (.*)$/ do |color|
code =
case color
when "magenta" then "\e[35m"
when /"(.*)"/ then "\e[#{$1}m"
end

# \e[35m = enable magenta
# \e[0m = reset colors
expect(all_output).to include("\e[35m" + "F" + "\e[0m")
expect(all_output).to include(code + "F" + "\e[0m")
end

Then /^the output from `([^`]+)` should contain "(.*?)"$/ do |cmd, expected_output|
Expand Down
26 changes: 17 additions & 9 deletions lib/rspec/core/formatters/console_codes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ module ConsoleCodes
# @private
VT100_CODES =
{
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:magenta => 35,
:cyan => 36,
:white => 37,
:bold => 1,
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:magenta => 35,
:cyan => 36,
:white => 37,
:bold_black => '1;30',
:bold_red => '1;31',
:bold_green => '1;32',
:bold_yellow => '1;33',
:bold_blue => '1;34',
:bold_magenta => '1;35',
:bold_cyan => '1;36',
:bold_white => '1;37',
:bold => 1,
}
# @private
VT100_CODE_VALUES = VT100_CODES.invert
Expand Down
24 changes: 24 additions & 0 deletions spec/rspec/core/formatters/console_codes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
end
end

context "when given a VT100 compound code" do
it "returns the code" do
expect(console_codes.console_code_for('1;32')).to eq '1;32'
end
end

context "when given a symbolic name" do
it "returns the code" do
expect(console_codes.console_code_for(:green)).to eq 32
Expand Down Expand Up @@ -41,19 +47,37 @@
end
end

context "when given a VT100 compound code" do
it "formats the text with it" do
expect(console_codes.wrap('abc', '1;32')).to eq "\e[1;32mabc\e[0m"
end
end

context "when given a symbolic color name" do
it "translates it to the correct integer code and formats the text with it" do
expect(console_codes.wrap('abc', :green)).to eq "\e[32mabc\e[0m"
end
end

context "when given a symbolic bold color name" do
it "translates it to the correct integer code and formats the text with it" do
expect(console_codes.wrap('abc', :bold_green)).to eq "\e[1;32mabc\e[0m"
end
end

context "when given an rspec code" do
it "returns the console code" do
RSpec.configuration.success_color = :blue # blue is 34
expect(console_codes.wrap('abc', :success)).to eq "\e[34mabc\e[0m"
end
end

context "when given a compound rspec code" do
it "returns the console code" do
RSpec.configuration.success_color = :bold_blue # blue is 34
expect(console_codes.wrap('abc', :success)).to eq "\e[1;34mabc\e[0m"
end
end

context "when given :bold" do
it "formats the text as bold" do
Expand Down