Class: Recheck::Command::Run
- Inherits:
-
Object
- Object
- Recheck::Command::Run
- Defined in:
- lib/recheck/commands.rb
Overview
Reporters
Instance Method Summary collapse
- #check_missing_files ⇒ Object
-
#initialize(argv: []) ⇒ Run
constructor
A new instance of Run.
- #load_checkers ⇒ Object
- #load_reporters(reporters) ⇒ Object
- #resolve_reporter_class(reporter_name) ⇒ Object
- #run ⇒ Object
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.(@argv) do "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_files ⇒ Object
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_checkers ⇒ Object
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.(file) rescue LoadError => e puts "Loading #{file} threw an exception: #{e.class}: #{e.}, #{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.}" exit Cli::EXIT_CODE[:load_error] rescue LoadError => e puts "Loading #{file} threw an exception: #{e.class}: #{e.}, #{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 |
#run ⇒ Object
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.(highlight: false) exit Cli::EXIT_CODE[:recheck_error] # ensure # puts "ensure" # exit Cli::EXIT_CODE[total_counts&.all_pass? ? :no_errors : :any_errors] end |