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

exit code 0 when there are exceptions prior to tests #41

Closed
nbogie opened this issue May 4, 2011 · 4 comments
Closed

exit code 0 when there are exceptions prior to tests #41

nbogie opened this issue May 4, 2011 · 4 comments
Labels

Comments

@nbogie
Copy link

nbogie commented May 4, 2011

This problem is demonstrated by putting the following code in spec/spec_helper.rb of a rails project.

require 'simplecov'
SimpleCov.start 'rails'
raise "some exception in the class loading before the tests start"

When rspec is invoked via rake, the above exception is logged to stderr and no tests are run, but the rake process exits 0 (i.e. success), where it should exit non-zero (failure) as it does when the SimpleCov.start is omitted.

Context:
This is a simplified version of the problem we encountered. We had a CI box that didn't receive a new config file required by a codebase change. When rake (rspec) ran, our app initialization code correctly raised an exception to protest but because our test suite was running under simplecov the rake (rspec) task exited 0 (success) indicating a passing build.

Environment:
ruby 1.9.2-p180,
simplecov gem 0.4.2
rspec gem 2.5.0
rubygems 1.6.2

Extra information:
I note that if I register an at_exit handler which does NOT call SimpleCov.result.format!, the exit code is propogated correctly (though of course no report will be produced). E.g. SimpleCov.at_exit { puts "simplecov at_exit handler - doing nothing" }

Thanks for any help, and for your work on this gem!

@colszowka
Copy link
Collaborator

Thanks for the report. I'll have a look into it, though I can't come up with an easy solution right now.

@nbogie
Copy link
Author

nbogie commented May 4, 2011

Hi, I've made a fix for this issue but it currently lacks automated tests:
https://github.com/nbogie/simplecov/commit/ec8bd54f9507204440665f51b34ee584d16516f4

In the manual test documented in the above issue, simplecov's at_exit() will be reached with $! populated with some exception other than SystemExit (specifically a RuntimeError). Simplecov was only calling exit explicitly if it found a SystemExit exception was present. I extended that to work for other exceptions. For some reason, the presence of $! was not anyway causing a non-zero exit, at least in the situation where the registered at_exit handler had to prepare a simplecov report. I haven't found good documentation on this aspect of ruby (pointers welcome).

I'm dubious of the existing approach of doing the reporting work (and of calling exit) from within an at_exit handler. Also, what happens if the report formatter raises an exception, given that it is also generally launched from this handler? Isn't there some other point at which to prepare the report rather than using the at_exit hook? I get that such a callback would likely be framework-dependent.

I've only glanced at the rest of the code; please excuse my ignorance.

Resources:

@nbogie nbogie closed this as completed May 4, 2011
@nbogie nbogie reopened this May 4, 2011
@colszowka
Copy link
Collaborator

I don't think there is a better way to capture the coverage results than with an at_exit handler - What other way could there be to let simplecov do it's thing when the test suite is finished?

Your commit makes a lot of sense to me, keeping this open so I remember to add tests and pull it in.

colszowka added a commit that referenced this issue Sep 28, 2011
@colszowka
Copy link
Collaborator

This is included in the new 0.5.4 release, thanks a lot!

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

No branches or pull requests

2 participants