Class: LearnTest::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/learn_test/reporter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strategy, options = {}) ⇒ Reporter

Returns a new instance of Reporter.



16
17
18
19
20
21
# File 'lib/learn_test/reporter.rb', line 16

def initialize(strategy, options = {})
  @strategy = strategy
  @output_path = options[:output_path] || File.join(Dir.home, '.learn.debug')
  @client = options[:client] || LearnTest::Client.new
  @debug = options[:debug]
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



8
9
10
# File 'lib/learn_test/reporter.rb', line 8

def debug
  @debug
end

#output_pathObject

Returns the value of attribute output_path.



8
9
10
# File 'lib/learn_test/reporter.rb', line 8

def output_path
  @output_path
end

Class Method Details

.report(strategy, options = {}) ⇒ Object



10
11
12
13
14
# File 'lib/learn_test/reporter.rb', line 10

def self.report(strategy, options = {})
  reporter = new(strategy, options)
  reporter.report
  reporter.retry_failed_reports
end

Instance Method Details

#failed_reportsObject



23
24
25
26
# File 'lib/learn_test/reporter.rb', line 23

def failed_reports
  return {} unless File.exists?(output_path)
  JSON.load(File.read(output_path)) || {}
end

#reportObject



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
# File 'lib/learn_test/reporter.rb', line 45

def report
  results = strategy.results
  endpoint = strategy.service_endpoint
  augment_results!(results)

  unless client.post_results(endpoint, results)
    puts 'There was a problem connecting to Learn. Not pushing test results.'.red if @debug

    save_failed_attempt(endpoint, results)
    return
  end

  logger = @debug ? Logger.new(STDOUT, level: Logger::DEBUG) : false
  repo = LearnTest::Git.open(options: { log: logger })

  res = repo.wip(message: 'Automatic test submission')

  unless res.success?
    puts 'There was a problem creating your WIP branch. Not pushing current branch state.'.red if @debug
    return
  end

  begin
    repo.push('origin', "#{res.wip_branch}:refs/heads/fis-wip", { force: true })
  rescue ::Git::GitExecuteError => e
    if @debug
      puts 'There was a problem connecting to GitHub. Not pushing current branch state.'.red
      puts e.message
    end
  end
end

#retry_failed_reportsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/learn_test/reporter.rb', line 28

def retry_failed_reports
  previous_reports = failed_reports
  previous_reports.delete_if do |endpoint, results|
    results.delete_if do |result|
      !!client.post_results(endpoint, result)
    end.empty?
  end

  if previous_reports.empty?
    FileUtils.rm_f(output_path)
  else
    File.open(output_path, 'w') do |file|
      file.write("#{JSON.dump(previous_reports)}\n")
    end
  end
end