#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require "#{LKP_SRC}/lib/string_ext"
require "#{LKP_SRC}/lib/array_ext"
require "#{LKP_SRC}/lib/tests/stats"

test_item = ''
fs_type = ''
build_type = ''

stats = LKP::Stats.new

while (line = $stdin.gets)
  line = line.resolve_invalid_bytes.plain_text

  case line
  # from /nvml/src/test/unittest/unittest.sh
  # 2732: msg "$UNITTEST_NAME: SETUP ($TEST/$REAL_FS/$BUILD$MCSTR$PROV$PM)"
  when %r{^(.+)/TEST[0-9]+: SETUP\s*\((.+)\)$}
    test_item = Regexp.last_match[1]
    subtest = Regexp.last_match[2].tr('/', '_')
  when %r{^(.+)/(TEST[0-9]+): (PASS|FAIL)}
    item = Regexp.last_match[1]
    name = Regexp.last_match[2]
    next unless test_item == item

    stats.add "#{item}_#{name}_#{subtest}", Regexp.last_match[3]
    subtest = nil
  when %r{RUNTESTS: stopping: (.+)/(TEST[0-9]+) failed}
    item = Regexp.last_match[1]
    name = Regexp.last_match[2]
    if line =~ /TEST=(.+) FS=(.+) BUILD=(.+)/
      test_item = item
      subtest_name = $1
      fs_type = $2
      build_type = $3
    end
    next unless test_item == item

    stats.add "#{item}_#{name}_#{subtest_name}_#{fs_type}_#{build_type}", 'fail'
    # set subtest to nil because when record SKIP test result need judge this value.
    # for example, when test output message is like following.
    #   blk_rw/TEST11: SETUP (check/pmem/debug)
    #   RUNTESTS: stopping: blk_rw/TEST11 failed, TEST=check FS=any BUILD=debug
    #   blk_rw/TEST12: SKIP DEVICE_DAX_PATH does not specify enough dax devices
    # if not set subtest to nil the output is like following.
    #   blk_rw_TEST11_check_any_debug.fail: 1
    #   blk_rw_TEST12_check_pmem_debug.skip: 1
    # after set subtest to nil the output is right.
    #   blk_rw_TEST11_check_any_debug.fail: 1
    #   blk_rw_TEST12.test_skip: 1
    subtest = nil
  when %r{RUNTESTS: stopping: (.+)/(TEST[0-9]+) timed out}
    item = Regexp.last_match[1]
    name = Regexp.last_match[2]
    next unless test_item == item

    stats.add "#{item}_#{name}_#{subtest}", 'timeout'
    subtest = nil
  when %r{^(.+)/(TEST[0-9]+): SKIP}
    item = Regexp.last_match[1]
    name = Regexp.last_match[2]

    if subtest && test_item == item
      stat_name = "#{item}_#{name}_#{subtest}"
      stat_result = 'skip'
      subtest = nil
    else
      stat_name = "#{item}_#{name}"
      stat_result = 'test_skip'
    end

    stats.add stat_name.to_s, stat_result.to_s unless stats.key? stat_name.to_s
  when /^(ignored_by_lkp)\s+(.*)\s+/
    stats.add $2, $1
  end
end

stats.dump
