| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-16 22:42:52 +01:00
										 |  |  | describe Tty do | 
					
						
							|  |  |  |   describe "::strip_ansi" do | 
					
						
							|  |  |  |     it "removes ANSI escape codes from a string" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.strip_ansi("\033\[36;7mhello\033\[0m")).to eq("hello") | 
					
						
							| 
									
										
										
										
											2017-02-16 22:42:52 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "::width" do | 
					
						
							|  |  |  |     it "returns an Integer" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.width).to be_kind_of(Integer) | 
					
						
							| 
									
										
										
										
											2017-02-16 22:42:52 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "cannot be negative" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.width).to be >= 0
 | 
					
						
							| 
									
										
										
										
											2017-02-16 22:42:52 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "::truncate" do | 
					
						
							|  |  |  |     it "truncates the text to the terminal width, minus 4, to account for '==> '" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       allow(described_class).to receive(:width).and_return(15) | 
					
						
							| 
									
										
										
										
											2017-02-16 22:42:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.truncate("foobar something very long")).to eq("foobar some") | 
					
						
							|  |  |  |       expect(described_class.truncate("truncate")).to eq("truncate") | 
					
						
							| 
									
										
										
										
											2017-02-16 22:42:52 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "doesn't truncate the text if the terminal is unsupported, i.e. the width is 0" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       allow(described_class).to receive(:width).and_return(0) | 
					
						
							|  |  |  |       expect(described_class.truncate("foobar something very long")).to eq("foobar something very long") | 
					
						
							| 
									
										
										
										
											2017-02-16 22:42:52 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   context "when $stdout is not a TTY" do | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  |     before do | 
					
						
							| 
									
										
										
										
											2017-02-16 22:42:52 +01:00
										 |  |  |       allow($stdout).to receive(:tty?).and_return(false) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "returns an empty string for all colors" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.red.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.green.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.yellow.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.blue.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.magenta.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.cyan.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.default.to_s).to eq("") | 
					
						
							| 
									
										
										
										
											2017-02-16 22:42:52 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   context "when $stdout is a TTY" do | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  |     before do | 
					
						
							| 
									
										
										
										
											2017-02-16 22:42:52 +01:00
										 |  |  |       allow($stdout).to receive(:tty?).and_return(true) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-12 11:06:55 -06:00
										 |  |  |     it "returns ANSI escape codes for colors" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.red.to_s).to eq("\033[31m") | 
					
						
							|  |  |  |       expect(described_class.green.to_s).to eq("\033[32m") | 
					
						
							|  |  |  |       expect(described_class.yellow.to_s).to eq("\033[33m") | 
					
						
							|  |  |  |       expect(described_class.blue.to_s).to eq("\033[34m") | 
					
						
							|  |  |  |       expect(described_class.magenta.to_s).to eq("\033[35m") | 
					
						
							|  |  |  |       expect(described_class.cyan.to_s).to eq("\033[36m") | 
					
						
							|  |  |  |       expect(described_class.default.to_s).to eq("\033[39m") | 
					
						
							| 
									
										
										
										
											2017-02-16 22:42:52 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-02-12 11:06:55 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it "returns an empty string for all colors when HOMEBREW_NO_COLOR is set" do | 
					
						
							|  |  |  |       ENV["HOMEBREW_NO_COLOR"] = "1" | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.red.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.green.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.yellow.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.blue.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.magenta.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.cyan.to_s).to eq("") | 
					
						
							|  |  |  |       expect(described_class.default.to_s).to eq("") | 
					
						
							| 
									
										
										
										
											2018-02-12 11:06:55 -06:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-02-16 22:42:52 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | end |