results_keeper 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8c9877f4580c196f8adfe980e5b4ac09b3443147
4
- data.tar.gz: 223977ee86856fbe16f20ee94873a896143ddd9b
3
+ metadata.gz: 0c4e73090baacd2c643c5660017efd309fe48462
4
+ data.tar.gz: f8a7806dddc8637b521dd54320a791a1ec7c9259
5
5
  SHA512:
6
- metadata.gz: 1d6fe87a3cd4978e129c0a20ceac7f43f9d7394983bc7e97c26fe0b7106119be55eeb7e348616d5f9cbd8ec299019fd86a56a2bac20a85c83d967e74fbebb736
7
- data.tar.gz: 5c8e2e0b975a912931474341aeba577b3aa9093a99aa729e6d2039328893aa2426bf52ec1ebc114b83fb6fc70b47d48d8f3cccee866317554234a1bb809dd961
6
+ metadata.gz: c736b710a9eb48c9756b26b9a5139d6add670da90fb3c5ad62d8ad282ba3281fc89ea7698d5e535f4553421e8dc8ce0ca687167e327db8c7b5c769eb002bae1a
7
+ data.tar.gz: b7405e367e633beab844a7b82da96428d7aa5a179a2add136bd59f3c1a34250abbb1a8261596d7eb618a532ce7eb033cd49054c6874006e6e451cfed3910ce74
@@ -1,118 +1,32 @@
1
- require 'net/http'
2
1
  require 'json'
3
- require 'rest_client'
4
- require_relative 'string'
5
2
  require 'singleton'
6
- require 'pry'
7
- require_relative 'test_data_generator'
3
+ require_relative 'results_keeper/results_sender'
4
+ require_relative 'results_keeper/messages'
8
5
 
9
6
  class ResultsKeeper
10
7
  include Singleton
11
8
 
12
- DEFAULT_PORT = 3000
13
- DEFAULT_HOST = 'localhost'
9
+ attr_reader :test_started_time, :results_sender
14
10
 
15
11
  def initialize
16
- @data_generator = TestDataGenerator.new
17
- @tmp_data ||= {}
18
- @tmp_data[:tests] ||= []
19
- @rk_host = ENV['RK_REPORT_SERVER_HOST'] || DEFAULT_HOST
20
- @rk_port = ENV['RK_REPORT_SERVER_PORT'] || DEFAULT_PORT
12
+ @results_sender = RkCucumber::ResultsSender.new
21
13
  end
22
14
 
23
15
  def send_revision
24
- @revision = send_json(@data_generator.revision_data,
25
- 'revisions',
26
- "#{@data_generator.project_name} > #{@data_generator.revision_name} started")
16
+ @results_sender.send_revision
27
17
  end
28
18
 
29
19
  def send_revision_complete
30
- send_not_sent_scenarios
31
- send_json(@data_generator.revision_completed_data,
32
- 'revisions',
33
- "#{@data_generator.revision_name} completed")
20
+ @results_sender.send_revision_complete
34
21
  end
35
22
 
36
23
  def send_test(scenario)
