Class: Diogenes::Evaluation::CiRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/diogenes/evaluation/ci_runner.rb,
sig/generated/diogenes/evaluation/ci_runner.rbs

Instance Method Summary collapse

Constructor Details

#initialize(description:, answers:, out:, err:, format: nil) ⇒ CiRunner

: (description: String, answers: String, out: IO, err: IO, ?format: String?) -> void

Parameters:

  • description: (String)
  • answers: (String)
  • out: (IO)
  • err: (IO)
  • format: (String, nil) (defaults to: nil)


10
11
12
13
14
15
16
# File 'lib/diogenes/evaluation/ci_runner.rb', line 10

def initialize(description:, answers:, out:, err:, format: nil)
  @description = description
  @parsed_answers = parse_answers(answers) #: Hash[Symbol, bool]
  @format = format
  @out = out
  @err = err
end

Instance Method Details

#build_resultsArray[untyped]

: () -> Array

Returns:

  • (Array[untyped])


43
44
45
# File 'lib/diogenes/evaluation/ci_runner.rb', line 43

def build_results
  Gates::ALL.map { |gate| Session::Result.new(gate:, passed: @parsed_answers[gate.key]) }
end

#output_json(results) ⇒ void

This method returns an undefined value.

: (Array) -> void

Parameters:

  • (Array[untyped])


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/diogenes/evaluation/ci_runner.rb', line 61

def output_json(results)
  all_passed = results.all?(&:passed?)
  @out.puts JSON.generate(
    feature: @description,
    verdict: all_passed ? "PROCEED" : "REJECT",
    all_passed: all_passed,
    gates: results.map.with_index(1) do |r, i|
      {number: i, key: r.gate.key, name: r.gate.name, passed: r.passed?}
    end
  )
end

#output_text(results) ⇒ void

This method returns an undefined value.

: (Array) -> void

Parameters:

  • (Array[untyped])


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/diogenes/evaluation/ci_runner.rb', line 48

def output_text(results)
  verdict = results.all?(&:passed?) ? "PROCEED" : "REJECT"
  @out.puts
  @out.puts "Evaluating: \"#{@description}\" (CI mode)"
  @out.puts
  results.each_with_index do |r, i|
    @out.puts "  Gate #{i + 1}#{r.gate.name.ljust(22)} #{r.passed? ? "PASS" : "FAIL"}"
  end
  @out.puts
  @out.puts "Verdict: #{verdict}"
end

#parse_answers(answers_str) ⇒ Hash[Symbol, bool]

: (String) -> Hash[Symbol, bool]

Parameters:

  • (String)

Returns:

  • (Hash[Symbol, bool])


35
36
37
38
39
40
# File 'lib/diogenes/evaluation/ci_runner.rb', line 35

def parse_answers(answers_str)
  answers_str.split(",").each_with_object({}) do |pair, hash|
    key, value = pair.split("=", 2)
    hash[key.strip.to_sym] = value&.strip == "pass"
  end
end

#runInteger

: () -> Integer

Returns:

  • (Integer)


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/diogenes/evaluation/ci_runner.rb', line 19

def run
  missing = Gates::ALL.map(&:key) - @parsed_answers.keys
  unless missing.empty?
    @err.puts "Missing answer#{"s" if missing.size > 1} for: #{missing.join(", ")}. " \
              "Provide all 5 gate answers: --answers failure_mode=pass,user_verifiable=pass,..."
    return 1
  end

  results = build_results
  (@format == "json") ? output_json(results) : output_text(results)
  results.all?(&:passed?) ? 0 : 1
end