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

at_exit block not executed? #685

Closed
Yoshiji opened this issue Jul 26, 2018 · 9 comments
Closed

at_exit block not executed? #685

Yoshiji opened this issue Jul 26, 2018 · 9 comments

Comments

@Yoshiji
Copy link

Yoshiji commented Jul 26, 2018

(related to simplecov-ruby/simplecov-html#62)

My custom SimpleCov.at_exit block is not executed in my test suite, and the default one seems to be ignored also (the html report is not generated after the test suite finished).

I am using:

  • simplecov-0.16.1
  • simplecov-html-0.10.2

In the test_helper.rb, I have:

require 'simplecov'
SimpleCov.command_name 'test:unit'
SimpleCov.coverage_dir('coverage/api/unit')

And my .simplecov file contains:

# rubocop:disable Style/FileName
require 'simplecov-lcov'
require 'simplecov-summary'

SimpleCov.coverage_dir('coverage/api')
SimpleCov.maximum_coverage_drop(0.5)

SimpleCov::Formatter::LcovFormatter.config.report_with_single_file = true
SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new([
  SimpleCov::Formatter::HTMLFormatter,
  SimpleCov::Formatter::LcovFormatter,
  SimpleCov::Formatter::SummaryFormatter
])

SimpleCov.profiles.define('adgear') do
  load_profile('rails')
  # [ bunch of regular add_filter and add_group('Some Name', 'some/path')
end

SimpleCov.start('adgear')
# rubocop:enable Style/FileName

I tried with simple SimpleCov.at_exit { puts "hello world" } or even SimpleCov.at_exit { 1/0 } (to trigger an error), but the block is not executed. What am I doing wrong here? Did I miss something?

@bf4
Copy link
Collaborator

bf4 commented Aug 3, 2018

How are you running your tests?

fwiw, Minitest autorun starts in an at_exit block so working with SimpleCov is always going to be a little weird and load-order dependent. I came across this quite a bit in ActiveModelSerializers

@PragTob
Copy link
Collaborator

PragTob commented Aug 11, 2018

Yes knowing how you run them or a code example might help track this down :)

@Yoshiji
Copy link
Author

Yoshiji commented Aug 13, 2018

I am using Minitest to run the tests, yes. Even if I remove all formatters to only leave the HTML Formatter, the report is not generated :(

@bf4
Copy link
Collaborator

bf4 commented Sep 2, 2018

what's the order in which you require minitest vs. simplecov? You need to require simplecov first, I believe.

See a situation where I've wrangled with this in the past (unrelated to simplecov) to force minitest to run in the END block so that it finishes before the at_exit rails-api/active_model_serializers@e3b9597

@Yoshiji
Copy link
Author

Yoshiji commented Sep 4, 2018

I require simplecov first, configure it (command_name and coverage_dir), then later in the test_helper I require stuff related to minitest.

@zenspider
Copy link
Contributor

Where is this and can I help at all? I'm experiencing this too. Point me at the relevant code on the simplecov side?

@zenspider
Copy link
Contributor

Looks like simplecov/defaults.rb is the culprit:

at_exit do
  # If we are in a different process than called start, don't interfere.
  next if SimpleCov.pid != Process.pid

  SimpleCov.set_exit_exception
  SimpleCov.run_exit_tasks!
end

In particular, at_exit ordering has changed recently-ish. I don't remember the version of ruby where it changed, but I can probably dig that up. Minitest has Minitest.after_tests { ... } to address this so it IS possible to get this fixed up. I'm just not sure the "proper" way to do that yet. I could use some input from the simplecov folk on that.

Also, the test identifier uses "MiniTest" which hasn't been in use for over 6 years now. I'll have a PR incoming on that one separate from anything here.

@zenspider
Copy link
Contributor

I got it working and basically did this:

at_exit do
  if defined? Minitest then
    Minitest.after_run do
      simplecov_at_exit
    end
  else
    simplecov_at_exit
  end
end

def simplecov_at_exit
  # If we are in a different process than called start, don't interfere.
  return if SimpleCov.pid != Process.pid

  SimpleCov.set_exit_exception
  SimpleCov.run_exit_tasks!
end

@PragTob
Copy link
Collaborator

PragTob commented Dec 3, 2019

This is being fixed in #756

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants