results_keeper_rspec 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36cdc1060d91d057f084dac5578d4bb3e06eaeef
4
- data.tar.gz: f73f981f7c0724d7b6a45478fcf9733f98ad553b
3
+ metadata.gz: f3cd9a3afd8dee63e52582d6c859e2ddfc512375
4
+ data.tar.gz: 278dd795b61ae28b30233b4236881a1fa07cac00
5
5
  SHA512:
6
- metadata.gz: c1cddd66c279b5ea1653c8e27cb058993722cfa291eefcbb43e83c3e51d1f06cdbbe4d79f5e7acfefffc5de883f788925a54525d9ae79636824bc00a95ff488e
7
- data.tar.gz: 70a4d7a0951395339a48f2a8cfc33df4211d75537022c945686051ec3d2de424ee4ebe7d3607bab5bd93bdf40d701939d777cc83df5f259205618522ef3381c7
6
+ metadata.gz: 370ec76222086a0a668aa4eefb3fbf1e5d1ea584bd1991d50f398c9c209ea713677de912ddbc102fb75333f739dca05d62ef6bb972f2825df06aed39b6177057
7
+ data.tar.gz: 2e1cebcd4d4ed7eb1f113238e14158b5ca9dc550d5751999d0bfd6f8ef503ec37cffd00cd7311e70c21d4f17d74efe47122bd5cba97fbb19149f0ba23c83c58d
@@ -1,96 +1,27 @@
1
- require 'net/http'
2
- require 'json'
3
- require 'rest_client'
4
- require_relative 'string'
1
+ require_relative 'results_keeper_rspec/results_sender'
5
2
  require 'singleton'
6
3
  require 'fileutils'
7
4
 
8
5
  class ResultsKeeperRspec
9
6
  include Singleton
10
7
 
11
- attr_reader :revision_name, :revision_project
12
-
13
- DEFAULT_PORT = 80
14
- DEFAULT_HOST = 'results-keeper-api.herokuapp.com'
8
+ attr_reader :results_sender
15
9
 
16
10
  def initialize
17
- @revision_name = ENV['REVISION_NAME'] || "Result for #{Time.now}"
18
- @revision_project = ENV['PROJECT_NAME'] || 'Default'
11
+ @results_sender = RkRspec::ResultsSender.new
19
12
  end
20
13
 
21
14
  def send_revision
22
- data = { revision: @revision_name, project: project_name }
23
- @revision = send_json(data, 'revisions')
15
+ @results_sender.send_revision
24
16
  end
25
17
 
26
18
  def send_revision_complete
27
- data = { revision: @revision_name, project: project_name, completed: 1 }
28
- send_json(data, 'revisions')
29
- remove_screenshots
19
+ @results_sender.send_revision_complete
30
20
  end
31
21
 
32
22
  def send_test(scenario)
33
- test_duration = Time.now - scenario.metadata[:execution_result].started_at
34
- @screenshot_path = save_screenshot(scenario) if ENV['SEND_SCREENSHOTS']
35
- status = scenario.exception ? 'failed' : 'passed'
36
- scenario_error = scenario.exception.message if scenario.exception
37
- run_path = "rspec #{scenario.location}"
38
- data = {
39
- name: "#{scenario.example_group.description} - #{scenario.description}",
40
- status: status,
41
- feature_name: scenario.example_group.description,
42
- run_path: run_path,
43
- error: scenario_error,
44
- revision_id: @revision['revision_id'],
45
- duration: test_duration
46
- }
47
- @test = send_json(data, 'tests')
48
- send_screenshot(@screenshot_path) if @screenshot_path
49
- end
50
-
51
- def save_screenshot(scenario)
52
- if scenario.exception
53
- screenshot_name = "#{Time.now.to_i}_#{rand(1000..9999)}.png"
54
- @file_path = "tmp/screenshots/#{screenshot_name}"
55
- Capybara.page.save_screenshot(@file_path)
56
- @file_path
57
- end
58
- end
59
-
60
- def send_json(body, path)
61
- @host = ENV['REPORT_SERVER_HOST'] || DEFAULT_HOST
62
- @port = ENV['REPORT_SERVER_PORT'] || DEFAULT_PORT
63
-
64
- body['secret_key'] = ENV['RK_SECRET_KEY']
65
- @path = "/api/#{path}"
66
- @body = body.to_json
67
-
68
- request = Net::HTTP::Post.new(@path, initheader = {'Content-Type' =>'application/json'})
69
- request.body = @body
70
- response = Net::HTTP.new(@host, @port).start {|http| http.request(request) }
71
- puts " RK: #{response.code} - #{response.message}".blue
72
- result_hash = JSON.parse(response.body) rescue console_message('There seems to be a problem. Are you authorized?')
73
- result_hash
74
- end
75
-
76
- def send_screenshot(screenshot_path)
77
- if ENV['SEND_SCREENSHOTS']
78
- params = { project: @revision['project_id'], revision: @revision['revision_id'], test: @test['id'] }
79
- RestClient.post("http://#{@host}:#{@port}/api/tests/upload_screenshot",
80
- :name_of_file_param => File.new(screenshot_path), :body => params)
81
- FileUtils.rm(screenshot_path)
23
+ unless @results_sender.send_test(scenario)
24
+ @results_sender.add_test_to_tmp(scenario)
82
25
  end
83
26
  end
84
-
85
- def project_name
86
- ENV['PROJECT_NAME']
87
- end
88
-
89
- def console_message(text)
90
- puts " RK: #{text}".blue
91
- end
92
-
93
- def remove_screenshots
94
- FileUtils.rm_rf(Dir.glob('tmp/screenshots/*'))
95
- end
96
27
  end
@@ -0,0 +1,38 @@
1
+ module RkRspec
2
+ class Config
3
+ class << self
4
+
5
+ def default
6
+ {
7
+ project_name: 'Default',
8
+ report_server_host: 'results-keeper.com',
9
+ report_server_port: 80,
10
+ api: {
11
+ revision_path: 'revisions',
12
+ test_path: 'tests'
13
+ }
14
+ }
15
+ end
16
+
17
+ def config
18
+ opts = {}
19
+ opts[:report_server_host] = ENV['RK_REPORT_SERVER_HOST'] if ENV['RK_REPORT_SERVER_HOST']
20
+ opts[:report_server_port] = ENV['RK_REPORT_SERVER_PORT'] if ENV['RK_REPORT_SERVER_PORT']
21
+ opts[:project_name] = ENV['RK_PROJECT_NAME'] || default_project_name
22
+ opts[:revision_name] = ENV['RK_REVISION_NAME'] || ENV['RK_REVISION_NAME'] = "Result for #{Time.now}"
23
+ opts[:secret_key] = ENV['RK_SECRET_KEY'] || raise(RkRspec::Messages.no_secret_key)
24
+ default.merge!(opts)
25
+ end
26
+
27
+ private
28
+
29
+ def default_project_name
30
+ begin
31
+ `pwd`.strip.split('/').last
32
+ rescue
33
+ 'Default'
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,50 @@
1
+ require_relative 'string'
2
+ require_relative 'config'
3
+
4
+ module RkRspec
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 #{RkRspec::Config.config[:report_server_host]}:#{RkRspec::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 #{RkRspec::Config.config[:report_server_host]}:#{RkRspec::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,88 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'rest_client'
4
+ require 'pry'
5
+ require_relative 'messages'
6
+ require_relative 'config'
7
+ require_relative 'test_data_generator'
8
+
9
+ module RkRspec
10
+ class ResultsSender
11
+
12
+ attr_reader :tmp_data
13
+
14
+ def initialize
15
+ @data_generator = RkRspec::TestDataGenerator.new
16
+ @tmp_data ||= {}
17
+ @tmp_data[:tests] ||= []
18
+ end
19
+
20
+ def send_revision
21
+ @revision = send_json(@data_generator.revision_data,
22
+ RkRspec::Config.default[:api][:revision_path],
23
+ "#{RkRspec::Config.config[:project_name]} > #{RkRspec::Config.config[:revision_name]} started")
24
+ end
25
+
26
+ def send_revision_complete
27
+ send_not_sent_scenarios
28
+ send_json(@data_generator.revision_completed_data,
29
+ RkRspec::Config.default[:api][:revision_path],
30
+ "#{RkRspec::Config.config[:revision_name]} completed")
31
+ end
32
+
33
+ def send_test(scenario)
34
+ # In case if the host is not available we need to send revision once more (when it comes back)
35
+ send_revision unless @revision
36
+ return unless @revision
37
+ screenshot_path = @data_generator.save_screenshot(scenario) if ENV['RK_SEND_SCREENSHOTS']
38
+ @test = send_json(@data_generator.test_data(scenario, @revision),
39
+ RkRspec::Config.default[:api][:test_path], nil)
40
+ send_screenshot(screenshot_path) if screenshot_path
41
+ @test
42
+ end
43
+
44
+ def send_screenshot(screenshot_path)
45
+ begin
46
+ params = { project: @revision['project_id'], revision: @revision['revision_id'], test: @test['id'] }
47
+ RestClient.post("http://#{RkRspec::Config.config[:report_server_host]}:#{RkRspec::Config.config[:report_server_port]}/api/tests/upload_screenshot",
48
+ :name_of_file_param => File.new(screenshot_path), :body => params)
49
+ File.delete(screenshot_path)
50
+ rescue
51
+ RkRspec::Messages.error_send_screenshot
52
+ end
53
+ end
54
+
55
+ def send_json(body, path, message_to_client=nil)
56
+ body['secret_key'] = RkRspec::Config.config[:secret_key]
57
+ @path = "/api/#{path}"
58
+ @body = body.to_json
59
+ begin
60
+ request = Net::HTTP::Post.new(@path, initheader = {'Content-Type' => 'application/json'})
61
+ request.body = @body
62
+ response = Net::HTTP.new(RkRspec::Config.config[:report_server_host],
63
+ RkRspec::Config.config[:report_server_port]).start { |http| http.request(request) }
64
+
65
+ RkRspec::Messages.info_message_to_client(response, message_to_client) if message_to_client
66
+ JSON.parse(response.body)
67
+ rescue
68
+ false
69
+ end
70
+ end
71
+
72
+ def send_not_sent_scenarios
73
+ if @revision
74
+ @tmp_data[:tests].each do |s|
75
+ s[:revision_id] = @revision['revision_id'] unless s[:revision_id]
76
+ RkRspec::Messages.info_trying_resend(s[:name])
77
+ send_json(s, RkRspec::Config.default[:api][:test_path]) if @revision
78
+ end
79
+ else
80
+ false
81
+ end
82
+ end
83
+
84
+ def add_test_to_tmp(scenario)
85
+ @tmp_data[:tests] << @data_generator.test_data(scenario, @revision)
86
+ end
87
+ end
88
+ end
File without changes
@@ -0,0 +1,40 @@
1
+ require_relative 'config'
2
+
3
+ module RkRspec
4
+ class TestDataGenerator
5
+
6
+ def revision_data
7
+ { revision: RkRspec::Config.config[:revision_name], project: RkRspec::Config.config[:project_name] }
8
+ end
9
+
10
+ def revision_completed_data
11
+ { revision: RkRspec::Config.config[:revision_name], project: RkRspec::Config.config[:project_name], completed: 1 }
12
+ end
13
+
14
+ def test_data(scenario, revision)
15
+ revision ||= { revision_id: nil }
16
+ test_duration = Time.now - scenario.metadata[:execution_result].started_at
17
+ status = scenario.exception ? 'failed' : 'passed'
18
+ scenario_error = scenario.exception.message if scenario.exception
19
+ run_path = "rspec #{scenario.location}"
20
+ {
21
+ name: "#{scenario.example_group.description} - #{scenario.description}",
22
+ status: status,
23
+ feature_name: scenario.example_group.description,
24
+ run_path: run_path,
25
+ error: scenario_error,
26
+ revision_id: revision['revision_id'],
27
+ duration: test_duration
28
+ }
29
+ end
30
+
31
+ def save_screenshot(scenario)
32
+ if scenario.exception
33
+ screenshot_name = "#{Time.now.to_i}_#{rand(1000..9999)}.png"
34
+ @file_path = "tmp/screenshots/#{screenshot_name}"
35
+ Capybara.page.save_screenshot(@file_path)
36
+ @file_path
37
+ end
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: results_keeper_rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Kozakov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-29 00:00:00.000000000 Z
11
+ date: 2017-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: flickraw
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rest-client
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Results Keeper is a application which can help you to track and manage
@@ -46,7 +46,11 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - lib/results_keeper_rspec.rb
49
- - lib/string.rb
49
+ - lib/results_keeper_rspec/config.rb
50
+ - lib/results_keeper_rspec/messages.rb
51
+ - lib/results_keeper_rspec/results_sender.rb
52
+ - lib/results_keeper_rspec/string.rb
53
+ - lib/results_keeper_rspec/test_data_generator.rb
50
54
  homepage: https://bitbucket.org/results_keeper/results_keeper_rspec/
51
55
  licenses:
52
56
  - MIT
@@ -57,17 +61,17 @@ require_paths:
57
61
  - lib
58
62
  required_ruby_version: !ruby/object:Gem::Requirement
59
63
  requirements:
60
- - - '>='
64
+ - - ">="
61
65
  - !ruby/object:Gem::Version
62
66
  version: '0'
63
67
  required_rubygems_version: !ruby/object:Gem::Requirement
64
68
  requirements:
65
- - - '>='
69
+ - - ">="
66
70
  - !ruby/object:Gem::Version
67
71
  version: '0'
68
72
  requirements: []
69
73
  rubyforge_project:
70
- rubygems_version: 2.4.8
74
+ rubygems_version: 2.6.14
71
75
  signing_key:
72
76
  specification_version: 4
73
77
  summary: Result Keeper Gem For Rspec Tests