cutest icon indicating copy to clipboard operation
cutest copied to clipboard

Fix backtrace filtering

Open djanowski opened this issue 9 years ago • 6 comments

And make it work with e.g. gemsets.

This works: https://gist.github.com/djanowski/afed5a759687921f0569

djanowski avatar Oct 20 '15 13:10 djanowski

Won't $LOAD_PATH often include the files that your code under test is stored in? If that's the case, you'll often end up with an empty stack trace.

britishtea avatar Oct 25 '15 14:10 britishtea

Not if you require this file before anything else, which is the most common use case, right?

djanowski avatar Oct 26 '15 15:10 djanowski

That's true (though I know of at least one project where I'd end up with empty backtraces :blush:).

Perhaps this is a good use case for Ruby's awk inheritance: BEGIN { $ORIGINAL_LOAD_PATH = $LOAD_PATH.dup }.

britishtea avatar Oct 26 '15 21:10 britishtea

The constant is created when the module is required, and the array is the result of adding Gem.path to $LOAD_PATH. So further modifications to $LOAD_PATH won't be a problem. Is that what you mean?

djanowski avatar Oct 27 '15 12:10 djanowski

Yes.

Consider the / my typical Rakefile:

$LOAD_PATH.unshift File.expand_path("./lib")

require "cutest" # or require "test_helper"
require "my_library"

task(:foo) { MyLibrary.do_something! }
task(:bar) { MyLibrary.do_something_else! }
task(:test) { Cutest.run(FileList["test/*_test.rb"]) }

In such cases you'd end up with an empty backtrace and it will not be obvious why. Capturing the $LOAD_PATH in a BEGIN block avoids this at least partially: it will execute before anything else does.

Another situation where you'd end up with an empty backtrace is running a single test file using ruby -I lib test/my_library_test.rb. A BEGIN block won't avoid this, unfortunately.

britishtea avatar Oct 27 '15 14:10 britishtea

Yes, it's true, this is opinionated based on the fact that I never include my application's directories in $LOAD_PATH. I always use require for libraries, require_relative for code inside my app.

Maybe just using Gem.path is enough.

djanowski avatar Oct 27 '15 21:10 djanowski