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.



89
90
91
92
93
# File 'lib/polyrun/quick/runner.rb', line 89

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

Class Method Details

.parallel_default_paths(cwd = Dir.pwd) ⇒ Object

Files Polyrun::Quick would run with no explicit paths (excludes normal RSpec/Minitest files).



66
67
68
69
70
71
72
73
74
75
# File 'lib/polyrun/quick/runner.rb', line 66

def self.parallel_default_paths(cwd = Dir.pwd)
  base = File.expand_path(cwd)
  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| quick_path_excluded?(p, base) }.sort
end

.quick_path_excluded?(path, base) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/polyrun/quick/runner.rb', line 77

def self.quick_path_excluded?(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

.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



179
180
181
# File 'lib/polyrun/quick/runner.rb', line 179

def default_globs
  Runner.parallel_default_paths(Dir.pwd)
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)


184
185
186
# File 'lib/polyrun/quick/runner.rb', line 184

def default_quick_exclude?(path, base)
  Runner.quick_path_excluded?(path, base)
end

#expand_paths(paths) ⇒ Object



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

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/polyrun/quick/runner.rb', line 117

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



151
152
153
154
155
156
157
158
159
160
# File 'lib/polyrun/quick/runner.rb', line 151

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



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

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



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

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