Skip to content

Commit

Permalink
Merge pull request #2 from nebulab/dp/multi-runs
Browse files Browse the repository at this point in the history
Dp/multi runs
  • Loading branch information
DanielePalombo committed Apr 17, 2023
2 parents 3ba3343 + d2bdfab commit 435ee4e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ RSpec.configure do |config|
end
```

# Run it on multiple jobs

Sometimes specs are run concurrently on multiple jobs. In order to collect the result on one file this gem provides some
helpful task to combine the result in one single file and then format it.

1. Remove the formatter from the after(:suite) hook
2. Save the coverage result on the same directory with different name for any job (e.g. tmp/deps/a.yml, tmp/deps/b.yml etc..)
3. Run the task to combine the YAML files `bundle exec rake 'deprecations:combine[tmp/deps/,deprecations_collector.yml]'`
4. Run the task to format the YAML to HTML `bundle exec rake 'deprecations:format[tmp/deps/,deprecations_collector.yml]'`

## Testing

Execute `bundle exec rspec` on the component root path, specs are based on an internal fake project.
Expand Down
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'pry'
require 'yaml'

RSpec::Core::RakeTask.new(:spec)

import './lib/tasks/deprecations/combine.rake'
import './lib/tasks/deprecations/format.rake'

task default: :spec

namespace :assets do
Expand Down
14 changes: 5 additions & 9 deletions lib/deprecations_collector/formatters/html/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ module DeprecationsCollector
module Formatters
module HTML
class Formatter
def format(result)
def format(result, output_path: DeprecationsCollector::Main.output_path)
public_r = './public/*'
Dir[File.join(File.dirname(__FILE__), public_r)].each do |path|
FileUtils.cp_r(path, asset_output_path)
FileUtils.cp_r(path, asset_output_path(output_path))
end

File.open(File.join(output_path, "index.html"), "wb") do |file|
file.puts template("layout").result(binding)
end
puts output_message(result)
puts output_message(result, output_path)
end

def output_message(result)
def output_message(result, output_path)
"Coverage report generated for #{result.count} files to #{output_path}."
end

def asset_output_path
def asset_output_path(output_path)
return @asset_output_path if defined?(@asset_output_path) && @asset_output_path

@asset_output_path = File.join(output_path, "assets", DeprecationsCollector::VERSION)
Expand All @@ -42,10 +42,6 @@ def template(name)
ERB.new(File.read(File.join(File.dirname(__FILE__), "views", "#{name}.erb")))
end

def output_path
DeprecationsCollector::Main.output_path
end

def assets_path(name)
File.join("./assets", DeprecationsCollector::VERSION, name)
end
Expand Down
8 changes: 2 additions & 6 deletions lib/deprecations_collector/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,16 @@ def start(suppress_deprecations: false)
@deprecation_matrix = {}
end

def save_results(file_name: 'deprecations_collector.yml')
result_and_stop_coverage
def save_results(matrix = @coverage_matrix, file_name: ENV['MATRIX_FILENAME'] || 'deprecations_collector.yml')
path = File.join(output_path, file_name)
FileUtils.mkdir_p(output_path)

File.open(path, 'w') do |f|
results = @coverage_matrix.sort.map { |k, v| [k, v.sort.to_h] }.to_h
results = matrix.sort.map { |k, v| [k, v.sort.to_h] }.to_h
f.write results.to_yaml
end
end

def result_and_stop_coverage
end

class << self
def method_missing(method, *args, **kwargs, &block)
if instance.respond_to?(method)
Expand Down
29 changes: 29 additions & 0 deletions lib/tasks/deprecations/combine.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'deprecations_collector'

namespace :deprecations do
desc "Combines all results into one"
task :combine, [:matrix_folder, :matrix_filename] do |_t, args|
matrix_folder = args[:matrix_folder].to_s

combined_matrix = Dir.entries(matrix_folder).select { |f| !f.start_with?('.') }.inject({}) do |temp_matrix, file_name|
begin
matrix = YAML.load_file("#{matrix_folder}/#{file_name}")

temp_matrix.merge(matrix) do |file, oldval, newval|
oldval.merge(newval) do |line, old_deprecation, new_deprecation|
old_deprecation + new_deprecation
end
end
rescue Psych::SyntaxError
temp_matrix
rescue Errno::EISDIR
temp_matrix
end
end

DeprecationsCollector::Main.output_path = args[:matrix_folder]
DeprecationsCollector::Main.save_results(combined_matrix, file_name: args[:matrix_filename])
end
end
14 changes: 14 additions & 0 deletions lib/tasks/deprecations/format.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require 'deprecations_collector'

namespace :deprecations do
desc "Format"
task :format, [:folder, :matrix_filename] do |_t, args|
folder = args[:folder]
matrix = YAML.load_file("#{folder}/#{args[:matrix_filename]}")

DeprecationsCollector::Main.output_path = args[:folder]
DeprecationsCollector::Formatters::HTML::Formatter.new.format(matrix)
end
end

0 comments on commit 435ee4e

Please sign in to comment.