cutest
cutest copied to clipboard
Fix backtrace filtering
And make it work with e.g. gemsets.
This works: https://gist.github.com/djanowski/afed5a759687921f0569
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.
Not if you require this file before anything else, which is the most common use case, right?
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 }
.
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?
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.
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.