archived-codacy / ruby-codacy-coverage

DEPRECATED Post coverage results to Codacy
12 stars 11 forks source link

code coverage push fails for a project with VCR cassettes #24

Closed dazza-codes closed 7 years ago

dazza-codes commented 7 years ago

Wrap the code coverage push so it turns off VCR exceptions, see

If you don't want to solve this issue at the level of code in this gem, just close this issue. There are options in VCR to ignore it and your documentation could make note of this as a possible problem when using VCR.

Here's a backtrace from a travis build:

I, [2016-11-29T19:27:57.361430 #3953]  INFO -- : Parsing simplecov result to Codacy format...
I, [2016-11-29T19:27:57.400825 #3953]  INFO -- : Preparing payload...
I, [2016-11-29T19:27:57.402613 #3953]  INFO -- : Posting 8002 bytes to https://api.codacy.com/2.0/coverage/b7d28a46f1f5c2893b884f3bd67b25cf8ea8a2e6/ruby
F, [2016-11-29T19:27:57.406366 #3953] FATAL -- : 

================================================================================

An HTTP request has been made that VCR does not know how to handle:

  POST https://api.codacy.com/2.0/coverage/b7d28a46f1f5c2893b884f3bd67b25cf8ea8a2e6/ruby

There is currently no cassette in use. There are a few ways
you can configure VCR to handle this request:
  * If you're surprised VCR is raising this error
    and want insight about how VCR attempted to handle the request,
    you can use the debug_logger configuration option to log more details [1].
  * If you want VCR to record this request and play it back during future test
    runs, you should wrap your test (or this portion of your test) in a
    `VCR.use_cassette` block [2].
  * If you only want VCR to handle requests made while a cassette is in use,
    configure `allow_http_connections_when_no_cassette = true`. VCR will
    ignore this request since it is made when there is no cassette [3].
  * If you want VCR to ignore this request (and others like it), you can
    set an `ignore_request` callback [4].

[1] https://www.relishapp.com/vcr/vcr/v/3-0-3/docs/configuration/debug-logging
[2] https://www.relishapp.com/vcr/vcr/v/3-0-3/docs/getting-started
[3] https://www.relishapp.com/vcr/vcr/v/3-0-3/docs/configuration/allow-http-connections-when-no-cassette
[4] https://www.relishapp.com/vcr/vcr/v/3-0-3/docs/configuration/ignore-request

================================================================================

 (VCR::Errors::UnhandledHTTPRequestError)

/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/vcr-3.0.3/lib/vcr/request_handler.rb:97:in `on_unhandled_request'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/vcr-3.0.3/lib/vcr/library_hooks/webmock.rb:129:in `on_unhandled_request'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/vcr-3.0.3/lib/vcr/request_handler.rb:24:in `handle'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/vcr-3.0.3/lib/vcr/library_hooks/webmock.rb:144:in `block in <module:WebMock>'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/webmock-2.1.0/lib/webmock/stub_registry.rb:28:in `block in register_global_stub'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/webmock-2.1.0/lib/webmock/request_pattern.rb:36:in `matches?'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/webmock-2.1.0/lib/webmock/stub_registry.rb:58:in `block in request_stub_for'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/webmock-2.1.0/lib/webmock/stub_registry.rb:57:in `each'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/webmock-2.1.0/lib/webmock/stub_registry.rb:57:in `detect'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/webmock-2.1.0/lib/webmock/stub_registry.rb:57:in `request_stub_for'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/webmock-2.1.0/lib/webmock/stub_registry.rb:50:in `response_for_request'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/webmock-2.1.0/lib/webmock/http_lib_adapters/net_http.rb:79:in `request'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/codacy-coverage-1.1.1/lib/codacy/client.rb:41:in `send_request'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/codacy-coverage-1.1.1/lib/codacy/client.rb:26:in `post_results'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/codacy-coverage-1.1.1/lib/codacy/formatter.rb:7:in `format'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/simplecov-0.12.0/lib/simplecov/result.rb:47:in `format!'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/simplecov-0.12.0/lib/simplecov/configuration.rb:172:in `block in at_exit'
/home/travis/build/dpn-admin/dpn-sync/vendor/bundle/ruby/2.3.0/gems/simplecov-0.12.0/lib/simplecov/defaults.rb:67:in `block in <top (required)>'
rtfpessoa commented 7 years ago

What would be the easiest solution on our side to fix this? (Ideally not depending on VCR gem)

dazza-codes commented 7 years ago

In this gem: test whether VCR is loaded; if so, disable it when doing the post and then enable it again. It might look something like:

VCR.turn_off! if Object.const_defined?('VCR') && VCR.respond_to?(:turn_off!)
# push the coverage data
VCR.turn_on! if Object.const_defined?('VCR') && VCR.respond_to?(:turn_on!)

If that code change works, then the error should be avoided every time, regardless of VCR config changes. AFAIK, the VCR gem is always loaded as VCR.

For documentation purposes, note that adding an ignore clause to the VCR config can avoid this error, e.g. the following options are working on the project that raised this error:

require 'vcr'
VCR.configure do |config|
  # other config options
  c.allow_http_connections_when_no_cassette = false
  c.ignore_hosts 'api.codacy.com'
end
rtfpessoa commented 7 years ago

In the first example. Even if we do not require the gem we will be able to invoke a method on it?

dazza-codes commented 7 years ago

Yes, there's nothing in that code that requires bundling the VCR gem. It just checks whether the VCR class (which has a constant name of 'VCR') is defined and, if it is, whether it responds to those VCR methods. Test it out to see whether it works without including the VCR gem, it should be OK.

rtfpessoa commented 7 years ago

We do not have any code using VCR. Are you able to test adding those two lines around https://github.com/codacy/ruby-codacy-coverage/blob/master/lib/codacy/client.rb#L26 locally and see if it works?

dazza-codes commented 7 years ago

My only project using codacy currently contains VCR and could not run the test suite easily without it. If you do not have it, that's actually better, because it should not fail without VCR. (Passing the baton back to ya.)

rtfpessoa commented 7 years ago

@Ok. I just merged #25 and added a note in the README.

Will be released soon.

rtfpessoa commented 7 years ago

@darrenleeweber released as version 1.1.4 let me know if it works.

dazza-codes commented 7 years ago

Updating from 1.1.1 to 1.1.4 failed in this travis build

Not sure why exactly, as no other changes were made. There seems to be a residual problem with WebMock that turning off VCR doesn't handle completely. Perhaps the troubleshooting tip in the README is sufficient (and revert this change)? I'm going to check one more thing first.

dazza-codes commented 7 years ago

Shoot, this is not working as expected. It may be that the project using both VCR and codacy-coverage needs to take responsibility for this. The easiest solution is already in the README#troubleshooting section. So, recommending scrapping the VCR-toggle solution and releasing a 1.1.5 to replace the 1.1.4 (mistake). Sorry I didn't see that coming.

rtfpessoa commented 7 years ago

Ok. Just reverted the toggle. 1.1.5.