Merge pull request #322 from mbj/fix/tput-instance
Remove boot time tput initialization
This commit is contained in:
		
						commit
						5903214033
					
				
					 5 changed files with 100 additions and 23 deletions
				
			
		| 
						 | 
				
			
			@ -1,3 +1,3 @@
 | 
			
		|||
---
 | 
			
		||||
threshold: 18
 | 
			
		||||
total_score: 1223
 | 
			
		||||
total_score: 1233
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,9 +13,10 @@ module Mutant
 | 
			
		|||
      # @api private
 | 
			
		||||
      #
 | 
			
		||||
      def self.build(output)
 | 
			
		||||
        tput = Tput.detect
 | 
			
		||||
        tty = output.respond_to?(:tty?) && output.tty?
 | 
			
		||||
        format = if !Mutant.ci? && tty && Tput::INSTANCE.available
 | 
			
		||||
          Format::Framed.new(tty:  tty, tput: Tput::INSTANCE)
 | 
			
		||||
        format = if !Mutant.ci? && tty && tput
 | 
			
		||||
          Format::Framed.new(tty:  tty, tput: tput)
 | 
			
		||||
        else
 | 
			
		||||
          Format::Progressive.new(tty: tty)
 | 
			
		||||
        end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,23 +3,41 @@ module Mutant
 | 
			
		|||
    class CLI
 | 
			
		||||
      # Interface to the optionally present tput binary
 | 
			
		||||
      class Tput
 | 
			
		||||
        include Adamantium, Concord::Public.new(:available, :prepare, :restore)
 | 
			
		||||
        include Adamantium, Concord::Public.new(:prepare, :restore)
 | 
			
		||||
 | 
			
		||||
        private_class_method :new
 | 
			
		||||
 | 
			
		||||
        capture = lambda do |command|
 | 
			
		||||
        # Return detected tput support
 | 
			
		||||
        #
 | 
			
		||||
        # @return [Tput]
 | 
			
		||||
        #   if tput support is present
 | 
			
		||||
        #
 | 
			
		||||
        # @return [nil]
 | 
			
		||||
        #   otherwise
 | 
			
		||||
        def self.detect
 | 
			
		||||
          reset   = capture('tput reset')
 | 
			
		||||
          save    = capture('tput sc') if reset
 | 
			
		||||
          restore = capture('tput rc') if save
 | 
			
		||||
          clean   = capture('tput ed') if restore
 | 
			
		||||
          new(reset + save, restore + clean) if clean
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        # Capture output
 | 
			
		||||
        #
 | 
			
		||||
        # @param [String] command
 | 
			
		||||
        #   command to run
 | 
			
		||||
        #
 | 
			
		||||
        # @return [String]
 | 
			
		||||
        #   stdout of command on success
 | 
			
		||||
        #
 | 
			
		||||
        # @return [nil]
 | 
			
		||||
        #   otherwise
 | 
			
		||||
        #
 | 
			
		||||
        def self.capture(command)
 | 
			
		||||
          stdout, _stderr, exitstatus = Open3.capture3(command)
 | 
			
		||||
          stdout if exitstatus.success?
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        reset   = capture.('tput reset')
 | 
			
		||||
        save    = capture.('tput sc') if reset
 | 
			
		||||
        restore = capture.('tput rc') if save
 | 
			
		||||
        clean   = capture.('tput ed') if restore
 | 
			
		||||
 | 
			
		||||
        UNAVAILABLE = new(false, nil, nil)
 | 
			
		||||
 | 
			
		||||
        INSTANCE = clean ? new(true, reset + save, restore + clean) : UNAVAILABLE
 | 
			
		||||
        private_class_method :capture
 | 
			
		||||
 | 
			
		||||
      end # TPUT
 | 
			
		||||
    end # CLI
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										38
									
								
								spec/unit/mutant/reporter/cli/tput_spec.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								spec/unit/mutant/reporter/cli/tput_spec.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,38 @@
 | 
			
		|||
