lib/quality/quality_checker.rb in quality-27.3.1 vs lib/quality/quality_checker.rb in quality-27.4.0

- old
+ new

@@ -7,19 +7,23 @@ # Runs a quality-checking, command, checks it agaist the existing # number of violations for that command, and decreases that number # if possible, or outputs data if the number of violations increased. class QualityChecker def initialize(cmd, command_options, output_dir, verbose, + minimum_threshold, + logger: STDOUT, count_file: File, count_io: IO, command_output_processor_class: Quality::CommandOutputProcessor, count_dir: Dir, process_class: Process) + @minimum_threshold = minimum_threshold @count_file = count_file @count_io = count_io @command_output_processor_class = command_output_processor_class + @logger = logger @count_dir = count_dir @cmd = cmd @command_options = command_options @verbose = verbose @count_dir.mkdir(output_dir) unless @count_file.exists?(output_dir) @@ -58,33 +62,54 @@ @command_options[:gives_error_code_on_no_relevant_code] raise("Error detected running #{full_cmd}. " \ "Exit status is #{exit_status}") if exit_status.nonzero? end + MAX_VIOLATIONS = 9_999_999_999 + def existing_violations - if @count_file.exist?(@filename) - @count_io.read(@filename).to_i - else - 9_999_999_999 + @existing_violations ||= + begin + if @count_file.exist?(@filename) + @count_io.read(@filename).to_i + else + MAX_VIOLATIONS + end + end + end + + def error_too_many_violations! + raise("Output from #{@cmd}\n\n#{@command_output}\n\n" \ + "Reduce total number of #{@cmd} violations " \ + "to #{existing_violations} or below!") + end + + def violations_to_write + @violations_to_write ||= [@violations, @minimum_threshold].max + end + + def report_ratchet + if @violations < existing_violations && + existing_violations != MAX_VIOLATIONS + @logger.puts 'Ratcheting quality up...' end end def ratchet_violations - existing = existing_violations - report_violations(existing) - if @violations > existing - raise("Output from #{@cmd}\n\n#{@command_output}\n\n" \ - "Reduce total number of #{@cmd} violations " \ - "to #{existing} or below!") - elsif @violations < existing - puts 'Ratcheting quality up...' - write_violations(@violations) + report_violations(existing_violations) + if @violations > [existing_violations, @minimum_threshold].max + error_too_many_violations! + elsif violations_to_write != existing_violations + report_ratchet + write_violations(violations_to_write) end end def report_violations(existing) - puts "Existing violations: #{existing}" - puts "Found #{@violations} #{@cmd} violations" + if existing != MAX_VIOLATIONS + @logger.puts "Existing violations: #{existing}" + end + @logger.puts "Found #{@violations} #{@cmd} violations" end def full_cmd args = @command_options[:args] || '' @found_output = false