Class: RspecSprint::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_sprint/collector.rb

Overview

Runs the host project’s RSpec suite ONCE with FactoryProf JSON enabled and RSpec’s JSON formatter writing to a dedicated file (so the host’s own formatter on stdout is not clobbered — design D10③). Returns a Result that downstream code (Diagnosis) and degraded-path handling read.

The pure pieces (build_command, factory_prof_path_from) are split out so most behavior is unit-testable without spawning a real rspec process.

Defined Under Namespace

Classes: Result

Constant Summary collapse

DEFAULT_OUT =
"tmp/rspec_sprint/rspec.json"
FPROF_LOG =
/Profile results to JSON:\s*(\S+)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rspec_args: [], out_path: DEFAULT_OUT, dir: ".") ⇒ Collector

Returns a new instance of Collector.



51
52
53
54
55
# File 'lib/rspec_sprint/collector.rb', line 51

def initialize(rspec_args: [], out_path: DEFAULT_OUT, dir: ".")
  @rspec_args = rspec_args
  @out_path = out_path
  @dir = dir
end

Class Method Details

.build_command(out_path:, rspec_args: []) ⇒ Object

Pure: the env + argv used to run rspec. FPROF=json makes test-prof write its FactoryProf JSON; –format json –out writes rspec’s JSON to its own file without replacing the host’s stdout formatter.



38
39
40
41
42
# File 'lib/rspec_sprint/collector.rb', line 38

def self.build_command(out_path:, rspec_args: [])
  env = { "FPROF" => "json" }
  argv = ["bundle", "exec", "rspec", "--format", "json", "--out", out_path, *rspec_args]
  [env, argv]
end

.factory_prof_path_from(stdout) ⇒ Object

Pure: pull the FactoryProf output path test-prof logs to stdout. nil means test-prof never ran (not required in the host’s boot) — Rule① will be skipped.



46
47
48
49
# File 'lib/rspec_sprint/collector.rb', line 46

def self.factory_prof_path_from(stdout)
  match = stdout.to_s.match(FPROF_LOG)
  match && match[1]
end

Instance Method Details

#runObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rspec_sprint/collector.rb', line 57

def run
  FileUtils.mkdir_p(File.dirname(File.join(@dir, @out_path)))
  env, argv = self.class.build_command(out_path: @out_path, rspec_args: @rspec_args)
  stdout, stderr, status = Open3.capture3(env, *argv, chdir: @dir)
  Result.new(
    rspec_json_path: File.join(@dir, @out_path),
    factory_prof_path: resolve_factory_prof_path(stdout),
    exit_status: status.exitstatus,
    stdout: stdout,
    stderr: stderr
  )
end