37
- # In case if the host is not available we need to send revision once more (when it comes back)
38
- send_revision unless @revision
39
- return add_test_to_tmp(scenario) unless @revision
40
- @screenshot_path = save_screenshot(scenario) if ENV['RK_SEND_SCREENSHOTS']
41
- @test = send_json(@data_generator.test_data(scenario, @revision), 'tests')
42
- send_screenshot(@screenshot_path) if @screenshot_path
43
- end
44
-
45
- def save_screenshot(scenario)
46
- if scenario.exception
47
- begin
48
- screenshot_name = "#{Time.now.to_i}_#{rand(1000..9999)}.png"
49
- @file_path = "tmp/screenshots/#{screenshot_name}"
50
- Capybara.page.save_screenshot(@file_path)
51
- @file_path
52
- rescue
53
- console_message('Could not take a screenshot :(')
54
- nil
55
- end
56
- end
57
- end
58
-
59
- def add_test_to_tmp(scenario)
60
- #console_warning_message("Could not send '#{scenario.name}' there seems to be a problem with #{@rk_host}:#{@rk_port}")
61
- @tmp_data[:tests] << scenario
62
- puts console_warning_message("Not sent scenarios amount: #{@tmp_data[:tests].count}")
63
- end
64
-
65
- def send_not_sent_scenarios
66
- if @revision
67
- @tmp_data[:tests].each do |s|
68
- console_message("Trying to re-send '#{s.name}' once more")
69
- send_test(s)
70
- end
71
- else
72
- false
73
- end
74
- end
75
-
76
- def send_json(body, path, message_to_client=nil)
77
-
78
- body['secret_key'] = ENV['RK_SECRET_KEY']
79
- @path = "/api/#{path}"
80
- @body = body.to_json
81
-
82
- begin
83
- request = Net::HTTP::Post.new(@path, initheader = {'Content-Type' => 'application/json'})
84
- request.body = @body
85
- response = Net::HTTP.new(@rk_host, @rk_port).start {|http| http.request(request)}
86
-
87
- message_to_client ? console_message("#{message_to_client} - #{response.code} - #{response.message}") : console_message("#{response.code} - #{response.message}")
88
- JSON.parse(response.body)
89
- rescue
90
- console_warning_message("There seems to be a problem with #{@rk_host}:#{@rk_port}, make sure your RK_SECRET_KEY is correct and server is up.")
91
- false
92
- end
93
- end
94
-
95
- def send_screenshot(screenshot_path)
96
- begin
97
- params = {project: @revision['project_id'], revision: @revision['revision_id'], test: @test['id']}
98
- RestClient.post("http://#{@rk_host}:#{@rk_port}/api/tests/upload_screenshot",
99
- :name_of_file_param => File.new(screenshot_path), :body => params)
100
- File.delete(screenshot_path)
101
- rescue => e
102
- console_warning_message("Could not send screenshot, there seems to be a problem with #{@rk_host}:#{@rk_port}")
24
+ unless @results_sender.send_test(scenario, @test_started_time)
25
+ @results_sender.add_test_to_tmp(scenario, @test_started_time)
103
26
  end
104
27
  end
105
28
 
106
29
  def test_started(scenarion=nil)
107
- @data_generator.test_started
108
- end
109
-
110
- def console_message(text)
111
- puts " Results Keeper: #{text}".blue unless ENV['HIDE_RK_MESSAGES'] == 'true'
112
- end
113
-
114
- def console_warning_message(text)
115
- puts " Results Keeper: #{text}".yellow unless ENV['HIDE_RK_MESSAGES'] == 'true'
30
+ @test_started_time = Time.now
116
31
  end
117
32
  end
