Class: Recheck::Command::Run

Inherits:
Object
  • Object
show all
Defined in:
lib/recheck/commands.rb

Overview

Reporters

Instance Method Summary collapse

Constructor Details

#initialize(argv: []) ⇒ Run

Returns a new instance of Run.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/recheck/commands.rb', line 28

def initialize(argv: [])
  @options = {
    reporters: []
  }
  @file_patterns = []

  @argv = argv
  @options = Optimist.options(@argv) do
    banner "recheck run: run the suite"
    opt :reporter, "<reporter>[:ARGS], can use multiple times", short: :r, multi: true, default: ["Recheck::Reporter::Default"]
  end
  @files_created = []

  @file_patterns = @options[:_leftovers]
end

Instance Method Details

#check_missing_filesObject



95
96
97
98
99
100
101
102
# File 'lib/recheck/commands.rb', line 95

def check_missing_files
  missing_files = @file_patterns.reject { |pattern| Dir.glob(pattern).any? }
  unless missing_files.empty?
    puts "Error: The following files do not exist:"
    missing_files.each { |file| puts file }
    exit Cli::EXIT_CODE[:load_error]
  end
end

#load_checkersObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/recheck/commands.rb', line 60

def load_checkers
  files = if @file_patterns.empty?
    Dir.glob("recheck/**/*.rb").sort
  else
    check_missing_files
    @file_patterns.flat_map do |pattern|
      if File.directory?(pattern)
        Dir.glob(File.join(pattern, "**/*.rb"))
      else
        Dir.glob(pattern)
      end
    end
  end

  files.each do |file|
    # bug: if the file has a syntax error, Ruby silently exits instead of raising SyntaxError
    require File.expand_path(file)
  rescue LoadError => e
    puts "Loading #{file} threw an exception: #{e.class}: #{e.message}, #{e.backtrace.first}"
    unless file.start_with?("recheck/")
      puts "that filename doesn't start with \"recheck/\", did you give the name of a model instead of its checker?"
    end
    exit Cli::EXIT_CODE[:load_error]
  end

  if Recheck::Checker::Base.checker_classes.empty?
    error = "No checks detected." +
      (@file_patterns.empty? ? " Did you run `bundle exec recheck setup`?" : "")
    warn error
    exit Cli::EXIT_CODE[:load_error]
  end

  Recheck::Checker::Base.checker_classes.map(&:new)
end

#load_reporters(reporters) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/recheck/commands.rb', line 104

def load_reporters(reporters)
  reporters.map { |option|
    class_name, arg = option.split(/(?<!:):(?!:)/, 2)
    resolve_reporter_class(class_name).new(arg:)
  }
rescue ArgumentError => e
  puts "Bad argument to Reporter (#{e.backtrace.first}): #{e.message}"
  exit Cli::EXIT_CODE[:load_error]
rescue LoadError => e
  puts "Loading #{file} threw an exception: #{e.class}: #{e.message}, #{e.backtrace.first}"
  exit Cli::EXIT_CODE[:load_error]
end

#resolve_reporter_class(reporter_name) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/recheck/commands.rb', line 117

def resolve_reporter_class(reporter_name)
  [Object, Recheck::Reporter].each do |namespace|
    return namespace.const_get(reporter_name)
  rescue NameError
    next
  end
  puts "Error: Reporter class '#{reporter_name}' not found globally or in Recheck::Reporter."
  exit Cli::EXIT_CODE[:load_error]
end

#runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/recheck/commands.rb', line 44

def run
  checkers = load_checkers
  reporters = load_reporters(@options[:reporter])

  total_counts = Runner.new(checkers:, reporters:).run
rescue Interrupt
  puts "\nOperation interrupted by user."
rescue => e
  puts "\nAn error occurred in Recheck:"
  puts e.full_message(highlight: false)
  exit Cli::EXIT_CODE[:recheck_error]
  # ensure
  #  puts "ensure"
  #  exit Cli::EXIT_CODE[total_counts&.all_pass? ? :no_errors : :any_errors]
end