class CaseManager

This class does all the job Organize the hole job, sending orders to others classes

Split into several files:

CaseManager: check_cases!

Class CaseManager Methods related with report

CaseManager show method

Public Class Methods

new() click to toggle source

Initialize CaseManager

# File lib/teuton/case_manager/case_manager.rb, line 28
def initialize
  @cases = []
  @report = Report.new(0)
  @report.filename = 'resume'
end

Public Instance Methods

export(args = {}) click to toggle source

Execute “export” order: Export every case report @param args (Hash) Export options

# File lib/teuton/case_manager/case_manager.rb, line 51
def export(args = {})
  if args.class != Hash
    puts "[ERROR] CaseManager#export: Argument = <#{args}>, " \
         "class = #{args.class}"
    puts '        Usage: export :format => :colored_text'
    raise '[ERROR] CaseManager#export: Argument error!'
  end
  # Export report files
  ExportManager.run(@report, @cases, args)
end
play(&block) click to toggle source

Execute “play” order: Start every single case test @param block (Block)

# File lib/teuton/case_manager/case_manager.rb, line 37
def play(&block)
  check_cases!
  instance_eval(&block)
  # Run export if user pass option command "--export=json"
  i = Application.instance.options['export']
  export(format: i.to_sym) unless i.nil?
  # Accept "configfile" param REVISE There exists?
  i = Application.instance.options['configfile']
  export(format: i.to_sym) unless i.nil?
end
send(args = {}) click to toggle source

Execute “send” order: Send every case report @param args (Hash) Send options

# File lib/teuton/case_manager/case_manager.rb, line 65
def send(args = {})
  threads = []
  puts ''
  puts "[INFO] Sending files...#{args.to_s}"
  @cases.each { |c| threads << Thread.new { c.send(args) } }
  threads.each(&:join)
end
show(mode = :resume) click to toggle source

Show cases and resume report data on screep @param mode (Symbol) Values => :resume, :cases or :all

# File lib/teuton/case_manager/show.rb, line 10
def show(mode = :resume)
  return if Application.instance.quiet?

  # Show resume report data on screen
  @report.show if %i[resume all].include? mode

  # Show cases report data on screen
  return if mode == :resume

  @cases.each do |c|
    puts '=' * 40
    c.show
  end
end

Private Instance Methods

build_hall_of_fame() click to toggle source
# File lib/teuton/case_manager/hall_of_fame.rb, line 8
def build_hall_of_fame
  celebrities = {}

  @cases.each do |c|
    grade = c.grade # report.tail[:grade]
    if celebrities[grade]
      label = celebrities[grade] + '*'
    else
      label = '*'
    end
    celebrities[grade] = label unless c.skip
  end

  a = celebrities.sort_by { |key, _value| key }
  list = a.reverse

  app = Application.instance
  app.options[:case_number] = @cases.size
  app.hall_of_fame = list
end
check_cases!() click to toggle source

Start checking every single case

# File lib/teuton/case_manager/check_cases.rb, line 10
def check_cases!
  app = Application.instance

  # Load configurations from config file
  configdata = ConfigFileReader.read(app.config_path)
  app.ialias = configdata[:alias]
  app.global = configdata[:global]
  app.global[:tt_testname] = app.global[:tt_testname] || app.test_name
  app.global[:tt_sequence] = false if app.global[:tt_sequence].nil?

  # Create out dir
  outdir = app.global[:tt_outdir] ||
           File.join('var', app.global[:tt_testname])
  ensure_dir outdir
  @report.output_dir = outdir

  # Fill report head
  open_main_report(app.config_path)

  # create cases and run
  configdata[:cases].each { |config| @cases << Case.new(config) }
  start_time = run_all_cases # run cases

  uniques = collect_uniques_for_all_cases
  close_reports_for_all_cases(uniques)
  close_main_report(start_time)
end
close_main_report(start_time) click to toggle source
# File lib/teuton/case_manager/report.rb, line 28
def close_main_report(start_time)
  finish_time = Time.now
  @report.tail[:start_time] = start_time
  @report.tail[:finish_time] = finish_time
  @report.tail[:duration] = finish_time - start_time

  verbose Rainbow("\n[INFO] Duration = #{format('%3.3f',(finish_time - start_time))}").yellow.bright
  verboseln Rainbow("    (#{finish_time})").yellow.bright
  verboseln '=' * @report.head[:tt_title].length
  verboseln ' '

  app = Application.instance
  @cases.each do |c|
    line = {}
    if c.skip?
      line = { skip: true, id: '-', grade: 0.0, letter: '',
              members: '-', conn_status: {},
              moodle_id: '', moodle_feedback: '' }
    else
      line[:skip] = false
      line[:id] = format('%<id>02d', { id: c.id.to_i })
      line[:letter] = app.letter[:error] if c.grade < 50.0
      line[:grade] = c.grade.to_f #format('  %3d', c.grade.to_f)
      line[:members] = c.members
      line[:conn_status] = c.conn_status
      line[:moodle_id] = c.get(:tt_moodle_id)
      line[:moodle_feedback] = "\"Filename: #{c.filename}. Date: #{Time.now}\""
    end
    @report.lines << line
  end
end
close_reports_for_all_cases(uniques) click to toggle source

1) Reevaluate every case with collected unique values 2) Close all case reports 3) And order to build hall of fame

# File lib/teuton/case_manager/check_cases.rb, line 76
def close_reports_for_all_cases(uniques)
  threads = []
  @cases.each { |c| threads << Thread.new { c.close uniques } }
  threads.each(&:join)

  # Build Hall of Fame
  build_hall_of_fame
end
collect_uniques_for_all_cases() click to toggle source

Collect uniques values for all cases

# File lib/teuton/case_manager/check_cases.rb, line 58
def collect_uniques_for_all_cases
  uniques = {} # Collect "unique" values from all cases
  @cases.each do |c|
    c.uniques.each do |key|
      if uniques[key].nil?
        uniques[key] = [c.id]
      else
        uniques[key] << c.id
      end
    end
  end
  uniques
end
open_main_report(config_filepath) click to toggle source

Open main report (resume report) @param config_filepath (String)

# File lib/teuton/case_manager/report.rb, line 12
def open_main_report(config_filepath)
  app = Application.instance

  @report.head[:tt_title] = "Executing [#{app.name}] (version #{Application::VERSION})"
  @report.head[:tt_scriptname] = trim(app.script_path)
  @report.head[:tt_configfile] = trim(config_filepath)
  @report.head[:tt_pwd] = app.running_basedir
  @report.head[:tt_debug] = true if @debug
  # @report.head[:tt_uses] = app.uses.join(', ') # TO-REVISE
  @report.head.merge!(app.global)

  verboseln ' '
  verboseln '=' * @report.head[:tt_title].length
  verboseln Rainbow(@report.head[:tt_title]).yellow.bright
end
run_all_cases() click to toggle source

Run all cases

# File lib/teuton/case_manager/check_cases.rb, line 40
def run_all_cases
  start_time = Time.now
  if Application.instance.global[:tt_sequence]
    verboseln Rainbow("[INFO] Running in sequence (#{start_time})").yellow.bright
    # Run every case in sequence
    @cases.each(&:play)
  else
    verboseln Rainbow("[INFO] Running in parallel (#{start_time})").yellow.bright
    threads = []
    # Run all cases in parallel
    @cases.each { |c| threads << Thread.new { c.play } }
    threads.each(&:join)
  end
  start_time
end
trim(input) click to toggle source

Trim string text when is too long @param input (String) @return String

# File lib/teuton/case_manager/report.rb, line 64
def trim(input)
  return input unless input.to_s.start_with? Dir.pwd.to_s
  return input if input == Dir.pwd.to_s

  output = input.to_s
  offset = (Dir.pwd).length + 1
  output = "#{input[offset, input.size]}"
  output.to_s
end