118
-
@@ -0,0 +1,40 @@
1
+ require_relative 'messages'
2
+
3
+ module RkCucumber
4
+ class Config
5
+ class << self
6
+
7
+ def default
8
+ {
9
+ project_name: 'Default',
10
+ report_server_host: 'results-keeper.com',
11
+ report_server_port: 80,
12
+ api: {
13
+ revision_path: 'revisions',
14
+ test_path: 'tests'
15
+ }
16
+ }
17
+ end
18
+
19
+ def config
20
+ opts = {}
21
+ opts[:report_server_host] = ENV['RK_REPORT_SERVER_HOST'] if ENV['RK_REPORT_SERVER_HOST']
22
+ opts[:report_server_port] = ENV['RK_REPORT_SERVER_PORT'] if ENV['RK_REPORT_SERVER_PORT']
23
+ opts[:project_name] = ENV['RK_PROJECT_NAME'] || default_project_name
24
+ opts[:revision_name] = ENV['RK_REVISION_NAME'] || ENV['RK_REVISION_NAME'] = "Result for #{Time.now}"
25
+ opts[:secret_key] = ENV['RK_SECRET_KEY'] || raise(RkCucumber::Messages.no_secret_key)
26
+ default.merge!(opts)
27
+ end
28
+
29
+ private
30
+
31
+ def default_project_name
32
+ begin
33
+ `pwd`.strip.split('/').last
34
+ rescue
35
+ 'Default'
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,50 @@
1
+ require_relative 'string'
2
+ require_relative 'config'
3
+
4
+ module RkCucumber
5
+ class Messages
6
+ class << self
7
+
8
+ # ERROR
9
+ def take_screenshot_error
10
+ console_warning_message('Could not take a screenshot :(')
11
+ end
12
+
13
+ def error_send_screenshot
14
+ console_warning_message("Could not send screenshot, there seems to be a problem with #{RkCucumber::Config.config[:report_server_host]}:#{RkCucumber::Config.config[:report_server_port]}")
15
+ end
16
+
17
+ def error_not_sent_scenario(amount)
18
+ console_warning_message("Not sent scenarios amount: #{amount}")
19
+ end
20
+
21
+ def no_secret_key
22
+ 'RK_SECRET_KEY env var not found. Please use export RK_SECRET_KEY=<your secret key> to set it.'
23
+ end
24
+
25
+ # INFO
26
+ def info_trying_resend(scenario_name)
27
+ console_message("Trying to re-send '#{scenario_name}' once more")
28
+ end
29
+
30
+ def info_message_to_client(response, message_to_client=nil)
31
+ message_to_client ? console_message("#{message_to_client} - #{response.code} - #{response.message}") : console_message("#{response.code} - #{response.message}")
32
+ end
33
+
34
+ # WARN
35
+ def warn_problem_with_rk
36
+ console_warning_message("There seems to be a problem with #{RkCucumber::Config.config[:report_server_host]}:#{RkCucumber::Config.config[:report_server_port]}, make sure your RK_SECRET_KEY is correct and server is up.")
37
+ end
38
+
39
+ def console_message(text)
40
+ private
41
+ puts " Results Keeper: #{text}".blue unless ENV['HIDE_RK_MESSAGES'] == 'true'
42
+ end
43
+
44
+ def console_warning_message(text)
45
+ private
46
+ puts " Results Keeper: #{text}".yellow unless ENV['HIDE_RK_MESSAGES'] == 'true'
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,90 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'rest_client'
4
+ require_relative 'messages'
5
+ require_relative 'test_data_generator'
6
+ require_relative 'config'
7
+
8
+ module RkCucumber
9
+ class ResultsSender
10
+
11
+ attr_reader :tmp_data
12
+
13
+ def initialize
14
+ @data_generator = RkCucumber::TestDataGenerator.new
15
+ @tmp_data ||= {}
16
+ @tmp_data[:tests] ||= []
17
+ end
18
+
19
+ def send_revision
20
+ @revision = send_json(@data_generator.revision_data,
21
+ RkCucumber::Config.default[:api][:revision_path],
22
+ "#{RkCucumber::Config.config[:project_name]} > #{RkCucumber::Config.config[:revision_name]} started")
23
+ end
24
+
25
+ def send_revision_complete
26
+ send_not_sent_scenarios
27
+ send_json(@data_generator.revision_completed_data,
28
+ RkCucumber::Config.default[:api][:revision_path],
29
+ "#{RkCucumber::Config.config[:revision_name]} completed")
30
+ end
31
+
32
+ def send_test(scenario, test_started_at)
33
+ # In case if the host is not available we need to send revision once more (when it comes back)
34
+ send_revision unless @revision
35
+ return nil unless @revision
36
+ screenshot_path = @data_generator.save_screenshot(scenario) if ENV['RK_SEND_SCREENSHOTS']
37
+ @test = send_json(@data_generator.test_data(scenario, @revision, test_started_at),
38
+ RkCucumber::Config.default[:api][:test_path])
39
+ return nil unless @test
40
+ send_screenshot(screenshot_path) if screenshot_path
41
+ @test
42
+ end
43
+
44
+ def send_not_sent_scenarios
45
+ if @revision
46
+ @tmp_data[:tests].each do |s|
47
+ s[:revision_id] = @revision['revision_id'] unless s[:revision_id]
48
+ RkCucumber::Messages.info_trying_resend(s[:name])
49
+ send_json(s, RkCucumber::Config.default[:api][:test_path]) if @revision
50
+ end
51
+ else
52
+ false
53
+ end
54
+ end
55
+
56
+ def send_json(body, path, message_to_client=nil)
57
+ body['secret_key'] = RkCucumber::Config.config[:secret_key]
58
+ @path = "/api/#{path}"
59
+ @body = body.to_json
60
+ begin
61
+ request = Net::HTTP::Post.new(@path, initheader = {'Content-Type' => 'application/json'})
62
+ request.body = @body
63
+ response = Net::HTTP.new(RkCucumber::Config.config[:report_server_host],
64
+ RkCucumber::Config.config[:report_server_port]).start {|http| http.request(request)}
65
+
66
+ RkCucumber::Messages.info_message_to_client(response, message_to_client)
67
+ JSON.parse(response.body)
68
+ rescue
69
+ RkCucumber::Messages.warn_problem_with_rk
70
+ false
71
+ end
72
+ end
73
+
74
+ def send_screenshot(screenshot_path)
75
+ begin
76
+ params = {project: @revision['project_id'], revision: @revision['revision_id'], test: @test['id']}
77
+ RestClient.post("http://#{RkCucumber::Config.config[:report_server_host]}:#{RkCucumber::Config.config[:report_server_port]}/api/tests/upload_screenshot",
78
+ :name_of_file_param => File.new(screenshot_path), :body => params)
79
+ File.delete(screenshot_path)
80
+ rescue
81
+ RkCucumber::Messages.error_send_screenshot
82
+ end
83
+ end
84
+
85
+ def add_test_to_tmp(scenario, test_started_at)
86
+ @tmp_data[:tests] << @data_generator.test_data(scenario, @revision, test_started_at)
87
+ RkCucumber::Messages.error_not_sent_scenario(@tmp_data[:tests].count)
88
+ end
89
+ end
90
+ end
File without changes
@@ -0,0 +1,67 @@
1
+ require_relative 'config'
2
+ require_relative 'messages'
3
+
4
+ module RkCucumber
5
+ class TestDataGenerator
6
+
7
+ SCENARIO_HOOKS = 'Before hook After hook Around hook'
8
+
9
+ def revision_data
10
+ { revision: RkCucumber::Config.config[:revision_name], project: RkCucumber::Config.config[:project_name] }
11
+ end
12
+
13
+ def revision_completed_data
14
+ { revision: RkCucumber::Config.config[:revision_name], project: RkCucumber::Config.config[:project_name], completed: 1 }
15
+ end
16
+
17
+ def test_data(scenario, revision, test_started_at)
18
+ revision ||= { revision_id: nil }
19
+ test_duration = Time.now - test_started_at
20
+ scenario_error = scenario.exception.message if scenario.exception
21
+ run_path = "cucumber #{scenario.location.file}:#{scenario.location.line}"
22
+ {
23
+ name: scenario.name,
24
+ status: scenario.status,
25
+ feature_name: scenario.feature.name,
26
+ run_path: run_path,
27
+ tags: scenario.tags.map(&:name).join(', '),
28
+ error: scenario_error,
29
+ revision_id: revision['revision_id'],
30
+ steps: scenario_steps(scenario),
31
+ duration: test_duration
32
+ }
33
+ end
34
+
35
+ def save_screenshot(scenario)
36
+ if scenario.exception
37
+ begin
38
+ screenshot_name = "#{Time.now.to_i}_#{rand(1000..9999)}.png"
39
+ @file_path = "tmp/screenshots/#{screenshot_name}"
40
+ Capybara.page.save_screenshot(@file_path)
41
+ @file_path
42
+ rescue
43
+ RkCucumber::Messages.take_screenshot_error
44
+ nil
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def step_keyword(test_step)
52
+ test_step.source.last.keyword
53
+ end
54
+
55
+ def scenario_steps(scenario)
56
+ steps = []
57
+ scenario.test_steps.each do |test_step|
58
+ if test_step.to_s.include? 'Cucumber::Core::Test'
59
+ steps << "#{step_keyword(test_step)} #{test_step.name.to_s}" unless SCENARIO_HOOKS.include?(test_step.name.to_s)
60
+ else
61
+ steps << "#{step_keyword(test_step)} #{test_step.to_s}" unless SCENARIO_HOOKS.include?(test_step.to_s)
62
+ end
63
+ end
64
+ steps
65
+ end
66
+ end
67
+ end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: results_keeper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Kozakov
8
+ - Volodymyr Zubchynskyi
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2017-10-31 00:00:00.000000000 Z
12
+ date: 2017-11-29 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: flickraw
@@ -46,8 +47,11 @@ extensions: []
46
47
  extra_rdoc_files: []