RSpec.describe Mutant::Reporter::CLI::Tput do
 | 
			
		||||
  describe '.detect' do
 | 
			
		||||
    subject { described_class.detect }
 | 
			
		||||
 | 
			
		||||
    def expect_command(command, stdout, success)
 | 
			
		||||
      allow(Open3).to receive(:capture3).with(command).ordered.and_return(
 | 
			
		||||
        [
 | 
			
		||||
          stdout,
 | 
			
		||||
          double('Stderr'),
 | 
			
		||||
          double('Exitstatus', success?: success)
 | 
			
		||||
        ]
 | 
			
		||||
      )
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    let(:tput_reset?) { true }
 | 
			
		||||
    let(:tput_sc?)    { true }
 | 
			
		||||
    let(:tput_rc?)    { true }
 | 
			
		||||
    let(:tput_ed?)    { true }
 | 
			
		||||
 | 
			
		||||
    before do
 | 
			
		||||
      expect_command('tput reset', '[reset]', tput_reset?)
 | 
			
		||||
      expect_command('tput sc', '[sc]', tput_sc?)
 | 
			
		||||
      expect_command('tput rc', '[rc]', tput_rc?)
 | 
			
		||||
      expect_command('tput ed', '[ed]', tput_ed?)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when all tput commands are supported' do
 | 
			
		||||
      its(:prepare) { should eql('[reset][sc]') }
 | 
			
		||||
      its(:restore) { should eql('[rc][ed]')    }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when tput reset fails' do
 | 
			
		||||
      let(:tput_reset?) { false }
 | 
			
		||||
 | 
			
		||||
      it { should be(nil) }
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -2,12 +2,20 @@ RSpec.describe Mutant::Reporter::CLI do
 | 
			
		|||
  setup_shared_context
 | 
			
		||||
 | 
			
		||||
  let(:object) { described_class.new(output, format) }
 | 
			
		||||
  let(:output) { StringIO.new }
 | 
			
		||||
  let(:output) { StringIO.new                        }
 | 
			
		||||
 | 
			
		||||
  let(:tput) do
 | 
			
		||||
    double(
 | 
			
		||||
      'tput',
 | 
			
		||||
      restore: '[tput-restore]',
 | 
			
		||||
      prepare: '[tput-prepare]'
 | 
			
		||||
    )
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  let(:framed_format) do
 | 
			
		||||
    described_class::Format::Framed.new(
 | 
			
		||||
      tty:  false,
 | 
			
		||||
      tput: described_class::Tput::UNAVAILABLE
 | 
			
		||||
      tput: tput
 | 
			
		||||
    )
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -43,7 +51,7 @@ RSpec.describe Mutant::Reporter::CLI do
 | 
			
		|||
    let(:framed_format) do
 | 
			
		||||
      described_class::Format::Framed.new(
 | 
			
		||||
        tty:  true,
 | 
			
		||||
        tput: described_class::Tput::INSTANCE
 | 
			
		||||
        tput: tput
 | 
			
		||||
      )
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -56,7 +64,19 @@ RSpec.describe Mutant::Reporter::CLI do
 | 
			
		|||
    let(:ci?)    { false                        }
 | 
			
		||||
 | 
			
		||||
    context 'when not on CI and on a tty' do
 | 
			
		||||
      it { should eql(described_class.new(output, framed_format)) }
 | 
			
		||||
      before do
 | 
			
		||||
        expect(described_class::Tput).to receive(:detect).and_return(tput)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      context 'and tput is available' do
 | 
			
		||||
        it { should eql(described_class.new(output, framed_format)) }
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      context 'and tput is not available' do
 | 
			
		||||
        let(:tput) { nil }
 | 
			
		||||
 | 
			
		||||
        it { should eql(described_class.new(output, progressive_format)) }
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when on CI' do
 | 
			
		||||
| 
						 | 
				
			
			@ -109,7 +129,7 @@ RSpec.describe Mutant::Reporter::CLI do
 | 
			
		|||
    end
 | 
			
		||||
 | 
			
		||||
    context 'on framed format' do
 | 
			
		||||
      it_reports ''
 | 
			
		||||
      it_reports '[tput-prepare]'
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -144,7 +164,7 @@ RSpec.describe Mutant::Reporter::CLI do
 | 
			
		|||
        update(:env_result) { { subject_results: [] } }
 | 
			
		||||
 | 
			
		||||
        it_reports <<-REPORT
 | 
			
		||||
          Mutant configuration:
 | 
			
		||||
          [tput-restore]Mutant configuration:
 | 
			
		||||
          Matcher:         #<Mutant::Matcher::Config match_expressions=[] subject_ignores=[] subject_selects=[]>
 | 
			
		||||
          Integration:     null
 | 
			
		||||
          Expect Coverage: 100.00%
 | 
			
		||||
| 
						 | 
				
			
			@ -169,7 +189,7 @@ RSpec.describe Mutant::Reporter::CLI do
 | 
			
		|||
          update(:status) { { active_jobs: [].to_set } }
 | 
			
		||||
 | 
			
		||||
          it_reports(<<-REPORT)
 | 
			
		||||
            Mutant configuration:
 | 
			
		||||
            [tput-restore]Mutant configuration:
 | 
			
		||||
            Matcher:         #<Mutant::Matcher::Config match_expressions=[] subject_ignores=[] subject_selects=[]>
 | 
			
		||||
            Integration:     null
 | 
			
		||||
            Expect Coverage: 100.00%
 | 
			
		||||
| 
						 | 
				
			
			@ -196,7 +216,7 @@ RSpec.describe Mutant::Reporter::CLI do
 | 
			
		|||
            update(:mutation_a_test_result) { { passed: true } }
 | 
			
		||||
 | 
			
		||||
            it_reports(<<-REPORT)
 | 
			
		||||
              Mutant configuration:
 | 
			
		||||
              [tput-restore]Mutant configuration:
 | 
			
		||||
              Matcher:         #<Mutant::Matcher::Config match_expressions=[] subject_ignores=[] subject_selects=[]>
 | 
			
		||||
              Integration:     null
 | 
			
		||||
              Expect Coverage: 100.00%
 | 
			
		||||
| 
						 | 
				
			
			@ -224,7 +244,7 @@ RSpec.describe Mutant::Reporter::CLI do
 | 
			
		|||
 | 
			
		||||
          context 'on success' do
 | 
			
		||||
            it_reports(<<-REPORT)
 | 
			
		||||
              Mutant configuration:
 | 
			
		||||
              [tput-restore]Mutant configuration:
 | 
			
		||||
              Matcher:         #<Mutant::Matcher::Config match_expressions=[] subject_ignores=[] subject_selects=[]>
 | 
			
		||||
              Integration:     null
 | 
			
		||||
              Expect Coverage: 100.00%
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue