class Shaf::Command::Test

Public Instance Methods

call() click to toggle source
# File lib/shaf/command/test.rb, line 12
def call
  disable_autorun

  bootstrap(env: 'test') do
    setup_loadpath
    reporter.start

    spec_files.each do |file|
      lines = lines_to_run(file)
      run(file, lines) if lines
    end

    reporter.report
  end
end

Private Instance Methods

disable_autorun() click to toggle source
# File lib/shaf/command/test.rb, line 34
def disable_autorun
  # This makes sure this at_exit handler is registered after minitest/autorun
  # This will make it run before the at_exit handler in minitest/autorun
  require 'minitest/autorun'
  at_exit do
    exit! reporter.passed?
  end
end
filters() click to toggle source
# File lib/shaf/command/test.rb, line 51
def filters
  @filters ||=
    if args.empty?
      [Filter::None]
    else
      args.map { |arg| Filter.new(arg) }
    end
end
lines_to_run(file) click to toggle source
# File lib/shaf/command/test.rb, line 60
def lines_to_run(file)
  lines = filters
    .select { |f| f.match? file }
    .map(&:lines)

  return if lines.empty?
  return [] if lines.any?(&:empty?)
  lines.flatten.to_set
end
methods_for(runnables) click to toggle source
# File lib/shaf/command/test.rb, line 113
def methods_for(runnables)
  methods = []

  runnables.each do |runnable|
    runnable.runnable_methods.each do |name|
      methods << RunnableMethod.from(runnable, name)
    end
  end

  methods.sort_by { |m| m.line }
end
relative_to_root(file) click to toggle source
# File lib/shaf/command/test.rb, line 125
def relative_to_root(file)
  file = File.expand_path(file)
  file.sub(File.join(Settings.app_root, ''), '')
end
reporter() click to toggle source
# File lib/shaf/command/test.rb, line 76
def reporter
  @reporter ||= Minitest::CompositeReporter.new.tap do |reporter|
    reporter << Minitest::SummaryReporter.new($stdout)
    reporter << Minitest::ProgressReporter.new($stdout)
  end
end
run(file, lines = []) click to toggle source
# File lib/shaf/command/test.rb, line 70
def run(file, lines = [])
  runners(file, lines).each do |runner|
    runner.call(reporter)
  end
end
runnables_in(file) click to toggle source
# File lib/shaf/command/test.rb, line 100
def runnables_in(file)
  require file

  @runnables ||= Set.new

  Minitest::Runnable.runnables.each_with_object([]) do |runnable, loaded|
    next unless runnable.runnable_methods.any?
    next if @runnables.include? runnable
    @runnables << runnable
    loaded << runnable
  end
end
runners(file, lines) click to toggle source
# File lib/shaf/command/test.rb, line 83
def runners(file, lines)
  runnables = runnables_in(file)

  return runnables.map { |r| Runner.new r } if lines.empty?

  methods = methods_for(runnables)

  lines.each_with_object([]) do |line, runners|
    if methods.empty? || line < methods.first.line
      runnables.map { |r| runners << Runner.new(r) }
    else
      spec = methods.partition { |m| m.line < line }.first.last or next
      runners << Runner.new(spec.runnable, spec.name)
    end
  end
end
setup_loadpath() click to toggle source
# File lib/shaf/command/test.rb, line 30
def setup_loadpath
  $LOAD_PATH.unshift(spec_dir) unless $LOAD_PATH.include?(spec_dir)
end
spec_dir() click to toggle source
# File lib/shaf/command/test.rb, line 43
def spec_dir
  @spec_dir ||= Settings.spec_dir || 'spec'
end
spec_files() click to toggle source
# File lib/shaf/command/test.rb, line 47
def spec_files
  Dir["#{spec_dir}/**/*.rb"]
end