47
48
  files:
48
49
  - lib/results_keeper.rb
49
- - lib/string.rb
50
- - lib/test_data_generator.rb
50
+ - lib/results_keeper/config.rb
51
+ - lib/results_keeper/messages.rb
52
+ - lib/results_keeper/results_sender.rb
53
+ - lib/results_keeper/string.rb
54
+ - lib/results_keeper/test_data_generator.rb
51
55
  homepage: https://bitbucket.org/results_keeper/results_keeper_cucumber/
52
56
  licenses:
53
57
  - MIT
@@ -68,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
72
  version: '0'
69
73
  requirements: []
70
74
  rubyforge_project:
71
- rubygems_version: 2.5.1
75
+ rubygems_version: 2.6.14
72
76
  signing_key:
73
77
  specification_version: 4
74
78
  summary: Result Keeper
@@ -1,67 +0,0 @@
1
- class TestDataGenerator
2
-
3
- SCENARIO_HOOKS = 'Before hook After hook Around hook'
4
-
5
- attr_reader :project_name, :revision_name
6
-
7
- def initialize
8
- @project_name ||= ENV['RK_PROJECT_NAME'] || default_project_name
9
- @revision_name ||= ENV['RK_REVISION_NAME'] || "Result for #{Time.now}"
10
- end
11
-
12
- def revision_data
13
- { revision: @revision_name, project: @project_name }
14
- end
15
-
16
- def revision_completed_data
17
- { revision: @revision_name, project: @project_name, completed: 1 }
18
- end
19
-
20
- def test_data(scenario, revision)
21
- test_duration = Time.now - @test_started_at
22
- scenario_error = scenario.exception.message if scenario.exception
23
- run_path = "cucumber #{scenario.location.file}:#{scenario.location.line}"
24
- {
25
- name: scenario.name,
26
- status: scenario.status,
27
- feature_name: scenario.feature.name,
28
- run_path: run_path,
29
- tags: scenario.tags.map(&:name).join(', '),
30
- error: scenario_error,
31
- revision_id: revision['revision_id'],
32
- steps: scenario_steps(scenario),
33
- duration: test_duration
34
- }
35
- end
36
-
37
- def test_started(scenarion=nil)
38
- @test_started_at = Time.now
39
- end
40
-
41
- private
42
-
43
- def step_keyword(test_step)
44
- test_step.source.last.keyword
45
- end
46
-
47
- def scenario_steps(scenario)
48
- steps = []
49
- scenario.test_steps.each do |test_step|
50
- if test_step.to_s.include? 'Cucumber::Core::Test'
51
- steps << "#{step_keyword(test_step)} #{test_step.name.to_s}" unless SCENARIO_HOOKS.include?(test_step.name.to_s)
52
- else
53
- steps << "#{step_keyword(test_step)} #{test_step.to_s}" unless SCENARIO_HOOKS.include?(test_step.to_s)
54
- end
55
- end
56
- steps
57
- end
58
-
59
-
60
- def default_project_name
61
- begin
62
- `pwd`.strip.split('/').last
63
- rescue
64
- 'Default'
65
- end
66
- end
67
- end