class RubyCritic::Command::Compare

Attributes

paths[R]
status_reporter[R]

Public Class Methods

new(options) click to toggle source
Calls superclass method
# File lib/rubycritic/commands/compare.rb, line 14
def initialize(options)
  super
  @original_config_root = Config.root
  @build_number = 0
end

Public Instance Methods

execute() click to toggle source
# File lib/rubycritic/commands/compare.rb, line 20
def execute
  if Config.send(:base_branch) == Config.send(:feature_branch)
    raise('The branch you are on and are comparing with are the same.
    Please switch to a different branch or choose a different branch to compare.')
  end
  compare_branches
  status_reporter.score = Config.feature_branch_score
  status_reporter
end

Private Instance Methods

analyse_branch(branch) click to toggle source

switch branch and analyse files

# File lib/rubycritic/commands/compare.rb, line 53
def analyse_branch(branch)
  SourceControlSystem::Git.switch_branch(Config.send(branch))
  critic = critique(branch)
  Config.send(:"#{branch}_score=", critic.score)
  Config.root = branch_directory(branch)
  report(critic)
end
analyse_modified_files() click to toggle source

generate report only for modified files

# File lib/rubycritic/commands/compare.rb, line 62
def analyse_modified_files
  modified_files = Config.feature_branch_collection.where(SourceControlSystem::Git.modified_files)
  analysed_modules = AnalysedModulesCollection.new(modified_files.map(&:path), modified_files)
  Config.root = Config.compare_root_directory
  report(analysed_modules)
end
branch_directory(branch) click to toggle source
# File lib/rubycritic/commands/compare.rb, line 92
def branch_directory(branch)
  "#{@original_config_root}/compare/#{Config.send(branch)}"
end
build_details() click to toggle source

create a txt file with the branch score details

# File lib/rubycritic/commands/compare.rb, line 97
def build_details
  details = "Base branch (#{Config.base_branch}) score: #{Config.base_branch_score} \n" \
            "Feature branch (#{Config.feature_branch}) score: #{Config.feature_branch_score} \n"
  File.write("#{Config.compare_root_directory}/build_details.txt", details)
end
compare_branches() click to toggle source
# File lib/rubycritic/commands/compare.rb, line 34
def compare_branches
  @build_number = Utils::BuildNumberFile.new.update_build_number
  set_root_paths
  original_no_browser_config = Config.no_browser
  Config.no_browser = true
  analyse_branch(:base_branch)
  analyse_branch(:feature_branch)
  Config.no_browser = original_no_browser_config
  analyse_modified_files
  compare_code_quality
end
compare_code_quality() click to toggle source
# File lib/rubycritic/commands/compare.rb, line 69
def compare_code_quality
  build_details
  compare_threshold
end
compare_threshold() click to toggle source

mark build as failed if the diff b/w the score of two branches is greater than threshold value

# File lib/rubycritic/commands/compare.rb, line 76
def compare_threshold
  return unless mark_build_fail?

  print("Threshold: #{Config.threshold_score}\n")
  print("Difference: #{(Config.base_branch_score - Config.feature_branch_score).abs}\n")
  abort('The score difference between the two branches is over the threshold.')
end
critique(branch) click to toggle source

store the analysed moduled collection based on the branch

# File lib/rubycritic/commands/compare.rb, line 104
def critique(branch)
  module_collection = AnalysersRunner.new(paths).run
  Config.send(:"#{branch}_collection=", module_collection)
  RevisionComparator.new(paths).statuses = module_collection
end
mark_build_fail?() click to toggle source
# File lib/rubycritic/commands/compare.rb, line 84
def mark_build_fail?
  Config.threshold_score >= 0 && threshold_reached?
end
set_root_paths() click to toggle source
# File lib/rubycritic/commands/compare.rb, line 46
def set_root_paths
  Config.base_root_directory = Pathname.new(branch_directory(:base_branch))
  Config.feature_root_directory = Pathname.new(branch_directory(:feature_branch))
  Config.compare_root_directory = Pathname.new("#{Config.root}/compare")
end
threshold_reached?() click to toggle source
# File lib/rubycritic/commands/compare.rb, line 88
def threshold_reached?
  (Config.base_branch_score - Config.feature_branch_score) > Config.threshold_score
end