From 4de519aa623c203b24cfc16bc46bc6a8c07a70ca Mon Sep 17 00:00:00 2001 From: James Couball Date: Sun, 6 Jul 2025 12:16:58 -0700 Subject: [PATCH] fix: fix Rubocop Metrics/AbcSize offense Most of this was fixed by excluding tests from this metric. This is fine since I plan on moving tests to RSpec in the near future. The other few instances were fixed by refactoring the methods. --- .rubocop.yml | 9 +++++-- .rubocop_todo.yml | 5 ---- lib/git/command_line.rb | 55 ++++++++++++++++++++++------------------- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 3cb0f1f5..f12a62d1 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,13 +3,18 @@ inherit_from: .rubocop_todo.yml inherit_gem: main_branch_shared_rubocop_config: config/rubocop.yml -# Don't care about complexity in TestUnit tests -# This should go away when we switch to RSpec +# Don't care about CyclomaticComplexity or AbcSize in TestUnit tests This should go +# away when we switch to RSpec. Metrics/CyclomaticComplexity: Exclude: - "tests/test_helper.rb" - "tests/units/**/*" +Metrics/AbcSize: + Exclude: + - "tests/test_helper.rb" + - "tests/units/**/*" + # Don't care so much about length of methods in tests Metrics/MethodLength: Exclude: diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 3ea6f65d..d85c7f54 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,11 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 50 -# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. -Metrics/AbcSize: - Max: 109 - # Offense count: 21 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: diff --git a/lib/git/command_line.rb b/lib/git/command_line.rb index befa43fe..cf1ef78f 100644 --- a/lib/git/command_line.rb +++ b/lib/git/command_line.rb @@ -278,50 +278,53 @@ def build_git_cmd(args) # def process_result(result, normalize, chomp, timeout) command = result.command - processed_out, processed_err = post_process_all([result.stdout, result.stderr], normalize, chomp) + processed_out, processed_err = post_process_output(result, normalize, chomp) + log_result(result, command, processed_out, processed_err) + command_line_result(command, result, processed_out, processed_err, timeout) + end + + def log_result(result, command, processed_out, processed_err) logger.info { "#{command} exited with status #{result}" } logger.debug { "stdout:\n#{processed_out.inspect}\nstderr:\n#{processed_err.inspect}" } + end + + def command_line_result(command, result, processed_out, processed_err, timeout) Git::CommandLineResult.new(command, result, processed_out, processed_err).tap do |processed_result| raise Git::TimeoutError.new(processed_result, timeout) if result.timeout? + raise Git::SignaledError, processed_result if result.signaled? + raise Git::FailedError, processed_result unless result.success? end end - # Post-process command output and return an array of the results - # - # @param raw_outputs [Array] the output to post-process - # @param normalize [Boolean] whether to normalize the output of each writer - # @param chomp [Boolean] whether to chomp the output of each writer + # Post-process and return an array of raw output strings # - # @return [Array] the processed output of each command output object that supports `#string` + # For each raw output string: # - # @api private + # * If normalize: is true, normalize the encoding by transcoding each line from + # the detected encoding to UTF-8. + # * If chomp: is true chomp the output after normalization. # - def post_process_all(raw_outputs, normalize, chomp) - raw_outputs.map { |raw_output| post_process(raw_output, normalize, chomp) } - end - - # Determine the output to return in the `CommandLineResult` + # Even if no post-processing is done based on the options, the strings returned + # are a copy of the raw output strings. The raw output strings are not modified. # - # If the writer can return the output by calling `#string` (such as a StringIO), - # then return the result of normalizing the encoding and chomping the output - # as requested. + # @param result [ProcessExecuter::ResultWithCapture] the command's output to post-process # - # If the writer does not support `#string`, then return nil. The output is - # assumed to be collected by the writer itself such as when the writer - # is a file instead of a StringIO. + # @param normalize [Boolean] whether to normalize the output of each writer + # @param chomp [Boolean] whether to chomp the output of each writer # - # @param raw_output [#string] the output to post-process - # @return [String, nil] + # @return [Array] # # @api private # - def post_process(raw_output, normalize, chomp) - output = raw_output.dup - output = output.lines.map { |l| Git::EncodingUtils.normalize_encoding(l) }.join if normalize - output.chomp! if chomp - output + def post_process_output(result, normalize, chomp) + [result.stdout, result.stderr].map do |raw_output| + output = raw_output.dup + output = output.lines.map { |l| Git::EncodingUtils.normalize_encoding(l) }.join if normalize + output.chomp! if chomp + output + end end end end