Class: SimpleCov::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov-s3.rb,
lib/simplecov-s3/version.rb

Defined Under Namespace

Classes: Formatter

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ S3

Returns a new instance of S3.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/simplecov-s3.rb', line 29

def initialize(opts)
  @connection = Fog::Storage.new(opts[:fog])
  @public_url_base = opts[:public_url_base]
  @shared_secret = opts[:shared_secret]
  @assets_url = opts[:assets_url]
  @bucket_name = opts[:bucket_name]
  @build_units = opts[:build_units].to_i
  if @build_units <= 1
    raise "Missing build_units in config (need a value greater than 0)"
  end
  @postback_url = opts[:postback_url]
  @build_unit = opts[:build_unit]
  @build_id = opts[:build_id]
  @job_id = opts[:job_id]
  @project_name = opts[:project_name] || "unknown"
  if @project_name.empty?
    @project_name = "unknown"
  end
  @history_limit = opts[:history_limit] || (@build_units.to_i * 3)
  @additional_artifacts_to_merge = opts[:additional_artifacts_to_merge] || []
end

Class Method Details

.ensured(task_to_invoke, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/simplecov-s3.rb', line 11

def self.ensured(task_to_invoke, &block)
  Rake::Task[task_to_invoke].invoke
ensure
  begin
    yield
  rescue => e
    puts e.inspect
    puts e.backtrace
  end
end

Instance Method Details

#pull_merge_and_push_fullObject



75
76
77
78
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/simplecov-s3.rb', line 75

def pull_merge_and_push_full
  if(json_files.size < @build_units)
    puts "Expected to see #{@build_units} files"
    puts "currently only #{json_files.size}"
    require 'pp'
    pp json_files.map(&:key)
    puts "Assuming some other build unit will be the last to exit, and merge/post full coverage"
    return false
  end
  puts "Coverage combined from #{json_files.size} units"
  results = []
  included_units = []
  json_files.each do |f|
    parsed = JSON.parse(f.body)
    included_units << parsed.delete("BUILD_UNIT")
    results << SimpleCov::Result.new(parsed)
  end
  puts "Included units: #{included_units.sort_by(&:to_s).inspect}"
  merged = {}
  results.each do |result|
    merged = result.original_result.merge_resultset(merged)
  end
  result = SimpleCov::Result.new(merged)
  uniq_id = SecureRandom.urlsafe_base64(33)
  push_coverage_to("#{@project_name}-#{Time.now.to_i}-coverageRESULT/#{uniq_id}", gen_body(result), result.covered_percent, @postback_url)
  @additional_artifacts_to_merge.each do |artifact|
    key = artifact[:key]
    file = artifact[:file]
    merge_with = artifact[:merge_with]
    files = files_for_key(key).map do |f|
      data = f.body
      puts "merging additional artifact #{f.key} #{data[0,100]}"
      data
    end
    while files.size > 1
      a, b, *files = files
      files << merge_with.call(a, b)
    end
    File.open(file, "w+"){|f| f.write(files.first)}
    puts "Merged artifact #{key} contents:"
    puts files.first
    save_merged_artifact_in_s3_at = "#{@project_name}-#{Time.now.to_i}-#{key}/#{uniq_id}"
    puts "Also saving in S3 at path: #{save_merged_artifact_in_s3_at}"
    put_file(save_merged_artifact_in_s3_at, files.first)
  end
  cleanup_files
end

#push_fullObject



123
124
125
126
127
# File 'lib/simplecov-s3.rb', line 123

def push_full
  result = SimpleCov::ResultMerger.merged_result
  push_coverage_to("#{@project_name}-#{Time.now.to_i}-coverageRESULT/#{SecureRandom.urlsafe_base64(33)}", gen_body(result), result.covered_percent, @postback_url)
  cleanup_files
end

#push_partial(opts = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/simplecov-s3.rb', line 51

def push_partial(opts = {})
  begin
    result = SimpleCov::ResultMerger.merged_result
    data_to_push = result.original_result.merge("BUILD_UNIT" => @build_unit).to_json
    put_file("#{@project_name}-#{commit_time}-coverageJSON/#{hashed_build_id}/#{@job_id}", data_to_push)
    @additional_artifacts_to_merge.each do |artifact|
      key = artifact[:key]
      file = artifact[:file]
      filedata = File.read(file)
      puts "pushing additional artifact #{key} #{file} #{filedata[0,100]}"
      put_file("#{@project_name}-#{commit_time}-#{key}/#{hashed_build_id}/#{@job_id}", filedata)
    end
  rescue => e
    puts e.inspect
    puts e.backtrace
    puts "SOMEHTING WENT WRONG, PUSHING EMPTY COVERAGE FILE"
    put_file("#{@project_name}-#{commit_time}-coverageJSON/#{hashed_build_id}/#{@job_id}", {}.to_json)
  end
  if opts[:debug]
    debug_folder = "#{@project_name}-#{Time.now.to_i}-coverageDEBUG/#{hashed_build_id}-#{@job_id}"
    push_coverage_to(debug_folder, gen_body(result))
  end
end