Class: Polyrun::Quick::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/polyrun/quick/runner.rb

Overview

rubocop:enable ThreadSafety/ClassAndModuleAttributes, ThreadSafety/ClassInstanceVariable

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out: $stdout, err: $stderr, verbose: false) ⇒ Runner

Returns a new instance of Runner.



65
66
67
68
69
# File 'lib/polyrun/quick/runner.rb', line 65

def initialize(out: $stdout, err: $stderr, verbose: false)
  @out = out
  @err = err
  @verbose = verbose
end

Class Method Details

.run(paths:, out: $stdout, err: $stderr, verbose: false) ⇒ Object



61
62
63
# File 'lib/polyrun/quick/runner.rb', line 61

def self.run(paths:, out: $stdout, err: $stderr, verbose: false)
  new(out: out, err: err, verbose: verbose).run(paths)
end

Instance Method Details

#default_globsObject



155
156
157
158
159
160
161
162
163
164
# File 'lib/polyrun/quick/runner.rb', line 155

def default_globs
  base = File.expand_path(Dir.pwd)
  globs = [
    File.join(base, "spec", "polyrun_quick", "**", "*.rb"),
    File.join(base, "test", "polyrun_quick", "**", "*.rb"),
    File.join(base, "spec", "**", "*.rb"),
    File.join(base, "test", "**", "*.rb")
  ]
  globs.flat_map { |g| Dir.glob(g) }.uniq.reject { |p| default_quick_exclude?(p, base) }.sort
end

#default_quick_exclude?(path, base) ⇒ Boolean

Omit RSpec/Minitest files and common helpers so polyrun quick with no args does not load normal suites.

Returns:

  • (Boolean)


167
168
169
170
171
172
173
174
175
176
177
# File 'lib/polyrun/quick/runner.rb', line 167

def default_quick_exclude?(path, base)
  rel = Pathname.new(path).relative_path_from(Pathname.new(base)).to_s
  parts = rel.split(File::SEPARATOR)
  bn = File.basename(path)
  return true if bn.end_with?("_spec.rb", "_test.rb")
  return true if %w[spec_helper.rb rails_helper.rb test_helper.rb].include?(bn)
  return true if parts[0] == "spec" && %w[support fixtures factories].include?(parts[1])
  return true if parts[0] == "test" && %w[support fixtures].include?(parts[1])

  false
end

#expand_paths(paths) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/polyrun/quick/runner.rb', line 138

def expand_paths(paths)
  return default_globs if paths.nil? || paths.empty?

  paths.flat_map do |p|
    expanded = File.expand_path(p)
    if File.directory?(expanded)
      Dir.glob(File.join(expanded, "**", "*.rb")).sort
    elsif /[*?\[]/.match?(p)
      Dir.glob(File.expand_path(p)).sort
    elsif File.file?(expanded)
      [expanded]
    else
      []
    end
  end.uniq
end

#load_quick_files!(files) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/polyrun/quick/runner.rb', line 93

def load_quick_files!(files)
  collector = Collector.new
  Quick.collector = collector

  files.each do |path|
    code = File.read(path)
    loader = Object.new
    loader.extend(DSL)
    loader.instance_eval(code, path, 1)
  rescue SyntaxError, StandardError => e
    Polyrun::Log.warn "polyrun quick: failed to load #{path}: #{e.class}: #{e.message}"
    Quick.collector = nil
    return nil
  end

  Quick.collector = nil
  collector
end

#quick_start_coverage_if_configured!Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/polyrun/quick/runner.rb', line 127

def quick_start_coverage_if_configured!
  return unless Polyrun::Coverage::Collector.coverage_requested_for_quick?(Dir.pwd)
  return if Polyrun::Coverage::Collector.started?

  require_relative "../coverage/rails"
  Polyrun::Coverage::Rails.start!(
    root: File.expand_path(Dir.pwd),
    meta: {"command_name" => "polyrun quick"}
  )
end

#run(paths) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/polyrun/quick/runner.rb', line 71

def run(paths)
  Quick.reset_capybara_flag!

  files = expand_paths(paths)
  if files.empty?
    Polyrun::Log.warn "polyrun quick: no files (pass paths or add Quick files under spec/ or test/, e.g. spec/polyrun_quick/**/*.rb or spec/**/*.rb excluding *_spec.rb / *_test.rb)"
    return 2
  end

  quick_start_coverage_if_configured!

  collector = load_quick_files!(files)
  return 1 unless collector

  reporter = Reporter.new(@out, @err, @verbose)
  run_examples!(collector, reporter)
  reporter.summary
ensure
  Quick.collector = nil
  Quick.reset_capybara_flag!
end

#run_examples!(collector, reporter) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/polyrun/quick/runner.rb', line 112

def run_examples!(collector, reporter)
  collector.groups.each do |root|
    root.each_example_with_ancestors do |chain, desc, block|
      inner = chain.last
      example_runner = ExampleRunner.new(reporter)
      example_runner.run(
        group_name: inner.full_name,
        description: desc,
        ancestor_chain: chain,
        block: block
      )
    end
  end
end