| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-08 00:57:08 +02:00
										 |  |  | require "delegate" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Test | 
					
						
							|  |  |  |   module Helper | 
					
						
							|  |  |  |     module OutputAsTTY | 
					
						
							|  |  |  |       # This is a custom wrapper for the `output` matcher, | 
					
						
							|  |  |  |       # used for testing output to a TTY: | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       #   expect { | 
					
						
							|  |  |  |       #     print "test" if $stdout.tty? | 
					
						
							|  |  |  |       #   }.to output("test").to_stdout.as_tty | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       #   expect { | 
					
						
							|  |  |  |       #     # command | 
					
						
							|  |  |  |       #   }.to output(...).to_stderr.as_tty.with_color | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       class Output < SimpleDelegator | 
					
						
							|  |  |  |         def matches?(block) | 
					
						
							|  |  |  |           return super(block) unless @tty | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-08 18:26:46 +02:00
										 |  |  |           colored_tty_block = lambda do | 
					
						
							| 
									
										
										
										
											2020-11-10 00:28:45 +11:00
										 |  |  |             instance_eval("$#{@output}  # $stdout", __FILE__, __LINE__).extend(Module.new do | 
					
						
							| 
									
										
										
										
											2017-07-08 18:26:46 +02:00
										 |  |  |               def tty? | 
					
						
							|  |  |  |                 true | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               alias_method :isatty, :tty? | 
					
						
							|  |  |  |             end) | 
					
						
							|  |  |  |             block.call | 
					
						
							| 
									
										
										
										
											2017-07-08 00:57:08 +02:00
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           return super(colored_tty_block) if @colors | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           uncolored_tty_block = lambda do | 
					
						
							| 
									
										
										
										
											2018-01-17 10:42:43 +00:00
										 |  |  |             instance_eval <<-EOS, __FILE__, __LINE__ + 1
 | 
					
						
							| 
									
										
										
										
											2020-11-10 00:28:45 +11:00
										 |  |  |               begin                                                       # begin | 
					
						
							|  |  |  |                 captured_stream = StringIO.new                            #   captured_stream = StringIO.new | 
					
						
							| 
									
										
										
										
											2017-07-08 18:26:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 00:28:45 +11:00
										 |  |  |                 original_stream = $#{@output}                             #   original_stream = $stdout | 
					
						
							|  |  |  |                 $#{@output} = captured_stream                             #   $stdout = captured_stream | 
					
						
							| 
									
										
										
										
											2017-07-08 18:26:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 00:28:45 +11:00
										 |  |  |                 colored_tty_block.call                                    #   colored_tty_block.call | 
					
						
							|  |  |  |               ensure                                                      # ensure | 
					
						
							|  |  |  |                 $#{@output} = original_stream                             #   $stdout = original_stream | 
					
						
							|  |  |  |                 $#{@output}.print Tty.strip_ansi(captured_stream.string)  #   $stdout.print Tty.strip_ansi(captured_stream.string) | 
					
						
							|  |  |  |               end                                                         # end | 
					
						
							| 
									
										
										
										
											2017-07-08 18:26:46 +02:00
										 |  |  |             EOS | 
					
						
							| 
									
										
										
										
											2017-07-08 00:57:08 +02:00
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           super(uncolored_tty_block) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def to_stdout | 
					
						
							|  |  |  |           @output = :stdout | 
					
						
							|  |  |  |           super | 
					
						
							|  |  |  |           self | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def to_stderr | 
					
						
							|  |  |  |           @output = :stderr | 
					
						
							|  |  |  |           super | 
					
						
							|  |  |  |           self | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def as_tty | 
					
						
							|  |  |  |           @tty = true | 
					
						
							| 
									
										
										
										
											2017-07-08 18:26:46 +02:00
										 |  |  |           return self if [:stdout, :stderr].include?(@output) | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-08 18:26:46 +02:00
										 |  |  |           raise "`as_tty` can only be chained to `stdout` or `stderr`." | 
					
						
							| 
									
										
										
										
											2017-07-08 00:57:08 +02:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def with_color | 
					
						
							|  |  |  |           @colors = true | 
					
						
							|  |  |  |           return self if @tty | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-08 00:57:08 +02:00
										 |  |  |           raise "`with_color` can only be chained to `as_tty`." | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def output(*args) | 
					
						
							|  |  |  |         core_matcher = super(*args) | 
					
						
							|  |  |  |         Output.new(core_matcher) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |