dot errors rspec custom formatter
I work with two terminals side by side, a wide one of the left for running and an 80 columns one on the right for editing. The left one often gets hidden behind a browser window but I can easily bring it back with a keyboard shortcut.
My Ruby coding is a long back and forth between those two terminals, write some code on the right, run one or more specs on the left, read the error messages, fix on the right... It happens without clicking, only keyboard interactions.
I wanted to navigate immediately from an error to its location in its spec file. At first, I was thinking about letting the click on the spec/nasty_spec.rb:66
trigger something but it would kill the "only keyboard interactions" joy.
I came up with a custom Rspec formatter.
# ~/.bash/rspec_dot_errors_formatter.rb
class DotErrorsFormatter
RSpec::Core::Formatters.register self, :dump_failures
def initialize(output)
@output = output
end
def dump_failures(notification) # as registered above
notification.failure_notifications.each do |fn|
m = fn.formatted_backtrace.first.match(/\A([^:]+_spec\.rb:\d+):in /)
@output << m[1] << "\n"
break
end
end
end
No magic here, it simply produces a list of lines looking like:
./spec/p/val_spec.rb:78 ./spec/z/applications_spec.rb:92
I placed this rspec_dot_errors_formatter.rb
file in my .bash/
directory and made sure my usual bxs
alias includes this formatter when calling rspec
:
alias bxs="bundle exec rspec \
--require ~/.bash/rspec_dot_errors_formatter.rb \
--format DotErrorsFormatter --out .errors \
--format d"
Note how I require the source file containing the custom formatter, then ask for it via --format
, ensure its output is piped to .errors
thanks to --out
and then fall back to my default formatter d
(for "documentation") followed by nothing. The --out
is only for the preceding --format
.
When running my specs, it will write down in a file named .errors
the error locations in the specs.
I combined that with my ListFiles() function in Vim and get my list of error location beween my list of open buffers and of recently opened files:
I can now navigate to the "== .errors" section and jump directly to the error in the spec.
The next refinement will probably consist in mining each failure stacktrace for relevant file locations in the source itself, not in the spec source.
2016-12-05 update: let the ~/.bash/rspec_dot_errors_formatter.rb
script add to .errors
only the first _spec.rb
files it encounters in the backtraces