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.



17
18
19
# File 'lib/rail_verdict/analyzers/rspec.rb', line 17

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



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

def probe(repository_root, runner: ProcessRunner, timeout_seconds: 15.0)
  command = @command_resolver.call(repository_root)
  invocation = Shared.invocation_for(command, ["--version"])
  result = runner.run(
    command.fetch(:executable),
    invocation.fetch("argv"),
    chdir: repository_root,
    timeout_seconds: timeout_seconds
  )

  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) ⇒ Object



48
49
50
51
52
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
# File 'lib/rail_verdict/analyzers/rspec.rb', line 48

def run(repository_root, runner: ProcessRunner, timeout_seconds: 30.0, probe_result: nil)
  command = @command_resolver.call(repository_root)
  probe_result ||= probe(repository_root, runner: runner, timeout_seconds: timeout_seconds)
  version_invocation = Shared.invocation_for(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

  invocation = Shared.invocation_for(command, ["--format", "json"])
  result = runner.run(
    command.fetch(:executable),
    invocation.fetch("argv"),
    chdir: repository_root,
    timeout_seconds: timeout_seconds
  )
  tool_version = probe_result.version

  return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: 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: 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: 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: invocation, status: "signaled", message: Shared.detail_for(result), tool_version: tool_version), []] if result.status == :signaled

  begin
    document = JSON.parse(result.stdout)
  rescue JSON::ParserError => error
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: 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: invocation, status: "malformed", message: Shared.bounded_message(error.message), tool_version: tool_version), []]
  end

  analyzer_result = AnalyzerResult.new(
    analyzer: ANALYZER_ID,
    tool_version: tool_version,
    invocation: invocation,
    execution_status: "succeeded",
    finding_ids: findings.map(&:id),
    evidence_summary: summary
  )
  [analyzer_result, findings]
end