Module: TestmetricsRspec::Shared

Included in:
TestmetricsRspec, ParallelTests, Persist
Defined in:
lib/testmetrics_rspec/shared.rb

Overview

Shared stuff between formatters

Constant Summary collapse

BRANCH_VARS =
%w[
  TRAVIS_EVENT_TYPE
  CIRCLE_BRANCH
  CI_COMMIT_REF_NAME
  SEMAPHORE_GIT_BRANCH
].freeze
SHA_VARS =
%w[TRAVIS_COMMIT CIRCLE_SHA1 CI_COMMIT_SHA SEMAPHORE_GIT_SHA].freeze

Instance Method Summary collapse

Instance Method Details

#ci_platformObject



77
78
79
80
81
82
83
84
85
# File 'lib/testmetrics_rspec/shared.rb', line 77

def ci_platform
  case correct_var(SHA_VARS)
  when 'TRAVIS_COMMIT' then 'Travis CI'
  when 'CIRCLE_SHA1' then 'Circle CI'
  when 'CI_COMMIT_SHA' then 'Gitlab CI'
  when 'SEMAPHORE_GIT_SHA' then 'Semaphore CI'
  else 'Unknown'
  end
end

#correct_var(vars) ⇒ Object



87
88
89
# File 'lib/testmetrics_rspec/shared.rb', line 87

def correct_var(vars)
  vars.find { |var| !ENV[var].nil? && ENV[var] != '' }
end

#format(description, run_time, status) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/testmetrics_rspec/shared.rb', line 17

def format(description, run_time, status)
  {
    name: description.delete("\0").delete("\x01").delete("\e"),
    total_run_time: run_time_in_microseconds(run_time),
    state: status
  }
end

#git_branchObject



52
53
54
55
56
57
58
59
60
# File 'lib/testmetrics_rspec/shared.rb', line 52

def git_branch
  correct_var = correct_var(BRANCH_VARS)

  if correct_var == 'TRAVIS_EVENT_TYPE'
    travis_branch(correct_var)
  else
    correct_var.nil? ? 'Unknown' : ENV[correct_var]
  end
end

#git_shaObject



72
73
74
75
# File 'lib/testmetrics_rspec/shared.rb', line 72

def git_sha
  correct_var = correct_var(SHA_VARS)
  correct_var.nil? ? 'Unknown' : ENV[correct_var]
end

#post(results) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/testmetrics_rspec/shared.rb', line 9

def post(results)
  Faraday.post do |req|
    req.url 'https://www.testmetrics.app/results'
    req.headers['Content-Type'] = 'application/json'
    req.body = results.to_json
  end
end

#resultsObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/testmetrics_rspec/shared.rb', line 29

def results
  {
    key: ENV['TESTMETRICS_PROJECT_KEY'] || 'Unknown',
    branch: git_branch,
    sha: git_sha,
    metadata: {
      ruby_version: RUBY_VERSION,
      ci_platform: ci_platform
    },
    tests: yield
  }
end

#run_time_in_microseconds(time) ⇒ Object



42
43
44
# File 'lib/testmetrics_rspec/shared.rb', line 42

def run_time_in_microseconds(time)
  (time * 1_000_000).round(0)
end

#start(notification) ⇒ Object



4
5
6
7
# File 'lib/testmetrics_rspec/shared.rb', line 4

def start(notification)
  TestmetricsRspec::Persist.call(starting_results)
  super
end

#starting_resultsObject



25
26
27
# File 'lib/testmetrics_rspec/shared.rb', line 25

def starting_results
  results { [] }
end

#travis_branch(var) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/testmetrics_rspec/shared.rb', line 62

def travis_branch(var)
  case ENV[var]
  when 'push'
    ENV['TRAVIS_BRANCH']
  when 'pull_request'
    ENV['TRAVIS_PULL_REQUEST_BRANCH']
  end
end