Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug with tracking time for pending specs in RSpec #109

Merged
merged 2 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

* TODO

### 1.21.1

* Fix a bug with tracking time for pending specs in RSpec

https://github.com/KnapsackPro/knapsack/pull/109

https://github.com/KnapsackPro/knapsack/compare/v1.21.0...v1.21.1

### 1.21.0

* Track time in before and after `:context` hooks
Expand Down
24 changes: 14 additions & 10 deletions lib/knapsack/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ def start_timer
end

def stop_timer
@execution_time = now_without_mock_time.to_f - @start_time
update_global_time
update_test_file_time
@execution_time
execution_time = now_without_mock_time.to_f - @start_time

if test_path
update_global_time(execution_time)
update_test_file_time(execution_time)
@test_path = nil
end

execution_time
end

def test_path
raise("test_path needs to be set by Knapsack Adapter's bind method") unless @test_path
@test_path.sub(/^\.\//, '')
@test_path.sub(/^\.\//, '') if @test_path
end

def time_exceeded?
Expand Down Expand Up @@ -62,13 +66,13 @@ def set_defaults
@test_path = nil
end

def update_global_time
@global_time += @execution_time
def update_global_time(execution_time)
@global_time += execution_time
end

def update_test_file_time
def update_test_file_time(execution_time)
@test_files_with_time[test_path] ||= 0
@test_files_with_time[test_path] += @execution_time
@test_files_with_time[test_path] += execution_time
end

def report_distributor
Expand Down
8 changes: 7 additions & 1 deletion spec/knapsack/tracker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

context 'when test_path not set' do
it do
expect { subject }.to raise_error("test_path needs to be set by Knapsack Adapter's bind method")
expect(subject).to be_nil
end
end

Expand Down Expand Up @@ -133,6 +133,9 @@
it { expect(tracker.test_files_with_time.keys.size).to eql 2 }
it { expect(tracker.test_files_with_time['a_spec.rb']).to be_within(delta).of(0.1) }
it { expect(tracker.test_files_with_time['b_spec.rb']).to be_within(delta).of(0.2) }
it 'resets test_path after time is measured' do
expect(tracker.test_path).to be_nil
end
end

context "with Timecop - Timecop shouldn't have impact on measured test time" do
Expand All @@ -157,6 +160,9 @@
it { expect(tracker.test_files_with_time.keys.size).to eql 2 }
it { expect(tracker.test_files_with_time['a_spec.rb']).to be_within(delta).of(0) }
it { expect(tracker.test_files_with_time['b_spec.rb']).to be_within(delta).of(0) }
it 'resets test_path after time is measured' do
expect(tracker.test_path).to be_nil
end
end
end

Expand Down