-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
162 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'flatware/rspec/duration_providers/example_statuses_provider' | ||
|
||
module Flatware | ||
module RSpec | ||
module DurationProviders | ||
module_function | ||
|
||
def lookup(provider) | ||
const_get("#{classify(provider)}Provider").new | ||
end | ||
|
||
def classify(underscore_name) | ||
underscore_name.to_s.split('_').map(&:capitalize).join | ||
end | ||
|
||
private_class_method :classify | ||
end | ||
end | ||
end |
54 changes: 54 additions & 0 deletions
54
lib/flatware/rspec/duration_providers/example_statuses_provider.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'forwardable' | ||
|
||
module Flatware | ||
module RSpec | ||
module DurationProviders | ||
class ExampleStatusesProvider | ||
extend Forwardable | ||
attr_reader :configuration | ||
|
||
def_delegators :configuration, :example_status_persistence_file_path | ||
|
||
def initialize(configuration: ::RSpec.configuration) | ||
@configuration = configuration | ||
end | ||
|
||
def seconds_per_file | ||
sum_seconds(load_persisted_example_statuses) | ||
end | ||
|
||
private | ||
|
||
def load_persisted_example_statuses | ||
::RSpec::Core::ExampleStatusPersister.load_from( | ||
example_status_persistence_file_path || '' | ||
) | ||
end | ||
|
||
def sum_seconds(statuses) | ||
statuses.select(&passing) | ||
.map { |example| parse_example(**example) } | ||
.reduce({}) do |times, example| | ||
times.merge( | ||
example.fetch(:file_name) => example.fetch(:seconds) | ||
) do |_, old = 0, new| | ||
old + new | ||
end | ||
end | ||
end | ||
|
||
def passing | ||
->(example) { example.fetch(:status) =~ /pass/i } | ||
end | ||
|
||
def parse_example(example_id:, run_time:, **) | ||
seconds = run_time.match(/\d+(\.\d+)?/).to_s.to_f | ||
file_name = ::RSpec::Core::Example.parse_id(example_id).first | ||
{ seconds: seconds, file_name: file_name } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
spec/flatware/rspec/duration_providers/example_statuses_provider_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'flatware/rspec/duration_providers/example_statuses_provider' | ||
|
||
describe Flatware::RSpec::DurationProviders::ExampleStatusesProvider do | ||
before do | ||
allow(RSpec::Core::ExampleStatusPersister).to( | ||
receive(:load_from).and_return(persisted_examples) | ||
) | ||
end | ||
|
||
let(:persisted_examples) do | ||
[ | ||
{ example_id: './fast_1_spec.rb[1]', run_time: '1 second' }, | ||
{ example_id: './fast_2_spec.rb[1]', run_time: '1 second' }, | ||
{ example_id: './fast_3_spec.rb[1]', run_time: '1 second' }, | ||
{ example_id: './slow_spec.rb[1]', run_time: '2 seconds' } | ||
].map { |example| example.merge status: 'passed' } | ||
end | ||
|
||
describe '#seconds_per_file' do | ||
subject { described_class.new.seconds_per_file } | ||
|
||
it 'returns an object of the specified provider class' do | ||
expect(subject).to eq( | ||
'./fast_1_spec.rb' => 1.0, | ||
'./fast_2_spec.rb' => 1.0, | ||
'./fast_3_spec.rb' => 1.0, | ||
'./slow_spec.rb' => 2.0 | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'flatware/rspec/duration_providers' | ||
|
||
describe Flatware::RSpec::DurationProviders do | ||
describe '#lookup' do | ||
before do | ||
Flatware::RSpec::DurationProviders.const_set(:MockProvider, mock_provider) | ||
end | ||
|
||
after do | ||
Flatware::RSpec::DurationProviders.send(:remove_const, :MockProvider) | ||
end | ||
|
||
let(:mock_provider) { Class.new } | ||
|
||
it 'returns an object of the specified provider class' do | ||
expect(described_class.lookup('mock')).to be_a(mock_provider) | ||
end | ||
|
||
it 'works with a string argument' do | ||
expect(described_class.lookup(:mock)).to be_a(mock_provider) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters