Class: LearnTest::RSpec::Runner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, user_id, repo_name, options) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/learn_test/rspec/runner.rb', line 7

def initialize(username, user_id, repo_name, options)
  @username = username
  @user_id = user_id
  @repo_name = repo_name
  @options = options
  @json_output = ""
  @parsed_output = nil
  @formatted_results = {}
  @connection = Faraday.new(url: SERVICE_URL) do |faraday|
    faraday.adapter  Faraday.default_adapter
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/learn_test/rspec/runner.rb', line 5

def connection
  @connection
end

#formatted_resultsObject

Returns the value of attribute formatted_results.



4
5
6
# File 'lib/learn_test/rspec/runner.rb', line 4

def formatted_results
  @formatted_results
end

#json_outputObject

Returns the value of attribute json_output.



4
5
6
# File 'lib/learn_test/rspec/runner.rb', line 4

def json_output
  @json_output
end

#keep_resultsObject

Returns the value of attribute keep_results.



4
5
6
# File 'lib/learn_test/rspec/runner.rb', line 4

def keep_results
  @keep_results
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/learn_test/rspec/runner.rb', line 5

def options
  @options
end

#parsed_outputObject

Returns the value of attribute parsed_output.



4
5
6
# File 'lib/learn_test/rspec/runner.rb', line 4

def parsed_output
  @parsed_output
end

#repo_nameObject (readonly)

Returns the value of attribute repo_name.



5
6
7
# File 'lib/learn_test/rspec/runner.rb', line 5

def repo_name
  @repo_name
end

#user_idObject (readonly)

Returns the value of attribute user_id.



5
6
7
# File 'lib/learn_test/rspec/runner.rb', line 5

def user_id
  @user_id
end

#usernameObject (readonly)

Returns the value of attribute username.



5
6
7
# File 'lib/learn_test/rspec/runner.rb', line 5

def username
  @username
end

Instance Method Details

#check_optionsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/learn_test/rspec/runner.rb', line 32

def check_options
  options_has_format = options.include?('--format') || options.include?('-f')
  if dot_rspec = read_dot_rspec
    if options_has_format
      if dot_rspec.any? {|dot_opt| dot_opt.match(/--format|-f/)}
        options << dot_rspec.reject {|dot_opt| dot_opt.match(/--format|-f/)}
      else
        options << dot_rspec
      end
    end
  end

  if options_has_format
    self.options.flatten!
  else
    options.unshift('--format documentation')
  end

  # Don't pass the test/local flag from learn binary to rspec runner.
  options.delete("--test")
  options.delete("-t")
  options.delete("-l")
  options.delete("--local")

  self.keep_results = !!options.delete('--keep')
end

#cleanupObject



117
118
119
# File 'lib/learn_test/rspec/runner.rb', line 117

def cleanup
  FileUtils.rm('.results.json') unless keep_results?
end

#format_resultsObject



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
# File 'lib/learn_test/rspec/runner.rb', line 79

def format_results
  self.formatted_results.merge!({
    username: username,
    github_user_id: user_id,
    repo_name: repo_name,
    build: {
      test_suite: [{
        framework: 'rspec',
        formatted_output: parsed_output,
        duration: parsed_output ? parsed_output[:summary][:duration] : nil
      }]
    },
    examples: parsed_output ? parsed_output[:summary][:example_count] : 1,
    passing_count: parsed_output ? parsed_output[:summary][:example_count] - parsed_output[:summary][:failure_count] - parsed_output[:summary][:pending_count] : 0,
    pending_count: parsed_output ? parsed_output[:summary][:pending_count] : 0,
    failure_count: parsed_output ? parsed_output[:summary][:failure_count] : 1,
    failure_descriptions: if parsed_output
      parsed_output[:examples].select do |example|
        example[:status] == "failed"
      end.map { |ex| ex[:full_description] }.join(";")
    else
      'A syntax error prevented RSpec from running.'
    end
  })
end

#jsonifyObject



75
76
77
# File 'lib/learn_test/rspec/runner.rb', line 75

def jsonify
  self.parsed_output = json_output ? Oj.load(json_output, symbol_keys: true) : nil
end

#keep_results?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/learn_test/rspec/runner.rb', line 121

def keep_results?
  keep_results
end

#push_resultsObject



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/learn_test/rspec/runner.rb', line 105

def push_results
  begin
    connection.post do |req|
      req.url SERVICE_ENDPOINT
      req.headers['Content-Type'] = 'application/json'
      req.body = Oj.dump(formatted_results, mode: :compat)
    end
  rescue Faraday::ConnectionFailed
    puts 'There was a problem connecting to Learn. Not pushing test results.'
  end
end

#read_dot_rspecObject



59
60
61
62
63
64
65
# File 'lib/learn_test/rspec/runner.rb', line 59

def read_dot_rspec
  if File.exist?('.rspec')
    File.readlines('.rspec').map(&:strip)
  else
    nil
  end
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/learn_test/rspec/runner.rb', line 20

def run
  check_options
  run_rspec
  if !options.include?('-h') && !options.include?('--help')
    set_json_output
    jsonify
    format_results
    push_results
    cleanup
  end
end

#run_rspecObject



67
68
69
# File 'lib/learn_test/rspec/runner.rb', line 67

def run_rspec
  system("rspec #{options.join(' ')} --format j --out .results.json")
end

#set_json_outputObject



71
72
73
# File 'lib/learn_test/rspec/runner.rb', line 71

def set_json_output
  self.json_output = File.exists?('.results.json') ? File.read('.results.json') : nil
end