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

Ensure clean environment for RSpec integration tests #1420

Merged
merged 1 commit into from
Mar 22, 2021

Conversation

marcotc
Copy link
Member

@marcotc marcotc commented Mar 19, 2021

When running a single example in the RSpec integration test suite (e.g. rspec --example 'RSpec hooks creates span for example'), I noticed that theses test would fail, but while running the whole file it would pass.

This happened because the RSpec test context created for testing was not completely isolated from the ddtrace RSpec test runner. We already isolated RSpec.configuration, but we forgot to also isolate RSpec.world. This PR does that. This behaviour became more apparent when working addressing issues around configuration leaks between tests.

Also, this PR refactors the code in spec/ddtrace/contrib/rspec/instrumentation_spec.rb, as there were a few unused variables and manual span tracking (which is already provided by https://github.com/DataDog/dd-trace-rb/blob/master/spec/support/tracer_helpers.rb).

@marcotc marcotc added the dev/testing Involves testing processes (e.g. RSpec) label Mar 19, 2021
@marcotc marcotc self-assigned this Mar 19, 2021
@marcotc marcotc requested a review from a team March 19, 2021 18:08
@codecov-io
Copy link

Codecov Report

Merging #1420 (9fec2f9) into master (c0762e7) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1420   +/-   ##
=======================================
  Coverage   98.13%   98.14%           
=======================================
  Files         771      771           
  Lines       36929    36908   -21     
=======================================
- Hits        36242    36222   -20     
+ Misses        687      686    -1     
Impacted Files Coverage Δ
spec/ddtrace/contrib/rspec/instrumentation_spec.rb 100.00% <100.00%> (ø)
...b/action_pack/action_controller/instrumentation.rb 95.71% <0.00%> (+1.42%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c0762e7...9fec2f9. Read the comment docs.

@marcotc marcotc added the integrations Involves tracing integrations label Mar 19, 2021
@marcotc marcotc changed the title Ensure clean environment for RSpec tests Ensure clean environment for RSpec integration tests Mar 19, 2021
Comment on lines +16 to +17
# Yields to a block in a new RSpec global context. All RSpec
# test configuration and execution should be wrapped in this method.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be done on an around block to avoid having to do it manually for each test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I like the explicitness -- we're running RSpec to test RSpec so having clear boundaries on when transitions between the nested under-test RSpec and the top-level running-test RSpec happen I think is good for avoiding confusion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding it to around limits the use of the host RSpec test suite features inside the test example.

Copy link
Member

@ivoanjo ivoanjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the cleanup 👍

spec = with_new_rspec_environment do
RSpec.describe 'some test' do
it 'foo' do; end
end.tap(&:run)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor -- we seem to be attaching run to every call to with_new_rspec_environment. What do you think of automatically calling run on the result of yield inside the helper method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of doing that, but chose against it as it gives a seemly minor code simplification at the expense of removing flexibility from with_new_rspec_environment users.

Having with_new_rspec_environment solely "give you a clean rspec environment" made more sense to me while writing these tests.


expect(spans.count).to eq(1)
span = spans.first
expect(span.span_type).to eq(Datadog::Ext::AppTypes::TEST)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These implicit helpers get me confused every time, because they show up from nowhere -- I need to grep the codebase to understand where they coming from.

It's a bit minor, but I'd really like if we could call them explicitly TracerHelpers.span (it's really obvious where it's coming from) or we had an explicit include TracerHelpers in this file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TracerHelpers.span everything is too verbose, include TracerHelpers could be nice.

@marcotc marcotc merged commit ce4a017 into master Mar 22, 2021
@marcotc marcotc deleted the rspec-test-refactor branch March 22, 2021 18:29
@github-actions github-actions bot added this to the 0.47.0 milestone Mar 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev/testing Involves testing processes (e.g. RSpec) integrations Involves tracing integrations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants