Class: RailVerdict::Analyzers::RSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/rail_verdict/analyzers/rspec.rb

Defined Under Namespace

Classes: MalformedOutput, Probe

Constant Summary collapse

ANALYZER_ID =
"rspec"
SUPPORTED_VERSIONS =
Gem::Requirement.new(">= 3.13", "< 4")

Instance Method Summary collapse

Constructor Details

#initialize(command_resolver: nil) ⇒ RSpec

Returns a new instance of RSpec.



19
20
21
# File 'lib/rail_verdict/analyzers/rspec.rb', line 19

def initialize(command_resolver: nil)
  @command_resolver = command_resolver || method(:default_command)
end

Instance Method Details

#probe(repository_root, runner: ProcessRunner, timeout_seconds: 15.0) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rail_verdict/analyzers/rspec.rb', line 23

def probe(repository_root, runner: ProcessRunner, timeout_seconds: 15.0)
  effective_timeout = [timeout_seconds.to_f, 5.0].min
  command = @command_resolver.call(repository_root)
  clean_prefix = clean_args_prefix(command.fetch(:args_prefix))
  clean_command = command.merge(args_prefix: clean_prefix)
  invocation = Shared.invocation_for(clean_command, ["--version"])
  result = runner.run(
    clean_command.fetch(:executable),
    invocation.fetch("argv"),
    chdir: repository_root,
    timeout_seconds: effective_timeout
  )

  return Probe.new(status: "unavailable", message: Shared.detail_for(result)) if result.status == :spawn_failed
  return Probe.new(status: "timed_out", message: Shared.detail_for(result)) if result.status == :timed_out
  return Probe.new(status: "signaled", message: Shared.detail_for(result)) if result.status == :signaled
  return Probe.new(status: "truncated", message: Shared.detail_for(result)) if Shared.truncated?(result)
  return Probe.new(status: "unavailable", message: Shared.detail_for(result)) unless result.exit_code == 0

  version = Shared.parse_semver(result.stdout)
  return Probe.new(status: "unsupported", message: "RSpec version could not be parsed") unless version
  return Probe.new(status: "unsupported", version: version, message: "unsupported RSpec version #{version}") unless SUPPORTED_VERSIONS.satisfied_by?(Gem::Version.new(version))

  Probe.new(status: "succeeded", version: version)
rescue ArgumentError
  Probe.new(status: "unsupported", message: "RSpec reported an invalid version")
rescue KeyError, ArgumentError => error
  Probe.new(status: "malformed", message: Shared.bounded_message(error.message))
end

#run(repository_root, runner: ProcessRunner, timeout_seconds: 30.0, probe_result: nil, configuration: nil) ⇒ Object



53
54
55
56
57
58
59
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rail_verdict/analyzers/rspec.rb', line 53

def run(repository_root, runner: ProcessRunner, timeout_seconds: 30.0, probe_result: nil, configuration: nil)
  command = @command_resolver.call(repository_root)
  clean_prefix = clean_args_prefix(command.fetch(:args_prefix))
  clean_command = command.merge(args_prefix: clean_prefix)
  probe_result ||= probe(repository_root, runner: runner, timeout_seconds: timeout_seconds)
  version_invocation = Shared.invocation_for(clean_command, ["--version"])

  unless probe_result.status == "succeeded"
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: version_invocation, status: probe_result.status, message: probe_result.message, tool_version: probe_result.version), []]
  end

  output_path = File.join(Dir.tmpdir, "railverdict-rspec-#{SecureRandom.hex(8)}.json")
  public_invocation = Shared.invocation_for(clean_command, ["--format", "json"])
  run_argv = clean_prefix.dup.concat(["--format", "json", "--out", output_path])

  max_stdout = resolve_stdout_limit(configuration, repository_root, 16 * 1024 * 1024)
  tool_version = probe_result.version

  begin
    result = runner.run(
      clean_command.fetch(:executable),
      run_argv,
      chdir: repository_root,
      timeout_seconds: timeout_seconds,
      max_stdout_bytes: max_stdout
    )

    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: public_invocation, status: "unavailable", message: Shared.detail_for(result), tool_version: tool_version), []] if result.status == :spawn_failed
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: public_invocation, status: "truncated", message: Shared.detail_for(result), tool_version: tool_version), []] if Shared.truncated?(result)
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: public_invocation, status: "timed_out", message: Shared.detail_for(result), tool_version: tool_version), []] if result.status == :timed_out
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: public_invocation, status: "signaled", message: Shared.detail_for(result), tool_version: tool_version), []] if result.status == :signaled

    unless File.file?(output_path)
      detail = Shared.detail_for(result)
      msg = detail.strip.empty? ? "RSpec did not produce structured output" : detail
      status = result.exit_code && result.exit_code != 0 ? "failed" : "malformed"
      return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: public_invocation, status: status, message: msg, tool_version: tool_version), []]
    end

    begin
      bytes = File.binread(output_path)
    rescue SystemCallError => error
      return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: public_invocation, status: "malformed", message: Shared.bounded_message(error.message), tool_version: tool_version), []]
    end

    if bytes.bytesize > max_stdout
      return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: public_invocation, status: "truncated", message: "RSpec output exceeds #{max_stdout} bytes", tool_version: tool_version), []]
    end

    text = bytes.dup.force_encoding(Encoding::UTF_8)
    unless text.valid_encoding?
      return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: public_invocation, status: "parse_failed", message: "RSpec output is not valid UTF-8", tool_version: tool_version), []]
    end

    begin
      document = JSON.parse(text)
    rescue JSON::ParserError => error
      return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: public_invocation, status: "parse_failed", message: Shared.bounded_message(error.message), tool_version: tool_version), []]
    end

    begin
      summary, findings = normalize_document(document)
    rescue MalformedOutput => error
      return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: public_invocation, status: "malformed", message: Shared.bounded_message(error.message), tool_version: tool_version), []]
    end

    # Process exit reconciliation (RH-02):
    # 0: all examples passed, 0 failures/errors
    # 1: failed examples present (failures > 0 or findings non-empty)
    # Any other exit code or contradiction: fail closed
    failures_count = summary["failures"] || 0
    if result.exit_code == 0
      if failures_count > 0 || !findings.empty?
        return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: public_invocation, status: "malformed", message: "RSpec exited with status 0 but reported #{failures_count} failures", tool_version: tool_version), []]
      end
    elsif result.exit_code == 1
      if failures_count == 0 && findings.empty?
        detail = Shared.detail_for(result)
        msg = detail.strip.empty? ? "RSpec exited with status 1 but reported 0 failed examples" : detail
        return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: public_invocation, status: "failed", message: msg, tool_version: tool_version), []]
      end
    else
      detail = Shared.detail_for(result)
      msg = detail.strip.empty? ? "RSpec exited with unexpected status #{result.exit_code}" : detail
      return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: public_invocation, status: "failed", message: msg, tool_version: tool_version), []]
    end

    analyzer_result = AnalyzerResult.new(
      analyzer: ANALYZER_ID,
      tool_version: tool_version,
      invocation: public_invocation,
      execution_status: "succeeded",
      finding_ids: findings.map(&:id),
      evidence_summary: summary
    )
    [analyzer_result, findings]
  ensure
    begin
      File.unlink(output_path) if output_path && File.exist?(output_path)
    rescue StandardError
      nil
    end
  end
end