Class: Judges::Test

Inherits:
Object show all
Defined in:
lib/judges/commands/test.rb

Overview

The test command.

This class is instantiated by the bin/judge command line interface. You are not supposed to instantiate it yourself.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(loog) ⇒ Test

Initialize.

Parameters:

  • loog (Loog)

    Logging facility



28
29
30
# File 'lib/judges/commands/test.rb', line 28

def initialize(loog)
  @loog = loog
end

Instance Method Details

#run(opts, args) ⇒ Object

Run the test command (called by the bin/judges script).

Parameters:

  • opts (Hash)

    Command line options (start with ‘–’)

  • args (Array)

    List of command line arguments

Raises:

  • (RuntimeError)

    If not exactly one argument provided



36
37
38
39
40
41
42
43
44
45
46
47
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
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
# File 'lib/judges/commands/test.rb', line 36

def run(opts, args)
  raise 'Exactly one argument required' unless args.size == 1
  dir = args[0]
  @loog.info("Testing judges in #{dir.to_rel}...")
  errors = []
  tested = 0
  tests = 0
  visible = []
  times = {}
  judges = Judges::Judges.new(dir, opts['lib'], @loog)
  elapsed(@loog, level: Logger::INFO) do
    judges.each_with_index do |judge, i|
      visible << judge.name
      next unless include?(opts, judge.name)
      @loog.info("\n👉 Testing #{judge.script} (##{i}) in #{judge.dir.to_rel}...")
      judge.tests.each do |f|
        tname = File.basename(f).gsub(/\.yml$/, '')
        visible << "  #{judge.name}/#{tname}"
        next unless include?(opts, judge.name, tname)
        yaml = YAML.load_file(f, permitted_classes: [Time])
        if yaml['skip']
          @loog.info("Skipped #{f.to_rel}")
          next
        end
        unless Judges::Categories.new(opts['enable'], opts['disable']).ok?(yaml['category'])
          @loog.info("Skipped #{f.to_rel} because of its category")
          next
        end
        @loog.info("🛠️ Testing #{f.to_rel}:")
        start = Time.now
        badge = "#{judge.name}/#{tname}"
        begin
          fb = Factbase.new
          prepare(fb, yaml)
          yaml['before']&.each do |n|
            j = judges.get(n)
            @loog.info("Running #{j.script} judge as a pre-condition...")
            test_one(fb, opts, j, n, yaml, assert: false)
          end
          test_one(fb, opts, judge, tname, yaml)
          yaml['after']&.each do |rb|
            @loog.info("Running #{rb} assertion script...")
            $fb = fb
            $loog = @loog
            load(File.join(judge.dir, rb), true)
          end
          tests += 1
        rescue StandardError => e
          @loog.warn(Backtrace.new(e))
          errors << badge
        end
        times[badge] = Time.now - start
      end
      tested += 1
    end
    unless times.empty?
      fmt = "%-60s\t%9s\t%-9s"
      @loog.info(
        [
          'Test summary:',
          format(fmt, 'Script', 'Seconds', 'Result'),
          format(fmt, '---', '---', '---'),
          times.sort_by { |_, v| v }.reverse.map do |script, sec|
            format(fmt, script, format('%.3f', sec), errors.include?(script) ? 'ERROR' : 'OK')
          end.join("\n  ")
        ].join("\n  ")
      )
    end
    throw :'👍 No judges tested' if tested.zero?
    throw :"👍 All #{tested} judge(s) but no tests passed" if tests.zero?
    throw :"👍 All #{tested} judge(s) and #{tests} tests passed" if errors.empty?
    throw :"❌ #{tested} judge(s) tested, #{errors.size} of them failed"
  end
  unless errors.empty?
    raise "#{errors.size} tests failed" unless opts['quiet']
    @loog.debug('Not failing the build with tests failures, due to the --quiet option')
  end
  return unless tested.zero? || tests.zero?
  if opts['judge'].nil?
    raise 'There are seems to be no judges' unless opts['quiet']
    @loog.debug('Not failing the build with no judges tested, due to the --quiet option')
  else
    raise 'There are seems to be no judges' if visible.empty?
    @loog.info("The following judges are available to use with the --judge option:\n  #{visible.join("\n  ")}")
  end
end